Implement functionality of 'import' button in CreateSudokuActivity

This commit is contained in:
uykek 2020-08-22 15:05:23 +02:00
parent 1bf2c9ec7f
commit 07552a1089
2 changed files with 68 additions and 6 deletions

View file

@ -42,6 +42,7 @@ import org.secuso.privacyfriendlysudoku.controller.qqwing.QQWing;
import org.secuso.privacyfriendlysudoku.game.GameDifficulty;
import org.secuso.privacyfriendlysudoku.game.GameType;
import org.secuso.privacyfriendlysudoku.ui.listener.IFinalizeDialogFragmentListener;
import org.secuso.privacyfriendlysudoku.ui.listener.IImportDialogFragmentListener;
import org.secuso.privacyfriendlysudoku.ui.view.CreateSudokuSpecialButtonLayout;
import org.secuso.privacyfriendlysudoku.ui.view.R;
import org.secuso.privacyfriendlysudoku.ui.view.SudokuFieldLayout;
@ -52,9 +53,10 @@ import org.secuso.privacyfriendlysudoku.ui.view.SudokuKeyboardLayout;
* IFinalizeDialogFragementListener. It is used to create custom sudokus, which are passed to the
* GameActivity afterwards.
*/
public class CreateSudokuActivity extends BaseActivity implements IFinalizeDialogFragmentListener {
public class CreateSudokuActivity extends BaseActivity implements IFinalizeDialogFragmentListener, IImportDialogFragmentListener {
GameController gameController;
SharedPreferences sharedPref;
SudokuFieldLayout layout;
SudokuKeyboardLayout keyboard;
TextView viewName ;
@ -63,7 +65,7 @@ public class CreateSudokuActivity extends BaseActivity implements IFinalizeDialo
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
if(sharedPref.getBoolean("pref_keep_screen_on", true)) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
@ -80,6 +82,12 @@ public class CreateSudokuActivity extends BaseActivity implements IFinalizeDialo
gameType, new int [boardSize], new int [boardSize], new boolean [boardSize][sectionSize]);
gameController.loadLevel(container);
setUpLayout();
}
private void setUpLayout() {
setContentView(R.layout.activity_create_sudoku);
layout = (SudokuFieldLayout)findViewById(R.id.sudokuLayout);
layout.setSettingsAndGame(sharedPref, gameController);
@ -96,13 +104,13 @@ public class CreateSudokuActivity extends BaseActivity implements IFinalizeDialo
keyboard.setKeyBoard(gameController.getSize(), p.x,layout.getHeight()-p.y, orientation);
specialButtonLayout = (CreateSudokuSpecialButtonLayout) findViewById(R.id.createSudokuLayout);
specialButtonLayout.setButtons(p.x, gameController, keyboard, getFragmentManager(), orientation, CreateSudokuActivity.this, this);
specialButtonLayout.setButtons(p.x, gameController, keyboard, getFragmentManager(), orientation,
CreateSudokuActivity.this, this, this);
viewName = (TextView) findViewById(R.id.gameModeText);
viewName.setText(getString(gameController.getGameType().getStringResID()));
gameController.notifyHighlightChangedListeners();
}
@Override
@ -186,6 +194,53 @@ public class CreateSudokuActivity extends BaseActivity implements IFinalizeDialo
}
}
public void onImportDialogPositiveClick(String input) {
String inputSudoku;
// a valid input needs to contain exactly one of these prefixes
String prefix1 = GameActivity.URL_SCHEME_WITHOUT_HOST + "://";
String prefix2 = GameActivity.URL_SCHEME_WITH_HOST + "://" + GameActivity.URL_HOST + "/";
/*
remove the present prefix, or, if the input contains neither of the prefixes, notify the user
that their input is not valid
*/
if (input.contains(prefix1)) {
inputSudoku = input.replace(prefix1, "");
} else if (input.contains(prefix2)) {
inputSudoku = input.replace(prefix2, "");
} else {
Toast.makeText(CreateSudokuActivity.this,
this.getString(R.string.menu_import_wrong_format_custom_sudoku) + " " + prefix1 + ", " + prefix2, Toast.LENGTH_LONG).show();
return;
}
boolean validSize = Math.sqrt(inputSudoku.length()) == gameController.getSize();
if (!validSize) {
Toast.makeText(CreateSudokuActivity.this, R.string.failed_to_verify_custom_sudoku_toast, Toast.LENGTH_LONG).show();
return;
}
//check whether or not the sudoku is valid and has a unique solution
boolean solvable = verify(gameController.getGameType(), inputSudoku);
// if the encoded sudoku is solvable, sent the code directly to the GameActivity; if not, notify the user
if (solvable) {
Toast.makeText(CreateSudokuActivity.this, R.string.finished_verifying_custom_sudoku_toast, Toast.LENGTH_LONG).show();
int boardSize = gameController.getGameType().getSize() * gameController.getGameType().getSize();
GameInfoContainer container = new GameInfoContainer(0, GameDifficulty.Unspecified,
gameController.getGameType(), new int [boardSize], new int [boardSize],
new boolean [boardSize][gameController.getGameType().getSize()]);
container.parseSetValues(inputSudoku);
gameController.loadLevel(container);
setUpLayout();
} else {
Toast.makeText(CreateSudokuActivity.this, R.string.failed_to_verify_custom_sudoku_toast, Toast.LENGTH_LONG).show();
}
}
/**
* Implements the onDialogNegativeClick() method of the IFinalizeDialogFragmentListener

View file

@ -43,8 +43,10 @@ import androidx.core.content.ContextCompat;
import org.secuso.privacyfriendlysudoku.controller.GameController;
import org.secuso.privacyfriendlysudoku.game.listener.IHighlightChangedListener;
import org.secuso.privacyfriendlysudoku.ui.GameActivity;
import org.secuso.privacyfriendlysudoku.ui.MainActivity;
import org.secuso.privacyfriendlysudoku.ui.listener.IFinalizeDialogFragmentListener;
import org.secuso.privacyfriendlysudoku.ui.listener.IHintDialogFragmentListener;
import org.secuso.privacyfriendlysudoku.ui.listener.IImportDialogFragmentListener;
import java.util.LinkedList;
@ -54,6 +56,7 @@ import static org.secuso.privacyfriendlysudoku.ui.view.CreateSudokuButtonType.ge
public class CreateSudokuSpecialButtonLayout extends LinearLayout implements IHighlightChangedListener {
IFinalizeDialogFragmentListener finalizeDialogFragmentListener;
IImportDialogFragmentListener importDialogFragmentListener;
CreateSudokuSpecialButton[] fixedButtons;
public int fixedButtonsCount = getSpecialButtons().size();
GameController gameController;
@ -77,7 +80,9 @@ public class CreateSudokuSpecialButtonLayout extends LinearLayout implements IHi
gameController.deleteSelectedCellsValue();
break;
case Import:
break;
MainActivity.ImportBoardDialog impDialog = new MainActivity.ImportBoardDialog();
impDialog.show(fragmentManager, "ImportDialogFragment");
break;
case Do:
gameController.ReDo();
break;
@ -108,12 +113,14 @@ public class CreateSudokuSpecialButtonLayout extends LinearLayout implements IHi
}
public void setButtons(int width, GameController gc, SudokuKeyboardLayout key, FragmentManager fm,
int orientation, Context cxt, IFinalizeDialogFragmentListener finalizeListener) {
int orientation, Context cxt, IFinalizeDialogFragmentListener finalizeListener,
IImportDialogFragmentListener importListener) {
fragmentManager = fm;
keyboard=key;
gameController = gc;
context = cxt;
finalizeDialogFragmentListener = finalizeListener;
importDialogFragmentListener = importListener;
if(gameController != null) {
gameController.registerHighlightChangedListener(this);
}