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

Commit

Permalink
Optimize InstallActivity
Browse files Browse the repository at this point in the history
  • Loading branch information
SanmerDev committed May 23, 2024
1 parent ec7a02b commit 794fe21
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
29 changes: 10 additions & 19 deletions app/src/main/kotlin/com/sanmer/mrepo/ui/activity/InstallActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand All @@ -36,6 +34,8 @@ class InstallActivity : ComponentActivity() {

if (intent.data == null) {
finish()
} else {
initModule(intent)
}

setContent {
Expand All @@ -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
) {
Expand All @@ -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 {
Expand Down
12 changes: 10 additions & 2 deletions app/src/main/kotlin/com/sanmer/mrepo/viewmodel/InstallViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 794fe21

Please sign in to comment.