From 794fe21b00e781a3983070f479a80a6af6d3ac1f Mon Sep 17 00:00:00 2001 From: SanmerDev Date: Thu, 23 May 2024 20:10:12 +0800 Subject: [PATCH] Optimize `InstallActivity` --- .../mrepo/ui/activity/InstallActivity.kt | 29 +++++++------------ .../mrepo/viewmodel/InstallViewModel.kt | 12 ++++++-- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/app/src/main/kotlin/com/sanmer/mrepo/ui/activity/InstallActivity.kt b/app/src/main/kotlin/com/sanmer/mrepo/ui/activity/InstallActivity.kt index 078a1460..d1543b1c 100644 --- a/app/src/main/kotlin/com/sanmer/mrepo/ui/activity/InstallActivity.kt +++ b/app/src/main/kotlin/com/sanmer/mrepo/ui/activity/InstallActivity.kt @@ -9,11 +9,9 @@ import androidx.activity.compose.setContent import androidx.activity.enableEdgeToEdge import androidx.activity.viewModels import androidx.compose.runtime.CompositionLocalProvider -import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.lifecycle.compose.collectAsStateWithLifecycle import androidx.lifecycle.lifecycleScope -import com.sanmer.mrepo.Compat import com.sanmer.mrepo.repository.UserPreferencesRepository import com.sanmer.mrepo.ui.providable.LocalUserPreferences import com.sanmer.mrepo.ui.theme.AppTheme @@ -27,7 +25,7 @@ import javax.inject.Inject @AndroidEntryPoint class InstallActivity : ComponentActivity() { @Inject lateinit var userPreferencesRepository: UserPreferencesRepository - private val viewModule: InstallViewModel by viewModels() + private val viewModel: InstallViewModel by viewModels() override fun onCreate(savedInstanceState: Bundle?) { Timber.d("InstallActivity onCreate") @@ -36,6 +34,8 @@ class InstallActivity : ComponentActivity() { if (intent.data == null) { finish() + } else { + initModule(intent) } setContent { @@ -48,16 +48,6 @@ class InstallActivity : ComponentActivity() { checkNotNull(userPreferences) } - LaunchedEffect(userPreferences) { - Compat.init(preferences.workingMode) - } - - LaunchedEffect(Compat.isAlive) { - if (Compat.isAlive) { - initModule(intent) - } - } - CompositionLocalProvider( LocalUserPreferences provides preferences ) { @@ -77,12 +67,13 @@ class InstallActivity : ComponentActivity() { super.onDestroy() } - private fun initModule(intent: Intent) = lifecycleScope.launch { - val zipUri = checkNotNull(intent.data) - viewModule.loadData( - context = applicationContext, - uri = zipUri - ) + private fun initModule(intent: Intent) { + lifecycleScope.launch { + viewModel.loadModule( + context = applicationContext, + uri = checkNotNull(intent.data) + ) + } } companion object { diff --git a/app/src/main/kotlin/com/sanmer/mrepo/viewmodel/InstallViewModel.kt b/app/src/main/kotlin/com/sanmer/mrepo/viewmodel/InstallViewModel.kt index 24ad9c20..98739eb8 100644 --- a/app/src/main/kotlin/com/sanmer/mrepo/viewmodel/InstallViewModel.kt +++ b/app/src/main/kotlin/com/sanmer/mrepo/viewmodel/InstallViewModel.kt @@ -62,7 +62,15 @@ class InstallViewModel @Inject constructor( } } - suspend fun loadData(context: Context, uri: Uri) = withContext(Dispatchers.IO) { + suspend fun loadModule(context: Context, uri: Uri) = withContext(Dispatchers.IO) { + val userPreferences = userPreferencesRepository.data.first() + + if (!Compat.init(userPreferences.workingMode)) { + event = Event.FAILED + console.add("- Service is not available") + return@withContext + } + val path = context.getPathForUri(uri) Timber.d("path = $path") @@ -92,7 +100,7 @@ class InstallViewModel @Inject constructor( } event = Event.FAILED - console.add("- Unknown file: path = $path, uri = $uri") + console.add("- Zip parsing failed") } private suspend fun install(zipPath: String) = withContext(Dispatchers.IO) {