diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index f76a9a7f..182abb87 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -35,6 +35,9 @@ + + + { } public Completable setItemReadState(Item item, boolean read) { - return database.itemDao().setReadState(item.getId(), read ? 1 : 0, !item.isReadChanged() ? 1 : 0); + return database.itemDao().setReadState(item.getId(), read, !item.isReadChanged()); } public Completable setAllItemsReadState(boolean read) { diff --git a/app/src/main/java/com/readrops/app/utils/SyncWorker.kt b/app/src/main/java/com/readrops/app/utils/SyncWorker.kt index a0c783a0..f3de7e86 100644 --- a/app/src/main/java/com/readrops/app/utils/SyncWorker.kt +++ b/app/src/main/java/com/readrops/app/utils/SyncWorker.kt @@ -1,6 +1,7 @@ package com.readrops.app.utils import android.app.PendingIntent +import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import androidx.core.app.NotificationCompat @@ -12,9 +13,11 @@ import com.readrops.app.ReadropsApp import com.readrops.app.activities.MainActivity import com.readrops.app.repositories.ARepository import com.readrops.readropsdb.Database +import com.readrops.readropsdb.entities.Item import com.readrops.readropsdb.entities.account.Account import com.readrops.readropslibrary.services.SyncResult import io.reactivex.disposables.Disposable +import io.reactivex.schedulers.Schedulers class SyncWorker(context: Context, parameters: WorkerParameters) : Worker(context, parameters) { @@ -24,7 +27,6 @@ class SyncWorker(context: Context, parameters: WorkerParameters) : Worker(contex private val database = Database.getInstance(applicationContext) override fun doWork(): Result { - val accounts = database.accountDao().selectAll() var result = Result.success() @@ -67,18 +69,26 @@ class SyncWorker(context: Context, parameters: WorkerParameters) : Worker(contex if (notifContent.title != null && notifContent.content != null) { val intent = Intent(applicationContext, MainActivity::class.java) - notifContent.item?.let { - intent.putExtra(ReadropsKeys.ITEM_ID, it.id) - intent.putExtra(ReadropsKeys.IMAGE_URL, it.imageLink) - } - val notificationBuilder = NotificationCompat.Builder(applicationContext, ReadropsApp.SYNC_CHANNEL_ID) .setContentTitle(notifContent.title) .setContentText(notifContent.content) .setSmallIcon(R.drawable.ic_notif) - .setContentIntent(PendingIntent.getActivity(applicationContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)) + .setContentIntent(PendingIntent.getActivity(applicationContext, 0, intent, 0)) .setAutoCancel(true) + notifContent.item?.let { + with(intent) { + putExtra(ReadropsKeys.ITEM_ID, it.id) + putExtra(ReadropsKeys.IMAGE_URL, it.imageLink) + } + + val feed = database.feedDao().getFeedById(it.feedId) + + notificationBuilder.addAction(buildReadlaterAction(it)) + .addAction(buildMarkAsRead(it)) + .setColor(feed.backgroundColor) + } + notifContent.largeIcon?.let { notificationBuilder.setLargeIcon(it) } @@ -89,9 +99,66 @@ class SyncWorker(context: Context, parameters: WorkerParameters) : Worker(contex } + private fun buildReadlaterAction(item: Item): NotificationCompat.Action { + val broadcastIntent = Intent(applicationContext, ReadLaterReceiver::class.java).apply { + putExtra(ReadropsKeys.ITEM_ID, item.id) + } + + return NotificationCompat.Action.Builder(R.drawable.ic_read_later, applicationContext.getString(R.string.read_later), + PendingIntent.getBroadcast(applicationContext, 0, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT)) + .setAllowGeneratedReplies(false) + .build() + } + + private fun buildMarkAsRead(item: Item): NotificationCompat.Action { + val broadcastIntent = Intent(applicationContext, MarkReadReceiver::class.java).apply { + putExtra(ReadropsKeys.ITEM_ID, item.id) + } + + return NotificationCompat.Action.Builder(R.drawable.ic_read, applicationContext.getString(R.string.read), + PendingIntent.getBroadcast(applicationContext, 0, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT)) + .setAllowGeneratedReplies(false) + .build() + } + + class MarkReadReceiver : BroadcastReceiver() { + + override fun onReceive(context: Context?, intent: Intent?) { + val itemId = intent?.getIntExtra(ReadropsKeys.ITEM_ID, 0)!! + + with(Database.getInstance(context)) { + itemDao().setReadState(itemId, true, true) + .subscribeOn(Schedulers.io()) + .subscribe() + } + + with(NotificationManagerCompat.from(context!!)) { + cancel(SYNC_RESULT_NOTIFICATION_ID) + } + } + } + + class ReadLaterReceiver : BroadcastReceiver() { + + override fun onReceive(context: Context?, intent: Intent?) { + val itemId = intent?.getIntExtra(ReadropsKeys.ITEM_ID, 0)!! + + with(Database.getInstance(context)) { + itemDao().setReadItLater(itemId) + .subscribeOn(Schedulers.io()) + .subscribe() + } + + with(NotificationManagerCompat.from(context!!)) { + cancel(SYNC_RESULT_NOTIFICATION_ID) + } + } + + } + companion object { val TAG = SyncWorker::class.java.simpleName private const val SYNC_NOTIFICATION_ID = 2 - private const val SYNC_RESULT_NOTIFICATION_ID = 3 + const val SYNC_RESULT_NOTIFICATION_ID = 3 } } \ No newline at end of file diff --git a/app/src/main/java/com/readrops/app/viewmodels/MainViewModel.java b/app/src/main/java/com/readrops/app/viewmodels/MainViewModel.java index b2f129b5..4dbea9f4 100644 --- a/app/src/main/java/com/readrops/app/viewmodels/MainViewModel.java +++ b/app/src/main/java/com/readrops/app/viewmodels/MainViewModel.java @@ -237,10 +237,7 @@ public class MainViewModel extends AndroidViewModel { } public Completable setItemReadItLater(int itemId) { - return Completable.create(emitter -> { - db.itemDao().setReadItLater(itemId); - emitter.onComplete(); - }); + return db.itemDao().setReadItLater(itemId); } //endregion diff --git a/readropsdb/src/main/java/com/readrops/readropsdb/dao/ItemDao.java b/readropsdb/src/main/java/com/readrops/readropsdb/dao/ItemDao.java index ed5a31b4..68efda64 100644 --- a/readropsdb/src/main/java/com/readrops/readropsdb/dao/ItemDao.java +++ b/readropsdb/src/main/java/com/readrops/readropsdb/dao/ItemDao.java @@ -34,11 +34,11 @@ public abstract class ItemDao implements BaseDao { * Set an item read or unread * * @param itemId id of the item to update - * @param readState 1 for read, 0 for unread + * @param read 1 for read, 0 for unread * @param readChanged */ - @Query("Update Item Set read_changed = :readChanged, read = :readState Where id = :itemId") - public abstract Completable setReadState(int itemId, int readState, int readChanged); + @Query("Update Item Set read_changed = :readChanged, read = :read Where id = :itemId") + public abstract Completable setReadState(int itemId, boolean read, boolean readChanged); @Query("Update Item set read_changed = 1, read = :readState Where feed_id In (Select id From Feed Where account_id = :accountId)") public abstract Completable setAllItemsReadState(int readState, int accountId); @@ -47,7 +47,7 @@ public abstract class ItemDao implements BaseDao { public abstract Completable setAllFeedItemsReadState(int feedId, int readState); @Query("Update Item set read_it_later = 1 Where id = :itemId") - public abstract void setReadItLater(int itemId); + public abstract Completable setReadItLater(int itemId); @Query("Select count(*) From Item Where feed_id = :feedId And read = 0") public abstract int getUnreadCount(int feedId);