From a1882b83f99cfac6e145da0ff47c8feb2fb919ad Mon Sep 17 00:00:00 2001 From: Andrew Date: Mon, 17 Jun 2024 18:56:14 +0930 Subject: [PATCH] Rename LoadAccount --- app/src/main/AndroidManifest.xml | 2 +- .../securesms/onboarding/LinkDeviceState.kt | 6 -- .../onboarding/landing/LandingActivity.kt | 4 +- .../LoadAccount.kt} | 97 +++++-------------- .../loadaccount/LoadAccountActivity.kt | 49 ++++++++++ .../LoadAccountViewModel.kt} | 13 ++- 6 files changed, 85 insertions(+), 86 deletions(-) delete mode 100644 app/src/main/java/org/thoughtcrime/securesms/onboarding/LinkDeviceState.kt rename app/src/main/java/org/thoughtcrime/securesms/onboarding/{LinkDeviceActivity.kt => loadaccount/LoadAccount.kt} (51%) create mode 100644 app/src/main/java/org/thoughtcrime/securesms/onboarding/loadaccount/LoadAccountActivity.kt rename app/src/main/java/org/thoughtcrime/securesms/onboarding/{LinkDeviceViewModel.kt => loadaccount/LoadAccountViewModel.kt} (90%) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index eaccd7372e..67e7bff74b 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -108,7 +108,7 @@ android:screenOrientation="portrait" android:theme="@style/Theme.Session.DayNight.FlatActionBar" /> diff --git a/app/src/main/java/org/thoughtcrime/securesms/onboarding/LinkDeviceState.kt b/app/src/main/java/org/thoughtcrime/securesms/onboarding/LinkDeviceState.kt deleted file mode 100644 index 5627e31bbc..0000000000 --- a/app/src/main/java/org/thoughtcrime/securesms/onboarding/LinkDeviceState.kt +++ /dev/null @@ -1,6 +0,0 @@ -package org.thoughtcrime.securesms.onboarding - -data class LinkDeviceState( - val recoveryPhrase: String = "", - val error: String? = null -) diff --git a/app/src/main/java/org/thoughtcrime/securesms/onboarding/landing/LandingActivity.kt b/app/src/main/java/org/thoughtcrime/securesms/onboarding/landing/LandingActivity.kt index cedde6879a..e47dbb9869 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/onboarding/landing/LandingActivity.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/onboarding/landing/LandingActivity.kt @@ -7,7 +7,7 @@ import dagger.hilt.android.AndroidEntryPoint import org.session.libsession.utilities.TextSecurePreferences import org.thoughtcrime.securesms.BaseActionBarActivity import org.thoughtcrime.securesms.crypto.IdentityKeyUtil -import org.thoughtcrime.securesms.onboarding.LinkDeviceActivity +import org.thoughtcrime.securesms.onboarding.loadaccount.LoadAccountActivity import org.thoughtcrime.securesms.onboarding.pickname.startPickDisplayNameActivity import org.thoughtcrime.securesms.service.KeyCachingService import org.thoughtcrime.securesms.ui.setComposeContent @@ -35,7 +35,7 @@ class LandingActivity: BaseActionBarActivity() { prefs.setHasViewedSeed(false) startPickDisplayNameActivity() }, - loadAccount = { start() }, + loadAccount = { start() }, openTerms = { open("https://getsession.org/terms-of-service") }, openPrivacyPolicy = { open("https://getsession.org/privacy-policy") } ) diff --git a/app/src/main/java/org/thoughtcrime/securesms/onboarding/LinkDeviceActivity.kt b/app/src/main/java/org/thoughtcrime/securesms/onboarding/loadaccount/LoadAccount.kt similarity index 51% rename from app/src/main/java/org/thoughtcrime/securesms/onboarding/LinkDeviceActivity.kt rename to app/src/main/java/org/thoughtcrime/securesms/onboarding/loadaccount/LoadAccount.kt index da1bb483af..b43badbb96 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/onboarding/LinkDeviceActivity.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/onboarding/loadaccount/LoadAccount.kt @@ -1,8 +1,5 @@ -package org.thoughtcrime.securesms.onboarding +package org.thoughtcrime.securesms.onboarding.loadaccount -import android.os.Bundle -import androidx.activity.viewModels -import androidx.camera.core.ExperimentalGetImage import androidx.compose.foundation.ExperimentalFoundationApi import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row @@ -16,92 +13,46 @@ import androidx.compose.foundation.pager.rememberPagerState import androidx.compose.material.Icon import androidx.compose.material.Text import androidx.compose.runtime.Composable -import androidx.compose.runtime.collectAsState -import androidx.compose.runtime.getValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.stringResource import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp -import androidx.lifecycle.lifecycleScope -import dagger.hilt.android.AndroidEntryPoint -import kotlinx.coroutines.launch +import kotlinx.coroutines.flow.Flow import network.loki.messenger.R -import org.session.libsession.utilities.TextSecurePreferences -import org.thoughtcrime.securesms.BaseActionBarActivity -import org.thoughtcrime.securesms.onboarding.loading.LoadingManager -import org.thoughtcrime.securesms.onboarding.messagenotifications.startMessageNotificationsActivity import org.thoughtcrime.securesms.onboarding.ui.ContinueButton import org.thoughtcrime.securesms.ui.LocalDimensions import org.thoughtcrime.securesms.ui.PreviewTheme import org.thoughtcrime.securesms.ui.base import org.thoughtcrime.securesms.ui.components.MaybeScanQrCode -import org.thoughtcrime.securesms.ui.components.OutlineButton import org.thoughtcrime.securesms.ui.components.SessionOutlinedTextField import org.thoughtcrime.securesms.ui.components.SessionTabRow import org.thoughtcrime.securesms.ui.contentDescription import org.thoughtcrime.securesms.ui.h4 -import org.thoughtcrime.securesms.ui.setComposeContent -import javax.inject.Inject - -private const val TAG = "LinkDeviceActivity" private val TITLES = listOf(R.string.sessionRecoveryPassword, R.string.qrScan) -@AndroidEntryPoint -@androidx.annotation.OptIn(ExperimentalGetImage::class) -class LinkDeviceActivity : BaseActionBarActivity() { +@OptIn(ExperimentalFoundationApi::class) +@Composable +internal fun LoadAccountScreen( + state: State, + qrErrors: Flow, + onChange: (String) -> Unit = {}, + onContinue: () -> Unit = {}, + onScan: (String) -> Unit = {} +) { + val pagerState = rememberPagerState { TITLES.size } - @Inject lateinit var prefs: TextSecurePreferences - @Inject lateinit var loadingManager: LoadingManager - - val viewModel: LinkDeviceViewModel by viewModels() - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - supportActionBar?.setTitle(R.string.activity_link_load_account) - prefs.setHasViewedSeed(true) - prefs.setConfigurationMessageSynced(false) - prefs.setRestorationTime(System.currentTimeMillis()) - prefs.setLastProfileUpdateTime(0) - - lifecycleScope.launch { - viewModel.eventFlow.collect { - loadingManager.load(it.mnemonic) - startMessageNotificationsActivity() - finish() - } - } - - setComposeContent { - val state by viewModel.stateFlow.collectAsState() - LoadAccountScreen(state, viewModel::onChange, viewModel::onContinue, viewModel::onScanQrCode) - } - } - - @OptIn(ExperimentalFoundationApi::class) - @Composable - fun LoadAccountScreen( - state: LinkDeviceState, - onChange: (String) -> Unit = {}, - onContinue: () -> Unit = {}, - onScan: (String) -> Unit = {} - ) { - val pagerState = rememberPagerState { TITLES.size } - - Column { - SessionTabRow(pagerState, TITLES) - HorizontalPager( - state = pagerState, - modifier = Modifier.weight(1f) - ) { page -> - val title = TITLES[page] - - when (title) { - R.string.sessionRecoveryPassword -> RecoveryPassword(state, onChange, onContinue) - R.string.qrScan -> MaybeScanQrCode(viewModel.qrErrorsFlow, onScan = onScan) - } + Column { + SessionTabRow(pagerState, TITLES) + HorizontalPager( + state = pagerState, + modifier = Modifier.weight(1f) + ) { page -> + when (TITLES[page]) { + R.string.sessionRecoveryPassword -> RecoveryPassword(state, onChange, onContinue) + R.string.qrScan -> MaybeScanQrCode(qrErrors, onScan = onScan) } } } @@ -109,14 +60,14 @@ class LinkDeviceActivity : BaseActionBarActivity() { @Preview @Composable -fun PreviewRecoveryPassword() { +private fun PreviewRecoveryPassword() { PreviewTheme { - RecoveryPassword(state = LinkDeviceState()) + RecoveryPassword(state = State()) } } @Composable -fun RecoveryPassword(state: LinkDeviceState, onChange: (String) -> Unit = {}, onContinue: () -> Unit = {}) { +private fun RecoveryPassword(state: State, onChange: (String) -> Unit = {}, onContinue: () -> Unit = {}) { Column { Column( modifier = Modifier.padding(horizontal = LocalDimensions.current.marginLarge) diff --git a/app/src/main/java/org/thoughtcrime/securesms/onboarding/loadaccount/LoadAccountActivity.kt b/app/src/main/java/org/thoughtcrime/securesms/onboarding/loadaccount/LoadAccountActivity.kt new file mode 100644 index 0000000000..91f1001142 --- /dev/null +++ b/app/src/main/java/org/thoughtcrime/securesms/onboarding/loadaccount/LoadAccountActivity.kt @@ -0,0 +1,49 @@ +package org.thoughtcrime.securesms.onboarding.loadaccount + +import android.os.Bundle +import androidx.activity.viewModels +import androidx.camera.core.ExperimentalGetImage +import androidx.compose.runtime.collectAsState +import androidx.compose.runtime.getValue +import androidx.lifecycle.lifecycleScope +import dagger.hilt.android.AndroidEntryPoint +import kotlinx.coroutines.launch +import network.loki.messenger.R +import org.session.libsession.utilities.TextSecurePreferences +import org.thoughtcrime.securesms.BaseActionBarActivity +import org.thoughtcrime.securesms.onboarding.loading.LoadingManager +import org.thoughtcrime.securesms.onboarding.messagenotifications.startMessageNotificationsActivity +import org.thoughtcrime.securesms.ui.setComposeContent +import javax.inject.Inject + +@AndroidEntryPoint +@androidx.annotation.OptIn(ExperimentalGetImage::class) +class LoadAccountActivity : BaseActionBarActivity() { + + @Inject lateinit var prefs: TextSecurePreferences + @Inject lateinit var loadingManager: LoadingManager + + private val viewModel: LinkDeviceViewModel by viewModels() + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + supportActionBar?.setTitle(R.string.activity_link_load_account) + prefs.setHasViewedSeed(true) + prefs.setConfigurationMessageSynced(false) + prefs.setRestorationTime(System.currentTimeMillis()) + prefs.setLastProfileUpdateTime(0) + + lifecycleScope.launch { + viewModel.eventFlow.collect { + loadingManager.load(it.mnemonic) + startMessageNotificationsActivity() + finish() + } + } + + setComposeContent { + val state by viewModel.stateFlow.collectAsState() + LoadAccountScreen(state, viewModel.qrErrorsFlow, viewModel::onChange, viewModel::onContinue, viewModel::onScanQrCode) + } + } +} diff --git a/app/src/main/java/org/thoughtcrime/securesms/onboarding/LinkDeviceViewModel.kt b/app/src/main/java/org/thoughtcrime/securesms/onboarding/loadaccount/LoadAccountViewModel.kt similarity index 90% rename from app/src/main/java/org/thoughtcrime/securesms/onboarding/LinkDeviceViewModel.kt rename to app/src/main/java/org/thoughtcrime/securesms/onboarding/loadaccount/LoadAccountViewModel.kt index e737345c45..abb1fa3489 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/onboarding/LinkDeviceViewModel.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/onboarding/loadaccount/LoadAccountViewModel.kt @@ -1,4 +1,4 @@ -package org.thoughtcrime.securesms.onboarding +package org.thoughtcrime.securesms.onboarding.loadaccount import android.app.Application import androidx.lifecycle.AndroidViewModel @@ -22,11 +22,16 @@ import javax.inject.Inject class LinkDeviceEvent(val mnemonic: ByteArray) +internal data class State( + val recoveryPhrase: String = "", + val error: String? = null +) + @HiltViewModel -class LinkDeviceViewModel @Inject constructor( +internal class LinkDeviceViewModel @Inject constructor( private val application: Application ): AndroidViewModel(application) { - private val state = MutableStateFlow(LinkDeviceState()) + private val state = MutableStateFlow(State()) val stateFlow = state.asStateFlow() private val event = Channel() @@ -56,7 +61,7 @@ class LinkDeviceViewModel @Inject constructor( } fun onChange(recoveryPhrase: String) { - state.value = LinkDeviceState(recoveryPhrase) + state.value = State(recoveryPhrase) } private fun onSuccess(seed: ByteArray) { viewModelScope.launch { event.send(LinkDeviceEvent(seed)) }