Skip to content

Commit

Permalink
Worker class for periodic notification (#10)
Browse files Browse the repository at this point in the history
* Work Manager Dependencies added

* Notification feature added, fixed #9

* Author name added to notification title and time range increased to 12 hours

* new app icon added

* minor UI changes at themes, toast and removed unwanted logs

Co-authored-by: Niket-Jain <52085669+Niket-Jain@users.noreply.github.com>
  • Loading branch information
nikeight and nikeight authored Aug 3, 2021
1 parent 10afc90 commit 2ab45b6
Show file tree
Hide file tree
Showing 76 changed files with 163 additions and 19 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ dependencies {
kapt(Hilt.daggerCompiler)
kapt(Hilt.hiltCompiler)

// Work Manager
implementation(WorkManager.workManager)

// Testing
testImplementation(Testing.core)
testImplementation(Testing.coroutines)
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<application
android:name=".application.MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:icon="@mipmap/ic_quoty_logo"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:roundIcon="@mipmap/ic_quoty_logo_round"
android:supportsRtl="true"
android:theme="@style/Theme.Quoty">
<activity android:name=".main.ui.AllQuotesActivity"></activity>
Expand Down
Binary file added app/src/main/ic_quoty_logo-playstore.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ class AllQuotesActivity : BaseActivity<MainViewModel, ActivityAllQuotesBinding>(
ItemTouchHelper.LEFT -> {
val quote = mAdapter.currentList[viewHolder.adapterPosition]
mViewModel.deleteQuote(quote)
Toast.makeText(this@AllQuotesActivity,"Quote deleted from db",Toast.LENGTH_SHORT).show()
Toast.makeText(this@AllQuotesActivity,"Quote deleted",Toast.LENGTH_SHORT).show()
}

ItemTouchHelper.RIGHT -> {
val quote = mAdapter.currentList[viewHolder.adapterPosition]
quote.isFavorite = true
mViewModel.updateQuote(quote)
mAdapter.notifyItemChanged(viewHolder.adapterPosition)
Toast.makeText(this@AllQuotesActivity,"Quote updated successfully",Toast.LENGTH_SHORT).show()
Toast.makeText(this@AllQuotesActivity,"Quote updated",Toast.LENGTH_SHORT).show()
}
}
}
Expand Down
42 changes: 34 additions & 8 deletions app/src/main/java/com/appchefs/quoty/main/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.content.ContextCompat
import androidx.lifecycle.Observer
import androidx.lifecycle.lifecycleScope
import androidx.work.*
import com.appchefs.quoty.R
import com.appchefs.quoty.databinding.ActivityMainBinding
import com.appchefs.quoty.main.base.BaseActivity
import com.appchefs.quoty.main.viewmodel.MainViewModel
import com.appchefs.quoty.utils.NetworkUtils
import com.appchefs.quoty.utils.Status
import com.appchefs.quoty.worker.NotificationWorker
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.ExperimentalCoroutinesApi
import java.util.concurrent.TimeUnit

@ExperimentalCoroutinesApi
@AndroidEntryPoint
Expand All @@ -36,9 +40,33 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
setContentView(mViewBinding.root)
clickEvents()
setupObservers()
Log.i(TAG,"On created Called")
Log.i(TAG, "On created Called")
}

private fun startNotificationWorK() {
val constraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()

val periodicNotificationWorkRequest =
PeriodicWorkRequestBuilder<NotificationWorker>(12, TimeUnit.HOURS)
.setConstraints(constraints)
.build()

WorkManager.getInstance(this)
.enqueue(periodicNotificationWorkRequest)

WorkManager.getInstance(this).getWorkInfoByIdLiveData(periodicNotificationWorkRequest.id)
.observe(this, { workInfo ->
if (workInfo != null && workInfo.state == WorkInfo.State.SUCCEEDED) {
Log.i("WorkStatus", "Success")
} else {
Log.i("WorkStatus", "Error")
}
})
}


private fun setupObservers() {
getRandomQuoteObserver()
getQuoteObserver()
Expand All @@ -49,19 +77,17 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
override fun onStart() {
super.onStart()
networkCheck()
Log.i(TAG,"On onStart Called")
startNotificationWorK()
}

override fun onResume() {
super.onResume()
loadRandomQuoteByDefault()
Log.i(TAG,"On Resumed Called")
}

private fun loadRandomQuoteByDefault(){
private fun loadRandomQuoteByDefault() {
mViewBinding.btnToggleGroup.check(R.id.btn_random)
mViewModel.getRandomQuote()
Log.i(TAG,"loadRandomQuoteByDefault called")
}

private fun networkCheck() {
Expand All @@ -79,7 +105,7 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
)
}
} else {
if (mViewModel.randomQuote.value is Status.Error){
if (mViewModel.randomQuote.value is Status.Error) {
loadRandomQuoteByDefault()
mViewBinding.btnToggleGroup.check(R.id.btn_random)
}
Expand Down Expand Up @@ -149,8 +175,8 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
showToast(state.message)
}
is Status.Success -> {
mViewBinding.tvQuote.text = state.data?.quoteContent ?: "loading"
mViewBinding.tvAuthor.text = state.data?.author ?: "..."
mViewBinding.tvQuote.text = state.data?.quoteContent ?: "Loading"
mViewBinding.tvAuthor.text = state.data?.author ?: "..."
}
is Status.Loading -> {
mViewBinding.tvQuote.text = getString(R.string.toast_msg_loading)
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/java/com/appchefs/quoty/utils/Constants.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.appchefs.quoty.utils

object Constants {

// Notifications
const val QUOTE_NOTIFICATION_CHANNEL = "quote_channel"
const val QUOTE_NOTIFICATION_ID = 123
const val QUOTE_NOTIFICATION_NAME = "quote_notification"
}
77 changes: 77 additions & 0 deletions app/src/main/java/com/appchefs/quoty/worker/NotificationWorker.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.appchefs.quoty.worker

import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters
import com.appchefs.quoty.R
import com.appchefs.quoty.data.remote.NetworkService
import com.appchefs.quoty.data.remote.Networking
import com.appchefs.quoty.utils.Constants
import javax.inject.Inject

class NotificationWorker(
context: Context,
workerParameters: WorkerParameters
) : CoroutineWorker(context, workerParameters) {

lateinit var networkService: NetworkService

override suspend fun doWork(): Result {

networkService = Networking.create()
val response = networkService.getRandomQuotes()

return if (response.isSuccessful && response.body() != null) {
val quoteContent = response.body()?.quoteContent
val author = response.body()?.author
sendNotification(quoteContent!!,author!!)
Log.i("WorkManager", "Success")
Result.success()
} else {
Log.i("WorkManager", "Error")
Result.failure()
}
}

private fun sendNotification(quoteContent: String, author: String) {

val notificationManager =
applicationContext
.getSystemService(Context.NOTIFICATION_SERVICE)
as NotificationManager

val notificationTitle = "Quote of the day by $author"

val notification = NotificationCompat.Builder(
applicationContext,
Constants.QUOTE_NOTIFICATION_CHANNEL
).setContentTitle(notificationTitle)
.setContentText(quoteContent)
.setSmallIcon(R.drawable.ic_quote_vector)
.setStyle(NotificationCompat.BigTextStyle())
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notification.setChannelId(Constants.QUOTE_NOTIFICATION_CHANNEL)

val quoteChannel = NotificationChannel(
Constants.QUOTE_NOTIFICATION_CHANNEL,
Constants.QUOTE_NOTIFICATION_NAME,
NotificationManager.IMPORTANCE_HIGH
)

notificationManager.createNotificationChannel(quoteChannel)
}

notificationManager.notify(Constants.QUOTE_NOTIFICATION_ID, notification.build())
}
}


10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_quote_vector.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="M6,17h3l2,-4L11,7L5,7v6h3zM14,17h3l2,-4L19,7h-6v6h3z"/>
</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/mipmap-anydpi-v26/ic_quoty_logo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@color/ic_quoty_logo_background"/>
<foreground android:drawable="@mipmap/ic_quoty_logo_foreground"/>
</adaptive-icon>
5 changes: 5 additions & 0 deletions app/src/main/res/mipmap-anydpi-v26/ic_quoty_logo_round.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@color/ic_quoty_logo_background"/>
<foreground android:drawable="@mipmap/ic_quoty_logo_foreground"/>
</adaptive-icon>
Binary file added app/src/main/res/mipmap-hdpi/ic_quoty_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/mipmap-mdpi/ic_quoty_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/mipmap-xhdpi/ic_quoty_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/mipmap-xxhdpi/ic_quoty_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/mipmap-xxxhdpi/ic_quoty_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 4 additions & 2 deletions app/src/main/res/values-night/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
<item name="colorPrimaryVariant">@color/purple_500</item>
<item name="colorOnPrimary">@color/white</item>

<!-- Fonts family -->
<item name="fontFamily">@font/mali_light</item>
<!-- Fonts family -->
<item name="fontFamily">@font/mali_regular</item>
<item name="drawableTint">@color/white</item>

<!-- Secondary brand color. -->
<item name="colorSecondary">@color/purple_200</item>
Expand All @@ -17,4 +18,5 @@
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>

</resources>
4 changes: 4 additions & 0 deletions app/src/main/res/values/ic_quoty_logo_background.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="ic_quoty_logo_background">#FFFFFF</color>
</resources>
5 changes: 2 additions & 3 deletions app/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
<item name="colorOnPrimary">@color/black</item>

<!-- Fonts family -->
<item name="fontFamily">@font/mali_semi_bold</item>
<item name="fontFamily">@font/mali_regular</item>
<item name="drawableTint">@color/black</item>

<!-- Secondary brand color. -->
<item name="colorSecondary">@color/purple_500</item>
Expand All @@ -18,6 +19,4 @@
<!-- Customize your theme here. -->
</style>



</resources>
Binary file modified buildSrc/build/classes/kotlin/main/WorkManager.class
Binary file not shown.
Binary file modified buildSrc/build/kotlin/compileKotlin/build-history.bin
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
2
1
4
3
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified buildSrc/build/kotlin/compileKotlin/last-build.bin
Binary file not shown.
Binary file modified buildSrc/build/libs/buildSrc.jar
Binary file not shown.
4 changes: 4 additions & 0 deletions buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,7 @@ object Room {
const val ktx = "androidx.room:room-ktx:2.2.6"
const val runtime = "androidx.room:room-runtime:2.2.6"
}

object WorkManager{
const val workManager = "androidx.work:work-runtime-ktx:2.2.0"
}

0 comments on commit 2ab45b6

Please sign in to comment.