Skip to content

Commit

Permalink
Android: Fixup TaskViewModel
Browse files Browse the repository at this point in the history
Suggestions by JosJuice and merges UserDataViewModel with TaskViewModel.
  • Loading branch information
t895 committed Feb 20, 2023
1 parent 476fc87 commit c5422ef
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import org.dolphinemu.dolphinemu.dialogs.NotificationDialog
import org.dolphinemu.dolphinemu.dialogs.TaskDialog
import org.dolphinemu.dolphinemu.dialogs.UserDataImportWarningDialog
import org.dolphinemu.dolphinemu.model.TaskViewModel
import org.dolphinemu.dolphinemu.model.UserDataViewModel
import org.dolphinemu.dolphinemu.utils.*
import org.dolphinemu.dolphinemu.utils.ThemeHelper.enableScrollTint
import org.dolphinemu.dolphinemu.utils.ThemeHelper.setNavigationBarColor
Expand All @@ -34,14 +33,11 @@ import java.util.zip.ZipInputStream
import java.util.zip.ZipOutputStream

class UserDataActivity : AppCompatActivity() {
private lateinit var userDataViewModel: UserDataViewModel
private lateinit var taskViewModel: TaskViewModel

private lateinit var mBinding: ActivityUserDataBinding

override fun onCreate(savedInstanceState: Bundle?) {
userDataViewModel = ViewModelProvider(this)[UserDataViewModel::class.java]

ThemeHelper.setTheme(this)

super.onCreate(savedInstanceState)
Expand Down Expand Up @@ -85,25 +81,22 @@ class UserDataActivity : AppCompatActivity() {
public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)

taskViewModel = ViewModelProvider(this)[TaskViewModel::class.java]
if (requestCode == REQUEST_CODE_IMPORT && resultCode == RESULT_OK) {
val arguments = Bundle()
arguments.putString(
UserDataImportWarningDialog.KEY_URI_RESULT,
data!!.data!!.toString()
)

userDataViewModel.mustRestartApp = true

val dialog = UserDataImportWarningDialog()
dialog.arguments = arguments
dialog.show(supportFragmentManager, UserDataImportWarningDialog.TAG)
} else if (requestCode == REQUEST_CODE_EXPORT && resultCode == RESULT_OK) {
taskViewModel = ViewModelProvider(this)[TaskViewModel::class.java]
taskViewModel.clear()
taskViewModel.resultGetter = {
taskViewModel.task = {
val resultResource = exportUserData(data!!.data!!)
taskViewModel.result.postValue(resultResource)
taskViewModel.isComplete.postValue(true)
taskViewModel.setResult(resultResource)
}

val arguments = Bundle()
Expand All @@ -129,7 +122,10 @@ class UserDataActivity : AppCompatActivity() {
// Activity not found. Perhaps it was removed by the OEM, or by some new Android version
// that didn't exist at the time of writing. Not much we can do other than tell the user.
val arguments = Bundle()
arguments.putInt(NotificationDialog.KEY_MESSAGE, R.string.user_data_open_system_file_manager_failed)
arguments.putInt(
NotificationDialog.KEY_MESSAGE,
R.string.user_data_open_system_file_manager_failed
)

val dialog = NotificationDialog()
dialog.arguments = arguments
Expand Down Expand Up @@ -157,6 +153,8 @@ class UserDataActivity : AppCompatActivity() {
if (!isDolphinUserDataBackup(source))
return R.string.user_data_import_invalid_file

taskViewModel.mustRestartApp = true

contentResolver.openInputStream(source).use { `is` ->
ZipInputStream(`is`).use { zis ->
val userDirectory = File(DirectoryInitialization.getUserDirectory())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class TaskDialog : DialogFragment() {
.setTitle(requireArguments().getInt(KEY_TITLE))
.setView(R.layout.dialog_indeterminate_progress)
if (requireArguments().getBoolean(KEY_CANCELLABLE)) {
dialogBuilder.setCancelable(false)
dialogBuilder.setCancelable(true)
.setNegativeButton(android.R.string.cancel) { dialog: DialogInterface, _: Int ->
viewModel.cancelled = true
dialog.dismiss()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder
import org.dolphinemu.dolphinemu.R
import org.dolphinemu.dolphinemu.activities.UserDataActivity
import org.dolphinemu.dolphinemu.model.TaskViewModel
import org.dolphinemu.dolphinemu.model.UserDataViewModel
import kotlin.system.exitProcess

class UserDataImportWarningDialog : DialogFragment() {
private lateinit var userDataViewModel: UserDataViewModel
private lateinit var taskViewModel: TaskViewModel

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
userDataViewModel = ViewModelProvider(requireActivity())[UserDataViewModel::class.java]
taskViewModel = ViewModelProvider(requireActivity())[TaskViewModel::class.java]

val dialog = MaterialAlertDialogBuilder(requireContext())
Expand All @@ -34,17 +31,16 @@ class UserDataImportWarningDialog : DialogFragment() {
taskArguments.putInt(TaskDialog.KEY_MESSAGE, R.string.do_not_close_app)
taskArguments.putBoolean(TaskDialog.KEY_CANCELLABLE, false)

taskViewModel.resultGetter = {
taskViewModel.result.postValue(
taskViewModel.task = {
taskViewModel.setResult(
(requireActivity() as UserDataActivity).importUserData(
requireArguments().getString(KEY_URI_RESULT)!!.toUri()
)
)
taskViewModel.isComplete.postValue(true)
}

taskViewModel.onResultDismiss = {
if (userDataViewModel.mustRestartApp) {
if (taskViewModel.mustRestartApp) {
exitProcess(0)
}
}
Expand All @@ -58,6 +54,6 @@ class UserDataImportWarningDialog : DialogFragment() {

companion object {
const val TAG = "UserDataImportWarningDialog"
const val KEY_URI_RESULT = "uri_result"
const val KEY_URI_RESULT = "uri"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,55 @@

package org.dolphinemu.dolphinemu.model

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.*

class TaskViewModel : ViewModel() {
val result = MutableLiveData<Int>()
val isComplete = MutableLiveData<Boolean>()
var cancelled = false
var isRunning = MutableLiveData<Boolean>()
var mustRestartApp = false

lateinit var resultGetter: () -> Unit
private val _result = MutableLiveData<Int>()
val result: LiveData<Int> get() = _result

private val _isComplete = MutableLiveData<Boolean>()
val isComplete: LiveData<Boolean> get() = _isComplete

private val _isRunning = MutableLiveData<Boolean>()
val isRunning: LiveData<Boolean> get() = _isRunning

lateinit var task: () -> Unit
var onResultDismiss: (() -> Unit)? = null

init {
result.value = 0
isComplete.value = false
isRunning.value = false
clear()
}

fun clear() {
result.value = 0
isComplete.value = false
_result.value = 0
_isComplete.value = false
cancelled = false
mustRestartApp = false
onResultDismiss = null
_isRunning.value = false
}

fun runTask() {
if (isRunning.value == true) return
isRunning.value = true
_isRunning.value = true

viewModelScope.launch {
withContext(Dispatchers.IO) {
resultGetter.invoke()
isRunning.postValue(false)
task.invoke()
_isRunning.postValue(false)
_isComplete.postValue(true)
}
}
}

fun setResult(result: Int) {
_result.postValue(result)
}
}

This file was deleted.

0 comments on commit c5422ef

Please sign in to comment.