diff --git a/app/src/main/java/com/g11x/checklistapp/LocalRepository.java b/app/src/main/java/com/g11x/checklistapp/LocalRepository.java index c1590e1..6099117 100644 --- a/app/src/main/java/com/g11x/checklistapp/LocalRepository.java +++ b/app/src/main/java/com/g11x/checklistapp/LocalRepository.java @@ -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 @@ -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. diff --git a/app/src/main/java/com/g11x/checklistapp/NotificationListActivity.java b/app/src/main/java/com/g11x/checklistapp/NotificationListActivity.java index 7de6b5a..e83ab30 100644 --- a/app/src/main/java/com/g11x/checklistapp/NotificationListActivity.java +++ b/app/src/main/java/com/g11x/checklistapp/NotificationListActivity.java @@ -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; @@ -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 { private NotificationAdapter adapter; private RecyclerView mRecyclerView; + private ContentObserver notificationContentObserver; @Override protected int getNavDrawerItemIndex() { @@ -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() { - @Override - public Loader onCreateLoader(int i, Bundle bundle) { - Loader 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 onCreateLoader(int i, Bundle bundle) { + if (notificationContentObserver != null) { + getContentResolver().unregisterContentObserver(notificationContentObserver); + } + notificationContentObserver = new ContentObserver(new Handler()) { @Override - public void onLoadFinished(Loader 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 loader) { - adapter.swapCursor(null); + public void onChange(boolean selfChange, Uri uri) { + getSupportLoaderManager().restartLoader(0, null, NotificationListActivity.this); + super.onChange(selfChange, uri); } - }); + }; + Loader 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 loader, Cursor cursor) { + adapter.swapCursor(cursor); + } + + @Override + public void onLoaderReset(Loader loader) { + adapter.swapCursor(null); } private static class NotificationAdapter extends RecyclerViewCursorAdapter {