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

Commit

Permalink
Optimize ModuleViewModel
Browse files Browse the repository at this point in the history
  • Loading branch information
SanmerDev committed Jun 11, 2024
1 parent b3fbff3 commit 88d86ed
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@ import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.platform.LocalContext
import androidx.core.net.toUri
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import dev.sanmer.mrepo.model.online.VersionItem
import dev.sanmer.mrepo.ui.activity.InstallActivity
import dev.sanmer.mrepo.ui.component.CollapsingTopAppBarDefaults
import dev.sanmer.mrepo.ui.screens.repository.view.pages.AboutPage
import dev.sanmer.mrepo.ui.screens.repository.view.pages.OverviewPage
Expand All @@ -28,22 +24,9 @@ fun ViewScreen(
navController: NavController,
viewModel: ModuleViewModel = hiltViewModel()
) {
val context = LocalContext.current

val scrollBehavior = CollapsingTopAppBarDefaults.scrollBehavior()
val pagerState = rememberPagerState { if (viewModel.isEmptyAbout) 2 else 3 }

val download: (VersionItem, Boolean) -> Unit = { item, install ->
viewModel.downloader(context, item) {
if (install) {
InstallActivity.start(
context = context,
uri = it.toUri()
)
}
}
}

Scaffold(
modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
topBar = {
Expand Down Expand Up @@ -77,14 +60,16 @@ fun ViewScreen(
notifyUpdates = viewModel.notifyUpdates,
isProviderAlive = viewModel.isProviderAlive,
setUpdatesTag = viewModel::setUpdatesTag,
onInstall = { download(it, true) },
onInstall = { context, item ->
viewModel.downloader(context, item, true)
}
)
1 -> VersionsPage(
versions = viewModel.versions,
localVersionCode = viewModel.localVersionCode,
isProviderAlive = viewModel.isProviderAlive,
getProgress = { viewModel.getProgress(it) },
onDownload = download
getProgress = viewModel::getProgress,
onDownload = viewModel::downloader
)
2 -> AboutPage(
online = viewModel.online
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.sanmer.mrepo.ui.screens.repository.view.pages

import android.content.Context
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
Expand All @@ -25,6 +26,7 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
Expand All @@ -43,7 +45,7 @@ fun OverviewPage(
isProviderAlive: Boolean,
notifyUpdates: Boolean,
setUpdatesTag: (Boolean) -> Unit,
onInstall: (VersionItem) -> Unit
onInstall: (Context, VersionItem) -> Unit
) = Column(
modifier = Modifier
.fillMaxSize()
Expand Down Expand Up @@ -95,13 +97,15 @@ fun OverviewPage(
private fun CloudItem(
item: VersionItem,
isProviderAlive: Boolean,
onInstall: (VersionItem) -> Unit
onInstall: (Context, VersionItem) -> Unit
) = Column(
modifier = Modifier
.padding(all = 16.dp)
.fillMaxWidth(),
verticalArrangement = Arrangement.spacedBy(16.dp)
) {
val context = LocalContext.current

Text(
text = stringResource(id = R.string.view_module_cloud),
style = MaterialTheme.typography.titleSmall,
Expand All @@ -121,7 +125,7 @@ private fun CloudItem(

ElevatedAssistChip(
enabled = isProviderAlive,
onClick = { onInstall(item) },
onClick = { onInstall(context, item) },
label = { Text(text = stringResource(id = R.string.module_install)) },
leadingIcon = {
Icon(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.sanmer.mrepo.ui.screens.repository.view.pages

import android.content.Context
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
Expand All @@ -22,48 +23,55 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import dev.sanmer.mrepo.R
import dev.sanmer.mrepo.database.entity.RepoEntity
import dev.sanmer.mrepo.model.online.VersionItem
import dev.sanmer.mrepo.ui.component.LabelItem
import dev.sanmer.mrepo.ui.component.VersionItemBottomSheet
import dev.sanmer.mrepo.utils.extensions.toDate
import kotlinx.coroutines.flow.Flow

@Composable
fun VersionsPage(
versions: List<Pair<RepoEntity, VersionItem>>,
localVersionCode: Int,
isProviderAlive: Boolean,
getProgress: @Composable (VersionItem) -> Float,
onDownload: (VersionItem, Boolean) -> Unit
) = LazyColumn(
modifier = Modifier.fillMaxSize()
getProgress: (VersionItem) -> Flow<Float>,
onDownload: (Context, VersionItem, Boolean) -> Unit
) {
items(
items = versions,
key = { it.first.url + it.second.versionCode }
) { (repo, item) ->
VersionItem(
item = item,
repo = repo,
localVersionCode = localVersionCode,
isProviderAlive = isProviderAlive,
onDownload = { onDownload(item, it) }
)
val context = LocalContext.current

val progress = getProgress(item)
if (progress != 0f) {
LinearProgressIndicator(
progress = { progress },
strokeCap = StrokeCap.Round,
modifier = Modifier
.height(2.dp)
.fillMaxWidth()
LazyColumn(
modifier = Modifier.fillMaxSize()
) {
items(
items = versions,
key = { it.first.url + it.second.versionCode }
) { (repo, item) ->
VersionItem(
item = item,
repo = repo,
localVersionCode = localVersionCode,
isProviderAlive = isProviderAlive,
onDownload = { onDownload(context, item, it) }
)
} else {
HorizontalDivider(thickness = 0.9.dp)

val progress by getProgress(item).collectAsStateWithLifecycle(initialValue = 0f)
if (progress != 0f) {
LinearProgressIndicator(
progress = { progress },
strokeCap = StrokeCap.Round,
modifier = Modifier
.height(2.dp)
.fillMaxWidth()
)
} else {
HorizontalDivider(thickness = 0.9.dp)
}
}
}
}
Expand Down
23 changes: 11 additions & 12 deletions app/src/main/kotlin/dev/sanmer/mrepo/viewmodel/ModuleViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package dev.sanmer.mrepo.viewmodel

import android.content.Context
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import dev.sanmer.mrepo.Compat
Expand All @@ -23,6 +21,7 @@ import dev.sanmer.mrepo.model.online.VersionItem
import dev.sanmer.mrepo.repository.LocalRepository
import dev.sanmer.mrepo.repository.UserPreferencesRepository
import dev.sanmer.mrepo.service.DownloadService
import dev.sanmer.mrepo.ui.activity.InstallActivity
import dev.sanmer.mrepo.ui.navigation.graphs.RepositoryScreen
import dev.sanmer.mrepo.utils.Utils
import kotlinx.coroutines.flow.first
Expand Down Expand Up @@ -85,7 +84,7 @@ class ModuleViewModel @Inject constructor(
val repo = localRepository.getRepoByUrl(it.repoUrl)

val item = repo to it
val track = repo to localRepository.getOnlineByIdAndUrl(
val track = repo to localRepository.getOnlineByIdAndUrl(
id = online.id,
repoUrl = it.repoUrl
).track
Expand All @@ -111,7 +110,7 @@ class ModuleViewModel @Inject constructor(
fun downloader(
context: Context,
item: VersionItem,
onSuccess: (File) -> Unit
install: Boolean
) {
viewModelScope.launch {
val downloadPath = userPreferencesRepository.data
Expand All @@ -135,7 +134,12 @@ class ModuleViewModel @Inject constructor(
val listener = object : DownloadService.IDownloadListener {
override fun getProgress(value: Float) {}
override fun onSuccess() {
onSuccess(downloadPath.resolve(filename))
if (install) {
InstallActivity.start(
context = context,
file = File(downloadPath, filename)
)
}
}

override fun onFailure(e: Throwable) {
Expand All @@ -151,13 +155,8 @@ class ModuleViewModel @Inject constructor(
}
}

@Composable
fun getProgress(item: VersionItem): Float {
val progress by DownloadService.getProgressByKey(item.toString())
.collectAsStateWithLifecycle(initialValue = 0f)

return progress
}
fun getProgress(item: VersionItem) =
DownloadService.getProgressByKey(item.toString())

companion object {
fun putModuleId(module: OnlineModule) =
Expand Down

0 comments on commit 88d86ed

Please sign in to comment.