Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve code using serializable extensions #4196

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import org.wikipedia.Constants
import org.wikipedia.WikipediaApp
import org.wikipedia.extensions.serializableExtra
import org.wikipedia.settings.SettingsActivity
import org.wikipedia.util.log.L

Expand Down Expand Up @@ -56,7 +57,8 @@ class BreadCrumbLogEvent(
}

fun logScreenShown(context: Context, fragment: Fragment? = null) {
val invokeSource = (fragment?.activity?.intent ?: (context as? Activity)?.intent)?.getSerializableExtra(Constants.INTENT_EXTRA_INVOKE_SOURCE) as? Constants.InvokeSource
val intent = (fragment?.activity?.intent ?: (context as? Activity)?.intent)
val invokeSource = intent?.serializableExtra<Constants.InvokeSource>(Constants.INTENT_EXTRA_INVOKE_SOURCE)
EventPlatformClient.submit(BreadCrumbLogEvent(BreadCrumbViewUtil.getReadableScreenName(context, fragment),
"show" + invokeSource?.let { ".from." + it.value }.orEmpty()))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.wikipedia.Constants.InvokeSource
import org.wikipedia.activity.BaseActivity
import org.wikipedia.databinding.ActivityArticleEditDetailsBinding
import org.wikipedia.extensions.parcelableExtra
import org.wikipedia.extensions.serializableExtra
import org.wikipedia.page.PageTitle

class ArticleEditDetailsActivity : BaseActivity() {
Expand All @@ -25,7 +26,7 @@ class ArticleEditDetailsActivity : BaseActivity() {
intent.getIntExtra(EXTRA_PAGE_ID, -1),
intent.getLongExtra(EXTRA_EDIT_REVISION_FROM, -1),
intent.getLongExtra(EXTRA_EDIT_REVISION_TO, -1),
intent.getSerializableExtra(INTENT_EXTRA_INVOKE_SOURCE) as InvokeSource)
intent.serializableExtra(INTENT_EXTRA_INVOKE_SOURCE)!!)

if (savedInstanceState == null) {
supportFragmentManager.commit { add(binding.fragmentContainer.id, fragment) }
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/org/wikipedia/extensions/Bundle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.wikipedia.extensions
import android.os.Bundle
import android.os.Parcelable
import androidx.core.os.BundleCompat
import java.io.Serializable

inline fun <reified T : Parcelable> Bundle.parcelable(key: String?): T? {
return BundleCompat.getParcelable(this, key, T::class.java)
Expand All @@ -11,3 +12,7 @@ inline fun <reified T : Parcelable> Bundle.parcelable(key: String?): T? {
inline fun <reified T : Parcelable> Bundle.parcelableArrayList(key: String?): ArrayList<T>? {
return BundleCompat.getParcelableArrayList(this, key, T::class.java)
}

inline fun <reified T : Serializable> Bundle.serializable(key: String?): T? {
return BundleCompat.getSerializable(this, key, T::class.java)
}
5 changes: 5 additions & 0 deletions app/src/main/java/org/wikipedia/extensions/Intent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ package org.wikipedia.extensions
import android.content.Intent
import android.os.Parcelable
import androidx.core.content.IntentCompat
import java.io.Serializable

inline fun <reified T : Parcelable> Intent.parcelableExtra(key: String?): T? {
return IntentCompat.getParcelableExtra(this, key, T::class.java)
}

inline fun <reified T : Serializable> Intent.serializableExtra(key: String?): T? {
return IntentCompat.getSerializableExtra(this, key, T::class.java)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import org.wikipedia.Constants.InvokeSource
import org.wikipedia.activity.SingleFragmentActivity
import org.wikipedia.dataclient.WikiSite
import org.wikipedia.extensions.parcelableExtra
import org.wikipedia.extensions.serializableExtra

class OnThisDayActivity : SingleFragmentActivity<OnThisDayFragment>() {

override fun createFragment(): OnThisDayFragment {
return OnThisDayFragment.newInstance(intent.getIntExtra(EXTRA_AGE, 0),
intent.parcelableExtra(Constants.ARG_WIKISITE)!!,
intent.getIntExtra(EXTRA_YEAR, -1),
intent.getSerializableExtra(Constants.INTENT_EXTRA_INVOKE_SOURCE) as InvokeSource)
intent.serializableExtra(Constants.INTENT_EXTRA_INVOKE_SOURCE)!!)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import org.wikipedia.dataclient.Service
import org.wikipedia.dataclient.WikiSite
import org.wikipedia.dataclient.wikidata.Entities
import org.wikipedia.descriptions.DescriptionEditActivity
import org.wikipedia.extensions.serializableExtra
import org.wikipedia.history.HistoryEntry
import org.wikipedia.page.ExclusiveBottomSheetPresenter
import org.wikipedia.page.LinkMovementMethodExt
Expand Down Expand Up @@ -81,7 +82,7 @@ class GalleryActivity : BaseActivity(), LinkPreviewDialog.LoadPageCallback, Gall

private val requestAddCaptionLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
if (it.resultCode == RESULT_OK) {
val action = it.data?.getSerializableExtra(Constants.INTENT_EXTRA_ACTION) as DescriptionEditActivity.Action?
val action = it.data?.serializableExtra<DescriptionEditActivity.Action>(Constants.INTENT_EXTRA_ACTION)
SuggestedEditsSnackbars.show(this, action, true, targetLanguageCode, false)
fetchGalleryDescription(currentItem)
setResult(ACTIVITY_RESULT_IMAGE_CAPTION_ADDED)
Expand Down
6 changes: 2 additions & 4 deletions app/src/main/java/org/wikipedia/page/PageActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import org.wikipedia.edit.EditSectionActivity
import org.wikipedia.events.ArticleSavedOrDeletedEvent
import org.wikipedia.events.ChangeTextSizeEvent
import org.wikipedia.extensions.parcelableExtra
import org.wikipedia.extensions.serializableExtra
import org.wikipedia.gallery.GalleryActivity
import org.wikipedia.history.HistoryEntry
import org.wikipedia.language.LangLinksActivity
Expand Down Expand Up @@ -156,11 +157,8 @@ class PageActivity : BaseActivity(), PageFragment.Callback, LinkPreviewDialog.Lo
private val requestSuggestedEditsLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
if (it.resultCode == RESULT_OK || it.resultCode == DescriptionEditSuccessActivity.RESULT_OK_FROM_EDIT_SUCCESS) {
pageFragment.refreshPage()
val data = it.data
val editLanguage = pageFragment.leadImageEditLang.orEmpty().ifEmpty { app.languageState.appLanguageCode }
val action = if (data != null && data.hasExtra(Constants.INTENT_EXTRA_ACTION))
data.getSerializableExtra(Constants.INTENT_EXTRA_ACTION) as DescriptionEditActivity.Action?
else null
val action = it.data?.serializableExtra<DescriptionEditActivity.Action>(Constants.INTENT_EXTRA_ACTION)

SuggestedEditsSnackbars.show(this, action, it.resultCode != DescriptionEditSuccessActivity.RESULT_OK_FROM_EDIT_SUCCESS,
editLanguage, action !== DescriptionEditActivity.Action.ADD_DESCRIPTION && action !== DescriptionEditActivity.Action.TRANSLATE_DESCRIPTION) {
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/org/wikipedia/random/RandomActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.wikipedia.Constants.InvokeSource
import org.wikipedia.activity.SingleFragmentActivity
import org.wikipedia.dataclient.WikiSite
import org.wikipedia.extensions.parcelableExtra
import org.wikipedia.extensions.serializableExtra

class RandomActivity : SingleFragmentActivity<RandomFragment>() {

Expand All @@ -28,6 +29,6 @@ class RandomActivity : SingleFragmentActivity<RandomFragment>() {

public override fun createFragment(): RandomFragment {
return RandomFragment.newInstance(intent.parcelableExtra(Constants.ARG_WIKISITE)!!,
intent.getSerializableExtra(Constants.INTENT_EXTRA_INVOKE_SOURCE) as InvokeSource)
intent.serializableExtra(Constants.INTENT_EXTRA_INVOKE_SOURCE)!!)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import org.wikipedia.R
import org.wikipedia.database.AppDatabase
import org.wikipedia.databinding.DialogAddToReadingListBinding
import org.wikipedia.extensions.parcelableArrayList
import org.wikipedia.extensions.serializable
import org.wikipedia.page.ExtendedBottomSheetDialogFragment
import org.wikipedia.page.PageTitle
import org.wikipedia.readinglist.ReadingListTitleDialog.readingListTitleDialog
Expand Down Expand Up @@ -49,7 +50,7 @@ open class AddToReadingListDialog : ExtendedBottomSheetDialogFragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
titles = requireArguments().parcelableArrayList(PAGE_TITLE_LIST)!!
invokeSource = requireArguments().getSerializable(Constants.INTENT_EXTRA_INVOKE_SOURCE) as InvokeSource
invokeSource = requireArguments().serializable(Constants.INTENT_EXTRA_INVOKE_SOURCE)!!
showDefaultList = requireArguments().getBoolean(SHOW_DEFAULT_LIST)
adapter = ReadingListAdapter()
}
Expand Down
16 changes: 7 additions & 9 deletions app/src/main/java/org/wikipedia/search/SearchActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@ import android.content.Intent
import org.wikipedia.Constants
import org.wikipedia.Constants.InvokeSource
import org.wikipedia.activity.SingleFragmentActivity
import org.wikipedia.extensions.serializableExtra
import org.wikipedia.util.log.L

class SearchActivity : SingleFragmentActivity<SearchFragment>() {
public override fun createFragment(): SearchFragment {
var source = intent.getSerializableExtra(Constants.INTENT_EXTRA_INVOKE_SOURCE) as InvokeSource?
if (source == null) {
when {
Intent.ACTION_SEND == intent.action -> { source = InvokeSource.INTENT_SHARE }
Intent.ACTION_PROCESS_TEXT == intent.action -> { source = InvokeSource.INTENT_PROCESS_TEXT }
else -> {
source = InvokeSource.INTENT_UNKNOWN
L.logRemoteErrorIfProd(RuntimeException("Unknown intent when launching SearchActivity: " + intent.action.orEmpty()))
}
val source = intent.serializableExtra(Constants.INTENT_EXTRA_INVOKE_SOURCE) ?: when {
Intent.ACTION_SEND == intent.action -> InvokeSource.INTENT_SHARE
Intent.ACTION_PROCESS_TEXT == intent.action -> InvokeSource.INTENT_PROCESS_TEXT
else -> {
L.logRemoteErrorIfProd(RuntimeException("Unknown intent when launching SearchActivity: " + intent.action.orEmpty()))
InvokeSource.INTENT_UNKNOWN
}
}
return SearchFragment.newInstance(source, intent.getStringExtra(QUERY_EXTRA), intent.getBooleanExtra(EXTRA_RETURN_LINK, false))
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/org/wikipedia/search/SearchFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import org.wikipedia.WikipediaApp
import org.wikipedia.analytics.eventplatform.PlacesEvent
import org.wikipedia.database.AppDatabase
import org.wikipedia.databinding.FragmentSearchBinding
import org.wikipedia.extensions.serializable
import org.wikipedia.history.HistoryEntry
import org.wikipedia.json.JsonUtil
import org.wikipedia.page.PageActivity
Expand Down Expand Up @@ -99,7 +100,7 @@ class SearchFragment : Fragment(), SearchResultsFragment.Callback, RecentSearche
if (savedInstanceState == null) {
handleIntent(requireActivity().intent)
}
invokeSource = requireArguments().getSerializable(Constants.INTENT_EXTRA_INVOKE_SOURCE) as InvokeSource
invokeSource = requireArguments().serializable(Constants.INTENT_EXTRA_INVOKE_SOURCE)!!
query = requireArguments().getString(ARG_QUERY)
returnLink = requireArguments().getBoolean(SearchActivity.EXTRA_RETURN_LINK, false)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import android.content.Intent
import org.wikipedia.Constants
import org.wikipedia.Constants.InvokeSource
import org.wikipedia.activity.SingleFragmentActivity
import org.wikipedia.extensions.serializableExtra
import org.wikipedia.settings.languages.WikipediaLanguagesFragment.Companion.newInstance
import org.wikipedia.widgets.WidgetProviderFeaturedPage.Companion.forceUpdateWidget

class WikipediaLanguagesActivity : SingleFragmentActivity<WikipediaLanguagesFragment>() {
override fun createFragment(): WikipediaLanguagesFragment {
return newInstance(intent.getSerializableExtra(Constants.INTENT_EXTRA_INVOKE_SOURCE) as InvokeSource)
return newInstance(intent.serializableExtra(Constants.INTENT_EXTRA_INVOKE_SOURCE)!!)
}

override fun onDestroy() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.wikipedia.Constants.InvokeSource
import org.wikipedia.R
import org.wikipedia.WikipediaApp
import org.wikipedia.databinding.FragmentWikipediaLanguagesBinding
import org.wikipedia.extensions.serializableExtra
import org.wikipedia.json.JsonUtil
import org.wikipedia.language.LanguagesListActivity
import org.wikipedia.push.WikipediaFirebaseMessagingService
Expand Down Expand Up @@ -47,7 +48,7 @@ class WikipediaLanguagesFragment : Fragment(), MenuProvider, WikipediaLanguagesI
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
_binding = FragmentWikipediaLanguagesBinding.inflate(inflater, container, false)
(requireActivity() as AppCompatActivity).setSupportActionBar(binding.toolbar)
invokeSource = requireActivity().intent.getSerializableExtra(Constants.INTENT_EXTRA_INVOKE_SOURCE) as InvokeSource
invokeSource = requireActivity().intent.serializableExtra(Constants.INTENT_EXTRA_INVOKE_SOURCE)!!
initialLanguageList = JsonUtil.encodeToString(app.languageState.appLanguageCodes).orEmpty()
requireActivity().addMenuProvider(this, viewLifecycleOwner, Lifecycle.State.RESUMED)
prepareWikipediaLanguagesList()
Expand Down Expand Up @@ -264,7 +265,7 @@ class WikipediaLanguagesFragment : Fragment(), MenuProvider, WikipediaLanguagesI
private inner class FooterViewHolder(itemView: View) : DefaultViewHolder<View>(itemView)

private fun wantResultFromItemClick(): Boolean {
val source = requireActivity().intent.getSerializableExtra(Constants.INTENT_EXTRA_INVOKE_SOURCE) as InvokeSource?
val source = requireActivity().intent.serializableExtra<InvokeSource>(Constants.INTENT_EXTRA_INVOKE_SOURCE)
return source != null && (source == InvokeSource.SEARCH || source == InvokeSource.TALK_TOPICS_ACTIVITY || source == InvokeSource.USER_CONTRIB_ACTIVITY)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import org.wikipedia.descriptions.DescriptionEditActivity.Action.IMAGE_RECOMMEND
import org.wikipedia.descriptions.DescriptionEditActivity.Action.TRANSLATE_CAPTION
import org.wikipedia.descriptions.DescriptionEditActivity.Action.TRANSLATE_DESCRIPTION
import org.wikipedia.descriptions.DescriptionEditActivity.Action.VANDALISM_PATROL
import org.wikipedia.extensions.serializableExtra
import org.wikipedia.page.PageTitle
import org.wikipedia.settings.Prefs
import org.wikipedia.suggestededits.SuggestionsActivity.Companion.EXTRA_SOURCE_ADDED_CONTRIBUTION
Expand Down Expand Up @@ -95,7 +96,7 @@ class SuggestedEditsCardsFragment : Fragment(), MenuProvider, SuggestedEditsItem
(requireActivity() as AppCompatActivity).apply {
setSupportActionBar(binding.toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.title = getActionBarTitle(intent.getSerializableExtra(Constants.INTENT_EXTRA_ACTION) as DescriptionEditActivity.Action)
supportActionBar?.title = getActionBarTitle(intent.serializableExtra(Constants.INTENT_EXTRA_ACTION)!!)
}

return binding.root
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.wikipedia.activity.BaseActivity
import org.wikipedia.databinding.ActivitySuggestedEditsFeedCardImageTagsBinding
import org.wikipedia.dataclient.mwapi.MwQueryPage
import org.wikipedia.descriptions.DescriptionEditActivity
import org.wikipedia.extensions.serializableExtra
import org.wikipedia.json.JsonUtil
import org.wikipedia.settings.Prefs
import org.wikipedia.util.DimenUtil
Expand All @@ -35,7 +36,7 @@ class SuggestedEditsImageTagEditActivity : BaseActivity(), SuggestedEditsItemFra
setImageZoomHelper()

suggestedEditsImageTagsFragment = supportFragmentManager.findFragmentById(R.id.imageTagFragment) as SuggestedEditsImageTagsFragment
suggestedEditsImageTagsFragment.invokeSource = intent.getSerializableExtra(Constants.INTENT_EXTRA_INVOKE_SOURCE) as Constants.InvokeSource
suggestedEditsImageTagsFragment.invokeSource = intent.serializableExtra(Constants.INTENT_EXTRA_INVOKE_SOURCE)!!

binding.addContributionButton.setOnClickListener { suggestedEditsImageTagsFragment.publish() }
binding.addContributionLandscapeImage.setOnClickListener { suggestedEditsImageTagsFragment.publish() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.os.Bundle
import org.wikipedia.Constants.INTENT_EXTRA_ACTION
import org.wikipedia.activity.SingleFragmentActivity
import org.wikipedia.descriptions.DescriptionEditActivity.Action
import org.wikipedia.extensions.serializableExtra
import org.wikipedia.suggestededits.SuggestedEditsCardsFragment.Companion.newInstance

class SuggestionsActivity : SingleFragmentActivity<SuggestedEditsCardsFragment>() {
Expand All @@ -23,7 +24,7 @@ class SuggestionsActivity : SingleFragmentActivity<SuggestedEditsCardsFragment>(
}

override fun createFragment(): SuggestedEditsCardsFragment {
return newInstance(intent.getSerializableExtra(INTENT_EXTRA_ACTION) as Action)
return newInstance(intent.serializableExtra(INTENT_EXTRA_ACTION)!!)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import org.wikipedia.dataclient.okhttp.HttpStatusException
import org.wikipedia.edit.EditHandler
import org.wikipedia.edit.EditSectionActivity
import org.wikipedia.extensions.parcelableExtra
import org.wikipedia.extensions.serializableExtra
import org.wikipedia.history.HistoryEntry
import org.wikipedia.history.SearchActionModeCallback
import org.wikipedia.notifications.NotificationActivity
Expand Down Expand Up @@ -187,7 +188,7 @@ class TalkTopicsActivity : BaseActivity(), WatchlistExpiryDialog.Callback {
viewModel.loadTopics()
}

invokeSource = intent.getSerializableExtra(Constants.INTENT_EXTRA_INVOKE_SOURCE) as Constants.InvokeSource
invokeSource = intent.serializableExtra(Constants.INTENT_EXTRA_INVOKE_SOURCE)!!

binding.talkNewTopicButton.isVisible = false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import org.wikipedia.analytics.eventplatform.AppearanceSettingInteractionEvent
import org.wikipedia.concurrency.FlowEventBus
import org.wikipedia.databinding.DialogThemeChooserBinding
import org.wikipedia.events.WebViewInvalidateEvent
import org.wikipedia.extensions.serializable
import org.wikipedia.page.ExtendedBottomSheetDialogFragment
import org.wikipedia.settings.Prefs
import org.wikipedia.util.DimenUtil
Expand Down Expand Up @@ -141,7 +142,7 @@ class ThemeChooserDialog : ExtendedBottomSheetDialogFragment() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
invokeSource = requireArguments().getSerializable(Constants.INTENT_EXTRA_INVOKE_SOURCE) as InvokeSource
invokeSource = requireArguments().serializable(Constants.INTENT_EXTRA_INVOKE_SOURCE)!!
appearanceSettingInteractionEvent = AppearanceSettingInteractionEvent(invokeSource)
}

Expand Down
Loading