Skip to content

Commit

Permalink
Merge pull request #54 from PaulRashidi/b_notification_requery
Browse files Browse the repository at this point in the history
Fix CP not notifying and NotificationList not auto-updating.
  • Loading branch information
PaulRashidi authored Aug 19, 2016
2 parents 9270d81 + 7f29a3e commit fef3dd3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 28 deletions.
9 changes: 7 additions & 2 deletions app/src/main/java/com/g11x/checklistapp/LocalRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ public String getType(@NonNull Uri uri) {
@Nullable
@Override
public Uri insert(@NonNull Uri uri, ContentValues contentValues) {
return Database.insert(openHelper.getWritableDatabase(), uri, contentValues);
Uri insertUri = Database.insert(openHelper.getWritableDatabase(), uri, contentValues);
getContext().getContentResolver().notifyChange(uri, null);
getContext().getContentResolver().notifyChange(insertUri, null);
return insertUri;
}

@Override
Expand All @@ -56,7 +59,9 @@ public int delete(@NonNull Uri uri, String s, String[] strings) {

@Override
public int update(@NonNull Uri uri, ContentValues contentValues, String selection, String[] selectionArgs) {
return Database.update(openHelper.getWritableDatabase(), uri, contentValues, selection, selectionArgs);
int updateValue = Database.update(openHelper.getWritableDatabase(), uri, contentValues, selection, selectionArgs);
getContext().getContentResolver().notifyChange(uri, null);
return updateValue;
}

// Helper class that actually creates and manages the provider's underlying data repository.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.g11x.checklistapp;

import android.content.ContentValues;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
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.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -17,10 +21,11 @@
import com.g11x.checklistapp.data.Database;
import com.g11x.checklistapp.data.Notification;

public class NotificationListActivity extends NavigationActivity {
public class NotificationListActivity extends NavigationActivity implements LoaderManager.LoaderCallbacks<Cursor> {

private NotificationAdapter adapter;
private RecyclerView mRecyclerView;
private ContentObserver notificationContentObserver;

@Override
protected int getNavDrawerItemIndex() {
Expand Down Expand Up @@ -63,41 +68,53 @@ public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
}
});
helper.attachToRecyclerView(mRecyclerView);
adapter = new NotificationAdapter(null);
mRecyclerView.setAdapter(adapter);

getSupportLoaderManager().initLoader(0, null, new LoaderManager.LoaderCallbacks<Cursor>() {
@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
Loader<Cursor> cursor = new CursorLoader(NotificationListActivity.this,
Database.Notification.CONTENT_URI,
Database.Notification.PROJECTION, "NOT " + Database.Notification.READ_COLUMN, null, Database.Notification.SENT_TIME);
return cursor;
}
getSupportLoaderManager().initLoader(0, null, this);
}

@Override
protected void onStart() {
super.onStart();
if (adapter != null) {
adapter.notifyDataSetChanged();
}
}

@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
if (notificationContentObserver != null) {
getContentResolver().unregisterContentObserver(notificationContentObserver);
}
notificationContentObserver = new ContentObserver(new Handler()) {
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
if (adapter == null) {
if (cursor != null) {
adapter = new NotificationAdapter(cursor);
mRecyclerView.setAdapter(adapter);
}
} else {
adapter.swapCursor(cursor);
}
public void onChange(boolean selfChange) {
getSupportLoaderManager().restartLoader(0, null, NotificationListActivity.this);
super.onChange(selfChange);
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
adapter.swapCursor(null);
public void onChange(boolean selfChange, Uri uri) {
getSupportLoaderManager().restartLoader(0, null, NotificationListActivity.this);
super.onChange(selfChange, uri);
}
});
};
Loader<Cursor> cursor = new CursorLoader(NotificationListActivity.this,
Database.Notification.CONTENT_URI,
Database.Notification.PROJECTION, "NOT " + Database.Notification.READ_COLUMN, null, Database.Notification.SENT_TIME);
getContentResolver().registerContentObserver(Database.Notification.CONTENT_URI, true, notificationContentObserver);
return cursor;
}

@Override
protected void onStart() {
super.onStart();
if (adapter != null) {
adapter.notifyDataSetChanged();
}
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
adapter.swapCursor(cursor);
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
adapter.swapCursor(null);
}

private static class NotificationAdapter extends RecyclerViewCursorAdapter<NotificationAdapter.ViewHolder> {
Expand Down

0 comments on commit fef3dd3

Please sign in to comment.