From a53cd2854e0bf568c900d14ba2a5795e7e3df643 Mon Sep 17 00:00:00 2001 From: t895 Date: Fri, 8 Dec 2023 22:02:04 -0500 Subject: android: Listen for directory selection in viewmodel Fixes broken game directory selection setup flow --- .../fragments/AddGameFolderDialogFragment.kt | 3 +++ .../org/yuzu/yuzu_emu/fragments/SetupFragment.kt | 28 ++++++++++++++++------ .../java/org/yuzu/yuzu_emu/model/GamesViewModel.kt | 2 +- .../java/org/yuzu/yuzu_emu/model/HomeViewModel.kt | 8 +++++++ 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/AddGameFolderDialogFragment.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/AddGameFolderDialogFragment.kt index dec2b7cf1..9fab88248 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/AddGameFolderDialogFragment.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/AddGameFolderDialogFragment.kt @@ -14,8 +14,10 @@ import org.yuzu.yuzu_emu.R import org.yuzu.yuzu_emu.databinding.DialogAddFolderBinding import org.yuzu.yuzu_emu.model.GameDir import org.yuzu.yuzu_emu.model.GamesViewModel +import org.yuzu.yuzu_emu.model.HomeViewModel class AddGameFolderDialogFragment : DialogFragment() { + private val homeViewModel: HomeViewModel by activityViewModels() private val gamesViewModel: GamesViewModel by activityViewModels() override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { @@ -30,6 +32,7 @@ class AddGameFolderDialogFragment : DialogFragment() { .setTitle(R.string.add_game_folder) .setPositiveButton(android.R.string.ok) { _: DialogInterface, _: Int -> val newGameDir = GameDir(folderUriString!!, binding.deepScanSwitch.isChecked) + homeViewModel.setGamesDirSelected(true) gamesViewModel.addFolder(newGameDir) } .setNegativeButton(android.R.string.cancel, null) diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/SetupFragment.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/SetupFragment.kt index c4277735d..eb5edaa10 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/SetupFragment.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/SetupFragment.kt @@ -4,6 +4,7 @@ package org.yuzu.yuzu_emu.fragments import android.Manifest +import android.annotation.SuppressLint import android.content.Intent import android.os.Build import android.os.Bundle @@ -75,6 +76,8 @@ class SetupFragment : Fragment() { return binding.root } + // This is using the correct scope, lint is just acting up + @SuppressLint("UnsafeRepeatOnLifecycleDetector") override fun onViewCreated(view: View, savedInstanceState: Bundle?) { mainActivity = requireActivity() as MainActivity @@ -206,12 +209,24 @@ class SetupFragment : Fragment() { ) } - viewLifecycleOwner.lifecycleScope.launch { - repeatOnLifecycle(Lifecycle.State.CREATED) { - homeViewModel.shouldPageForward.collect { - if (it) { - pageForward() - homeViewModel.setShouldPageForward(false) + viewLifecycleOwner.lifecycleScope.apply { + launch { + repeatOnLifecycle(Lifecycle.State.CREATED) { + homeViewModel.shouldPageForward.collect { + if (it) { + pageForward() + homeViewModel.setShouldPageForward(false) + } + } + } + } + launch { + repeatOnLifecycle(Lifecycle.State.CREATED) { + homeViewModel.gamesDirSelected.collect { + if (it) { + gamesDirCallback.onStepCompleted() + homeViewModel.setGamesDirSelected(false) + } } } } @@ -339,7 +354,6 @@ class SetupFragment : Fragment() { registerForActivityResult(ActivityResultContracts.OpenDocumentTree()) { result -> if (result != null) { mainActivity.processGamesDir(result) - gamesDirCallback.onStepCompleted() } } diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/GamesViewModel.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/GamesViewModel.kt index 752d98c10..fd925235b 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/GamesViewModel.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/GamesViewModel.kt @@ -133,7 +133,7 @@ class GamesViewModel : ViewModel() { viewModelScope.launch { withContext(Dispatchers.IO) { NativeConfig.addGameDir(gameDir) - getGameDirs() + getGameDirs(true) } } diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/HomeViewModel.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/HomeViewModel.kt index 251b5a667..07e65b028 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/HomeViewModel.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/HomeViewModel.kt @@ -6,6 +6,7 @@ package org.yuzu.yuzu_emu.model import androidx.lifecycle.ViewModel import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.asStateFlow class HomeViewModel : ViewModel() { val navigationVisible: StateFlow> get() = _navigationVisible @@ -17,6 +18,9 @@ class HomeViewModel : ViewModel() { val shouldPageForward: StateFlow get() = _shouldPageForward private val _shouldPageForward = MutableStateFlow(false) + private val _gamesDirSelected = MutableStateFlow(false) + val gamesDirSelected get() = _gamesDirSelected.asStateFlow() + var navigatedToSetup = false fun setNavigationVisibility(visible: Boolean, animated: Boolean) { @@ -36,4 +40,8 @@ class HomeViewModel : ViewModel() { fun setShouldPageForward(pageForward: Boolean) { _shouldPageForward.value = pageForward } + + fun setGamesDirSelected(selected: Boolean) { + _gamesDirSelected.value = selected + } } -- cgit v1.2.3