-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #249 from GuoXiCheng/dev-c
Dev
- Loading branch information
Showing
19 changed files
with
384 additions
and
79 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
app/src/main/java/com/android/skip/data/download/ApkDownloadRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.android.skip.data.download | ||
|
||
import com.android.skip.data.network.MyApiNetwork | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class ApkDownloadRepository @Inject constructor( | ||
private val myApiNetwork: MyApiNetwork | ||
) { | ||
suspend fun downloadAPK(latestVersion: String, onDownloadProcess: (process: Int) -> Unit) = | ||
withContext(Dispatchers.IO) { | ||
myApiNetwork.fetchAPK(latestVersion, onDownloadProcess) | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
app/src/main/java/com/android/skip/data/download/ApkDownloadViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.android.skip.data.download | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withContext | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class ApkDownloadViewModel @Inject constructor( | ||
private val apkDownloadRepository: ApkDownloadRepository | ||
) : ViewModel() { | ||
private val _isShowDialog = MutableLiveData(false) | ||
val isShowDialog: LiveData<Boolean> = _isShowDialog | ||
|
||
private val _apkDownloadProcess = MutableLiveData(0) | ||
val apkDownloadProcess: LiveData<Int> = _apkDownloadProcess | ||
|
||
fun changeDialogState(showDialog: Boolean) { | ||
_isShowDialog.postValue(showDialog) | ||
} | ||
|
||
private fun changeApkDownloadProcess(process: Int) { | ||
_apkDownloadProcess.postValue(process) | ||
} | ||
|
||
fun downloadAPK(latestVersion: String) { | ||
viewModelScope.launch { | ||
withContext(Dispatchers.IO) { | ||
apkDownloadRepository.downloadAPK(latestVersion) { | ||
changeApkDownloadProcess(it) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
app/src/main/java/com/android/skip/ui/about/download/ApkDownloadDialog.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.android.skip.ui.about.download | ||
|
||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.material3.AlertDialog | ||
import androidx.compose.material3.Button | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.Text | ||
import androidx.compose.material3.TextButton | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.livedata.observeAsState | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.res.stringResource | ||
import com.android.skip.R | ||
import com.android.skip.data.download.ApkDownloadViewModel | ||
import com.android.skip.data.version.ApkVersionViewModel | ||
import com.blankj.utilcode.util.AppUtils | ||
import com.blankj.utilcode.util.StringUtils.getString | ||
|
||
@Composable | ||
fun ApkDownloadDialog( | ||
apkDownloadViewModel: ApkDownloadViewModel, | ||
apkVersionViewModel: ApkVersionViewModel | ||
) { | ||
val showDialog = apkDownloadViewModel.isShowDialog.observeAsState() | ||
|
||
val versionPostState = apkVersionViewModel.versionPostState.observeAsState() | ||
|
||
val apkDownloadProcess = apkDownloadViewModel.apkDownloadProcess.observeAsState() | ||
|
||
if (showDialog.value == true) { | ||
AlertDialog( | ||
modifier = Modifier.fillMaxWidth(), | ||
containerColor = MaterialTheme.colorScheme.background, | ||
title = { | ||
Text(text = stringResource(id = R.string.dialog_new_version_released)) | ||
}, | ||
text = { | ||
Column { | ||
Text( | ||
text = getString( | ||
R.string.dialog_update_version, | ||
AppUtils.getAppVersionName(), | ||
versionPostState.value?.latestVersion | ||
) | ||
) | ||
apkDownloadProcess.value?.let { process -> | ||
if (process > 0) { | ||
Text( | ||
text = getString( | ||
R.string.dialog_downloading, | ||
apkDownloadProcess.value | ||
) | ||
) | ||
} | ||
} | ||
} | ||
|
||
}, | ||
onDismissRequest = { | ||
apkDownloadViewModel.changeDialogState(false) | ||
}, | ||
confirmButton = { | ||
Button(onClick = { | ||
versionPostState.value?.latestVersion?.let { | ||
apkDownloadViewModel.downloadAPK( | ||
it | ||
) | ||
} | ||
}) { | ||
Text(text = stringResource(id = R.string.dialog_update_now)) | ||
} | ||
}, | ||
dismissButton = { | ||
TextButton(onClick = { apkDownloadViewModel.changeDialogState(false) }) { | ||
Text(text = stringResource(id = R.string.dialog_not_update)) | ||
} | ||
} | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.