filter icons && NotificationListLoaderManager
This commit is contained in:
@@ -4,12 +4,12 @@ import android.database.Cursor;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.Nullable;
|
||||
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.RecyclerView;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
@@ -22,9 +22,7 @@ import butterknife.Bind;
|
||||
import butterknife.ButterKnife;
|
||||
import timber.log.Timber;
|
||||
|
||||
public class NotificationListFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor>, NotificationListAdapter.NotificationClickListener {
|
||||
|
||||
final int LOADER_ID = 124;
|
||||
public class NotificationListFragment extends Fragment implements NotificationListLoaderCallbacks.NotificationListView, NotificationListAdapter.NotificationClickListener {
|
||||
|
||||
@Bind(R.id.nList_emptyView)
|
||||
View mEmptyView;
|
||||
@@ -33,6 +31,18 @@ public class NotificationListFragment extends Fragment implements LoaderManager.
|
||||
RecyclerView mNotificationList;
|
||||
|
||||
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
|
||||
@Override
|
||||
@@ -40,7 +50,6 @@ public class NotificationListFragment extends Fragment implements LoaderManager.
|
||||
View view = inflater.inflate(R.layout.notification_list_fragment, container, false);
|
||||
ButterKnife.bind(this, view);
|
||||
mNotificationList.setLayoutManager(new LinearLayoutManager(getContext()));
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
@@ -50,43 +59,50 @@ public class NotificationListFragment extends Fragment implements LoaderManager.
|
||||
mAdapter = new NotificationListAdapter(getContext(), null);
|
||||
mAdapter.setNotificationClickListener(this);
|
||||
mNotificationList.setAdapter(mAdapter);
|
||||
getLoaderManager().initLoader(LOADER_ID, Bundle.EMPTY, this);
|
||||
mLoaderManager.loadNotificationsUnfiltered();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
|
||||
Timber.d("Loader created.");
|
||||
return new CursorLoader(
|
||||
getContext(),
|
||||
Logged_notificationTable.CONTENT_URI,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
Logged_notificationTable.FIELD_DATE + " DESC"
|
||||
);
|
||||
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
||||
super.onCreateOptionsMenu(menu, inflater);
|
||||
inflater.inflate(R.menu.notification_list_frag, menu);
|
||||
if (isFiltered)
|
||||
menu.findItem(R.id.menu_filter).setVisible(false);
|
||||
else
|
||||
menu.findItem(R.id.menu_clear_filter).setVisible(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
|
||||
Timber.d("Loader finished. " + data.getCount() + " items.");
|
||||
mAdapter.changeCursor(data);
|
||||
checkIfEmpty();
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case R.id.menu_filter:
|
||||
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
|
||||
public void onLoaderReset(Loader<Cursor> loader) {
|
||||
Timber.d("Loader reset.");
|
||||
mAdapter.changeCursor(null);
|
||||
checkIfEmpty();
|
||||
}
|
||||
|
||||
private void checkIfEmpty() {
|
||||
public void onChangeCursor(Cursor cursor) {
|
||||
mAdapter.changeCursor(cursor);
|
||||
if (mAdapter.getItemCount() == 0) {
|
||||
mEmptyView.setVisibility(View.VISIBLE);
|
||||
mNotificationList.setVisibility(View.GONE);
|
||||
mEmptyView.setVisibility(View.VISIBLE);
|
||||
} else {
|
||||
mEmptyView.setVisibility(View.GONE);
|
||||
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:orderInCategory="100"
|
||||
android:title="@string/menu_item_settings"
|
||||
app:showAsAction="never"
|
||||
/>
|
||||
app:showAsAction="never" />
|
||||
</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="app_icon_cd">Application icon for %s</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">
|
||||
<item>Alarm</item>
|
||||
|
||||
Reference in New Issue
Block a user