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 4336f0d..44a344c 100644 --- a/DroidFishApp/src/main/java/org/petero/droidfish/activities/EditPGN.java +++ b/DroidFishApp/src/main/java/org/petero/droidfish/activities/EditPGN.java @@ -18,27 +18,12 @@ package org.petero.droidfish.activities; -import java.io.File; -import java.util.ArrayList; -import java.util.Locale; - -import org.petero.droidfish.ColorTheme; -import org.petero.droidfish.DroidFishApp; -import org.petero.droidfish.ObjectCache; -import org.petero.droidfish.R; -import org.petero.droidfish.Util; -import org.petero.droidfish.activities.PGNFile.GameInfo; -import org.petero.droidfish.activities.PGNFile.GameInfoResult; -import org.petero.droidfish.gamelogic.Pair; - import android.app.AlertDialog; import android.app.Dialog; import android.app.ListActivity; import android.app.ProgressDialog; -import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; -import android.content.DialogInterface.OnCancelListener; import android.content.SharedPreferences.Editor; import android.content.res.Configuration; import android.os.Bundle; @@ -49,14 +34,26 @@ import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; -import android.widget.AdapterView; import android.widget.ArrayAdapter; -import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; -import android.widget.AdapterView.OnItemClickListener; -import android.widget.AdapterView.OnItemLongClickListener; + +import androidx.databinding.DataBindingUtil; + +import org.petero.droidfish.ColorTheme; +import org.petero.droidfish.DroidFishApp; +import org.petero.droidfish.ObjectCache; +import org.petero.droidfish.R; +import org.petero.droidfish.Util; +import org.petero.droidfish.activities.PGNFile.GameInfo; +import org.petero.droidfish.activities.PGNFile.GameInfoResult; +import org.petero.droidfish.databinding.SelectGameBinding; +import org.petero.droidfish.gamelogic.Pair; + +import java.io.File; +import java.util.ArrayList; +import java.util.Locale; public abstract class EditPGN extends ListActivity { static ArrayList gamesInFile = new ArrayList<>(); @@ -65,8 +62,6 @@ public abstract class EditPGN extends ListActivity { ProgressDialog progress; private GameInfo selectedGi = null; ArrayAdapter aa = null; - TextView hintText = null; - EditText filterText = null; SharedPreferences settings; int defaultItem = 0; @@ -80,6 +75,8 @@ public abstract class EditPGN extends ListActivity { boolean loadGame; // True when loading game, false when saving String pgnToSave; + private SelectGameBinding binding; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -124,7 +121,7 @@ public abstract class EditPGN extends ListActivity { }); workThread.start(); } else if ("org.petero.droidfish.loadFileNextGame".equals(action) || - "org.petero.droidfish.loadFilePrevGame".equals(action)) { + "org.petero.droidfish.loadFilePrevGame".equals(action)) { pgnFile = new PGNFile(fileName); loadGame = true; boolean next = action.equals("org.petero.droidfish.loadFileNextGame"); @@ -227,9 +224,9 @@ public abstract class EditPGN extends ListActivity { @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { - case R.id.item_delete_file: - reShowDialog(DELETE_PGN_FILE_DIALOG); - break; + case R.id.item_delete_file: + reShowDialog(DELETE_PGN_FILE_DIALOG); + break; } return false; } @@ -237,7 +234,7 @@ public abstract class EditPGN extends ListActivity { private void showList() { progress = null; removeDialog(PROGRESS_DIALOG); - setContentView(R.layout.select_game); + binding = DataBindingUtil.setContentView(this, R.layout.select_game); Util.overrideViewAttribs(findViewById(android.R.id.content)); aa = new ArrayAdapter(this, R.layout.select_game_list_item, gamesInFile) { @Override @@ -272,24 +269,26 @@ public abstract class EditPGN extends ListActivity { return true; }); - filterText = findViewById(R.id.select_game_filter); - filterText.addTextChangedListener(new TextWatcher() { + binding.selectGameFilter.addTextChangedListener(new TextWatcher() { @Override - public void afterTextChanged(Editable s) { } + public void afterTextChanged(Editable s) { + } + @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { } + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + } + @Override public void onTextChanged(CharSequence s, int start, int before, int count) { aa.getFilter().filter(s); lastSearchString = s.toString(); } }); - filterText.setText(lastSearchString); - hintText = findViewById(R.id.select_game_hint); + binding.selectGameFilter.setText(lastSearchString); if (loadGame) { - hintText.setVisibility(View.GONE); + binding.selectGameHint.setVisibility(View.GONE); } else { - hintText.setText(R.string.save_game_hint); + binding.selectGameHint.setText(R.string.save_game_hint); } lv.requestFocus(); } @@ -304,7 +303,9 @@ public abstract class EditPGN extends ListActivity { final static int SAVE_GAME_DIALOG = 2; final static int DELETE_PGN_FILE_DIALOG = 3; - /** Remove and show a dialog. */ + /** + * Remove and show a dialog. + */ private void reShowDialog(int id) { removeDialog(id); showDialog(id); @@ -313,74 +314,81 @@ public abstract class EditPGN extends ListActivity { @Override protected Dialog onCreateDialog(int id) { switch (id) { - case PROGRESS_DIALOG: - progress = new ProgressDialog(this); - progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); - progress.setTitle(R.string.reading_pgn_file); - progress.setOnCancelListener(dialog -> { - canceled = true; - Thread thr = workThread; - if (thr != null) - thr.interrupt(); - }); - return progress; - case DELETE_GAME_DIALOG: { - final GameInfo gi = selectedGi; - selectedGi = null; - if (gi == null) + case PROGRESS_DIALOG: + progress = new ProgressDialog(this); + progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); + progress.setTitle(R.string.reading_pgn_file); + progress.setOnCancelListener(dialog -> { + canceled = true; + Thread thr = workThread; + if (thr != null) + thr.interrupt(); + }); + return progress; + case DELETE_GAME_DIALOG: { + final GameInfo gi = selectedGi; + selectedGi = null; + if (gi == null) + return null; + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle(R.string.delete_game); + String msg = gi.toString(); + builder.setMessage(msg); + builder.setPositiveButton(R.string.yes, (dialog, id14) -> { + deleteGame(gi); + dialog.cancel(); + }); + builder.setNegativeButton(R.string.no, (dialog, id13) -> dialog.cancel()); + return builder.create(); + } + case SAVE_GAME_DIALOG: { + final GameInfo gi = selectedGi; + selectedGi = null; + if (gi == null) + return null; + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle(R.string.save_game_question); + final CharSequence[] items = { + getString(R.string.before_selected), + getString(R.string.after_selected), + getString(R.string.replace_selected), + }; + builder.setItems(items, (dialog, item) -> { + GameInfo giToReplace; + switch (item) { + case 0: + giToReplace = new GameInfo().setNull(gi.startPos); + break; + case 1: + giToReplace = new GameInfo().setNull(gi.endPos); + break; + case 2: + giToReplace = gi; + break; + default: + finish(); + return; + } + pgnFile.replacePGN(pgnToSave, giToReplace); + finish(); + }); + return builder.create(); + } + case DELETE_PGN_FILE_DIALOG: { + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle(R.string.delete_file_question); + String name = new File(pgnFile.getName()).getName(); + String msg = String.format(Locale.US, getString(R.string.delete_named_file), name); + builder.setMessage(msg); + builder.setPositiveButton(R.string.yes, (dialog, id12) -> { + pgnFile.delete(); + finish(); + }); + builder.setNegativeButton(R.string.no, (dialog, id1) -> dialog.cancel()); + return builder.create(); + } + default: return null; - AlertDialog.Builder builder = new AlertDialog.Builder(this); - builder.setTitle(R.string.delete_game); - String msg = gi.toString(); - builder.setMessage(msg); - builder.setPositiveButton(R.string.yes, (dialog, id14) -> { - deleteGame(gi); - dialog.cancel(); - }); - builder.setNegativeButton(R.string.no, (dialog, id13) -> dialog.cancel()); - return builder.create(); - } - case SAVE_GAME_DIALOG: { - final GameInfo gi = selectedGi; - selectedGi = null; - if (gi == null) - return null; - AlertDialog.Builder builder = new AlertDialog.Builder(this); - builder.setTitle(R.string.save_game_question); - final CharSequence[] items = { - getString(R.string.before_selected), - getString(R.string.after_selected), - getString(R.string.replace_selected), - }; - builder.setItems(items, (dialog, item) -> { - GameInfo giToReplace; - switch (item) { - case 0: giToReplace = new GameInfo().setNull(gi.startPos); break; - case 1: giToReplace = new GameInfo().setNull(gi.endPos); break; - case 2: giToReplace = gi; break; - default: - finish(); return; - } - pgnFile.replacePGN(pgnToSave, giToReplace); - finish(); - }); - return builder.create(); - } - case DELETE_PGN_FILE_DIALOG: { - AlertDialog.Builder builder = new AlertDialog.Builder(this); - builder.setTitle(R.string.delete_file_question); - String name = new File(pgnFile.getName()).getName(); - String msg = String.format(Locale.US, getString(R.string.delete_named_file), name); - builder.setMessage(msg); - builder.setPositiveButton(R.string.yes, (dialog, id12) -> { - pgnFile.delete(); - finish(); - }); - builder.setNegativeButton(R.string.no, (dialog, id1) -> dialog.cancel()); - return builder.create(); - } - default: - return null; } } @@ -395,15 +403,15 @@ public abstract class EditPGN extends ListActivity { if (p.first != GameInfoResult.OK) { gamesInFile = new ArrayList<>(); switch (p.first) { - case OUT_OF_MEMORY: - runOnUiThread(() -> DroidFishApp.toast(R.string.file_too_large, Toast.LENGTH_SHORT)); - break; - case NOT_PGN: - runOnUiThread(() -> DroidFishApp.toast(R.string.not_a_pgn_file, Toast.LENGTH_SHORT)); - break; - case CANCEL: - case OK: - break; + case OUT_OF_MEMORY: + runOnUiThread(() -> DroidFishApp.toast(R.string.file_too_large, Toast.LENGTH_SHORT)); + break; + case NOT_PGN: + runOnUiThread(() -> DroidFishApp.toast(R.string.not_a_pgn_file, Toast.LENGTH_SHORT)); + break; + case CANCEL: + case OK: + break; } setResult(RESULT_CANCELED); finish(); @@ -431,10 +439,10 @@ public abstract class EditPGN extends ListActivity { private void deleteGame(GameInfo gi) { if (pgnFile.deleteGame(gi, gamesInFile)) { ListView lv = getListView(); - int pos = lv.pointToPosition(0,0); + int pos = lv.pointToPosition(0, 0); aa = new ArrayAdapter<>(this, R.layout.select_game_list_item, gamesInFile); setListAdapter(aa); - String s = filterText.getText().toString(); + String s = binding.selectGameFilter.getText().toString(); aa.getFilter().filter(s); lv.setSelection(pos); // Update lastModTime, since current change has already been handled diff --git a/DroidFishApp/src/main/java/org/petero/droidfish/activities/LoadFEN.java b/DroidFishApp/src/main/java/org/petero/droidfish/activities/LoadFEN.java index 583874c..0371d05 100644 --- a/DroidFishApp/src/main/java/org/petero/droidfish/activities/LoadFEN.java +++ b/DroidFishApp/src/main/java/org/petero/droidfish/activities/LoadFEN.java @@ -18,22 +18,6 @@ package org.petero.droidfish.activities; -import java.io.File; -import java.util.ArrayList; -import java.util.concurrent.CountDownLatch; - -import org.petero.droidfish.ChessBoardPlay; -import org.petero.droidfish.ColorTheme; -import org.petero.droidfish.DroidFishApp; -import org.petero.droidfish.R; -import org.petero.droidfish.Util; -import org.petero.droidfish.activities.FENFile.FenInfo; -import org.petero.droidfish.activities.FENFile.FenInfoResult; -import org.petero.droidfish.gamelogic.ChessParseError; -import org.petero.droidfish.gamelogic.Pair; -import org.petero.droidfish.gamelogic.Position; -import org.petero.droidfish.gamelogic.TextIO; - import android.app.Activity; import android.app.Dialog; import android.app.DialogFragment; @@ -48,16 +32,30 @@ import android.content.res.Configuration; import android.os.Bundle; import android.preference.PreferenceManager; import android.view.View; -import android.view.View.OnClickListener; import android.view.ViewGroup; -import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; -import android.widget.AdapterView.OnItemClickListener; -import android.widget.AdapterView.OnItemLongClickListener; + +import androidx.databinding.DataBindingUtil; + +import org.petero.droidfish.ColorTheme; +import org.petero.droidfish.DroidFishApp; +import org.petero.droidfish.R; +import org.petero.droidfish.Util; +import org.petero.droidfish.activities.FENFile.FenInfo; +import org.petero.droidfish.activities.FENFile.FenInfoResult; +import org.petero.droidfish.databinding.LoadFenBinding; +import org.petero.droidfish.gamelogic.ChessParseError; +import org.petero.droidfish.gamelogic.Pair; +import org.petero.droidfish.gamelogic.Position; +import org.petero.droidfish.gamelogic.TextIO; + +import java.io.File; +import java.util.ArrayList; +import java.util.concurrent.CountDownLatch; public class LoadFEN extends ListActivity { private static ArrayList fensInFile = new ArrayList<>(); @@ -76,8 +74,7 @@ public class LoadFEN extends ListActivity { private CountDownLatch progressLatch = null; private boolean canceled = false; - private ChessBoardPlay cb; - private Button okButton; + LoadFenBinding binding; @Override protected void onCreate(Bundle savedInstanceState) { @@ -126,7 +123,7 @@ public class LoadFEN extends ListActivity { }); workThread.start(); } else if ("org.petero.droidfish.loadNextFen".equals(action) || - "org.petero.droidfish.loadPrevFen".equals(action)) { + "org.petero.droidfish.loadPrevFen".equals(action)) { fenFile = new FENFile(fileName); boolean next = action.equals("org.petero.droidfish.loadNextFen"); final int loadItem = defaultItem + (next ? 1 : -1); @@ -192,17 +189,13 @@ public class LoadFEN extends ListActivity { progress = null; removeProgressDialog(); setContentView(R.layout.load_fen); - - cb = findViewById(R.id.loadfen_chessboard); - okButton = findViewById(R.id.loadfen_ok); - Button cancelButton = findViewById(R.id.loadfen_cancel); - - okButton.setEnabled(false); - okButton.setOnClickListener(v -> { + binding = DataBindingUtil.setContentView(this, R.layout.load_fen); + binding.loadfenOk.setEnabled(false); + binding.loadfenOk.setOnClickListener(v -> { if (selectedFi != null) sendBackResult(selectedFi, false); }); - cancelButton.setOnClickListener(v -> { + binding.loadfenCancel.setOnClickListener(v -> { setResult(RESULT_CANCELED); finish(); }); @@ -235,8 +228,8 @@ public class LoadFEN extends ListActivity { chessPos = e2.pos; } if (chessPos != null) { - cb.setPosition(chessPos); - okButton.setEnabled(true); + binding.loadfenChessboard.setPosition(chessPos); + binding.loadfenOk.setEnabled(true); } }); lv.setOnItemLongClickListener((parent, view, pos, id) -> { @@ -260,18 +253,18 @@ public class LoadFEN extends ListActivity { @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); - if (cb != null) { - Position pos = cb.pos; + if (binding.loadfenChessboard != null) { + Position pos = binding.loadfenChessboard.pos; showList(); - cb.setPosition(pos); - okButton.setEnabled(selectedFi != null); + binding.loadfenChessboard.setPosition(pos); + binding.loadfenOk.setEnabled(selectedFi != null); } } public static class ProgressFragment extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { - LoadFEN a = (LoadFEN)getActivity(); + LoadFEN a = (LoadFEN) getActivity(); ProgressDialog progress = new ProgressDialog(a); progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progress.setTitle(R.string.reading_fen_file); @@ -279,12 +272,13 @@ public class LoadFEN extends ListActivity { a.progressLatch.countDown(); return progress; } + @Override public void onCancel(DialogInterface dialog) { super.onCancel(dialog); Activity a = getActivity(); if (a instanceof LoadFEN) { - LoadFEN lf = (LoadFEN)a; + LoadFEN lf = (LoadFEN) a; lf.canceled = true; Thread thr = lf.workThread; if (thr != null) @@ -301,7 +295,7 @@ public class LoadFEN extends ListActivity { private void removeProgressDialog() { Fragment f = getFragmentManager().findFragmentByTag("progress"); if (f instanceof DialogFragment) - ((DialogFragment)f).dismiss(); + ((DialogFragment) f).dismiss(); } private boolean readFile() { diff --git a/DroidFishApp/src/main/res/layout-land/editoptions.xml b/DroidFishApp/src/main/res/layout-land/editoptions.xml index 71dab40..6580cfa 100644 --- a/DroidFishApp/src/main/res/layout-land/editoptions.xml +++ b/DroidFishApp/src/main/res/layout-land/editoptions.xml @@ -17,6 +17,7 @@ android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" /> + + diff --git a/DroidFishApp/src/main/res/layout-land/load_fen.xml b/DroidFishApp/src/main/res/layout-land/load_fen.xml index 0c8e7e4..275a89c 100644 --- a/DroidFishApp/src/main/res/layout-land/load_fen.xml +++ b/DroidFishApp/src/main/res/layout-land/load_fen.xml @@ -1,54 +1,63 @@ - - + + - - - - - - - + + - - + android:orientation="horizontal"> + + - - - - - - + android:orientation="vertical"> + + + + + + + + + + + +