bind launch action to app card

This commit is contained in:
X1nto 2021-11-22 17:31:15 +04:00
parent b3e36746dc
commit 19f8219c3c
5 changed files with 75 additions and 19 deletions

View File

@ -0,0 +1,3 @@
package com.vanced.manager.core.util
const val VANCED_PACKAGE = ""

View File

@ -2,10 +2,11 @@ package com.vanced.manager.di
import com.vanced.manager.ui.viewmodel.InstallViewModel
import com.vanced.manager.ui.viewmodel.MainViewModel
import org.koin.android.ext.koin.androidApplication
import org.koin.androidx.viewmodel.dsl.viewModel
import org.koin.dsl.module
val viewModelModule = module {
viewModel { MainViewModel(get()) }
viewModel { MainViewModel(get(), androidApplication()) }
viewModel { InstallViewModel(get(), get(), get(), get(), get(), get()) }
}

View File

@ -137,7 +137,13 @@ fun HomeLayout(
)
},
onAppUninstallClick = { /*TODO*/ },
onAppLaunchClick = { /*TODO*/ },
onAppLaunchClick = {
viewModel.launchApp(
appName = app.name,
appPackage = app.packageName,
appPackageRoot = app.packageNameRoot
)
},
onAppInfoClick = {
showAppInfoDialog = true
}

View File

@ -1,21 +1,32 @@
package com.vanced.manager.ui.viewmodel
import android.app.Application
import android.content.ActivityNotFoundException
import android.content.ComponentName
import android.content.Intent
import android.util.Log
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.vanced.manager.core.preferences.holder.managerVariantPref
import com.vanced.manager.core.preferences.holder.musicEnabled
import com.vanced.manager.core.preferences.holder.vancedEnabled
import com.vanced.manager.domain.model.App
import com.vanced.manager.network.util.MICROG_NAME
import com.vanced.manager.network.util.MUSIC_NAME
import com.vanced.manager.network.util.VANCED_NAME
import com.vanced.manager.repository.JsonRepository
import kotlinx.coroutines.launch
class MainViewModel(
private val repository: JsonRepository
) : ViewModel() {
private val repository: JsonRepository,
private val app: Application
) : AndroidViewModel(app) {
private val isNonroot
get() = managerVariantPref == "nonroot"
sealed class AppState {
data class Fetching(val placeholderAppsCount: Int) : AppState()
@ -28,8 +39,6 @@ class MainViewModel(
fun fetch() {
viewModelScope.launch {
val isNonroot = managerVariantPref == "nonroot"
var appsCount = 0
if (vancedEnabled) appsCount++
@ -53,13 +62,48 @@ class MainViewModel(
} catch (e: Exception) {
val error = "failed to fetch: \n${e.stackTraceToString()}"
appState = AppState.Error(error)
Log.d("MainViewModel", error)
Log.d(TAG, error)
}
}
}
fun launchApp(
appName: String,
appPackage: String,
appPackageRoot: String?
) {
val pkg = if (isNonroot) appPackage else appPackageRoot ?: appPackage
val component = ComponentName(
/* pkg = */ appPackage,
/* cls = */ when (appName) {
VANCED_NAME -> "$pkg.HomeActivity"
MUSIC_NAME -> "$pkg.activities.MusicActivity"
MICROG_NAME -> "org.microg.gms.ui.SettingsActivity"
else -> throw IllegalArgumentException("$appName is not a valid app")
}
)
try {
app.startActivity(
Intent().apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
setComponent(component)
}
)
} catch (e: ActivityNotFoundException) {
Log.d(TAG, "Unable to launch $appName")
e.printStackTrace()
}
}
init {
fetch()
}
companion object {
const val TAG = "MainViewModel"
}
}

View File

@ -75,17 +75,19 @@ fun AppCard(
)
},
appActionsRow = {
IconButton(onClick = onAppUninstallClick) {
Icon(
imageVector = Icons.Rounded.DeleteForever,
contentDescription = "Uninstall"
)
}
IconButton(onClick = onAppLaunchClick) {
Icon(
imageVector = Icons.Rounded.Launch,
contentDescription = "Launch",
)
if (appInstalledVersion != null) {
IconButton(onClick = onAppUninstallClick) {
Icon(
imageVector = Icons.Rounded.DeleteForever,
contentDescription = "Uninstall"
)
}
IconButton(onClick = onAppLaunchClick) {
Icon(
imageVector = Icons.Rounded.Launch,
contentDescription = "Launch",
)
}
}
IconButton(onClick = onAppDownloadClick) {
Icon(