filter icons && NotificationListLoaderManager
This commit is contained in:
@@ -4,12 +4,12 @@ import android.database.Cursor;
|
|||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
import android.support.v4.app.Fragment;
|
import android.support.v4.app.Fragment;
|
||||||
import android.support.v4.app.LoaderManager;
|
|
||||||
import android.support.v4.content.CursorLoader;
|
|
||||||
import android.support.v4.content.Loader;
|
|
||||||
import android.support.v7.widget.LinearLayoutManager;
|
import android.support.v7.widget.LinearLayoutManager;
|
||||||
import android.support.v7.widget.RecyclerView;
|
import android.support.v7.widget.RecyclerView;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
|
import android.view.Menu;
|
||||||
|
import android.view.MenuInflater;
|
||||||
|
import android.view.MenuItem;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
|
|
||||||
@@ -22,9 +22,7 @@ import butterknife.Bind;
|
|||||||
import butterknife.ButterKnife;
|
import butterknife.ButterKnife;
|
||||||
import timber.log.Timber;
|
import timber.log.Timber;
|
||||||
|
|
||||||
public class NotificationListFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor>, NotificationListAdapter.NotificationClickListener {
|
public class NotificationListFragment extends Fragment implements NotificationListLoaderCallbacks.NotificationListView, NotificationListAdapter.NotificationClickListener {
|
||||||
|
|
||||||
final int LOADER_ID = 124;
|
|
||||||
|
|
||||||
@Bind(R.id.nList_emptyView)
|
@Bind(R.id.nList_emptyView)
|
||||||
View mEmptyView;
|
View mEmptyView;
|
||||||
@@ -33,6 +31,18 @@ public class NotificationListFragment extends Fragment implements LoaderManager.
|
|||||||
RecyclerView mNotificationList;
|
RecyclerView mNotificationList;
|
||||||
|
|
||||||
private NotificationListAdapter mAdapter;
|
private NotificationListAdapter mAdapter;
|
||||||
|
private boolean isFiltered = false;
|
||||||
|
private NotificationListLoaderManager mLoaderManager;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCreate(@Nullable Bundle savedInstanceState) {
|
||||||
|
setHasOptionsMenu(true);
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
mLoaderManager = new NotificationListLoaderManager(
|
||||||
|
getContext(),
|
||||||
|
getLoaderManager(),
|
||||||
|
this);
|
||||||
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@Override
|
@Override
|
||||||
@@ -40,7 +50,6 @@ public class NotificationListFragment extends Fragment implements LoaderManager.
|
|||||||
View view = inflater.inflate(R.layout.notification_list_fragment, container, false);
|
View view = inflater.inflate(R.layout.notification_list_fragment, container, false);
|
||||||
ButterKnife.bind(this, view);
|
ButterKnife.bind(this, view);
|
||||||
mNotificationList.setLayoutManager(new LinearLayoutManager(getContext()));
|
mNotificationList.setLayoutManager(new LinearLayoutManager(getContext()));
|
||||||
|
|
||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,43 +59,50 @@ public class NotificationListFragment extends Fragment implements LoaderManager.
|
|||||||
mAdapter = new NotificationListAdapter(getContext(), null);
|
mAdapter = new NotificationListAdapter(getContext(), null);
|
||||||
mAdapter.setNotificationClickListener(this);
|
mAdapter.setNotificationClickListener(this);
|
||||||
mNotificationList.setAdapter(mAdapter);
|
mNotificationList.setAdapter(mAdapter);
|
||||||
getLoaderManager().initLoader(LOADER_ID, Bundle.EMPTY, this);
|
mLoaderManager.loadNotificationsUnfiltered();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
|
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
||||||
Timber.d("Loader created.");
|
super.onCreateOptionsMenu(menu, inflater);
|
||||||
return new CursorLoader(
|
inflater.inflate(R.menu.notification_list_frag, menu);
|
||||||
getContext(),
|
if (isFiltered)
|
||||||
Logged_notificationTable.CONTENT_URI,
|
menu.findItem(R.id.menu_filter).setVisible(false);
|
||||||
null,
|
else
|
||||||
null,
|
menu.findItem(R.id.menu_clear_filter).setVisible(false);
|
||||||
null,
|
|
||||||
Logged_notificationTable.FIELD_DATE + " DESC"
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
Timber.d("Loader finished. " + data.getCount() + " items.");
|
switch (item.getItemId()) {
|
||||||
mAdapter.changeCursor(data);
|
case R.id.menu_filter:
|
||||||
checkIfEmpty();
|
mLoaderManager.loadNotificationsUnfiltered();
|
||||||
|
setIsFiltered(true);
|
||||||
|
return true;
|
||||||
|
case R.id.menu_clear_filter:
|
||||||
|
mLoaderManager.loadNotificationsUnfiltered();
|
||||||
|
setIsFiltered(false);
|
||||||
|
return true;
|
||||||
|
default:
|
||||||
|
return super.onOptionsItemSelected(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIsFiltered(boolean filtered) {
|
||||||
|
isFiltered = filtered;
|
||||||
|
if (getActivity() != null)
|
||||||
|
getActivity().supportInvalidateOptionsMenu();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onLoaderReset(Loader<Cursor> loader) {
|
public void onChangeCursor(Cursor cursor) {
|
||||||
Timber.d("Loader reset.");
|
mAdapter.changeCursor(cursor);
|
||||||
mAdapter.changeCursor(null);
|
|
||||||
checkIfEmpty();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void checkIfEmpty() {
|
|
||||||
if (mAdapter.getItemCount() == 0) {
|
if (mAdapter.getItemCount() == 0) {
|
||||||
mEmptyView.setVisibility(View.VISIBLE);
|
|
||||||
mNotificationList.setVisibility(View.GONE);
|
mNotificationList.setVisibility(View.GONE);
|
||||||
|
mEmptyView.setVisibility(View.VISIBLE);
|
||||||
} else {
|
} else {
|
||||||
mEmptyView.setVisibility(View.GONE);
|
|
||||||
mNotificationList.setVisibility(View.VISIBLE);
|
mNotificationList.setVisibility(View.VISIBLE);
|
||||||
|
mEmptyView.setVisibility(View.GONE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,53 @@
|
|||||||
|
package net.headlezz.notificationlogger.notificationlist;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.database.Cursor;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.support.v4.app.LoaderManager;
|
||||||
|
import android.support.v4.content.CursorLoader;
|
||||||
|
import android.support.v4.content.Loader;
|
||||||
|
|
||||||
|
import net.headlezz.notificationlogger.logger.Logged_notificationTable;
|
||||||
|
|
||||||
|
import timber.log.Timber;
|
||||||
|
|
||||||
|
public class NotificationListLoaderCallbacks implements LoaderManager.LoaderCallbacks<Cursor> {
|
||||||
|
|
||||||
|
public interface NotificationListView {
|
||||||
|
void onChangeCursor(Cursor cursor);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Context mContext;
|
||||||
|
private NotificationListView mView;
|
||||||
|
|
||||||
|
public NotificationListLoaderCallbacks(Context context, NotificationListView view) {
|
||||||
|
mContext = context;
|
||||||
|
mView = view;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
|
||||||
|
Timber.d("Loader created.");
|
||||||
|
return new CursorLoader(
|
||||||
|
mContext,
|
||||||
|
Logged_notificationTable.CONTENT_URI,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
Logged_notificationTable.FIELD_DATE + " DESC"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
|
||||||
|
Timber.d("Loader finished. " + data.getCount() + " items.");
|
||||||
|
mView.onChangeCursor(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onLoaderReset(Loader<Cursor> loader) {
|
||||||
|
Timber.d("Loader reset.");
|
||||||
|
mView.onChangeCursor(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package net.headlezz.notificationlogger.notificationlist;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.support.v4.app.LoaderManager;
|
||||||
|
|
||||||
|
public class NotificationListLoaderManager {
|
||||||
|
|
||||||
|
final int LOADER_ID = 124;
|
||||||
|
|
||||||
|
LoaderManager mLoaderManager;
|
||||||
|
NotificationListLoaderCallbacks mLoaderCallbacks;
|
||||||
|
|
||||||
|
public NotificationListLoaderManager(Context context, LoaderManager loaderManager, NotificationListLoaderCallbacks.NotificationListView notificationListView) {
|
||||||
|
mLoaderManager = loaderManager;
|
||||||
|
mLoaderCallbacks = new NotificationListLoaderCallbacks(context, notificationListView);
|
||||||
|
}
|
||||||
|
|
||||||
|
void loadNotificationsUnfiltered() {
|
||||||
|
mLoaderManager.restartLoader(LOADER_ID, Bundle.EMPTY, mLoaderCallbacks);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -5,6 +5,5 @@
|
|||||||
android:id="@+id/menu_settings"
|
android:id="@+id/menu_settings"
|
||||||
android:orderInCategory="100"
|
android:orderInCategory="100"
|
||||||
android:title="@string/menu_item_settings"
|
android:title="@string/menu_item_settings"
|
||||||
app:showAsAction="never"
|
app:showAsAction="never" />
|
||||||
/>
|
|
||||||
</menu>
|
</menu>
|
||||||
14
app/src/main/res/menu/notification_list_frag.xml
Normal file
14
app/src/main/res/menu/notification_list_frag.xml
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||||
|
<item
|
||||||
|
android:id="@+id/menu_filter"
|
||||||
|
android:orderInCategory="101"
|
||||||
|
android:title="@string/menu_item_filter"
|
||||||
|
app:showAsAction="never" />
|
||||||
|
<item
|
||||||
|
android:id="@+id/menu_clear_filter"
|
||||||
|
android:orderInCategory="102"
|
||||||
|
android:title="@string/menu_item_clear_filter"
|
||||||
|
app:showAsAction="never" />
|
||||||
|
</menu>
|
||||||
@@ -55,6 +55,8 @@
|
|||||||
<string name="notification_small_icon_cd">Notification small icon</string>
|
<string name="notification_small_icon_cd">Notification small icon</string>
|
||||||
<string name="app_icon_cd">Application icon for %s</string>
|
<string name="app_icon_cd">Application icon for %s</string>
|
||||||
<string name="notification_info_dialog_app_info_error">Failed to display app info.</string>
|
<string name="notification_info_dialog_app_info_error">Failed to display app info.</string>
|
||||||
|
<string name="menu_item_filter">Filter</string>
|
||||||
|
<string name="menu_item_clear_filter">Clear filter</string>
|
||||||
|
|
||||||
<string-array name="create_categories">
|
<string-array name="create_categories">
|
||||||
<item>Alarm</item>
|
<item>Alarm</item>
|
||||||
|
|||||||
Reference in New Issue
Block a user