Skip to content
This repository has been archived by the owner on May 19, 2023. It is now read-only.

Commit

Permalink
WorkerManager and Notification support for reminders added AND detekt…
Browse files Browse the repository at this point in the history
… and Ktlint support Added (#66)

* Base Integration of Detekt Analytics tool done.

* added the detekt as pre-push git-hooks.

* Enabled the ktlint and also added it into the pre-push git-hooks.

* change order and define the taks to generate output in some dir

* change some files and tasks and testing that pre-push is working or not

* manually it is running but while push it is not working

* code cleanup.

* tried once more that scripts is working or not

* fixed detekt and lint errors.

* fixed detekt errors.

* Working on Notification and WorkManager

* Working on Notification and WorkManager

* Working on Notification and WorkManager

* n

* modified

* changed gitignore

* refactoring

* refactoring

* Update misc.xml

* Update gradle.xml

* Update gradle.xml

* Update gradle.xml

* Update misc.xml

* Delete misc.xml

* Delete gradle.xml

* Update gradle.xml

* Update gradle.xml

* Update misc.xml

* changed NoteXApp to myContent

* added NotificationDTO.kt

* Update misc.xml

* Update gradle.xml

* Update gradle.xml

* Update gradle.xml

* Update misc.xml

* Update gradle.xml

* Update gradle.xml

* Update misc.xml

* detekt requested changes applied

* ktlint changes applied

* ktlint changes applied

Co-authored-by: Abhinav Suman <[email protected]>
Co-authored-by: Abhinav Suman <[email protected]>
Co-authored-by: MosesATJTC <[email protected]>
Co-authored-by: Arunshaik2001 <[email protected]>
Co-authored-by: Shaik Ahron <[email protected]>
  • Loading branch information
6 people authored Oct 17, 2022
1 parent 7d3e5fd commit e0a54c6
Show file tree
Hide file tree
Showing 42 changed files with 1,427 additions and 94 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ local.properties

# Android Studio / IntelliJ IDEA
*.iws
/.idea
/.idea/libraries
/.idea/tasks.xml
/.idea/caches
Expand All @@ -24,6 +25,7 @@ local.properties
/.idea/modules.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
/.idea/misc.xml
/out/

.DS_Store
Expand Down
19 changes: 0 additions & 19 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 0 additions & 20 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ dependencies {
implementation("androidx.navigation:navigation-compose:${LibVersion.navigationComposeVersion}")
implementation("androidx.datastore:datastore-preferences:${LibVersion.dataStoreVersion}")
implementation("androidx.paging:paging-compose:1.0.0-alpha16")
implementation("androidx.work:work-runtime-ktx:2.7.1")
// DO NOT upgrade desugar_jdk_libs to 1.2.0
coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:1.1.6")

Expand Down Expand Up @@ -133,4 +134,7 @@ dependencies {
androidTestImplementation("androidx.compose.ui:ui-test-junit4:${LibVersion.composeVersion}")
debugImplementation("androidx.compose.ui:ui-tooling:${LibVersion.composeVersion}")
debugImplementation("androidx.compose.ui:ui-test-manifest:${LibVersion.composeVersion}")

implementation("androidx.hilt:hilt-work:1.0.0")
kapt("androidx.hilt:hilt-compiler:1.0.0")
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.hadiyarajesh.notex

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
Expand All @@ -21,4 +19,4 @@ class ExampleInstrumentedTest {
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.hadiyarajesh.notex", appContext.packageName)
}
}
}
8 changes: 8 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
tools:node="remove">
</provider>

<receiver android:name=".reminder.notification.NotificationBroadCastReceiver"/>
</application>

</manifest>
4 changes: 2 additions & 2 deletions app/src/main/java/com/hadiyarajesh/notex/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import android.os.Build
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import com.hadiyarajesh.notex.ui.NoteXApp
import com.hadiyarajesh.notex.ui.reminders.myContent
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
NoteXApp()
myContent()
}

createNotificationChannel()
Expand Down
15 changes: 14 additions & 1 deletion app/src/main/java/com/hadiyarajesh/notex/MyApplication.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
package com.hadiyarajesh.notex

import android.app.Application
import androidx.hilt.work.HiltWorkerFactory
import androidx.work.Configuration
import dagger.hilt.android.HiltAndroidApp
import javax.inject.Inject

@HiltAndroidApp
class MyApplication: Application()
class MyApplication : Application(), Configuration.Provider {

@Inject
lateinit var workerFactory: HiltWorkerFactory

override fun getWorkManagerConfiguration(): Configuration {
return Configuration.Builder()
.setWorkerFactory(workerFactory)
.build()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ object InstantConverter {
@TypeConverter
fun toInstant(value: String?): Instant? = value?.let { Instant.parse(value) }


@JvmStatic
@TypeConverter
fun getLocalDate(instant: Instant): LocalDate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import java.time.Instant
@Dao
interface ReminderDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertOrUpdate(reminder: Reminder)
suspend fun insertOrUpdate(reminder: Reminder): Long

/**
* This method will add reminder to folder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ data class Folder(
val title: String,
val description: String?,
val folderType: FolderType,
//HexCode of a color
// HexCode of a color
val color: String? = null,
val createdOn: Instant,
val updatedOn: Instant
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ data class Note(
val content: String?,
val archived: Boolean,
val archivedOn: Instant? = null,
//HexCode of a color
// HexCode of a color
val color: String? = null,
val createdOn: Instant,
val updatedOn: Instant,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ data class Reminder(
val cancelledOn: Instant? = null,
val completed: Boolean,
val completedOn: Instant?,
//HexCode of a color
// HexCode of a color
val color: String? = null,
val createdOn: Instant,
val updatedOn: Instant,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
Expand Down
9 changes: 8 additions & 1 deletion app/src/main/java/com/hadiyarajesh/notex/di/ScopeModule.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.hadiyarajesh.notex.di

import com.hadiyarajesh.notex.reminder.notification.NotificationHelper
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import javax.inject.Singleton

@InstallIn(SingletonComponent::class)
@Module
Expand All @@ -18,4 +19,10 @@ class ScopeModule {
fun provideCoroutineScope(): CoroutineScope {
return CoroutineScope(SupervisorJob() + Dispatchers.IO)
}

@Singleton
@Provides
fun provideNotificationHelper(): NotificationHelper {
return NotificationHelper()
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
package com.hadiyarajesh.notex.network.api

interface NoteApi {
}
interface NoteApi
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.hadiyarajesh.notex.reminder.notification

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import androidx.annotation.CallSuper
import androidx.core.app.NotificationManagerCompat
import com.hadiyarajesh.notex.R
import com.hadiyarajesh.notex.reminder.worker.ReminderWorkManager
import dagger.hilt.android.AndroidEntryPoint
import java.time.Instant
import java.time.temporal.ChronoUnit
import javax.inject.Inject

@AndroidEntryPoint
class NotificationBroadCastReceiver : HiltBroadcastReceiver() {

companion object {
const val TAG = "NotificationBroadCast"
}

@Inject
lateinit var reminderWorkManager: ReminderWorkManager

override fun onReceive(context: Context, intent: Intent) {
super.onReceive(context, intent)
when (intent.action) {
context.resources.getString(R.string.done_action) ->
reminderWorkManager.cancelWorkRequest(
context,
intent.getStringExtra(context.resources.getString(R.string.worker_tag)) ?: ""
)
context.resources.getString(R.string.postpone_action) -> {
reminderWorkManager.cancelWorkRequest(
context,
intent.getStringExtra(context.resources.getString(R.string.worker_tag)) ?: ""
)

reminderWorkManager.createWorkRequestAndEnqueue(
context,
time = Instant.now().plus(1, ChronoUnit.HOURS),
isFirstTime = false,
reminderId = intent.getLongExtra(
context.resources.getString(R.string.reminder_id),
-1
)
)
}
else -> Log.i(TAG, "Nothing to Perform this Action ${intent.action}")
}

with(NotificationManagerCompat.from(context)) {
cancel(intent.getIntExtra(context.resources.getString(R.string.notification_id), -1))
}
}
}

abstract class HiltBroadcastReceiver : BroadcastReceiver() {
@SuppressWarnings("EmptyFunctionBlock")
@CallSuper
override fun onReceive(context: Context, intent: Intent) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.hadiyarajesh.notex.reminder.notification

data class NotificationDTO(
val title: String,
val subTitle: String,
val reminderId: Long,
val workerTag: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.hadiyarajesh.notex.reminder.notification

import android.app.PendingIntent
import android.app.PendingIntent.FLAG_IMMUTABLE
import android.content.Context
import android.content.Intent
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import com.hadiyarajesh.notex.MainActivity
import com.hadiyarajesh.notex.R
import kotlin.random.Random

class NotificationHelper {

fun createNotification(
context: Context,
notificationDTO: NotificationDTO
) {
val actionIntent = Intent(context, MainActivity::class.java)
val actionPendingIntent = PendingIntent.getActivity(
context, 0,
actionIntent, PendingIntent.FLAG_UPDATE_CURRENT or FLAG_IMMUTABLE
)

@SuppressWarnings("MagicNumber")
val notificationId = Random(121).nextInt(10000)

val postponeIntent = Intent(context, NotificationBroadCastReceiver::class.java).apply {
action = context.resources.getString(R.string.postpone_action)
putExtra(context.resources.getString(R.string.worker_tag), notificationDTO.workerTag)
putExtra(context.resources.getString(R.string.reminder_id), notificationDTO.reminderId)
putExtra(context.resources.getString(R.string.notification_id), notificationId)
}

val postponePendingIntent = PendingIntent.getBroadcast(
context, 0,
postponeIntent, PendingIntent.FLAG_UPDATE_CURRENT or FLAG_IMMUTABLE
)

val cancelIntent = Intent(context, NotificationBroadCastReceiver::class.java).apply {
action = context.resources.getString(R.string.done_action)
putExtra(context.resources.getString(R.string.worker_tag), notificationDTO.workerTag)
putExtra(context.resources.getString(R.string.notification_id), notificationId)
}

val cancelPendingIntent = PendingIntent.getBroadcast(
context, 0,
cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT or FLAG_IMMUTABLE
)

val builder =
NotificationCompat.Builder(context, context.getString(R.string.notification_channel_id))
.setSmallIcon(R.drawable.ic_note_filled)
.setContentTitle(notificationDTO.title)
.setContentText(notificationDTO.subTitle)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(actionPendingIntent)
.addAction(R.drawable.ic_note_filled, context.getString(R.string.one_hour), postponePendingIntent)
.addAction(R.drawable.ic_note_filled, context.getString(R.string.done), cancelPendingIntent)
.setAutoCancel(true)

with(NotificationManagerCompat.from(context)) {
// notificationId is a unique int for each notification that you must define
notify(notificationId, builder.build())
}
}
}
Loading

0 comments on commit e0a54c6

Please sign in to comment.