use of com.nolanlawson.keepscore.data.TimePeriod in project KeepScore by nolanlawson.
the class MainActivity method onResume.
@Override
public void onResume() {
super.onResume();
log.d("onResume()");
List<GameSummary> games = getAllGames();
Collections.sort(games, GameSummary.byRecentlySaved());
log.d("loaded games %s", games);
SortedMap<TimePeriod, List<GameSummary>> organizedGames = organizeGamesByTimePeriod(games);
adapter = new SeparatedListAdapter<SavedGameAdapter>(this);
for (Entry<TimePeriod, List<GameSummary>> entry : organizedGames.entrySet()) {
TimePeriod timePeriod = entry.getKey();
List<GameSummary> gamesSection = entry.getValue();
SavedGameAdapter subAdapter = new SavedGameAdapter(this, gamesSection);
if (lastChecked != null) {
// reload the checked items from when the user last quit
subAdapter.setChecked(lastChecked);
}
subAdapter.setOnCheckChangedRunnable(new Runnable() {
@Override
public void run() {
showOrHideButtonRow();
}
});
adapter.addSection(getString(timePeriod.getTitleResId()), subAdapter);
}
setListAdapter(adapter);
if (lastPosition != null) {
// scroll to the user's last position when they quit
getListView().setSelection(lastPosition);
}
lastPosition = null;
lastChecked = null;
}
use of com.nolanlawson.keepscore.data.TimePeriod in project KeepScore by nolanlawson.
the class MainActivity method onNewGameCreated.
private void onNewGameCreated(Game newGame) {
GameSummary newGameSummary = GameSummary.fromGame(newGame);
// if the appropriate section doesn't exist, need to create it
TimePeriod timePeriodForThisGame = getTimePeriod(new Date(), newGameSummary);
String sectionForThisGame = getString(timePeriodForThisGame.getTitleResId());
if (adapter.getCount() == 0 || !adapter.getSectionsMap().keySet().contains(sectionForThisGame)) {
SavedGameAdapter subAdapter = new SavedGameAdapter(MainActivity.this, new ArrayList<GameSummary>(Collections.singleton(newGameSummary)));
subAdapter.setOnCheckChangedRunnable(new Runnable() {
@Override
public void run() {
showOrHideButtonRow();
}
});
Map<String, Integer> sectionsToOrder = new HashMap<String, Integer>();
for (TimePeriod timePeriod : TimePeriod.values()) {
sectionsToOrder.put(getString(timePeriod.getTitleResId()), timePeriod.ordinal());
}
int index = 0;
for (int i = 0; i < adapter.getSectionHeaders().getCount(); i++) {
String section = adapter.getSectionHeaders().getItem(i);
if (sectionsToOrder.get(sectionForThisGame) < sectionsToOrder.get(section)) {
break;
}
index++;
}
adapter.insertSection(sectionForThisGame, index, subAdapter);
} else {
// just insert it into the proper section
SavedGameAdapter subAdapter = adapter.getSectionsMap().get(sectionForThisGame);
subAdapter.add(newGameSummary);
subAdapter.sort(GameSummary.byRecentlySaved());
}
adapter.notifyDataSetChanged();
adapter.refreshSections();
fastScrollView.listItemsChanged();
}
use of com.nolanlawson.keepscore.data.TimePeriod in project KeepScore by nolanlawson.
the class MainActivity method organizeGamesByTimePeriod.
private SortedMap<TimePeriod, List<GameSummary>> organizeGamesByTimePeriod(List<GameSummary> games) {
SortedMap<TimePeriod, List<GameSummary>> result = new TreeMap<TimePeriod, List<GameSummary>>();
Iterator<TimePeriod> timePeriodIterator = Arrays.asList(TimePeriod.values()).iterator();
TimePeriod timePeriod = timePeriodIterator.next();
Date date = new Date();
for (GameSummary game : games) {
// them in order
while (!timePeriodMatches(date, timePeriod, game)) {
timePeriod = timePeriodIterator.next();
}
List<GameSummary> existing = result.get(timePeriod);
if (existing == null) {
result.put(timePeriod, new ArrayList<GameSummary>(Collections.singleton(game)));
} else {
existing.add(game);
}
}
return result;
}
Aggregations