diff --git a/app/src/main/java/tu_darmstadt/sudoku/controller/SaveLoadGameStateController.java b/app/src/main/java/tu_darmstadt/sudoku/controller/SaveLoadGameStateController.java index c0f6a9a..358bfd8 100644 --- a/app/src/main/java/tu_darmstadt/sudoku/controller/SaveLoadGameStateController.java +++ b/app/src/main/java/tu_darmstadt/sudoku/controller/SaveLoadGameStateController.java @@ -117,7 +117,7 @@ public class SaveLoadGameStateController { return list; } - private void deleteGameStateFile(GameInfoContainer gic) { + public void deleteGameStateFile(GameInfoContainer gic) { File dir = context.getDir(SAVES_DIR, 0); File file = new File(dir, SAVE_PREFIX+gic.getID()+FILE_EXTENSION); diff --git a/app/src/main/java/tu_darmstadt/sudoku/ui/IDeleteDialogFragmentListener.java b/app/src/main/java/tu_darmstadt/sudoku/ui/IDeleteDialogFragmentListener.java new file mode 100644 index 0000000..33260a0 --- /dev/null +++ b/app/src/main/java/tu_darmstadt/sudoku/ui/IDeleteDialogFragmentListener.java @@ -0,0 +1,11 @@ +package tu_darmstadt.sudoku.ui; + +import android.app.DialogFragment; + +/** + * Created by Chris on 24.11.2015. + */ +public interface IDeleteDialogFragmentListener { + public void onDialogPositiveClick(int position); + public void onDialogNegativeClick(int position); +} diff --git a/app/src/main/java/tu_darmstadt/sudoku/ui/LoadGameActivity.java b/app/src/main/java/tu_darmstadt/sudoku/ui/LoadGameActivity.java index 88fb0e7..d08aec2 100644 --- a/app/src/main/java/tu_darmstadt/sudoku/ui/LoadGameActivity.java +++ b/app/src/main/java/tu_darmstadt/sudoku/ui/LoadGameActivity.java @@ -1,6 +1,12 @@ package tu_darmstadt.sudoku.ui; +import android.annotation.SuppressLint; +import android.app.Activity; +import android.app.AlertDialog; +import android.app.Dialog; +import android.app.DialogFragment; import android.content.Context; +import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.preference.PreferenceManager; @@ -10,6 +16,7 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; +import android.widget.ArrayAdapter; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; @@ -17,9 +24,8 @@ import android.widget.RatingBar; import android.widget.TextView; import java.text.DateFormat; -import java.text.FieldPosition; -import java.text.ParsePosition; import java.util.Date; +import java.util.LinkedList; import java.util.List; import java.util.TimeZone; @@ -28,10 +34,11 @@ import tu_darmstadt.sudoku.controller.helper.GameInfoContainer; import tu_darmstadt.sudoku.game.GameDifficulty; import tu_darmstadt.sudoku.ui.view.R; -public class LoadGameActivity extends AppCompatActivity { +public class LoadGameActivity extends AppCompatActivity implements IDeleteDialogFragmentListener { List loadableGameList; SharedPreferences settings; + LoadGameAdapter loadGameAdapter; @Override protected void onCreate(Bundle savedInstanceState) { @@ -41,6 +48,11 @@ public class LoadGameActivity extends AppCompatActivity { settings = PreferenceManager.getDefaultSharedPreferences(this); + init(); + } + + public void init() { + SaveLoadGameStateController saveLoadGameStateController = new SaveLoadGameStateController(this, settings); loadableGameList = saveLoadGameStateController.loadGameStateInfo(); @@ -58,17 +70,77 @@ public class LoadGameActivity extends AppCompatActivity { AdapterView.OnItemLongClickListener longClickListener = new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView parent, View view, int position, long id) { - return false; + DeleteDialogFragment deleteDialog = new DeleteDialogFragment(); + deleteDialog.setPosition(position); + deleteDialog.show(getFragmentManager(), "DeleteDialogFragment"); + + return true; } }; ListView listView = (ListView)findViewById(R.id.load_game_list); - listView.setAdapter(new LoadGameAdapter(this, loadableGameList)); + loadGameAdapter = new LoadGameAdapter(this, loadableGameList); + listView.setAdapter(loadGameAdapter); listView.setOnItemClickListener(clickListener); listView.setOnItemLongClickListener(longClickListener); } + @Override + public void onDialogPositiveClick(int position) { + SaveLoadGameStateController saveLoadGameStateController = new SaveLoadGameStateController(getApplicationContext(), settings); + saveLoadGameStateController.deleteGameStateFile(loadableGameList.get(position)); + loadGameAdapter.delete(position); + } + + @Override + public void onDialogNegativeClick(int position) { + // do nothing + } + + @SuppressLint("ValidFragment") + public class DeleteDialogFragment extends DialogFragment { + + private int position = 0; + + public void setPosition(int position) { + this.position = position; + } + public int getPosition() { + return position; + } + + LinkedList listeners = new LinkedList<>(); + + @Override + public void onAttach(Activity activity) { + super.onAttach(activity); + // Verify that the host activity implements the callback interface + if(activity instanceof IDeleteDialogFragmentListener) { + listeners.add((IDeleteDialogFragmentListener) activity); + } + } + + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + // Use the Builder class for convenient dialog construction + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + builder.setMessage(R.string.loadgame_delete_confirmation) + .setPositiveButton(R.string.loadgame_delete_confirm, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + for(IDeleteDialogFragmentListener l : listeners) { + l.onDialogPositiveClick(getPosition()); + } + } + }) + .setNegativeButton(R.string.loadgame_delete_cancel, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + // User cancelled the dialog + } + }); + return builder.create(); + } + } private class LoadGameAdapter extends BaseAdapter { @@ -81,6 +153,11 @@ public class LoadGameActivity extends AppCompatActivity { this.loadableGameList = loadableGameList; } + public void delete(int position) { + loadableGameList.remove(position); + notifyDataSetChanged(); + } + @Override public int getCount() { return loadableGameList.size(); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 49a1c9e..0d7c2ad 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -62,4 +62,9 @@ X Sudoku 9x9 Hyper Sudoku 9x9 + + Are you sure you want to delete this save? + Delete + Cancel +