Skip to content

Commit

Permalink
Use id from existing views instead of "hard-coded" values
Browse files Browse the repository at this point in the history
And improve our extension
  • Loading branch information
bmarty committed Dec 7, 2021
1 parent 147935d commit d3a1746
Show file tree
Hide file tree
Showing 41 changed files with 196 additions and 233 deletions.
1 change: 1 addition & 0 deletions changelog.d/4650.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Cleanup id ref. Use type views instead
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ class ${activityClass} : VectorBaseActivity(), ToolbarConfigurable {
<#if createFragmentArgs>
val fragmentArgs: ${fragmentArgsClass} = intent?.extras?.getParcelable(EXTRA_FRAGMENT_ARGS)
?: return
addFragment(R.id.simpleFragmentContainer, ${fragmentClass}::class.java, fragmentArgs)
addFragment(views.simpleFragmentContainer.id, ${fragmentClass}::class.java, fragmentArgs)
<#else>
addFragment(R.id.simpleFragmentContainer, ${fragmentClass}::class.java)
addFragment(views.simpleFragmentContainer.id, ${fragmentClass}::class.java)
</#if>
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package im.vector.app.features.debug.settings

import dagger.hilt.android.AndroidEntryPoint
import im.vector.app.R
import im.vector.app.core.extensions.addFragment
import im.vector.app.core.platform.VectorBaseActivity
import im.vector.app.databinding.ActivitySimpleBinding
Expand All @@ -30,7 +29,7 @@ class DebugPrivateSettingsActivity : VectorBaseActivity<ActivitySimpleBinding>()
override fun initUiAndData() {
if (isFirstCreation()) {
addFragment(
R.id.simpleFragmentContainer,
views.simpleFragmentContainer,
DebugPrivateSettingsFragment::class.java
)
}
Expand Down
46 changes: 24 additions & 22 deletions vector/src/main/java/im/vector/app/core/extensions/Activity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ package im.vector.app.core.extensions
import android.app.Activity
import android.content.Intent
import android.os.Parcelable
import android.view.View
import android.view.ViewGroup
import android.view.ViewParent
import android.view.WindowManager
import androidx.activity.ComponentActivity
import androidx.activity.result.ActivityResult
Expand All @@ -33,65 +36,64 @@ fun ComponentActivity.registerStartForActivityResult(onResult: (ActivityResult)
}

fun AppCompatActivity.addFragment(
frameId: Int,
container: ViewGroup,
fragment: Fragment,
allowStateLoss: Boolean = false
) {
supportFragmentManager.commitTransaction(allowStateLoss) { add(frameId, fragment) }
allowStateLoss: Boolean = false) {
supportFragmentManager.commitTransaction(allowStateLoss) { add(container.id, fragment) }
}

fun <T : Fragment> AppCompatActivity.addFragment(
frameId: Int,
container: ViewGroup,
fragmentClass: Class<T>,
params: Parcelable? = null,
tag: String? = null,
allowStateLoss: Boolean = false
) {
allowStateLoss: Boolean = false) {
supportFragmentManager.commitTransaction(allowStateLoss) {
add(frameId, fragmentClass, params.toMvRxBundle(), tag)
add(container.id, fragmentClass, params.toMvRxBundle(), tag)
}
}

fun AppCompatActivity.replaceFragment(
frameId: Int,
container: ViewGroup,
fragment: Fragment,
tag: String? = null,
allowStateLoss: Boolean = false
) {
supportFragmentManager.commitTransaction(allowStateLoss) { replace(frameId, fragment, tag) }
allowStateLoss: Boolean = false) {
supportFragmentManager.commitTransaction(allowStateLoss) {
replace(container.id, fragment, tag)
}
}

fun <T : Fragment> AppCompatActivity.replaceFragment(
frameId: Int,
container: ViewGroup,
fragmentClass: Class<T>,
params: Parcelable? = null,
tag: String? = null,
allowStateLoss: Boolean = false
) {
allowStateLoss: Boolean = false) {
supportFragmentManager.commitTransaction(allowStateLoss) {
replace(frameId, fragmentClass, params.toMvRxBundle(), tag)
replace(container.id, fragmentClass, params.toMvRxBundle(), tag)
}
}

fun AppCompatActivity.addFragmentToBackstack(
frameId: Int,
container: ViewGroup,
fragment: Fragment,
tag: String? = null,
allowStateLoss: Boolean = false
) {
supportFragmentManager.commitTransaction(allowStateLoss) { replace(frameId, fragment).addToBackStack(tag) }
allowStateLoss: Boolean = false) {
supportFragmentManager.commitTransaction(allowStateLoss) {
replace(container.id, fragment).addToBackStack(tag)
}
}

fun <T : Fragment> AppCompatActivity.addFragmentToBackstack(
frameId: Int,
container: ViewGroup,
fragmentClass: Class<T>,
params: Parcelable? = null,
tag: String? = null,
allowStateLoss: Boolean = false,
option: ((FragmentTransaction) -> Unit)? = null) {
supportFragmentManager.commitTransaction(allowStateLoss) {
option?.invoke(this)
replace(frameId, fragmentClass, params.toMvRxBundle(), tag).addToBackStack(tag)
replace(container.id, fragmentClass, params.toMvRxBundle(), tag).addToBackStack(tag)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import android.content.Context
import android.content.Intent
import com.google.android.material.appbar.MaterialToolbar
import dagger.hilt.android.AndroidEntryPoint
import im.vector.app.R
import im.vector.app.core.extensions.addFragment
import im.vector.app.core.platform.ToolbarConfigurable
import im.vector.app.core.platform.VectorBaseActivity
Expand Down Expand Up @@ -61,7 +60,7 @@ class AttachmentsPreviewActivity : VectorBaseActivity<ActivitySimpleBinding>(),
override fun initUiAndData() {
if (isFirstCreation()) {
val fragmentArgs: AttachmentsPreviewArgs = intent?.extras?.getParcelable(EXTRA_FRAGMENT_ARGS) ?: return
addFragment(R.id.simpleFragmentContainer, AttachmentsPreviewFragment::class.java, fragmentArgs)
addFragment(views.simpleFragmentContainer, AttachmentsPreviewFragment::class.java, fragmentArgs)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class ReAuthActivity : SimpleFragmentActivity() {
// so we assume that after the user close the tab we return success and let caller retry the UIA flow :/
if (isFirstCreation()) {
addFragment(
R.id.container,
views.container,
PromptFragment::class.java
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class CreateDirectRoomActivity : SimpleFragmentActivity() {
.launchIn(lifecycleScope)
if (isFirstCreation()) {
addFragment(
R.id.container,
views.container,
UserListFragment::class.java,
UserListFragmentArgs(
title = getString(R.string.fab_menu_create_chat),
Expand All @@ -95,28 +95,28 @@ class CreateDirectRoomActivity : SimpleFragmentActivity() {

private fun openAddByQrCode() {
if (checkPermissions(PERMISSIONS_FOR_TAKING_PHOTO, this, permissionCameraLauncher)) {
addFragment(R.id.container, CreateDirectRoomByQrCodeFragment::class.java)
addFragment(views.container, CreateDirectRoomByQrCodeFragment::class.java)
}
}

private fun openPhoneBook() {
// Check permission first
if (checkPermissions(PERMISSIONS_FOR_MEMBERS_SEARCH, this, permissionReadContactLauncher)) {
addFragmentToBackstack(R.id.container, ContactsBookFragment::class.java)
addFragmentToBackstack(views.container, ContactsBookFragment::class.java)
}
}

private val permissionReadContactLauncher = registerForPermissionsResult { allGranted, deniedPermanently ->
if (allGranted) {
doOnPostResume { addFragmentToBackstack(R.id.container, ContactsBookFragment::class.java) }
doOnPostResume { addFragmentToBackstack(views.container, ContactsBookFragment::class.java) }
} else if (deniedPermanently) {
onPermissionDeniedSnackbar(R.string.permissions_denied_add_contact)
}
}

private val permissionCameraLauncher = registerForPermissionsResult { allGranted, deniedPermanently ->
if (allGranted) {
addFragment(R.id.container, CreateDirectRoomByQrCodeFragment::class.java)
addFragment(views.container, CreateDirectRoomByQrCodeFragment::class.java)
} else if (deniedPermanently) {
onPermissionDeniedSnackbar(R.string.permissions_denied_qr_code)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ class KeysBackupRestoreActivity : SimpleFragmentActivity() {
val isBackupCreatedFromPassphrase =
viewModel.keyVersionResult.value?.getAuthDataAsMegolmBackupAuthData()?.privateKeySalt != null
if (isBackupCreatedFromPassphrase) {
replaceFragment(R.id.container, KeysBackupRestoreFromPassphraseFragment::class.java)
replaceFragment(views.container, KeysBackupRestoreFromPassphraseFragment::class.java)
} else {
replaceFragment(R.id.container, KeysBackupRestoreFromKeyFragment::class.java)
replaceFragment(views.container, KeysBackupRestoreFromKeyFragment::class.java)
}
}
}
Expand All @@ -91,13 +91,13 @@ class KeysBackupRestoreActivity : SimpleFragmentActivity() {
viewModel.navigateEvent.observeEvent(this) { uxStateEvent ->
when (uxStateEvent) {
KeysBackupRestoreSharedViewModel.NAVIGATE_TO_RECOVER_WITH_KEY -> {
addFragmentToBackstack(R.id.container, KeysBackupRestoreFromKeyFragment::class.java, allowStateLoss = true)
addFragmentToBackstack(views.container, KeysBackupRestoreFromKeyFragment::class.java, allowStateLoss = true)
}
KeysBackupRestoreSharedViewModel.NAVIGATE_TO_SUCCESS -> {
viewModel.keyVersionResult.value?.version?.let {
KeysBackupBanner.onRecoverDoneForVersion(this, it)
}
replaceFragment(R.id.container, KeysBackupRestoreSuccessFragment::class.java, allowStateLoss = true)
replaceFragment(views.container, KeysBackupRestoreSuccessFragment::class.java, allowStateLoss = true)
}
KeysBackupRestoreSharedViewModel.NAVIGATE_TO_4S -> {
launch4SActivity()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class KeysBackupManageActivity : SimpleFragmentActivity() {
override fun initUiAndData() {
super.initUiAndData()
if (supportFragmentManager.fragments.isEmpty()) {
replaceFragment(R.id.container, KeysBackupSettingsFragment::class.java)
replaceFragment(views.container, KeysBackupSettingsFragment::class.java)
viewModel.handle(KeyBackupSettingsAction.Init)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class KeysBackupSetupActivity : SimpleFragmentActivity() {
override fun initUiAndData() {
super.initUiAndData()
if (isFirstCreation()) {
replaceFragment(R.id.container, KeysBackupSetupStep1Fragment::class.java)
replaceFragment(views.container, KeysBackupSetupStep1Fragment::class.java)
}

viewModel = viewModelProvider.get(KeysBackupSetupSharedViewModel::class.java)
Expand All @@ -80,11 +80,11 @@ class KeysBackupSetupActivity : SimpleFragmentActivity() {
when (uxStateEvent) {
KeysBackupSetupSharedViewModel.NAVIGATE_TO_STEP_2 -> {
supportFragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)
replaceFragment(R.id.container, KeysBackupSetupStep2Fragment::class.java)
replaceFragment(views.container, KeysBackupSetupStep2Fragment::class.java)
}
KeysBackupSetupSharedViewModel.NAVIGATE_TO_STEP_3 -> {
supportFragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)
replaceFragment(R.id.container, KeysBackupSetupStep3Fragment::class.java)
replaceFragment(views.container, KeysBackupSetupStep3Fragment::class.java)
}
KeysBackupSetupSharedViewModel.NAVIGATE_FINISH -> {
val resultIntent = Intent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder
import dagger.hilt.android.AndroidEntryPoint
import im.vector.app.R
import im.vector.app.core.error.ErrorFormatter
import im.vector.app.core.extensions.commitTransaction
import im.vector.app.core.extensions.replaceFragment
import im.vector.app.core.platform.SimpleFragmentActivity
import im.vector.app.core.platform.VectorBaseBottomSheetDialogFragment
import im.vector.app.features.crypto.recover.SetupMode
Expand Down Expand Up @@ -84,7 +84,7 @@ class SharedSecureStorageActivity :
SharedSecureStorageViewState.Step.ResetAll -> SharedSecuredStorageResetAllFragment::class
}

showFragment(fragment, Bundle())
showFragment(fragment)
}

private fun observeViewEvents(it: SharedSecureStorageViewEvent?) {
Expand Down Expand Up @@ -131,15 +131,14 @@ class SharedSecureStorageActivity :
}
}

private fun showFragment(fragmentClass: KClass<out Fragment>, bundle: Bundle) {
private fun showFragment(fragmentClass: KClass<out Fragment>) {
if (supportFragmentManager.findFragmentByTag(fragmentClass.simpleName) == null) {
supportFragmentManager.commitTransaction {
replace(R.id.container,
fragmentClass.java,
bundle,
fragmentClass.simpleName
)
}
replaceFragment(
views.container,
fragmentClass.java,
null,
fragmentClass.simpleName
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class RoomDevToolActivity : SimpleFragmentActivity(), FragmentManager.OnBackStac
val classJava = RoomDevToolFragment::class.java
val tag = classJava.name
if (supportFragmentManager.findFragmentByTag(tag) == null) {
replaceFragment(R.id.container, RoomDevToolFragment::class.java)
replaceFragment(views.container, RoomDevToolFragment::class.java)
} else {
supportFragmentManager.popBackStack()
}
Expand Down Expand Up @@ -155,14 +155,14 @@ class RoomDevToolActivity : SimpleFragmentActivity(), FragmentManager.OnBackStac
if (supportFragmentManager.findFragmentByTag(tag) == null) {
supportFragmentManager.beginTransaction()
.setCustomAnimations(R.anim.fade_in, R.anim.fade_out, R.anim.fade_in, R.anim.fade_out)
.replace(R.id.container, fragment, tag)
.replace(views.container.id, fragment, tag)
.addToBackStack(tag)
.commit()
} else {
if (!supportFragmentManager.popBackStackImmediate(tag, 0)) {
supportFragmentManager.beginTransaction()
.setCustomAnimations(R.anim.fade_in, R.anim.fade_out, R.anim.fade_in, R.anim.fade_out)
.replace(R.id.container, fragment, tag)
.replace(views.container.id, fragment, tag)
.addToBackStack(tag)
.commit()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ class HomeActivity :
sharedActionViewModel = viewModelProvider.get(HomeSharedActionViewModel::class.java)
views.drawerLayout.addDrawerListener(drawerListener)
if (isFirstCreation()) {
replaceFragment(R.id.homeDetailFragmentContainer, HomeDetailFragment::class.java)
replaceFragment(R.id.homeDrawerFragmentContainer, HomeDrawerFragment::class.java)
replaceFragment(views.homeDetailFragmentContainer, HomeDetailFragment::class.java)
replaceFragment(views.homeDrawerFragmentContainer, HomeDrawerFragment::class.java)
}

sharedActionViewModel
Expand All @@ -192,7 +192,7 @@ class HomeActivity :
// When switching from space to group or group to space, we need to reload the fragment
// To be removed when dropping legacy groups
if (sharedAction.clearFragment) {
replaceFragment(R.id.homeDetailFragmentContainer, HomeDetailFragment::class.java, allowStateLoss = true)
replaceFragment(views.homeDetailFragmentContainer, HomeDetailFragment::class.java, allowStateLoss = true)
} else {
// nop
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import com.airbnb.mvrx.Mavericks
import com.airbnb.mvrx.viewModel
import com.google.android.material.appbar.MaterialToolbar
import dagger.hilt.android.AndroidEntryPoint
import im.vector.app.R
import im.vector.app.core.extensions.endKeepScreenOn
import im.vector.app.core.extensions.hideKeyboard
import im.vector.app.core.extensions.keepScreenOn
Expand Down Expand Up @@ -108,8 +107,8 @@ class RoomDetailActivity :
currentRoomId = roomDetailArgs.roomId

if (isFirstCreation()) {
replaceFragment(R.id.roomDetailContainer, RoomDetailFragment::class.java, roomDetailArgs)
replaceFragment(R.id.roomDetailDrawerContainer, BreadcrumbsFragment::class.java)
replaceFragment(views.roomDetailContainer, RoomDetailFragment::class.java, roomDetailArgs)
replaceFragment(views.roomDetailDrawerContainer, BreadcrumbsFragment::class.java)
}

sharedActionViewModel = viewModelProvider.get(RoomDetailSharedActionViewModel::class.java)
Expand Down Expand Up @@ -146,7 +145,7 @@ class RoomDetailActivity :
if (currentRoomId != switchToRoom.roomId) {
currentRoomId = switchToRoom.roomId
requireActiveMembershipViewModel.handle(RequireActiveMembershipAction.ChangeRoom(switchToRoom.roomId))
replaceFragment(R.id.roomDetailContainer, RoomDetailFragment::class.java, RoomDetailArgs(switchToRoom.roomId))
replaceFragment(views.roomDetailContainer, RoomDetailFragment::class.java, RoomDetailArgs(switchToRoom.roomId))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import android.os.Bundle
import androidx.appcompat.widget.SearchView
import com.airbnb.mvrx.Mavericks
import dagger.hilt.android.AndroidEntryPoint
import im.vector.app.R
import im.vector.app.core.extensions.addFragment
import im.vector.app.core.platform.VectorBaseActivity
import im.vector.app.databinding.ActivitySearchBinding
Expand All @@ -47,7 +46,7 @@ class SearchActivity : VectorBaseActivity<ActivitySearchBinding>() {
override fun initUiAndData() {
if (isFirstCreation()) {
val fragmentArgs: SearchArgs = intent?.extras?.getParcelable(Mavericks.KEY_ARG) ?: return
addFragment(R.id.searchFragmentContainer, SearchFragment::class.java, fragmentArgs, FRAGMENT_TAG)
addFragment(views.searchFragmentContainer, SearchFragment::class.java, fragmentArgs, FRAGMENT_TAG)
}
views.searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String): Boolean {
Expand Down
Loading

0 comments on commit d3a1746

Please sign in to comment.