Continue implementing reminders.

This commit is contained in:
Docile-Alligator
2026-06-27 22:27:37 -04:00
parent 78ca3451ac
commit 27eb3313fc
8 changed files with 102 additions and 22 deletions

View File

@ -482,6 +482,7 @@
android:permission="android.permission.BIND_JOB_SERVICE" />
<receiver android:name=".broadcastreceivers.DownloadedMediaDeleteActionBroadcastReceiver" />
<receiver android:name=".broadcastreceivers.ReminderAlarmReceiver" />
</application>
</manifest>

View File

@ -90,6 +90,7 @@ import ml.docilealligator.infinityforreddit.fragments.ViewPostDetailFragment;
import ml.docilealligator.infinityforreddit.fragments.ViewPostDetailFragmentNew;
import ml.docilealligator.infinityforreddit.fragments.ViewRedditGalleryImageOrGifFragment;
import ml.docilealligator.infinityforreddit.fragments.ViewRedditGalleryVideoFragment;
import ml.docilealligator.infinityforreddit.broadcastreceivers.ReminderAlarmReceiver;
import ml.docilealligator.infinityforreddit.services.DownloadMediaService;
import ml.docilealligator.infinityforreddit.services.DownloadRedditVideoService;
import ml.docilealligator.infinityforreddit.services.EditProfileService;
@ -339,6 +340,8 @@ public interface AppComponent {
void inject(SetReminderActivity setReminderActivity);
void inject(ReminderAlarmReceiver reminderAlarmReceiver);
@Component.Factory
interface Factory {
AppComponent create(@BindsInstance Application application);

View File

@ -0,0 +1,81 @@
package ml.docilealligator.infinityforreddit.broadcastreceivers
import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import ml.docilealligator.infinityforreddit.Infinity
import ml.docilealligator.infinityforreddit.R
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase
import ml.docilealligator.infinityforreddit.activities.ViewPostDetailActivity
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper
import ml.docilealligator.infinityforreddit.reminder.Reminder
import ml.docilealligator.infinityforreddit.utils.NotificationUtils
import java.util.Random
import javax.inject.Inject
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
class ReminderAlarmReceiver: BroadcastReceiver() {
@Inject
lateinit var mRedditRoomDatabase: RedditDataRoomDatabase
@Inject
lateinit var mCustomThemeWrapper: CustomThemeWrapper
@OptIn(DelicateCoroutinesApi::class)
override fun onReceive(context: Context, intent: Intent) {
val reminder: Reminder? = intent.getParcelableExtra(EXTRA_REMINDER)
reminder?.let {
(context.applicationContext as Infinity).appComponent.inject(this)
val notificationManager = NotificationUtils.getNotificationManager(context)
val builder = NotificationUtils.buildNotification(
notificationManager,
context, context.getString(R.string.reminder), it.content, context.getString(if (it.commentId.isNotEmpty()) R.string.comment else R.string.post),
NotificationUtils.CHANNEL_ID_NEW_MESSAGES,
NotificationUtils.CHANNEL_NEW_MESSAGES,
NotificationUtils.GROUP_REMINDER, mCustomThemeWrapper.colorPrimaryLightTheme
)
val intent = Intent(context, ViewPostDetailActivity::class.java)
intent.putExtra(ViewPostDetailActivity.EXTRA_POST_ID, it.postId)
intent.putExtra(ViewPostDetailActivity.EXTRA_SINGLE_COMMENT_ID, it.commentId)
val pendingIntent =
PendingIntent.getActivity(context, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
builder.setContentIntent(pendingIntent)
try {
notificationManager.notify(
NotificationUtils.REMINDER_NOTIFICATION_ID + Random().nextInt(10000), builder.build()
)
} catch (e: SecurityException) {
e.printStackTrace()
}
doAsync(GlobalScope) {
try {
mRedditRoomDatabase.reminderDao().deleteReminder(it)
} catch (e: Exception) {
e.printStackTrace()
}
}
}
}
companion object {
const val EXTRA_REMINDER = "ER"
}
}
fun BroadcastReceiver.doAsync(
appScope: CoroutineScope,
coroutineContext: CoroutineContext = EmptyCoroutineContext,
block: suspend CoroutineScope.() -> Unit
) {
val pendingResult = goAsync()
appScope.launch(coroutineContext) { block() }.invokeOnCompletion { pendingResult.finish() }
}

View File

@ -1,18 +0,0 @@
package ml.docilealligator.infinityforreddit.reminder
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
class ReminderAlarmReceiver: BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val reminder: Reminder? = intent.getParcelableExtra(EXTRA_REMINDER)
reminder?.let {
}
}
companion object {
val EXTRA_REMINDER = "ER"
}
}

View File

@ -1,6 +1,7 @@
package ml.docilealligator.infinityforreddit.reminder
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
@ -16,4 +17,7 @@ interface ReminderDao {
@Query("SELECT * FROM reminders")
fun getAllRemindersFlow(): Flow<List<Reminder>>
@Delete
suspend fun deleteReminder(reminder: Reminder)
}

View File

@ -6,6 +6,7 @@ import android.app.PendingIntent
import android.content.Intent
import kotlinx.coroutines.flow.Flow
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase
import ml.docilealligator.infinityforreddit.broadcastreceivers.ReminderAlarmReceiver
class ReminderManager(
private val applicationContext: Application,
@ -15,10 +16,15 @@ class ReminderManager(
suspend fun setReminder(reminder: Reminder) {
redditRoomDatabase.reminderDao().insert(reminder)
val alarmIntent = Intent(applicationContext,
ReminderAlarmReceiver::class.java).let { intent ->
PendingIntent.getBroadcast(applicationContext, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
val alarmIntent = Intent(
applicationContext,
ReminderAlarmReceiver::class.java
).let { intent ->
intent.putExtra(ReminderAlarmReceiver.EXTRA_REMINDER, reminder)
PendingIntent.getBroadcast(
applicationContext, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
}
alarmManager.set(

View File

@ -29,11 +29,13 @@ public class NotificationUtils {
public static final int DOWNLOAD_GIF_NOTIFICATION_ID = 50000;
public static final int MATERIAL_YOU_NOTIFICATION_ID = 60000;
public static final int EDIT_PROFILE_SERVICE_NOTIFICATION_ID = 70000;
public static final int REMINDER_NOTIFICATION_ID = 80000;
private static final int SUMMARY_BASE_ID_UNREAD_MESSAGE = 0;
private static final int NOTIFICATION_BASE_ID_UNREAD_MESSAGE = 1;
private static final String GROUP_USER_BASE = "ml.docilealligator.infinityforreddit.";
public static final String GROUP_REMINDER = "ml.docilealligator.infinityforreddit.reminder@group";
public static NotificationCompat.Builder buildNotification(NotificationManagerCompat notificationManager,
Context context, String title, String content,

View File

@ -1659,4 +1659,5 @@
<string name="reminder_time_must_be_in_future">The reminder time must be in the future.</string>
<string name="invalid_reminder_post_id">Invalid post id</string>
<string name="reminder_set">Reminder set</string>
<string name="reminder">Reminder</string>
</resources>