Add actions for single result notification case

This commit is contained in:
Shinokuni 2020-02-22 14:56:12 +01:00
parent ffeb955a82
commit 1676e9f637
5 changed files with 84 additions and 17 deletions

View File

@ -35,6 +35,9 @@
<service android:name=".utils.feedscolors.FeedsColorsIntentService" />
<receiver android:name=".utils.SyncWorker$MarkReadReceiver" />
<receiver android:name=".utils.SyncWorker$ReadLaterReceiver" />
<activity android:name=".activities.SettingsActivity" />
<activity
android:name=".activities.SplashActivity"

View File

@ -109,7 +109,7 @@ public abstract class ARepository<T> {
}
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) {

View File

@ -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
}
}

View File

@ -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

View File

@ -34,11 +34,11 @@ public abstract class ItemDao implements BaseDao<Item> {
* 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<Item> {
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);