Skip to content

Commit

Permalink
Implement upload notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
profiluefter committed May 24, 2021
1 parent 16cdf34 commit c9607e5
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package me.profiluefter.profinote

import android.app.PendingIntent
import android.content.Context
import androidx.navigation.NavDeepLinkBuilder
import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import me.profiluefter.profinote.activities.NoteDetailsFragmentArgs
import me.profiluefter.profinote.data.DeepLinkProvider
import me.profiluefter.profinote.data.entities.Note
import javax.inject.Inject

class NavigationDeepLinkProvider @Inject constructor(@ApplicationContext private val context: Context) : DeepLinkProvider {
override fun deepLinkToNote(note: Note): PendingIntent = NavDeepLinkBuilder(context)
.setGraph(R.navigation.nav_graph)
.setDestination(R.id.noteDetailsFragment)
.setArguments(NoteDetailsFragmentArgs(note).toBundle())
.createPendingIntent()
}

@Module
@InstallIn(SingletonComponent::class)
interface NavigationDeepLinkProviderBinding {
@Binds
fun navigationDeepLinkProvider(provider: NavigationDeepLinkProvider): DeepLinkProvider
}
3 changes: 3 additions & 0 deletions data-lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,7 @@ dependencies {
api "androidx.room:room-runtime:$room_version"
implementation "androidx.room:room-ktx:$room_version"
kapt "androidx.room:room-compiler:$room_version"

// Notifications
implementation 'androidx.core:core-ktx:1.3.2'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package me.profiluefter.profinote.data

import android.app.PendingIntent
import me.profiluefter.profinote.data.entities.Note

interface DeepLinkProvider {
fun deepLinkToNote(note: Note): PendingIntent
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package me.profiluefter.profinote.data

import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkCapabilities.*
import android.os.Build
import android.preference.PreferenceManager
import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.core.content.getSystemService
import androidx.lifecycle.LiveData
import androidx.lifecycle.map
import androidx.lifecycle.switchMap
Expand All @@ -17,11 +23,14 @@ import me.profiluefter.profinote.data.remote.NotesAPI
import java.time.LocalDateTime
import javax.inject.Inject
import javax.inject.Provider
import kotlin.random.Random
import kotlin.random.nextInt

class NotesRepository @Inject constructor(
private val remote: NotesAPI,
private val local: NotesDatabase,
private val credentials: Provider<Credentials>,
private val deepLinkProvider: DeepLinkProvider,
@ApplicationContext private val context: Context
) {
private val username: String
Expand All @@ -31,6 +40,10 @@ class NotesRepository @Inject constructor(

private val logTag = "NotesRepository"

private val notificationManager by lazy { context.getSystemService<NotificationManager>()!! }

private val sharedPreferences by lazy { PreferenceManager.getDefaultSharedPreferences(context) }

fun getListLive(localID: Int): LiveData<TodoList> =
local.listDao().getByLocalIDLive(localID).switchMap { list ->
local.todoDao().getByListIDLive(localID).map { todos ->
Expand Down Expand Up @@ -180,6 +193,8 @@ class NotesRepository @Inject constructor(
}

private suspend fun synchronizeTodos() {
createUploadNotificationChannel()

val localTodos = local.todoDao().getAll()

val (deleteTodos, remainingLocal) =
Expand All @@ -195,8 +210,12 @@ class NotesRepository @Inject constructor(
Log.d(logTag, "Uploading ${uploadTodos.size} todos")
uploadTodos.forEach {
val (_, newID) = remote.newTodo(it, username, password)
// New item uploaded, showing notification
showUploadNotification(it)
local.todoDao().changeID(it.localID, newID)
}
if(uploadTodos.isNotEmpty())
showUploadParentNotification()

val remainingTodos = local.todoDao().getAll()

Expand Down Expand Up @@ -249,6 +268,56 @@ class NotesRepository @Inject constructor(
local.todoDao().update(localEdits)
}

private fun createUploadNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationManager.createNotificationChannel(
NotificationChannel(
context.getString(R.string.notification_upload_channel_id),
context.getString(R.string.notification_upload_channel_name),
NotificationManager.IMPORTANCE_LOW
)
)
}
}

private fun showUploadNotification(it: RawTodo) {
if(!sharedPreferences.getBoolean("upload_notification", true)) return

val notification =
NotificationCompat.Builder(context, context.getString(R.string.notification_upload_channel_id))
.setSmallIcon(R.drawable.baseline_task_24)
.setContentTitle(it.title)
.setContentText(it.description)
.setStyle(NotificationCompat.BigTextStyle().bigText(it.description))
.setPriority(NotificationCompat.PRIORITY_LOW)
.setGroup(context.getString(R.string.notification_upload_group_id))
.setContentIntent(deepLinkProvider.deepLinkToNote(Note.from(it)))
.build()
notificationManager.notify(
Random.nextInt(Integer.MIN_VALUE..Integer.MAX_VALUE),
notification
)
}

private fun showUploadParentNotification() {
if(!sharedPreferences.getBoolean("upload_notification", true)) return

val notification = NotificationCompat.Builder(context, context.getString(R.string.notification_upload_channel_id))
.setSmallIcon(R.drawable.baseline_task_24)
.setContentTitle(context.getString(R.string.notification_upload_title))
.setContentText(context.getString(R.string.notification_upload_description))
.setPriority(NotificationCompat.PRIORITY_LOW)
.setGroup(context.getString(R.string.notification_upload_group_id))
.setGroupSummary(true)
.setStyle(NotificationCompat.InboxStyle()
.setSummaryText(context.getString(R.string.notification_upload_description)))
.build()
notificationManager.notify(
Random.nextInt(Integer.MIN_VALUE..Integer.MAX_VALUE),
notification
)
}

private fun isNetworkAvailable(): Boolean =
context.getSystemService(ConnectivityManager::class.java).run {
getNetworkCapabilities(activeNetwork)?.run {
Expand Down
10 changes: 10 additions & 0 deletions data-lib/src/main/res/drawable/baseline_task_20.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="20dp"
android:height="20dp"
android:viewportWidth="20"
android:viewportHeight="20"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M12,2H5.5C4.67,2 4,2.67 4,3.5v13C4,17.33 4.67,18 5.5,18h9c0.83,0 1.5,-0.67 1.5,-1.5V6L12,2zM9.15,15l-2.83,-2.83l1.13,-1.13l1.7,1.7l3.47,-3.46l1.13,1.13L9.15,15zM11,7V3l4,4H11z"/>
</vector>
10 changes: 10 additions & 0 deletions data-lib/src/main/res/drawable/baseline_task_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M14,2H6C4.9,2 4.01,2.9 4.01,4L4,20c0,1.1 0.89,2 1.99,2H18c1.1,0 2,-0.9 2,-2V8L14,2zM10.94,18L7.4,14.46l1.41,-1.41l2.12,2.12l4.24,-4.24l1.41,1.41L10.94,18zM13,9V3.5L18.5,9H13z"/>
</vector>
10 changes: 10 additions & 0 deletions data-lib/src/main/res/drawable/ic_baseline_task_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M14,2H6C4.9,2 4.01,2.9 4.01,4L4,20c0,1.1 0.89,2 1.99,2H18c1.1,0 2,-0.9 2,-2V8L14,2zM10.94,18L7.4,14.46l1.41,-1.41l2.12,2.12l4.24,-4.24l1.41,1.41L10.94,18zM13,9V3.5L18.5,9H13z"/>
</vector>
8 changes: 8 additions & 0 deletions data-lib/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="notification_upload_channel_id">upload</string>
<string name="notification_upload_channel_name">Upload Notifications</string>
<string name="notification_upload_group_id">upload_notification_group</string>
<string name="notification_upload_title">Tasks uploaded</string>
<string name="notification_upload_description">The following tasks were uploaded</string>
</resources>

0 comments on commit c9607e5

Please sign in to comment.