-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from omer358/dev
resolve #39
- Loading branch information
Showing
19 changed files
with
283 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 14 additions & 1 deletion
15
app/src/main/java/com/example/rememberme/RememberMeApplication.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,21 @@ | ||
package com.example.rememberme | ||
|
||
import android.app.Application | ||
import androidx.hilt.work.HiltWorkerFactory | ||
import androidx.work.Configuration | ||
import dagger.hilt.android.HiltAndroidApp | ||
import javax.inject.Inject | ||
|
||
@HiltAndroidApp | ||
class RememberMeApplication: Application() { | ||
class RememberMeApplication : Application(), Configuration.Provider { | ||
|
||
@Inject | ||
lateinit var workerFactory: HiltWorkerFactory | ||
|
||
override val workManagerConfiguration: Configuration | ||
get() = Configuration.Builder() | ||
.setWorkerFactory(workerFactory) | ||
.build() | ||
|
||
|
||
} |
78 changes: 78 additions & 0 deletions
78
app/src/main/java/com/example/rememberme/data/manager/NotificationService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package com.example.rememberme.data.manager | ||
|
||
import android.Manifest | ||
import android.app.NotificationChannel | ||
import android.app.NotificationManager | ||
import android.app.PendingIntent | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.content.pm.PackageManager | ||
import android.net.Uri | ||
import android.os.Build | ||
import androidx.core.app.ActivityCompat | ||
import androidx.core.app.NotificationCompat | ||
import androidx.core.app.NotificationManagerCompat | ||
import com.example.rememberme.R | ||
import com.example.rememberme.domain.model.People | ||
import javax.inject.Inject | ||
|
||
class NotificationService @Inject constructor(private val context: Context) { | ||
|
||
private val CHANNEL_ID = "people_notification_channel" | ||
private val NOTIFICATION_ID = 0 | ||
|
||
|
||
init { | ||
createNotificationChannel() | ||
} | ||
|
||
private fun createNotificationChannel() { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
val name = "People Notification" | ||
val descriptionText = "Reminders about the people you have met" | ||
val importance = NotificationManager.IMPORTANCE_HIGH | ||
val channel = NotificationChannel(CHANNEL_ID, name, importance).apply { | ||
description = descriptionText | ||
} | ||
val notificationManager: NotificationManager = | ||
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager | ||
notificationManager.createNotificationChannel(channel) | ||
} | ||
} | ||
|
||
fun showNotification(person: People) { | ||
val deepLinkIntent = Intent( | ||
Intent.ACTION_VIEW, | ||
Uri.parse("app://people/${person.id}") | ||
).apply { | ||
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK | ||
} | ||
|
||
val pendingIntent: PendingIntent = PendingIntent.getActivity( | ||
context, | ||
0, | ||
deepLinkIntent, | ||
PendingIntent.FLAG_IMMUTABLE | ||
) | ||
|
||
val builder = NotificationCompat.Builder(context, CHANNEL_ID) | ||
.setSmallIcon(R.drawable.ic_m2) | ||
.setContentTitle("Do you remember ${person.firstName}?") | ||
.setContentText("You met at ${person.place}") | ||
.setPriority(NotificationCompat.PRIORITY_MAX) | ||
.setChannelId(CHANNEL_ID) | ||
.setContentIntent(pendingIntent) | ||
.setAutoCancel(true) | ||
|
||
with(NotificationManagerCompat.from(context)) { | ||
if (ActivityCompat.checkSelfPermission( | ||
context, | ||
Manifest.permission.POST_NOTIFICATIONS | ||
) != PackageManager.PERMISSION_GRANTED | ||
) { | ||
return | ||
} | ||
notify(NOTIFICATION_ID, builder.build()) | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
app/src/main/java/com/example/rememberme/data/manager/NotificationWorker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.example.rememberme.data.manager | ||
|
||
|
||
import android.content.Context | ||
import android.util.Log | ||
import androidx.hilt.work.HiltWorker | ||
import androidx.work.Worker | ||
import androidx.work.WorkerParameters | ||
import com.example.rememberme.domain.usecases.people.GetAllPeople | ||
import dagger.assisted.Assisted | ||
import dagger.assisted.AssistedInject | ||
import kotlinx.coroutines.runBlocking | ||
import kotlin.random.Random | ||
|
||
@HiltWorker | ||
class NotificationWorker | ||
@AssistedInject constructor( | ||
@Assisted context: Context, | ||
@Assisted workerParams: WorkerParameters, | ||
private val getAllPeople: GetAllPeople, | ||
private val notificationService: NotificationService | ||
) : Worker(context, workerParams) { | ||
|
||
override fun doWork(): Result { | ||
Log.d(TAG, "Performing long running task in scheduled job") | ||
return runBlocking { | ||
getAllPeople().collect { people -> | ||
if (people.isNotEmpty()) { | ||
Log.d(TAG, "People: $people") | ||
val randomPerson = people[Random.nextInt(people.size)] | ||
notificationService.showNotification(randomPerson) | ||
} else { | ||
Log.d(TAG, "No people found") | ||
} | ||
} | ||
Log.d(TAG, "Work finished") | ||
Result.success() | ||
} | ||
} | ||
companion object { | ||
private const val TAG = "NotificationWorker" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
app/src/main/java/com/example/rememberme/domain/model/RemindersRepetition.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
package com.example.rememberme.domain.model | ||
|
||
enum class RemindersRepetition { | ||
OnceADay, | ||
ThreeADay, | ||
FiveADay | ||
OnceADay { override fun toString(): String ="Once a day" }, | ||
ThreeADay { override fun toString(): String = "Three a day" }, | ||
FiveADay { override fun toString(): String = "Five a day" } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
app/src/main/java/com/example/rememberme/domain/usecases/people/PeopleUseCases.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.