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.
This commit is contained in:
Peter Osterlund 2020-02-10 21:17:49 +01:00
parent 683a238bff
commit 65537e70f8
3 changed files with 42 additions and 15 deletions

View File

@ -2305,11 +2305,12 @@ public class DroidFish extends Activity
} }
private Dialog fileMenuDialog() { private Dialog fileMenuDialog() {
final int LOAD_LAST_FILE = 0; final int LOAD_LAST_FILE = 0;
final int LOAD_GAME = 1; final int LOAD_GAME = 1;
final int LOAD_POS = 2; final int LOAD_POS = 2;
final int LOAD_SCID_GAME = 3; final int LOAD_SCID_GAME = 3;
final int SAVE_GAME = 4; final int SAVE_GAME = 4;
final int LOAD_DELETED_GAME = 5;
setAutoMode(AutoMode.OFF); setAutoMode(AutoMode.OFF);
List<String> lst = new ArrayList<>(); List<String> lst = new ArrayList<>();
@ -2322,6 +2323,9 @@ public class DroidFish extends Activity
if (hasScidProvider()) { if (hasScidProvider()) {
lst.add(getString(R.string.load_scid_game)); actions.add(LOAD_SCID_GAME); 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); lst.add(getString(R.string.save_game)); actions.add(SAVE_GAME);
AlertDialog.Builder builder = new AlertDialog.Builder(this); AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.load_save_menu); builder.setTitle(R.string.load_save_menu);
@ -2345,6 +2349,9 @@ public class DroidFish extends Activity
case LOAD_SCID_GAME: case LOAD_SCID_GAME:
selectScidFile(); selectScidFile();
break; break;
case LOAD_DELETED_GAME:
loadPGNFromFile(getAutoSaveFile(), false);
break;
} }
}); });
return builder.create(); return builder.create();
@ -3515,13 +3522,21 @@ public class DroidFish extends Activity
/** Load a PGN game from a file. */ /** Load a PGN game from a file. */
private void loadPGNFromFile(String pathName) { private void loadPGNFromFile(String pathName) {
Editor editor = settings.edit(); loadPGNFromFile(pathName, true);
editor.putString("currentPGNFile", pathName); }
editor.putInt("currFT", FT_PGN);
editor.apply(); /** 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); Intent i = new Intent(DroidFish.this, EditPGNLoad.class);
i.setAction("org.petero.droidfish.loadFile"); i.setAction("org.petero.droidfish.loadFile");
i.putExtra("org.petero.droidfish.pathname", pathName); i.putExtra("org.petero.droidfish.pathname", pathName);
i.putExtra("org.petero.droidfish.updateDefFilePos", updateCurrFile);
setEditPGNBackup(i, pathName); setEditPGNBackup(i, pathName);
startActivityForResult(i, RESULT_LOAD_PGN); startActivityForResult(i, RESULT_LOAD_PGN);
} }

View File

@ -69,6 +69,7 @@ public abstract class EditPGN extends AppCompatActivity {
private SharedPreferences settings; private SharedPreferences settings;
private long defaultFilePos = 0; private long defaultFilePos = 0;
private boolean updateDefaultFilePos;
private long currentFilePos = 0; private long currentFilePos = 0;
private String lastSearchString = ""; private String lastSearchString = "";
private String lastFileName = ""; private String lastFileName = "";
@ -111,6 +112,7 @@ public abstract class EditPGN extends AppCompatActivity {
String action = i.getAction(); String action = i.getAction();
String fileName = i.getStringExtra("org.petero.droidfish.pathname"); String fileName = i.getStringExtra("org.petero.droidfish.pathname");
backup = i.getBooleanExtra("org.petero.droidfish.backup", false); backup = i.getBooleanExtra("org.petero.droidfish.backup", false);
updateDefaultFilePos = i.getBooleanExtra("org.petero.droidfish.updateDefFilePos", true);
canceled = false; canceled = false;
if ("org.petero.droidfish.loadFile".equals(action)) { if ("org.petero.droidfish.loadFile".equals(action)) {
pgnFile = new PGNFile(fileName); pgnFile = new PGNFile(fileName);
@ -159,7 +161,7 @@ public abstract class EditPGN extends AppCompatActivity {
finish(); finish();
} else { } else {
GameInfo gi = gamesInFile.get(loadItem); GameInfo gi = gamesInFile.get(loadItem);
defaultFilePos = gi.startPos; setDefaultFilePos(gi.startPos);
sendBackResult(gi); sendBackResult(gi);
} }
}); });
@ -283,7 +285,7 @@ public abstract class EditPGN extends AppCompatActivity {
if (selectedGi == null) if (selectedGi == null)
return; return;
if (loadGame) { if (loadGame) {
defaultFilePos = selectedGi.startPos; setDefaultFilePos(selectedGi.startPos);
sendBackResult(selectedGi); sendBackResult(selectedGi);
} else { } else {
reShowDialog(SAVE_GAME_DIALOG); 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() { private boolean readFile() {
String fileName = pgnFile.getName(); String fileName = pgnFile.getName();
if (!fileName.equals(lastFileName)) if (!fileName.equals(lastFileName))
defaultFilePos = 0; setDefaultFilePos(0);
long modTime = new File(fileName).lastModified(); long modTime = new File(fileName).lastModified();
if (cacheValid && (modTime == lastModTime) && fileName.equals(lastFileName)) if (cacheValid && (modTime == lastModTime) && fileName.equals(lastFileName))
return true; return true;
try { try {
gamesInFile = pgnFile.getGameInfo(this, progress); gamesInFile = pgnFile.getGameInfo(this, progress);
cacheValid = true; if (updateDefaultFilePos) {
lastModTime = modTime; cacheValid = true;
lastFileName = fileName; lastModTime = modTime;
lastFileName = fileName;
} else {
cacheValid = false;
}
return true; return true;
} catch (PGNFile.CancelException ignore) { } catch (PGNFile.CancelException ignore) {
} catch (PGNFile.NotPgnFile ex) { } catch (PGNFile.NotPgnFile ex) {

View File

@ -39,6 +39,7 @@ If you are running on battery power, it is recommended that you change settings
<string name="load_game">Load game from PGN file</string> <string name="load_game">Load game from PGN file</string>
<string name="load_position">Load position from file</string> <string name="load_position">Load position from file</string>
<string name="load_scid_game">Load game from Scid file</string> <string name="load_scid_game">Load game from Scid file</string>
<string name="load_del_game">Load deleted game</string>
<string name="save_game">Save game to PGN file</string> <string name="save_game">Save game to PGN file</string>
<string name="get_fen">Retrieve Position</string> <string name="get_fen">Retrieve Position</string>
<string name="repeat_last_move">Repeat last move</string> <string name="repeat_last_move">Repeat last move</string>