@Override public void onReceive(Context context, Intent intent) { Bundle questObj = intent.getExtras(); if (intent.getAction().equalsIgnoreCase(Constants.INTENT_FILTER_NEW_QUEST)) { if (questObj.getBoolean(Constants.INTENT_EXTRA_SINGLE_QUEST)) { int questType = questObj.getInt("type"); Quest q = new Quest(); q.setId(questObj.getLong("id")); q.setTitle(questObj.getString("title")); q.setCredits(questObj.getLong("credits")); q.setExperience(questObj.getLong("experience")); String expirationTime = questObj.getString("expirationTime"); if (expirationTime != null && !expirationTime.equals("null")) { q.setExpirationTime(expirationTime); } q.setStatus(Quest.QuestStatus.Accepted); if (questType == Quest.QuestType.Distance.ordinal()) { q.setType(Quest.QuestType.Distance); int distance = questObj.getInt("distance"); q = new DistanceQuest(q, distance); } else if (questType == Quest.QuestType.Capture.ordinal()) { q.setType(Quest.QuestType.Capture); GamePlace.PlaceType placeType = GamePlace.PlaceType.values()[questObj.getInt("placeType")]; int placeTypeValue = questObj.getInt("placeTypeValue"); q = new CaptureQuest(q, placeType, placeTypeValue); } else if (questType == Quest.QuestType.Collect.ordinal()) { q.setType(Quest.QuestType.Collect); Constants.Characteristic characteristic = Constants.Characteristic.values()[questObj.getInt("characteristic")]; int characteristicAmount = questObj.getInt("characteristicAmount"); q = new CollectQuest(q, characteristic, characteristicAmount); } questsAdapter.add(q); } else { questsAdapter.refreshWith(App.getQuestManager().getQuests()); } } else if (intent.getAction().equalsIgnoreCase(Constants.INTENT_FILTER_QUEST_UPDATE)) { Quest q = new Quest(); q.setProgress(questObj.getInt("progress")); q.setStatus(Quest.QuestStatus.values()[questObj.getInt("status")]); q.setId(questObj.getLong("questId")); questsAdapter.update(q); } checkEmptyList(); }
@Override public boolean onContextItemSelected(MenuItem item) { int position = ((QuestsAdapter) questsList.getAdapter()).getPosition(); switch (item.getItemId()) { case R.id.menu_delete: Quest quest = questsAdapter.getItem(position); App.getQuestManager().deleteQuest(quest); questsAdapter.remove(quest); checkEmptyList(); return true; default: return super.onContextItemSelected(item); } }
/** Checks whether adapter with items is empty. If so - display appropriate message. */ private void checkEmptyList() { if (questsAdapter.getItemCount() > 0) { emptyList.setVisibility(View.GONE); } else { emptyList.setVisibility(View.VISIBLE); } }