Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create notification actions #51

Merged
merged 3 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/SplashScreenTheme"
android:theme="@style/Theme.App.SplashScreen"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/SplashScreenTheme">
android:theme="@style/Theme.App.SplashScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
Expand All @@ -40,6 +38,15 @@
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
tools:node="remove" />

<receiver
android:name=".presentation.onboarding.NotificationActionReceiver"
android:exported="true">
<intent-filter>
<action android:name="ACTION_OKAY" />
<action android:name="ACTION_DELETE" />
</intent-filter>
</receiver>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ import androidx.core.app.NotificationManagerCompat
import androidx.core.content.ContextCompat
import com.example.rememberme.R
import com.example.rememberme.domain.model.People
import com.example.rememberme.presentation.onboarding.NotificationActionReceiver
import com.example.rememberme.utils.Constants.NOTIFICATION_ID
import com.example.rememberme.utils.Constants.PEOPLE_CHANNEL_ID
import javax.inject.Inject

class NotificationService @Inject constructor(private val context: Context) {

companion object {
private const val CHANNEL_ID = "people_notification_channel"
private const val NOTIFICATION_ID = 0
private const val TAG = "NotificationService"
}

init {
Expand All @@ -36,7 +38,7 @@ class NotificationService @Inject constructor(private val context: Context) {
val name = context.getString(R.string.notification_channel_name)
val descriptionText = context.getString(R.string.notification_channel_description)
val importance = NotificationManager.IMPORTANCE_HIGH
val channel = NotificationChannel(CHANNEL_ID, name, importance).apply {
val channel = NotificationChannel(PEOPLE_CHANNEL_ID, name, importance).apply {
description = descriptionText
}
val notificationManager: NotificationManager =
Expand Down Expand Up @@ -71,20 +73,54 @@ class NotificationService @Inject constructor(private val context: Context) {
val drawable: Drawable? = ContextCompat.getDrawable(context, person.avatar)
val largeIcon: Bitmap? = drawable?.let { drawableToBitmap(it) }

val notification = NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_m2)
// Action for "Okay"
val okayIntent = Intent(context, NotificationActionReceiver::class.java).apply {
action = "ACTION_OKAY"
}
val okayPendingIntent: PendingIntent = PendingIntent.getBroadcast(
context,
0,
okayIntent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)

// Action for "Delete"
val deleteIntent = Intent(context, NotificationActionReceiver::class.java).apply {
action = "ACTION_DELETE"
putExtra("personId", person.id)
}
val deletePendingIntent: PendingIntent = PendingIntent.getBroadcast(
context,
1,
deleteIntent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)

val notification = NotificationCompat.Builder(context, PEOPLE_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notifications)
.setContentTitle(context.getString(R.string.notification_title, person.firstName))
.setContentText(context.getString(R.string.notification_text, person.place))
.setLargeIcon(largeIcon)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setCategory(NotificationCompat.CATEGORY_REMINDER)
.addAction(
R.drawable.baseline_thumb_up_24,
context.getString(R.string.okay),
okayPendingIntent
)
.addAction(
R.drawable.baseline_delete_24,
context.getString(R.string.delete),
deletePendingIntent
)
.build()

NotificationManagerCompat.from(context).notify(NOTIFICATION_ID, notification)
}


private fun drawableToBitmap(drawable: Drawable): Bitmap {
return if (drawable is BitmapDrawable) {
drawable.bitmap
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/java/com/example/rememberme/di/AppModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.CoroutineDispatcher
import javax.inject.Singleton

@Module
Expand Down Expand Up @@ -78,14 +79,15 @@ object AppModule {
@Provides
@Singleton
fun providePeopleUseCases(
peopleRepository: PeopleRepository
peopleRepository: PeopleRepository,
dispatcher: CoroutineDispatcher
): PeopleUseCases {
return PeopleUseCases(
getAllPeople = GetAllPeople(peopleRepository),
getPersonById = GetPersonById(peopleRepository),
insertPerson = InsertNewPerson(peopleRepository),
updatePerson = UpdatePerson(peopleRepository),
deletePersonById = DeletePersonById(peopleRepository)
deletePersonById = DeletePersonById(peopleRepository, dispatcher)
)
}

Expand Down
18 changes: 18 additions & 0 deletions app/src/main/java/com/example/rememberme/di/CoroutineModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.rememberme.di

import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object CoroutineModule {

@Provides
@Singleton
fun provideIoDispatcher(): CoroutineDispatcher = Dispatchers.IO
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package com.example.rememberme.domain.usecases.people

import com.example.rememberme.domain.repository.PeopleRepository
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import javax.inject.Inject

class DeletePersonById @Inject constructor(
private val peopleRepository: PeopleRepository,
private val dispatcher: CoroutineDispatcher = Dispatchers.IO
private val dispatcher: CoroutineDispatcher
) {
suspend operator fun invoke(personId: Long) {
withContext(dispatcher){
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.example.rememberme.presentation.onboarding

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import androidx.core.app.NotificationManagerCompat
import com.example.rememberme.domain.usecases.people.DeletePersonById
import com.example.rememberme.utils.Constants.NOTIFICATION_ID
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import javax.inject.Inject

@AndroidEntryPoint
class NotificationActionReceiver : BroadcastReceiver() {

@Inject
lateinit var deletePersonById: DeletePersonById
@OptIn(DelicateCoroutinesApi::class)
override fun onReceive(context: Context, intent: Intent) {
val notificationManager = NotificationManagerCompat.from(context)

when (intent.action) {
"ACTION_OKAY" -> {
// Handle the "Okay" action
notificationManager.cancel(NOTIFICATION_ID)

}
"ACTION_DELETE" -> {
val personId = intent.getLongExtra("personId",-1)
GlobalScope.launch {
Log.i(TAG,"Deleting the person with id: $personId")
deletePersonById(personId)
notificationManager.cancel(NOTIFICATION_ID)
}
}
}
}
companion object{
private const val TAG = "NotificationActionRecei"
}
}
2 changes: 2 additions & 0 deletions app/src/main/java/com/example/rememberme/utils/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ object Constants {
const val REMEMBER_ME_SETTING_NAME = "remember_me_settingS"
const val UI_MODE = "uiMode"
const val SCHEDULE = "schedule"
const val PEOPLE_CHANNEL_ID = "people_notification_channel"
const val NOTIFICATION_ID = 0
}
Loading