Search in sources :

Example 1 with TimePeriod

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;
}
Also used : TimePeriod(com.nolanlawson.keepscore.data.TimePeriod) SavedGameAdapter(com.nolanlawson.keepscore.data.SavedGameAdapter) List(java.util.List) ArrayList(java.util.ArrayList) GameSummary(com.nolanlawson.keepscore.db.GameSummary)

Example 2 with TimePeriod

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();
}
Also used : HashMap(java.util.HashMap) TimePeriod(com.nolanlawson.keepscore.data.TimePeriod) SavedGameAdapter(com.nolanlawson.keepscore.data.SavedGameAdapter) GameSummary(com.nolanlawson.keepscore.db.GameSummary) Date(java.util.Date)

Example 3 with TimePeriod

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;
}
Also used : TimePeriod(com.nolanlawson.keepscore.data.TimePeriod) List(java.util.List) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) GameSummary(com.nolanlawson.keepscore.db.GameSummary) Date(java.util.Date)

Aggregations

TimePeriod (com.nolanlawson.keepscore.data.TimePeriod)3 GameSummary (com.nolanlawson.keepscore.db.GameSummary)3 SavedGameAdapter (com.nolanlawson.keepscore.data.SavedGameAdapter)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 List (java.util.List)2 HashMap (java.util.HashMap)1 TreeMap (java.util.TreeMap)1