use of com.nolanlawson.keepscore.db.GameDBHelper in project KeepScore by nolanlawson.
the class MainActivity method showEditGameNameDialog.
private void showEditGameNameDialog(final GameSummary game) {
final EditText editText = new EditText(this);
editText.setHint(R.string.hint_game_name);
editText.setText(StringUtil.nullToEmpty(game.getName()));
editText.setInputType(InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
editText.setSingleLine();
new AlertDialog.Builder(this).setTitle(R.string.title_edit_game_name).setView(editText).setCancelable(true).setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(final DialogInterface dialog, int which) {
final String newName = StringUtil.nullToEmpty(editText.getText().toString());
// update database in the background to avoid
// jankiness
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
GameDBHelper dbHelper = null;
try {
dbHelper = new GameDBHelper(MainActivity.this);
dbHelper.updateGameName(game.getId(), newName);
} finally {
if (dbHelper != null) {
dbHelper.close();
}
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
game.setName(newName.trim());
adapter.notifyDataSetChanged();
dialog.dismiss();
}
}.execute((Void) null);
}
}).setNegativeButton(android.R.string.cancel, null).show();
}
use of com.nolanlawson.keepscore.db.GameDBHelper in project KeepScore by nolanlawson.
the class MainActivity method loadBackupInBackground.
private LoadGamesBackupResult loadBackupInBackground(Uri uri, Format format, Runnable onProgress) {
// use the start date as a unique identifier; it's a
// millisecond-timestamp, so it should work
GamesBackup gamesBackup;
try {
String xmlData = SdcardHelper.open(uri, format, getContentResolver());
gamesBackup = GamesBackupSerializer.deserialize(xmlData);
} catch (Exception e) {
log.e(e, "unexpected");
return null;
}
List<Game> loadedGames = new ArrayList<Game>();
int numFound = 0, numDuplicates = 0;
GameDBHelper dbHelper = null;
try {
dbHelper = new GameDBHelper(this);
for (Game game : gamesBackup.getGames()) {
numFound++;
if (dbHelper.existsByDateStarted(game.getDateStarted())) {
numDuplicates++;
} else {
// don't update 'dateSaved'
dbHelper.saveGame(game, false);
// value - keep original
loadedGames.add(game);
}
onProgress.run();
}
} finally {
if (dbHelper != null) {
dbHelper.close();
}
}
// this is just for the summary message we show the user
LoadGamesBackupResult result = new LoadGamesBackupResult();
result.setLoadedGames(loadedGames);
result.setNumDuplicates(numDuplicates);
result.setNumFound(numFound);
// Pre-version 3, we don't have the filename in the deserialized XML
String filenameToDisplay = gamesBackup.getFilename() != null ? gamesBackup.getFilename() : uri.getLastPathSegment();
result.setFilename(filenameToDisplay);
return result;
}
use of com.nolanlawson.keepscore.db.GameDBHelper in project KeepScore by nolanlawson.
the class MainActivity method saveBackup.
private void saveBackup(final Format format, final Location location, final List<Integer> gameIds, final Callback<String> onSuccessWithFilename) {
if (!SdcardHelper.isAvailable()) {
ToastHelper.showLong(this, R.string.toast_no_sdcard);
return;
}
final String filename = SdcardHelper.createBackupFilename(format);
final ProgressDialog progressDialog = showProgressDialog(R.string.text_saving, gameIds.size());
new AsyncTask<Void, Void, Boolean>() {
@Override
protected Boolean doInBackground(Void... params) {
List<Game> games = new ArrayList<Game>();
GameDBHelper dbHelper = null;
try {
dbHelper = new GameDBHelper(MainActivity.this);
for (Integer gameId : gameIds) {
Game game = dbHelper.findGameById(gameId);
games.add(game);
publishProgress((Void) null);
}
} finally {
if (dbHelper != null) {
dbHelper.close();
}
}
GamesBackup gamesBackup = new GamesBackup();
gamesBackup.setVersion(GamesBackupSerializer.CURRENT_VERSION);
gamesBackup.setDateSaved(System.currentTimeMillis());
gamesBackup.setAutomatic(false);
gamesBackup.setGameCount(games.size());
gamesBackup.setGames(games);
gamesBackup.setFilename(filename);
String xmlData = GamesBackupSerializer.serialize(gamesBackup);
return SdcardHelper.save(filename, format, location, xmlData);
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
if (result) {
onSuccessWithFilename.onCallback(filename);
} else {
ToastHelper.showLong(MainActivity.this, R.string.toast_save_backup_failed);
}
progressDialog.dismiss();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
progressDialog.incrementProgressBy(1);
}
}.execute((Void) null);
}
use of com.nolanlawson.keepscore.db.GameDBHelper in project KeepScore by nolanlawson.
the class PlayerNameHelper method getPlayerNameSuggestions.
public static List<String> getPlayerNameSuggestions(Context context) {
// populate player name suggestions from previous games by grabbing the
// names from the database
GameDBHelper dbHelper = null;
try {
dbHelper = new GameDBHelper(context);
List<String> suggestions = dbHelper.findDistinctPlayerNames();
List<String> filteredSuggestions = new ArrayList<String>();
// filter out null/empty/whitespace names
for (String suggestion : suggestions) {
if (StringUtil.isEmptyOrWhitespace(suggestion)) {
continue;
}
filteredSuggestions.add(suggestion.trim());
}
// sort, case insensitive
Collections.sort(filteredSuggestions, String.CASE_INSENSITIVE_ORDER);
return filteredSuggestions;
} finally {
if (dbHelper != null) {
dbHelper.close();
}
}
}
use of com.nolanlawson.keepscore.db.GameDBHelper in project KeepScore by nolanlawson.
the class MainActivity method copyGame.
private void copyGame(Game game, final boolean resetScores) {
final Game newGame = game.makeCleanCopy();
if (resetScores) {
for (PlayerScore playerScore : newGame.getPlayerScores()) {
playerScore.setScore(PreferenceHelper.getIntPreference(R.string.CONSTANT_pref_initial_score, R.string.CONSTANT_pref_initial_score_default, this));
playerScore.setHistory(new ArrayList<Delta>());
}
}
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
GameDBHelper dbHelper = null;
try {
dbHelper = new GameDBHelper(MainActivity.this);
dbHelper.saveGame(newGame);
} finally {
if (dbHelper != null) {
dbHelper.close();
}
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
onNewGameCreated(newGame);
ToastHelper.showShort(MainActivity.this, resetScores ? R.string.toast_rematch_created : R.string.toast_game_copied);
}
}.execute((Void) null);
}
Aggregations