You can now delete entries in the continue game screen by long clicking them.

This commit is contained in:
Christopher Beckmann 2015-11-24 12:32:36 +01:00
parent e2ecfe9292
commit d2bed7b2a5
4 changed files with 99 additions and 6 deletions

View file

@ -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);

View file

@ -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);
}

View file

@ -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<GameInfoContainer> 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<IDeleteDialogFragmentListener> 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();

View file

@ -62,4 +62,9 @@
<string name="gametype_x_9x9">X Sudoku 9x9</string>
<string name="gametype_hyper_9x9">Hyper Sudoku 9x9</string>
<!-- ### Continue Game ### -->
<string name="loadgame_delete_confirmation">Are you sure you want to delete this save?</string>
<string name="loadgame_delete_confirm">Delete</string>
<string name="loadgame_delete_cancel">Cancel</string>
</resources>