Skip to content

Commit

Permalink
Add serializable extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Isira-Seneviratne committed Aug 21, 2024
1 parent 279c224 commit 28ebbd9
Show file tree
Hide file tree
Showing 29 changed files with 81 additions and 55 deletions.
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 @@ -13,6 +13,7 @@ import org.wikipedia.Constants
import org.wikipedia.dataclient.ServiceFactory
import org.wikipedia.descriptions.DescriptionEditActivity
import org.wikipedia.extensions.parcelable
import org.wikipedia.extensions.serializable
import org.wikipedia.suggestededits.PageSummaryForEdit
import org.wikipedia.util.Resource

Expand All @@ -22,7 +23,7 @@ class ImagePreviewViewModel(bundle: Bundle) : ViewModel() {
_uiState.value = Resource.Error(throwable)
}
var pageSummaryForEdit = bundle.parcelable<PageSummaryForEdit>(ImagePreviewDialog.ARG_SUMMARY)!!
var action = bundle.getSerializable(ImagePreviewDialog.ARG_ACTION) as DescriptionEditActivity.Action?
var action = bundle.serializable<DescriptionEditActivity.Action>(ImagePreviewDialog.ARG_ACTION)

private val _uiState = MutableStateFlow(Resource<FilePage>())
val uiState = _uiState.asStateFlow()
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.SingleFragmentActivity
import org.wikipedia.commons.ImagePreviewDialog
import org.wikipedia.extensions.parcelableExtra
import org.wikipedia.extensions.serializableExtra
import org.wikipedia.history.HistoryEntry
import org.wikipedia.page.ExclusiveBottomSheetPresenter
import org.wikipedia.page.PageTitle
Expand All @@ -24,7 +25,7 @@ class DescriptionEditActivity : SingleFragmentActivity<DescriptionEditFragment>(

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val action = intent.getSerializableExtra(Constants.INTENT_EXTRA_ACTION) as Action
val action = intent.serializableExtra<Action>(Constants.INTENT_EXTRA_ACTION)

if (action == Action.ADD_DESCRIPTION && Prefs.isDescriptionEditTutorialEnabled) {
Prefs.isDescriptionEditTutorialEnabled = false
Expand All @@ -33,15 +34,14 @@ class DescriptionEditActivity : SingleFragmentActivity<DescriptionEditFragment>(
}

public override fun createFragment(): DescriptionEditFragment {
val invokeSource = intent.getSerializableExtra(Constants.INTENT_EXTRA_INVOKE_SOURCE) as InvokeSource
val action = intent.getSerializableExtra(Constants.INTENT_EXTRA_ACTION) as Action
val title = intent.parcelableExtra<PageTitle>(Constants.ARG_TITLE)!!
return DescriptionEditFragment.newInstance(title,
intent.getStringExtra(EXTRA_HIGHLIGHT_TEXT),
intent.parcelableExtra(EXTRA_SOURCE_SUMMARY),
intent.parcelableExtra(EXTRA_TARGET_SUMMARY),
action,
invokeSource)
return DescriptionEditFragment.newInstance(
intent.parcelableExtra(Constants.ARG_TITLE)!!,
intent.getStringExtra(EXTRA_HIGHLIGHT_TEXT),
intent.parcelableExtra(EXTRA_SOURCE_SUMMARY),
intent.parcelableExtra(EXTRA_TARGET_SUMMARY),
intent.serializableExtra(Constants.INTENT_EXTRA_ACTION)!!,
intent.serializableExtra(Constants.INTENT_EXTRA_INVOKE_SOURCE)!!
)
}

override fun onBackPressed() {
Expand All @@ -67,8 +67,7 @@ class DescriptionEditActivity : SingleFragmentActivity<DescriptionEditFragment>(
} else {
ExclusiveBottomSheetPresenter.show(supportFragmentManager,
LinkPreviewDialog.newInstance(HistoryEntry(summary.pageTitle,
if (intent.hasExtra(Constants.INTENT_EXTRA_INVOKE_SOURCE) && intent.getSerializableExtra
(Constants.INTENT_EXTRA_INVOKE_SOURCE) === InvokeSource.PAGE_ACTIVITY)
if (intent.serializableExtra<InvokeSource>(Constants.INTENT_EXTRA_INVOKE_SOURCE) === InvokeSource.PAGE_ACTIVITY)
HistoryEntry.SOURCE_EDIT_DESCRIPTION else HistoryEntry.SOURCE_SUGGESTED_EDITS)))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import org.wikipedia.dataclient.okhttp.OkHttpConnectionFactory
import org.wikipedia.dataclient.wikidata.EntityPostResponse
import org.wikipedia.edit.EditTags
import org.wikipedia.extensions.parcelable
import org.wikipedia.extensions.serializable
import org.wikipedia.language.AppLanguageLookUpTable
import org.wikipedia.login.LoginActivity
import org.wikipedia.notifications.AnonymousNotificationHelper
Expand Down Expand Up @@ -132,8 +133,8 @@ class DescriptionEditFragment : Fragment() {
super.onCreate(savedInstanceState)
pageTitle = requireArguments().parcelable(Constants.ARG_TITLE)!!
highlightText = requireArguments().getString(ARG_HIGHLIGHT_TEXT)
action = requireArguments().getSerializable(ARG_ACTION) as DescriptionEditActivity.Action
invokeSource = requireArguments().getSerializable(Constants.INTENT_EXTRA_INVOKE_SOURCE) as InvokeSource
action = requireArguments().serializable(ARG_ACTION)!!
invokeSource = requireArguments().serializable(Constants.INTENT_EXTRA_INVOKE_SOURCE)!!
sourceSummary = requireArguments().parcelable(ARG_SOURCE_SUMMARY)
targetSummary = requireArguments().parcelable(ARG_TARGET_SUMMARY)
EditAttemptStepEvent.logInit(pageTitle, EditAttemptStepEvent.INTERFACE_OTHER)
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ import org.wikipedia.dataclient.wikidata.EntityPostResponse
import org.wikipedia.edit.Edit
import org.wikipedia.edit.EditTags
import org.wikipedia.extensions.parcelable
import org.wikipedia.extensions.serializable
import org.wikipedia.page.PageTitle
import org.wikipedia.suggestededits.provider.EditingSuggestionsProvider
import org.wikipedia.util.Resource
import org.wikipedia.util.SingleLiveData
import org.wikipedia.watchlist.WatchlistExpiry

class ArticleEditDetailsViewModel(bundle: Bundle) : ViewModel() {

private val invokeSource = bundle.getSerializable(Constants.INTENT_EXTRA_INVOKE_SOURCE) as InvokeSource
private val invokeSource = bundle.serializable<InvokeSource>(Constants.INTENT_EXTRA_INVOKE_SOURCE)

val watchedStatus = MutableLiveData<Resource<MwQueryPage>>()
val rollbackRights = MutableLiveData<Resource<Boolean>>()
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/org/wikipedia/edit/EditSectionActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import org.wikipedia.edit.preview.EditPreviewFragment
import org.wikipedia.edit.richtext.SyntaxHighlighter
import org.wikipedia.edit.summaries.EditSummaryFragment
import org.wikipedia.extensions.parcelableExtra
import org.wikipedia.extensions.serializableExtra
import org.wikipedia.history.HistoryEntry
import org.wikipedia.login.LoginActivity
import org.wikipedia.notifications.AnonymousNotificationHelper
Expand Down Expand Up @@ -182,7 +183,7 @@ class EditSectionActivity : BaseActivity(), ThemeChooserDialog.Callback, EditPre
sectionID = intent.getIntExtra(EXTRA_SECTION_ID, -1)
sectionAnchor = intent.getStringExtra(EXTRA_SECTION_ANCHOR)
textToHighlight = intent.getStringExtra(EXTRA_HIGHLIGHT_TEXT)
invokeSource = intent.getSerializableExtra(Constants.INTENT_EXTRA_INVOKE_SOURCE) as Constants.InvokeSource
invokeSource = intent.serializableExtra(Constants.INTENT_EXTRA_INVOKE_SOURCE)!!

setSupportActionBar(binding.toolbar)
supportActionBar?.title = ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ import org.wikipedia.dataclient.Service
import org.wikipedia.dataclient.ServiceFactory
import org.wikipedia.dataclient.WikiSite
import org.wikipedia.extensions.parcelable
import org.wikipedia.extensions.serializable
import org.wikipedia.page.PageTitle
import org.wikipedia.staticdata.FileAliasData
import org.wikipedia.util.L10nUtil
import org.wikipedia.util.StringUtil
import org.wikipedia.util.log.L

class InsertMediaViewModel(bundle: Bundle) : ViewModel() {

val invokeSource = bundle.getSerializable(Constants.INTENT_EXTRA_INVOKE_SOURCE) as Constants.InvokeSource
val invokeSource = bundle.serializable<Constants.InvokeSource>(Constants.INTENT_EXTRA_INVOKE_SOURCE)
val wikiSite = bundle.parcelable<WikiSite>(Constants.ARG_WIKISITE)!!
var searchQuery = StringUtil.removeHTMLTags(StringUtil.removeUnderscores(bundle.getString(InsertMediaActivity.EXTRA_SEARCH_QUERY)!!))
val originalSearchQuery = searchQuery
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ import org.wikipedia.dataclient.ServiceFactory
import org.wikipedia.dataclient.WikiSite
import org.wikipedia.dataclient.mwapi.TemplateDataResponse
import org.wikipedia.extensions.parcelable
import org.wikipedia.extensions.serializable
import org.wikipedia.page.Namespace
import org.wikipedia.page.PageTitle
import org.wikipedia.settings.Prefs

class TemplatesSearchViewModel(bundle: Bundle) : ViewModel() {

val invokeSource = bundle.getSerializable(Constants.INTENT_EXTRA_INVOKE_SOURCE) as Constants.InvokeSource
val invokeSource = bundle.serializable<Constants.InvokeSource>(Constants.INTENT_EXTRA_INVOKE_SOURCE)
val wikiSite = bundle.parcelable<WikiSite>(Constants.ARG_WIKISITE)!!
val isFromDiff = bundle.getBoolean(TemplatesSearchActivity.EXTRA_FROM_DIFF, false)
var searchQuery: String? = null
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 @@ -12,6 +12,7 @@ import org.wikipedia.Constants
import org.wikipedia.dataclient.ServiceFactory
import org.wikipedia.dataclient.WikiSite
import org.wikipedia.extensions.parcelable
import org.wikipedia.extensions.serializable
import org.wikipedia.util.DateUtil
import org.wikipedia.util.Resource
import java.util.Calendar
Expand All @@ -25,7 +26,7 @@ class OnThisDayViewModel(bundle: Bundle) : ViewModel() {
val wikiSite = bundle.parcelable<WikiSite>(Constants.ARG_WIKISITE)!!
val age = bundle.getInt(OnThisDayActivity.EXTRA_AGE, 0)
val year = bundle.getInt(OnThisDayActivity.EXTRA_YEAR, 0)
val invokeSource = bundle.getSerializable(Constants.INTENT_EXTRA_INVOKE_SOURCE) as Constants.InvokeSource
val invokeSource = bundle.serializable<Constants.InvokeSource>(Constants.INTENT_EXTRA_INVOKE_SOURCE)
val date = DateUtil.getDefaultDateFor(age)

private val _uiState = MutableStateFlow(Resource<List<OnThisDay.Event>>())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import org.wikipedia.dataclient.ServiceFactory
import org.wikipedia.dataclient.WikiSite
import org.wikipedia.dataclient.mwapi.MwQueryPage
import org.wikipedia.descriptions.DescriptionEditActivity
import org.wikipedia.extensions.serializable
import org.wikipedia.page.Namespace
import org.wikipedia.page.PageTitle
import org.wikipedia.suggestededits.PageSummaryForEdit
Expand All @@ -22,9 +23,8 @@ import org.wikipedia.util.Resource
import org.wikipedia.util.StringUtil

class SuggestedEditsCardItemViewModel(bundle: Bundle) : ViewModel() {

val age = bundle.getInt(SuggestedEditsCardItemFragment.EXTRA_AGE)
var cardActionType = bundle.getSerializable(SuggestedEditsCardItemFragment.EXTRA_ACTION_TYPE) as DescriptionEditActivity.Action
var cardActionType = bundle.serializable<DescriptionEditActivity.Action>(SuggestedEditsCardItemFragment.EXTRA_ACTION_TYPE)
var sourceSummaryForEdit: PageSummaryForEdit? = null
var targetSummaryForEdit: PageSummaryForEdit? = null
var imageTagPage: MwQueryPage? = null
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/org/wikipedia/gallery/GalleryActivity.kt
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 @@ -54,6 +54,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 @@ -145,11 +146,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
Loading

0 comments on commit 28ebbd9

Please sign in to comment.