diff --git a/app/src/main/java/com/vanced/manager/ui/MainActivity.kt b/app/src/main/java/com/vanced/manager/ui/MainActivity.kt index 8ff9716c..bb3c5dff 100644 --- a/app/src/main/java/com/vanced/manager/ui/MainActivity.kt +++ b/app/src/main/java/com/vanced/manager/ui/MainActivity.kt @@ -23,11 +23,11 @@ import com.vanced.manager.ui.theme.ManagerTheme import com.vanced.manager.ui.theme.isDark import com.vanced.manager.ui.util.Screen import com.vanced.manager.ui.viewmodel.InstallViewModel -import org.koin.android.ext.android.inject +import org.koin.androidx.viewmodel.ext.android.viewModel class MainActivity : ComponentActivity() { - private val installViewModel: InstallViewModel by inject() + private val installViewModel: InstallViewModel by viewModel() private val backPressHandler = BackPressHandler() diff --git a/app/src/main/java/com/vanced/manager/ui/screens/InstallScreen.kt b/app/src/main/java/com/vanced/manager/ui/screens/InstallScreen.kt index be6017d8..7e95292c 100644 --- a/app/src/main/java/com/vanced/manager/ui/screens/InstallScreen.kt +++ b/app/src/main/java/com/vanced/manager/ui/screens/InstallScreen.kt @@ -45,6 +45,9 @@ fun InstallScreen( var startedProcess by rememberSaveable { mutableStateOf(false) } + val logs = viewModel.logs + val status = viewModel.status + // I don't know why, I don't know how, // but it works as intended LaunchedEffect(startedProcess) { @@ -60,7 +63,7 @@ fun InstallScreen( ManagerTopAppBar( title = managerString(R.string.toolbar_install), ) - when (val status = viewModel.status) { + when (status) { is InstallViewModel.Status.Progress -> { ManagerProgressIndicator(status.progress) } @@ -72,9 +75,9 @@ fun InstallScreen( } }, floatingActionButton = { - if (viewModel.status is InstallViewModel.Status.Installed) { + if (status is InstallViewModel.Status.Installed) { ExtendedFloatingActionButton( - text = { /*TODO*/ }, + text = { ManagerText("Finish") }, icon = { Icon(Icons.Rounded.Done, null) }, @@ -88,7 +91,7 @@ fun InstallScreen( .fillMaxSize() .padding(paddingValues), ) { - items(viewModel.logs) { log -> + items(logs) { log -> when (log) { is InstallViewModel.Log.Success -> { ManagerText( diff --git a/app/src/main/java/com/vanced/manager/ui/viewmodel/InstallViewModel.kt b/app/src/main/java/com/vanced/manager/ui/viewmodel/InstallViewModel.kt index fa08a6b7..2d399076 100644 --- a/app/src/main/java/com/vanced/manager/ui/viewmodel/InstallViewModel.kt +++ b/app/src/main/java/com/vanced/manager/ui/viewmodel/InstallViewModel.kt @@ -63,12 +63,14 @@ class InstallViewModel( } fun postInstallStatus(pmStatus: Int, extra: String) { - if (pmStatus == PackageInstaller.STATUS_SUCCESS) { - status = Status.Installed - logs.add(Log.Success("Successfully installed")) - } else { - status = Status.Failure - logs.add(Log.Error("Failed to install app", extra)) + viewModelScope.launch(Dispatchers.IO) { + if (pmStatus == PackageInstaller.STATUS_SUCCESS) { + status = Status.Installed + log(Log.Success("Successfully installed")) + } else { + status = Status.Failure + log(Log.Error("Failed to install app", extra)) + } } } @@ -80,14 +82,14 @@ class InstallViewModel( downloader.download(appVersions) { downloadStatus -> when (downloadStatus) { - is DownloadStatus.File -> logs.add(Log.Info("Downloading ${downloadStatus.fileName}")) - is DownloadStatus.Error -> logs.add(Log.Error( + is DownloadStatus.File -> log(Log.Info("Downloading ${downloadStatus.fileName}")) + is DownloadStatus.Error -> log(Log.Error( displayText = downloadStatus.displayError, stacktrace = downloadStatus.stacktrace )) is DownloadStatus.Progress -> status = Status.Progress(downloadStatus.progress / 100) is DownloadStatus.StartInstall -> { - logs.add(Log.Success("Successfully downloaded $appName")) + log(Log.Success("Successfully downloaded $appName")) installApp(appName, appVersions) } } @@ -123,6 +125,10 @@ class InstallViewModel( else -> throw IllegalArgumentException("$appName is not a valid app") } + private fun log(data: Log) { + logs.add(data) + } + private fun clear() { logs.clear() status = Status.Idle