From 65537e70f8ee75994080b30c24f17510f47deb09 Mon Sep 17 00:00:00 2001 From: Peter Osterlund Date: Mon, 10 Feb 2020 21:17:49 +0100 Subject: [PATCH] New file action "load deleted game" The new action "load deleted game" loads a recently deleted game from the autosave file, without affecting the "last used file" and "current position in last used file" properties. --- .../java/org/petero/droidfish/DroidFish.java | 33 ++++++++++++++----- .../petero/droidfish/activities/EditPGN.java | 23 +++++++++---- DroidFishApp/src/main/res/values/strings.xml | 1 + 3 files changed, 42 insertions(+), 15 deletions(-) diff --git a/DroidFishApp/src/main/java/org/petero/droidfish/DroidFish.java b/DroidFishApp/src/main/java/org/petero/droidfish/DroidFish.java index 9fbde56..1995786 100644 --- a/DroidFishApp/src/main/java/org/petero/droidfish/DroidFish.java +++ b/DroidFishApp/src/main/java/org/petero/droidfish/DroidFish.java @@ -2305,11 +2305,12 @@ public class DroidFish extends Activity } private Dialog fileMenuDialog() { - final int LOAD_LAST_FILE = 0; - final int LOAD_GAME = 1; - final int LOAD_POS = 2; - final int LOAD_SCID_GAME = 3; - final int SAVE_GAME = 4; + final int LOAD_LAST_FILE = 0; + final int LOAD_GAME = 1; + final int LOAD_POS = 2; + final int LOAD_SCID_GAME = 3; + final int SAVE_GAME = 4; + final int LOAD_DELETED_GAME = 5; setAutoMode(AutoMode.OFF); List lst = new ArrayList<>(); @@ -2322,6 +2323,9 @@ public class DroidFish extends Activity if (hasScidProvider()) { lst.add(getString(R.string.load_scid_game)); actions.add(LOAD_SCID_GAME); } + if (storageAvailable() && (new File(getAutoSaveFile())).exists()) { + lst.add(getString(R.string.load_del_game)); actions.add(LOAD_DELETED_GAME); + } lst.add(getString(R.string.save_game)); actions.add(SAVE_GAME); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(R.string.load_save_menu); @@ -2345,6 +2349,9 @@ public class DroidFish extends Activity case LOAD_SCID_GAME: selectScidFile(); break; + case LOAD_DELETED_GAME: + loadPGNFromFile(getAutoSaveFile(), false); + break; } }); return builder.create(); @@ -3515,13 +3522,21 @@ public class DroidFish extends Activity /** Load a PGN game from a file. */ private void loadPGNFromFile(String pathName) { - Editor editor = settings.edit(); - editor.putString("currentPGNFile", pathName); - editor.putInt("currFT", FT_PGN); - editor.apply(); + loadPGNFromFile(pathName, true); + } + + /** Load a PGN game from a file. */ + private void loadPGNFromFile(String pathName, boolean updateCurrFile) { + if (updateCurrFile) { + Editor editor = settings.edit(); + editor.putString("currentPGNFile", pathName); + editor.putInt("currFT", FT_PGN); + editor.apply(); + } Intent i = new Intent(DroidFish.this, EditPGNLoad.class); i.setAction("org.petero.droidfish.loadFile"); i.putExtra("org.petero.droidfish.pathname", pathName); + i.putExtra("org.petero.droidfish.updateDefFilePos", updateCurrFile); setEditPGNBackup(i, pathName); startActivityForResult(i, RESULT_LOAD_PGN); } diff --git a/DroidFishApp/src/main/java/org/petero/droidfish/activities/EditPGN.java b/DroidFishApp/src/main/java/org/petero/droidfish/activities/EditPGN.java index 210b0b9..69b7d12 100644 --- a/DroidFishApp/src/main/java/org/petero/droidfish/activities/EditPGN.java +++ b/DroidFishApp/src/main/java/org/petero/droidfish/activities/EditPGN.java @@ -69,6 +69,7 @@ public abstract class EditPGN extends AppCompatActivity { private SharedPreferences settings; private long defaultFilePos = 0; + private boolean updateDefaultFilePos; private long currentFilePos = 0; private String lastSearchString = ""; private String lastFileName = ""; @@ -111,6 +112,7 @@ public abstract class EditPGN extends AppCompatActivity { String action = i.getAction(); String fileName = i.getStringExtra("org.petero.droidfish.pathname"); backup = i.getBooleanExtra("org.petero.droidfish.backup", false); + updateDefaultFilePos = i.getBooleanExtra("org.petero.droidfish.updateDefFilePos", true); canceled = false; if ("org.petero.droidfish.loadFile".equals(action)) { pgnFile = new PGNFile(fileName); @@ -159,7 +161,7 @@ public abstract class EditPGN extends AppCompatActivity { finish(); } else { GameInfo gi = gamesInFile.get(loadItem); - defaultFilePos = gi.startPos; + setDefaultFilePos(gi.startPos); sendBackResult(gi); } }); @@ -283,7 +285,7 @@ public abstract class EditPGN extends AppCompatActivity { if (selectedGi == null) return; if (loadGame) { - defaultFilePos = selectedGi.startPos; + setDefaultFilePos(selectedGi.startPos); sendBackResult(selectedGi); } else { reShowDialog(SAVE_GAME_DIALOG); @@ -430,18 +432,27 @@ public abstract class EditPGN extends AppCompatActivity { } } + private void setDefaultFilePos(long pos) { + if (updateDefaultFilePos) + defaultFilePos = pos; + } + private boolean readFile() { String fileName = pgnFile.getName(); if (!fileName.equals(lastFileName)) - defaultFilePos = 0; + setDefaultFilePos(0); long modTime = new File(fileName).lastModified(); if (cacheValid && (modTime == lastModTime) && fileName.equals(lastFileName)) return true; try { gamesInFile = pgnFile.getGameInfo(this, progress); - cacheValid = true; - lastModTime = modTime; - lastFileName = fileName; + if (updateDefaultFilePos) { + cacheValid = true; + lastModTime = modTime; + lastFileName = fileName; + } else { + cacheValid = false; + } return true; } catch (PGNFile.CancelException ignore) { } catch (PGNFile.NotPgnFile ex) { diff --git a/DroidFishApp/src/main/res/values/strings.xml b/DroidFishApp/src/main/res/values/strings.xml index 0b0b441..9e1bd4f 100644 --- a/DroidFishApp/src/main/res/values/strings.xml +++ b/DroidFishApp/src/main/res/values/strings.xml @@ -39,6 +39,7 @@ If you are running on battery power, it is recommended that you change settings Load game from PGN file Load position from file Load game from Scid file + Load deleted game Save game to PGN file Retrieve Position Repeat last move