mirror of
https://github.com/sudoxnym/habitica-android.git
synced 2026-07-14 02:01:56 +00:00
add badge to filter menu item
This commit is contained in:
parent
6c8f394d94
commit
b6c760bc6b
7 changed files with 102 additions and 27 deletions
5
Habitica/res/drawable/badge_bg.xml
Normal file
5
Habitica/res/drawable/badge_bg.xml
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
|
||||
<corners android:radius="3dp" />
|
||||
<solid android:color="@color/brand_400" />
|
||||
</shape>
|
||||
26
Habitica/res/layout/filter_menu_item.xml
Normal file
26
Habitica/res/layout/filter_menu_item.xml
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="48dp"
|
||||
android:layout_height="48dp"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_gravity="right|end"
|
||||
tools:background="@color/brand_100"
|
||||
android:clickable="true">
|
||||
|
||||
<ImageView
|
||||
android:layout_width="48dp"
|
||||
android:layout_height="48dp"
|
||||
android:src="@drawable/ic_action_filter_list"
|
||||
android:scaleType="center"/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/badge_textview"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentRight="true"
|
||||
android:padding="4dp"
|
||||
tools:text="99"
|
||||
android:textColor="@color/white"
|
||||
android:background="@drawable/badge_bg"/>
|
||||
|
||||
</RelativeLayout>
|
||||
|
|
@ -13,7 +13,7 @@
|
|||
<!-- Search, should appear as action button -->
|
||||
<item android:id="@+id/action_search"
|
||||
android:icon="@drawable/ic_action_filter_list"
|
||||
app:actionLayout="@layout/filter_menu_item"
|
||||
android:title="@string/filter"
|
||||
app:showAsAction="ifRoom" />
|
||||
|
||||
</menu>
|
||||
|
|
|
|||
|
|
@ -3,14 +3,16 @@ package com.habitrpg.android.habitica.helpers;
|
|||
import com.magicmicky.habitrpgwrapper.lib.models.tasks.Task;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Created by magicmicky on 02/10/15.
|
||||
*/
|
||||
public class TaskFilterHelper {
|
||||
private List<String> tagsId;
|
||||
private String activeFilter;
|
||||
private Map<String, String> activeFilters = new HashMap<>();
|
||||
|
||||
public TaskFilterHelper() {
|
||||
tagsId = new ArrayList<>();
|
||||
|
|
@ -21,8 +23,8 @@ public class TaskFilterHelper {
|
|||
this.tagsId.add(tags);
|
||||
}
|
||||
|
||||
public int howMany() {
|
||||
return this.tagsId.size() + (activeFilter != null ? 1 : 0);
|
||||
public int howMany(String type) {
|
||||
return this.tagsId.size() + (activeFilters.get(type) != null ? 1 : 0);
|
||||
}
|
||||
|
||||
public List<String> getTags() {
|
||||
|
|
@ -38,9 +40,10 @@ public class TaskFilterHelper {
|
|||
}
|
||||
|
||||
public List<Task> filter(List<Task> tasks) {
|
||||
List<Task> filtered = new ArrayList<Task>();
|
||||
List<Task> filtered = new ArrayList<>();
|
||||
String activeFilter = activeFilters.get(tasks.get(0).type);
|
||||
for (Task task : tasks) {
|
||||
if (isFiltered(task)) {
|
||||
if (isFiltered(task, activeFilter)) {
|
||||
filtered.add(task);
|
||||
}
|
||||
}
|
||||
|
|
@ -48,7 +51,7 @@ public class TaskFilterHelper {
|
|||
return filtered;
|
||||
}
|
||||
|
||||
private boolean isFiltered(Task task) {
|
||||
private boolean isFiltered(Task task, String activeFilter) {
|
||||
if (!task.containsAllTagIds(tagsId)) {
|
||||
return false;
|
||||
}
|
||||
|
|
@ -75,7 +78,11 @@ public class TaskFilterHelper {
|
|||
return true;
|
||||
}
|
||||
|
||||
public void setActiveFilter(String activeFilter) {
|
||||
this.activeFilter = activeFilter;
|
||||
public void setActiveFilter(String type, String activeFilter) {
|
||||
activeFilters.put(type, activeFilter);
|
||||
}
|
||||
|
||||
public String getActiveFilter(String type) {
|
||||
return activeFilters.get(type);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ public abstract class BaseTasksRecyclerViewAdapter<VH extends BaseTaskViewHolder
|
|||
protected CrashlyticsProxy crashlyticsProxy;
|
||||
protected List<Task> content;
|
||||
protected List<Task> filteredContent;
|
||||
int layoutResource;
|
||||
private int layoutResource;
|
||||
Context context;
|
||||
private TaskFilterHelper taskFilterHelper;
|
||||
|
||||
|
|
@ -81,11 +81,11 @@ public abstract class BaseTasksRecyclerViewAdapter<VH extends BaseTaskViewHolder
|
|||
return filteredContent != null ? filteredContent.size() : 0;
|
||||
}
|
||||
|
||||
public View getContentView(ViewGroup parent) {
|
||||
View getContentView(ViewGroup parent) {
|
||||
return getContentView(parent, layoutResource);
|
||||
}
|
||||
|
||||
public View getContentView(ViewGroup parent, int layoutResource) {
|
||||
protected View getContentView(ViewGroup parent, int layoutResource) {
|
||||
return LayoutInflater.from(parent.getContext()).inflate(layoutResource, parent, false);
|
||||
}
|
||||
|
||||
|
|
@ -105,7 +105,7 @@ public abstract class BaseTasksRecyclerViewAdapter<VH extends BaseTaskViewHolder
|
|||
}
|
||||
|
||||
public void filter() {
|
||||
if (this.taskFilterHelper == null || this.taskFilterHelper.howMany() == 0) {
|
||||
if (this.taskFilterHelper == null || this.taskFilterHelper.howMany(taskType) == 0) {
|
||||
filteredContent = content;
|
||||
} else {
|
||||
filteredContent = new ObservableArrayList<>();
|
||||
|
|
|
|||
|
|
@ -75,16 +75,14 @@ public class TaskRecyclerViewFragment extends BaseFragment implements View.OnCli
|
|||
@Nullable
|
||||
private SortableTasksRecyclerViewAdapter.SortTasksCallback sortCallback;
|
||||
private ItemTouchHelper.Callback mItemTouchCallback;
|
||||
private String activeFilter;
|
||||
|
||||
public static TaskRecyclerViewFragment newInstance(@Nullable HabitRPGUser user, String classType, @Nullable String activeFilter,
|
||||
public static TaskRecyclerViewFragment newInstance(@Nullable HabitRPGUser user, String classType,
|
||||
@Nullable SortableTasksRecyclerViewAdapter.SortTasksCallback sortCallback) {
|
||||
TaskRecyclerViewFragment fragment = new TaskRecyclerViewFragment();
|
||||
fragment.setRetainInstance(true);
|
||||
fragment.user = user;
|
||||
fragment.classType = classType;
|
||||
fragment.sortCallback = sortCallback;
|
||||
fragment.activeFilter = activeFilter;
|
||||
return fragment;
|
||||
}
|
||||
|
||||
|
|
@ -129,8 +127,6 @@ public class TaskRecyclerViewFragment extends BaseFragment implements View.OnCli
|
|||
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
||||
super.onCreateView(inflater, container, savedInstanceState);
|
||||
|
||||
taskFilterHelper.setActiveFilter(activeFilter);
|
||||
|
||||
mItemTouchCallback = new ItemTouchHelper.Callback() {
|
||||
private Integer mFromPosition = null;
|
||||
|
||||
|
|
@ -295,8 +291,7 @@ public class TaskRecyclerViewFragment extends BaseFragment implements View.OnCli
|
|||
}
|
||||
|
||||
public void setActiveFilter(String activeFilter) {
|
||||
this.activeFilter = activeFilter;
|
||||
taskFilterHelper.setActiveFilter(activeFilter);
|
||||
taskFilterHelper.setActiveFilter(classType, activeFilter);
|
||||
recyclerAdapter.filter();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import android.support.design.widget.CoordinatorLayout;
|
|||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentPagerAdapter;
|
||||
import android.support.v4.view.GravityCompat;
|
||||
import android.support.v4.view.MenuItemCompat;
|
||||
import android.support.v4.view.ViewPager;
|
||||
import android.util.SparseArray;
|
||||
import android.view.LayoutInflater;
|
||||
|
|
@ -18,6 +19,8 @@ import android.view.MenuItem;
|
|||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.FrameLayout;
|
||||
import android.widget.RelativeLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.github.clans.fab.FloatingActionButton;
|
||||
import com.github.clans.fab.FloatingActionMenu;
|
||||
|
|
@ -59,9 +62,9 @@ public class TasksFragment extends BaseMainFragment {
|
|||
MenuItem refreshItem;
|
||||
FloatingActionMenu floatingMenu;
|
||||
SparseArray<TaskRecyclerViewFragment> viewFragmentsDictionary = new SparseArray<>();
|
||||
Map<String, String> activeTaskFilters = new HashMap<>();
|
||||
|
||||
private boolean displayingTaskForm;
|
||||
private TextView filterCountTextView;
|
||||
|
||||
public void setActivity(MainActivity activity) {
|
||||
super.setActivity(activity);
|
||||
|
|
@ -131,6 +134,11 @@ public class TasksFragment extends BaseMainFragment {
|
|||
@Override
|
||||
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
||||
inflater.inflate(R.menu.menu_main_activity, menu);
|
||||
|
||||
RelativeLayout badgeLayout = (RelativeLayout) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
|
||||
filterCountTextView = (TextView) badgeLayout.findViewById(R.id.badge_textview);
|
||||
badgeLayout.setOnClickListener(view -> showFilterDialog());
|
||||
updateFilterIcon();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -157,9 +165,8 @@ public class TasksFragment extends BaseMainFragment {
|
|||
}
|
||||
dialog.setActiveTags(taskFilterHelper.getTags());
|
||||
String taskType = getActiveFragment().classType;
|
||||
dialog.setTaskType(taskType, activeTaskFilters.get(taskType));
|
||||
dialog.setTaskType(taskType, taskFilterHelper.getActiveFilter(taskType));
|
||||
dialog.setListener((activeTaskFilter, activeTags) -> {
|
||||
activeTaskFilters.put(taskType, activeTaskFilter);
|
||||
int activePos = viewPager.getCurrentItem();
|
||||
if (activePos >= 1) {
|
||||
viewFragmentsDictionary.get(activePos-1).recyclerAdapter.filter();
|
||||
|
|
@ -169,6 +176,7 @@ public class TasksFragment extends BaseMainFragment {
|
|||
viewFragmentsDictionary.get(activePos+1).recyclerAdapter.filter();
|
||||
}
|
||||
taskFilterHelper.setTags(activeTags);
|
||||
updateFilterIcon();
|
||||
});
|
||||
dialog.show();
|
||||
}
|
||||
|
|
@ -197,16 +205,16 @@ public class TasksFragment extends BaseMainFragment {
|
|||
|
||||
switch (position) {
|
||||
case 0:
|
||||
fragment = TaskRecyclerViewFragment.newInstance(user, Task.TYPE_HABIT, activeTaskFilters.get(Task.TYPE_HABIT), sortCallback);
|
||||
fragment = TaskRecyclerViewFragment.newInstance(user, Task.TYPE_HABIT, sortCallback);
|
||||
break;
|
||||
case 1:
|
||||
fragment = TaskRecyclerViewFragment.newInstance(user, Task.TYPE_DAILY, activeTaskFilters.get(Task.TYPE_DAILY), sortCallback);
|
||||
fragment = TaskRecyclerViewFragment.newInstance(user, Task.TYPE_DAILY, sortCallback);
|
||||
break;
|
||||
case 3:
|
||||
fragment = TaskRecyclerViewFragment.newInstance(user, Task.TYPE_REWARD, null, null);
|
||||
fragment = TaskRecyclerViewFragment.newInstance(user, Task.TYPE_REWARD, null);
|
||||
break;
|
||||
default:
|
||||
fragment = TaskRecyclerViewFragment.newInstance(user, Task.TYPE_TODO, activeTaskFilters.get(Task.TYPE_TODO),sortCallback);
|
||||
fragment = TaskRecyclerViewFragment.newInstance(user, Task.TYPE_TODO,sortCallback);
|
||||
}
|
||||
|
||||
viewFragmentsDictionary.put(position, fragment);
|
||||
|
|
@ -234,6 +242,39 @@ public class TasksFragment extends BaseMainFragment {
|
|||
return "";
|
||||
}
|
||||
});
|
||||
|
||||
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
|
||||
@Override
|
||||
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPageSelected(int position) {
|
||||
bottomNavigation.selectTabAtPosition(position);
|
||||
updateFilterIcon();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPageScrollStateChanged(int state) {
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void updateFilterIcon() {
|
||||
int filterCount = 0;
|
||||
if (getActiveFragment() != null) {
|
||||
filterCount = taskFilterHelper.howMany(getActiveFragment().classType);
|
||||
}
|
||||
if (filterCount == 0) {
|
||||
filterCountTextView.setText(null);
|
||||
filterCountTextView.setVisibility(View.GONE);
|
||||
} else {
|
||||
filterCountTextView.setText(String.valueOf(filterCount));
|
||||
filterCountTextView.setVisibility(View.VISIBLE);
|
||||
}
|
||||
|
||||
}
|
||||
// endregion
|
||||
|
||||
|
|
@ -284,6 +325,7 @@ public class TasksFragment extends BaseMainFragment {
|
|||
getActiveFragment().onRefresh();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private TaskRecyclerViewFragment getActiveFragment() {
|
||||
return viewFragmentsDictionary.get(viewPager.getCurrentItem());
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue