converting the settings activity payload to a sealed class, this allows us to have custom arguments for the sub settings pages

This commit is contained in:
Adam Brown 2021-10-07 13:07:30 +01:00
parent 05166944d8
commit 728f34f53c
3 changed files with 59 additions and 12 deletions

View File

@ -353,6 +353,11 @@ class DefaultNavigator @Inject constructor(
context.startActivity(intent)
}
override fun openSettings(context: Context, payload: SettingsActivityPayload) {
val intent = VectorSettingsActivity.getIntent(context, payload)
context.startActivity(intent)
}
override fun openDebug(context: Context) {
context.startActivity(Intent(context, DebugMenuActivity::class.java))
}

View File

@ -19,6 +19,7 @@ package im.vector.app.features.navigation
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Parcelable
import android.view.View
import androidx.activity.result.ActivityResultLauncher
import androidx.core.util.Pair
@ -31,6 +32,7 @@ import im.vector.app.features.roomdirectory.RoomDirectoryData
import im.vector.app.features.roomdirectory.roompreview.RoomPreviewData
import im.vector.app.features.settings.VectorSettingsActivity
import im.vector.app.features.share.SharedData
import kotlinx.parcelize.Parcelize
import org.matrix.android.sdk.api.session.permalinks.PermalinkData
import org.matrix.android.sdk.api.session.room.model.roomdirectory.PublicRoom
import org.matrix.android.sdk.api.session.terms.TermsService
@ -83,6 +85,8 @@ interface Navigator {
fun openSettings(context: Context, directAccess: Int = VectorSettingsActivity.EXTRA_DIRECT_ACCESS_ROOT)
fun openSettings(context: Context, payload: SettingsActivityPayload)
fun openDebug(context: Context)
fun openKeysBackupSetup(context: Context, showManualExport: Boolean)
@ -139,3 +143,14 @@ interface Navigator {
fun openCallTransfer(context: Context, callId: String)
}
sealed interface SettingsActivityPayload : Parcelable {
@Parcelize object Root : SettingsActivityPayload
@Parcelize object AdvancedSettings : SettingsActivityPayload
@Parcelize object SecurityPrivacy : SettingsActivityPayload
@Parcelize object SecurityPrivacyManageSessions : SettingsActivityPayload
@Parcelize object General : SettingsActivityPayload
@Parcelize object Notifications : SettingsActivityPayload
@Parcelize object DiscoverySettings : SettingsActivityPayload
}

View File

@ -15,8 +15,10 @@
*/
package im.vector.app.features.settings
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Parcelable
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.preference.Preference
@ -27,14 +29,18 @@ import im.vector.app.core.extensions.replaceFragment
import im.vector.app.core.platform.VectorBaseActivity
import im.vector.app.databinding.ActivityVectorSettingsBinding
import im.vector.app.features.discovery.DiscoverySettingsFragment
import im.vector.app.features.navigation.SettingsActivityPayload
import im.vector.app.features.settings.devices.VectorSettingsDevicesFragment
import im.vector.app.features.settings.notifications.VectorSettingsNotificationPreferenceFragment
import im.vector.app.features.settings.threepids.ThreePidsSettingsFragment
import kotlinx.parcelize.Parcelize
import org.matrix.android.sdk.api.failure.GlobalError
import org.matrix.android.sdk.api.session.Session
import timber.log.Timber
import javax.inject.Inject
private const val KEY_ACTIVITY_PAYLOAD = "settings-activity-payload"
/**
* Displays the client settings.
*/
@ -64,27 +70,27 @@ class VectorSettingsActivity : VectorBaseActivity<ActivityVectorSettingsBinding>
if (isFirstCreation()) {
// display the fragment
when (intent.getIntExtra(EXTRA_DIRECT_ACCESS, EXTRA_DIRECT_ACCESS_ROOT)) {
EXTRA_DIRECT_ACCESS_GENERAL ->
when (readPayload<SettingsActivityPayload>(SettingsActivityPayload.Root)) {
SettingsActivityPayload.General ->
replaceFragment(R.id.vector_settings_page, VectorSettingsGeneralFragment::class.java, null, FRAGMENT_TAG)
EXTRA_DIRECT_ACCESS_ADVANCED_SETTINGS ->
SettingsActivityPayload.AdvancedSettings ->
replaceFragment(R.id.vector_settings_page, VectorSettingsAdvancedSettingsFragment::class.java, null, FRAGMENT_TAG)
EXTRA_DIRECT_ACCESS_SECURITY_PRIVACY ->
SettingsActivityPayload.SecurityPrivacy ->
replaceFragment(R.id.vector_settings_page, VectorSettingsSecurityPrivacyFragment::class.java, null, FRAGMENT_TAG)
EXTRA_DIRECT_ACCESS_SECURITY_PRIVACY_MANAGE_SESSIONS ->
SettingsActivityPayload.SecurityPrivacyManageSessions ->
replaceFragment(R.id.vector_settings_page,
VectorSettingsDevicesFragment::class.java,
null,
FRAGMENT_TAG)
EXTRA_DIRECT_ACCESS_NOTIFICATIONS -> {
SettingsActivityPayload.Notifications -> {
requestHighlightPreferenceKeyOnResume(VectorPreferences.SETTINGS_ENABLE_THIS_DEVICE_PREFERENCE_KEY)
replaceFragment(R.id.vector_settings_page, VectorSettingsNotificationPreferenceFragment::class.java, null, FRAGMENT_TAG)
}
EXTRA_DIRECT_ACCESS_DISCOVERY_SETTINGS -> {
SettingsActivityPayload.DiscoverySettings -> {
replaceFragment(R.id.vector_settings_page, DiscoverySettingsFragment::class.java, null, FRAGMENT_TAG)
}
else ->
else ->
replaceFragment(R.id.vector_settings_page, VectorSettingsRootFragment::class.java, null, FRAGMENT_TAG)
}
}
@ -157,10 +163,22 @@ class VectorSettingsActivity : VectorBaseActivity<ActivityVectorSettingsBinding>
}
companion object {
fun getIntent(context: Context, directAccess: Int) = Intent(context, VectorSettingsActivity::class.java)
.apply { putExtra(EXTRA_DIRECT_ACCESS, directAccess) }
fun getIntent(context: Context, directAccess: Int) = Companion.getIntent(context, when (directAccess) {
EXTRA_DIRECT_ACCESS_ROOT -> SettingsActivityPayload.Root
EXTRA_DIRECT_ACCESS_ADVANCED_SETTINGS -> SettingsActivityPayload.AdvancedSettings
EXTRA_DIRECT_ACCESS_SECURITY_PRIVACY -> SettingsActivityPayload.SecurityPrivacy
EXTRA_DIRECT_ACCESS_SECURITY_PRIVACY_MANAGE_SESSIONS -> SettingsActivityPayload.SecurityPrivacyManageSessions
EXTRA_DIRECT_ACCESS_GENERAL -> SettingsActivityPayload.General
EXTRA_DIRECT_ACCESS_NOTIFICATIONS -> SettingsActivityPayload.Notifications
EXTRA_DIRECT_ACCESS_DISCOVERY_SETTINGS -> SettingsActivityPayload.DiscoverySettings
else -> {
Timber.w("Unknown directAccess: $directAccess defaulting to Root")
SettingsActivityPayload.Root
}
})
private const val EXTRA_DIRECT_ACCESS = "EXTRA_DIRECT_ACCESS"
fun getIntent(context: Context, payload: SettingsActivityPayload) = Intent(context, VectorSettingsActivity::class.java)
.applyPayload(payload)
const val EXTRA_DIRECT_ACCESS_ROOT = 0
const val EXTRA_DIRECT_ACCESS_ADVANCED_SETTINGS = 1
@ -173,3 +191,12 @@ class VectorSettingsActivity : VectorBaseActivity<ActivityVectorSettingsBinding>
private const val FRAGMENT_TAG = "VectorSettingsPreferencesFragment"
}
}
private fun <T : Parcelable> Activity.readPayload(default: T): T {
return intent.getParcelableExtra(KEY_ACTIVITY_PAYLOAD) ?: default
}
private fun <T : Parcelable> Intent.applyPayload(payload: T): Intent {
return putExtra(KEY_ACTIVITY_PAYLOAD, payload)
}