Skip to content

Commit

Permalink
finish install and content
Browse files Browse the repository at this point in the history
  • Loading branch information
BadKiko committed Oct 15, 2023
1 parent aeedda3 commit d6b1b32
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 25 deletions.
9 changes: 9 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
android:supportsRtl="true"
android:theme="@style/Theme.Kuppdater"
tools:targetApi="31">
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_path" />
</provider>
<activity
android:name=".MainActivity"
android:exported="true"
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/xml/provider_path.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="external_files"
path="." />
</paths>
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ data class UpdateSheetDescriptionState(
@Composable
fun rememberUpdateSheetDescription(
dialogTitle: String = "Обновление",
dialogContent: String = "Список изменений:",
dialogContent: String = "Список изменений",
dialogInstallButton: String = "Установить",
dialogCancelButton: String = "Отмена",
isCancelable: Boolean = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ data class UpdateSheetLoadingState(
@Composable
fun rememberUpdateSheetLoading(
dialogTitle: String = "Установка",
dialogContent: String = "Производится установка:"
dialogContent: String = "Производится установка"
): UpdateSheetLoadingState {
return remember(
dialogTitle,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.kiko.kuppdater.ui.sheet

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.LinearProgressIndicator
Expand All @@ -23,6 +24,7 @@ fun UpdateInstallingSheet(updateSheetLoadingState: UpdateSheetLoadingState) {
) {
Text(updateSheetLoadingState.dialogTitle, style = MaterialTheme.typography.titleLarge)
Text(updateSheetLoadingState.dialogContent)
Spacer(modifier = Modifier.padding(16.dp))
LinearProgressIndicator(modifier = Modifier.padding(24.dp, 0.dp))
}
}
60 changes: 42 additions & 18 deletions kuppdater/src/main/java/com/kiko/kuppdater/ui/sheet/UpdateSheet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import com.kiko.kuppdater.data.states.UpdateSheetState
import com.kiko.kuppdater.data.states.UpdateState
import com.kiko.kuppdater.data.states.rememberUpdateSheetDescription
import com.kiko.kuppdater.ui.viewmodel.UpdateSheetViewModel

@OptIn(ExperimentalMaterial3Api::class)
Expand All @@ -18,27 +19,50 @@ fun UpdateSheet(
updateSheetViewModel: UpdateSheetViewModel = UpdateSheetViewModel()
) {
val context = LocalContext.current
updateSheetViewModel.getUpdateData(updateSheetState.url, context)

AlertDialog(onDismissRequest = {}) {
Card(
shape = RoundedCornerShape(
16.dp
)
) {
when (updateSheetViewModel.updateState) {
is UpdateState.UpdateFailed -> TODO()
UpdateState.UpdateIdle -> UpdateDescriptionSheet(
updateSheetDescriptionState = updateSheetState.descriptionState,
onClickInstall = {
updateSheetViewModel.getUpdateData(updateSheetState.url, context)
})

UpdateState.UpdateLoading -> UpdateProcessSheet(
updateSheetLoadingState = updateSheetState.loadingState,
updateSheetViewModel.downloadProgress
if (updateSheetViewModel.needUpdate) {
AlertDialog(onDismissRequest = {}) {
Card(
shape = RoundedCornerShape(
16.dp
)
) {
when (updateSheetViewModel.updateState) {
is UpdateState.UpdateFailed -> UpdateDescriptionSheet(
updateSheetDescriptionState = rememberUpdateSheetDescription(
dialogTitle = "Ошибка",
dialogContent = (updateSheetViewModel.updateState as UpdateState.UpdateFailed).errorMsg,
dialogInstallButton = "Ок",
),
onClickInstall = {
updateSheetViewModel.updateSheetState(UpdateState.UpdateIdle)
}
)

is UpdateState.UpdateSuccess -> UpdateInstallingSheet(updateSheetLoadingState = updateSheetState.loadingState)
UpdateState.UpdateIdle -> UpdateDescriptionSheet(
updateSheetDescriptionState = updateSheetState.descriptionState.copy(
dialogContent = updateSheetState.descriptionState.dialogContent + "\n\n${
updateSheetViewModel.updateJsonEntity.releaseNotes.joinToString("\n")
}"
),
onClickInstall = {
updateSheetViewModel.updateSheetState(UpdateState.UpdateLoading)

updateSheetViewModel.downloadApk(
updateSheetViewModel.updateJsonEntity.url,
context
)

})

UpdateState.UpdateLoading -> UpdateProcessSheet(
updateSheetLoadingState = updateSheetState.loadingState,
updateSheetViewModel.downloadProgress
)

is UpdateState.UpdateSuccess -> UpdateInstallingSheet(updateSheetLoadingState = updateSheetState.loadingState)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,29 @@ class UpdateSheetViewModel() : ViewModel() {
var updateState by mutableStateOf<UpdateState>(UpdateState.UpdateIdle)
private set

var needUpdate by mutableStateOf(false)
private set

var downloadProgress by mutableFloatStateOf(0f)
private set

var updateJsonEntity = UpdateJsonEntity.create()

fun getUpdateData(url: String, context: Context) {
viewModelScope.launch {

updateState = UpdateState.UpdateLoading

UpdateJsonUseCase().getUpdateData(url).collect { response ->
when (response) {
is ApiResponse.Failure -> {
updateState = UpdateState.UpdateFailed(response.message())
}

is ApiResponse.Success -> {
downloadApk(response.data.url, context)
response.data.let {
needUpdate = context.packageManager.getPackageInfo(
context.packageName,
0
).versionCode < it.latestVersionCode

updateJsonEntity = UpdateJsonEntity(
it.latestVersion, it.latestVersionCode, it.url, it.releaseNotes
)
Expand All @@ -56,7 +60,7 @@ class UpdateSheetViewModel() : ViewModel() {
}
}

private fun downloadApk(url: String, context: Context) {
fun downloadApk(url: String, context: Context) {
val kDownloader = KDownloader.create(context)
val file =
File(
Expand All @@ -75,4 +79,8 @@ class UpdateSheetViewModel() : ViewModel() {
ApkInstaller.installApplication(context, file)
}, onError = { UpdateState.UpdateFailed(it) })
}

fun updateSheetState(state: UpdateState) {
updateState = state
}
}

0 comments on commit d6b1b32

Please sign in to comment.