From 5dbf842a46a68de87471c3f8e265f284ebc2c362 Mon Sep 17 00:00:00 2001
From: Charles Lombardo <clombardo169@gmail.com>
Date: Mon, 22 May 2023 18:32:18 -0400
Subject: [PATCH] android: Improve missing game handling

Previously the app would crash if you selected a game that no longer existed. Now we show an error message and reload the games list to remove any invalid games from the list.
---
 .../org/yuzu/yuzu_emu/adapters/GameAdapter.kt | 19 ++++++++++++++++++-
 .../app/src/main/res/values/strings.xml       |  1 +
 2 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/adapters/GameAdapter.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/adapters/GameAdapter.kt
index a9653475f..7f9e2e2d4 100644
--- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/adapters/GameAdapter.kt
+++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/adapters/GameAdapter.kt
@@ -3,15 +3,18 @@
 
 package org.yuzu.yuzu_emu.adapters
 
-import android.annotation.SuppressLint
 import android.graphics.Bitmap
 import android.graphics.BitmapFactory
+import android.net.Uri
 import android.text.TextUtils
 import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
 import android.widget.ImageView
+import android.widget.Toast
 import androidx.appcompat.app.AppCompatActivity
+import androidx.documentfile.provider.DocumentFile
+import androidx.lifecycle.ViewModelProvider
 import androidx.lifecycle.lifecycleScope
 import androidx.preference.PreferenceManager
 import androidx.recyclerview.widget.AsyncDifferConfig
@@ -27,6 +30,7 @@ import org.yuzu.yuzu_emu.databinding.CardGameBinding
 import org.yuzu.yuzu_emu.activities.EmulationActivity
 import org.yuzu.yuzu_emu.model.Game
 import org.yuzu.yuzu_emu.adapters.GameAdapter.GameViewHolder
+import org.yuzu.yuzu_emu.model.GamesViewModel
 
 class GameAdapter(private val activity: AppCompatActivity) :
     ListAdapter<Game, GameViewHolder>(AsyncDifferConfig.Builder(DiffCallback()).build()),
@@ -53,6 +57,19 @@ class GameAdapter(private val activity: AppCompatActivity) :
      */
     override fun onClick(view: View) {
         val holder = view.tag as GameViewHolder
+
+        val gameExists = DocumentFile.fromSingleUri(YuzuApplication.appContext, Uri.parse(holder.game.path))?.exists() == true
+        if (!gameExists) {
+            Toast.makeText(
+                YuzuApplication.appContext,
+                R.string.loader_error_file_not_found,
+                Toast.LENGTH_LONG
+            ).show()
+
+            ViewModelProvider(activity)[GamesViewModel::class.java].reloadGames(true)
+            return
+        }
+
         val preferences = PreferenceManager.getDefaultSharedPreferences(YuzuApplication.appContext)
         preferences.edit()
             .putLong(
diff --git a/src/android/app/src/main/res/values/strings.xml b/src/android/app/src/main/res/values/strings.xml
index e171cf799..03a5ffc7e 100644
--- a/src/android/app/src/main/res/values/strings.xml
+++ b/src/android/app/src/main/res/values/strings.xml
@@ -177,6 +177,7 @@
     <string name="loader_error_video_core">An error occurred initializing the video core</string>
     <string name="loader_error_video_core_description">This is usually caused by an incompatible GPU driver. Installing a custom GPU driver may resolve this problem.</string>
     <string name="loader_error_invalid_format">Unable to load ROM</string>
+    <string name="loader_error_file_not_found">ROM file does not exist</string>
 
     <!-- Emulation Menu -->
     <string name="emulation_exit">Exit Emulation</string>