Skip to content
This repository was archived by the owner on Jul 2, 2024. It is now read-only.

Commit 794fe21

Browse files
committed
Optimize InstallActivity
1 parent ec7a02b commit 794fe21

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

app/src/main/kotlin/com/sanmer/mrepo/ui/activity/InstallActivity.kt

+10-19
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@ import androidx.activity.compose.setContent
99
import androidx.activity.enableEdgeToEdge
1010
import androidx.activity.viewModels
1111
import androidx.compose.runtime.CompositionLocalProvider
12-
import androidx.compose.runtime.LaunchedEffect
1312
import androidx.compose.runtime.getValue
1413
import androidx.lifecycle.compose.collectAsStateWithLifecycle
1514
import androidx.lifecycle.lifecycleScope
16-
import com.sanmer.mrepo.Compat
1715
import com.sanmer.mrepo.repository.UserPreferencesRepository
1816
import com.sanmer.mrepo.ui.providable.LocalUserPreferences
1917
import com.sanmer.mrepo.ui.theme.AppTheme
@@ -27,7 +25,7 @@ import javax.inject.Inject
2725
@AndroidEntryPoint
2826
class InstallActivity : ComponentActivity() {
2927
@Inject lateinit var userPreferencesRepository: UserPreferencesRepository
30-
private val viewModule: InstallViewModel by viewModels()
28+
private val viewModel: InstallViewModel by viewModels()
3129

3230
override fun onCreate(savedInstanceState: Bundle?) {
3331
Timber.d("InstallActivity onCreate")
@@ -36,6 +34,8 @@ class InstallActivity : ComponentActivity() {
3634

3735
if (intent.data == null) {
3836
finish()
37+
} else {
38+
initModule(intent)
3939
}
4040

4141
setContent {
@@ -48,16 +48,6 @@ class InstallActivity : ComponentActivity() {
4848
checkNotNull(userPreferences)
4949
}
5050

51-
LaunchedEffect(userPreferences) {
52-
Compat.init(preferences.workingMode)
53-
}
54-
55-
LaunchedEffect(Compat.isAlive) {
56-
if (Compat.isAlive) {
57-
initModule(intent)
58-
}
59-
}
60-
6151
CompositionLocalProvider(
6252
LocalUserPreferences provides preferences
6353
) {
@@ -77,12 +67,13 @@ class InstallActivity : ComponentActivity() {
7767
super.onDestroy()
7868
}
7969

80-
private fun initModule(intent: Intent) = lifecycleScope.launch {
81-
val zipUri = checkNotNull(intent.data)
82-
viewModule.loadData(
83-
context = applicationContext,
84-
uri = zipUri
85-
)
70+
private fun initModule(intent: Intent) {
71+
lifecycleScope.launch {
72+
viewModel.loadModule(
73+
context = applicationContext,
74+
uri = checkNotNull(intent.data)
75+
)
76+
}
8677
}
8778

8879
companion object {

app/src/main/kotlin/com/sanmer/mrepo/viewmodel/InstallViewModel.kt

+10-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,15 @@ class InstallViewModel @Inject constructor(
6262
}
6363
}
6464

65-
suspend fun loadData(context: Context, uri: Uri) = withContext(Dispatchers.IO) {
65+
suspend fun loadModule(context: Context, uri: Uri) = withContext(Dispatchers.IO) {
66+
val userPreferences = userPreferencesRepository.data.first()
67+
68+
if (!Compat.init(userPreferences.workingMode)) {
69+
event = Event.FAILED
70+
console.add("- Service is not available")
71+
return@withContext
72+
}
73+
6674
val path = context.getPathForUri(uri)
6775
Timber.d("path = $path")
6876

@@ -92,7 +100,7 @@ class InstallViewModel @Inject constructor(
92100
}
93101

94102
event = Event.FAILED
95-
console.add("- Unknown file: path = $path, uri = $uri")
103+
console.add("- Zip parsing failed")
96104
}
97105

98106
private suspend fun install(zipPath: String) = withContext(Dispatchers.IO) {

0 commit comments

Comments
 (0)