Skip to content

Commit 2ab45b6

Browse files
authored
Worker class for periodic notification (#10)
* 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>
1 parent 10afc90 commit 2ab45b6

76 files changed

Lines changed: 163 additions & 19 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

app/build.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ dependencies {
111111
kapt(Hilt.daggerCompiler)
112112
kapt(Hilt.hiltCompiler)
113113

114+
// Work Manager
115+
implementation(WorkManager.workManager)
116+
114117
// Testing
115118
testImplementation(Testing.core)
116119
testImplementation(Testing.coroutines)

app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
<application
66
android:name=".application.MyApplication"
77
android:allowBackup="true"
8-
android:icon="@mipmap/ic_launcher"
8+
android:icon="@mipmap/ic_quoty_logo"
99
android:label="@string/app_name"
10-
android:roundIcon="@mipmap/ic_launcher_round"
10+
android:roundIcon="@mipmap/ic_quoty_logo_round"
1111
android:supportsRtl="true"
1212
android:theme="@style/Theme.Quoty">
1313
<activity android:name=".main.ui.AllQuotesActivity"></activity>
23.6 KB

app/src/main/java/com/appchefs/quoty/main/ui/AllQuotesActivity.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,15 @@ class AllQuotesActivity : BaseActivity<MainViewModel, ActivityAllQuotesBinding>(
5757
ItemTouchHelper.LEFT -> {
5858
val quote = mAdapter.currentList[viewHolder.adapterPosition]
5959
mViewModel.deleteQuote(quote)
60-
Toast.makeText(this@AllQuotesActivity,"Quote deleted from db",Toast.LENGTH_SHORT).show()
60+
Toast.makeText(this@AllQuotesActivity,"Quote deleted",Toast.LENGTH_SHORT).show()
6161
}
6262

6363
ItemTouchHelper.RIGHT -> {
6464
val quote = mAdapter.currentList[viewHolder.adapterPosition]
6565
quote.isFavorite = true
6666
mViewModel.updateQuote(quote)
6767
mAdapter.notifyItemChanged(viewHolder.adapterPosition)
68-
Toast.makeText(this@AllQuotesActivity,"Quote updated successfully",Toast.LENGTH_SHORT).show()
68+
Toast.makeText(this@AllQuotesActivity,"Quote updated",Toast.LENGTH_SHORT).show()
6969
}
7070
}
7171
}

app/src/main/java/com/appchefs/quoty/main/ui/MainActivity.kt

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@ import androidx.activity.viewModels
1414
import androidx.appcompat.app.AppCompatDelegate
1515
import androidx.core.content.ContextCompat
1616
import androidx.lifecycle.Observer
17+
import androidx.lifecycle.lifecycleScope
18+
import androidx.work.*
1719
import com.appchefs.quoty.R
1820
import com.appchefs.quoty.databinding.ActivityMainBinding
1921
import com.appchefs.quoty.main.base.BaseActivity
2022
import com.appchefs.quoty.main.viewmodel.MainViewModel
2123
import com.appchefs.quoty.utils.NetworkUtils
2224
import com.appchefs.quoty.utils.Status
25+
import com.appchefs.quoty.worker.NotificationWorker
2326
import dagger.hilt.android.AndroidEntryPoint
2427
import kotlinx.coroutines.ExperimentalCoroutinesApi
28+
import java.util.concurrent.TimeUnit
2529

2630
@ExperimentalCoroutinesApi
2731
@AndroidEntryPoint
@@ -36,9 +40,33 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
3640
setContentView(mViewBinding.root)
3741
clickEvents()
3842
setupObservers()
39-
Log.i(TAG,"On created Called")
43+
Log.i(TAG, "On created Called")
4044
}
4145

46+
private fun startNotificationWorK() {
47+
val constraints = Constraints.Builder()
48+
.setRequiredNetworkType(NetworkType.CONNECTED)
49+
.build()
50+
51+
val periodicNotificationWorkRequest =
52+
PeriodicWorkRequestBuilder<NotificationWorker>(12, TimeUnit.HOURS)
53+
.setConstraints(constraints)
54+
.build()
55+
56+
WorkManager.getInstance(this)
57+
.enqueue(periodicNotificationWorkRequest)
58+
59+
WorkManager.getInstance(this).getWorkInfoByIdLiveData(periodicNotificationWorkRequest.id)
60+
.observe(this, { workInfo ->
61+
if (workInfo != null && workInfo.state == WorkInfo.State.SUCCEEDED) {
62+
Log.i("WorkStatus", "Success")
63+
} else {
64+
Log.i("WorkStatus", "Error")
65+
}
66+
})
67+
}
68+
69+
4270
private fun setupObservers() {
4371
getRandomQuoteObserver()
4472
getQuoteObserver()
@@ -49,19 +77,17 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
4977
override fun onStart() {
5078
super.onStart()
5179
networkCheck()
52-
Log.i(TAG,"On onStart Called")
80+
startNotificationWorK()
5381
}
5482

5583
override fun onResume() {
5684
super.onResume()
5785
loadRandomQuoteByDefault()
58-
Log.i(TAG,"On Resumed Called")
5986
}
6087

61-
private fun loadRandomQuoteByDefault(){
88+
private fun loadRandomQuoteByDefault() {
6289
mViewBinding.btnToggleGroup.check(R.id.btn_random)
6390
mViewModel.getRandomQuote()
64-
Log.i(TAG,"loadRandomQuoteByDefault called")
6591
}
6692

6793
private fun networkCheck() {
@@ -79,7 +105,7 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
79105
)
80106
}
81107
} else {
82-
if (mViewModel.randomQuote.value is Status.Error){
108+
if (mViewModel.randomQuote.value is Status.Error) {
83109
loadRandomQuoteByDefault()
84110
mViewBinding.btnToggleGroup.check(R.id.btn_random)
85111
}
@@ -149,8 +175,8 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
149175
showToast(state.message)
150176
}
151177
is Status.Success -> {
152-
mViewBinding.tvQuote.text = state.data?.quoteContent ?: "loading"
153-
mViewBinding.tvAuthor.text = state.data?.author ?: "..."
178+
mViewBinding.tvQuote.text = state.data?.quoteContent ?: "Loading"
179+
mViewBinding.tvAuthor.text = state.data?.author ?: "..."
154180
}
155181
is Status.Loading -> {
156182
mViewBinding.tvQuote.text = getString(R.string.toast_msg_loading)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.appchefs.quoty.utils
2+
3+
object Constants {
4+
5+
// Notifications
6+
const val QUOTE_NOTIFICATION_CHANNEL = "quote_channel"
7+
const val QUOTE_NOTIFICATION_ID = 123
8+
const val QUOTE_NOTIFICATION_NAME = "quote_notification"
9+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.appchefs.quoty.worker
2+
3+
import android.app.NotificationChannel
4+
import android.app.NotificationManager
5+
import android.content.Context
6+
import android.os.Build
7+
import android.util.Log
8+
import androidx.core.app.NotificationCompat
9+
import androidx.work.CoroutineWorker
10+
import androidx.work.WorkerParameters
11+
import com.appchefs.quoty.R
12+
import com.appchefs.quoty.data.remote.NetworkService
13+
import com.appchefs.quoty.data.remote.Networking
14+
import com.appchefs.quoty.utils.Constants
15+
import javax.inject.Inject
16+
17+
class NotificationWorker(
18+
context: Context,
19+
workerParameters: WorkerParameters
20+
) : CoroutineWorker(context, workerParameters) {
21+
22+
lateinit var networkService: NetworkService
23+
24+
override suspend fun doWork(): Result {
25+
26+
networkService = Networking.create()
27+
val response = networkService.getRandomQuotes()
28+
29+
return if (response.isSuccessful && response.body() != null) {
30+
val quoteContent = response.body()?.quoteContent
31+
val author = response.body()?.author
32+
sendNotification(quoteContent!!,author!!)
33+
Log.i("WorkManager", "Success")
34+
Result.success()
35+
} else {
36+
Log.i("WorkManager", "Error")
37+
Result.failure()
38+
}
39+
}
40+
41+
private fun sendNotification(quoteContent: String, author: String) {
42+
43+
val notificationManager =
44+
applicationContext
45+
.getSystemService(Context.NOTIFICATION_SERVICE)
46+
as NotificationManager
47+
48+
val notificationTitle = "Quote of the day by $author"
49+
50+
val notification = NotificationCompat.Builder(
51+
applicationContext,
52+
Constants.QUOTE_NOTIFICATION_CHANNEL
53+
).setContentTitle(notificationTitle)
54+
.setContentText(quoteContent)
55+
.setSmallIcon(R.drawable.ic_quote_vector)
56+
.setStyle(NotificationCompat.BigTextStyle())
57+
.setDefaults(NotificationCompat.DEFAULT_ALL)
58+
.setPriority(NotificationCompat.PRIORITY_HIGH)
59+
.setAutoCancel(true)
60+
61+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
62+
notification.setChannelId(Constants.QUOTE_NOTIFICATION_CHANNEL)
63+
64+
val quoteChannel = NotificationChannel(
65+
Constants.QUOTE_NOTIFICATION_CHANNEL,
66+
Constants.QUOTE_NOTIFICATION_NAME,
67+
NotificationManager.IMPORTANCE_HIGH
68+
)
69+
70+
notificationManager.createNotificationChannel(quoteChannel)
71+
}
72+
73+
notificationManager.notify(Constants.QUOTE_NOTIFICATION_ID, notification.build())
74+
}
75+
}
76+
77+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportWidth="24"
5+
android:viewportHeight="24"
6+
android:tint="?attr/colorControlNormal">
7+
<path
8+
android:fillColor="@android:color/white"
9+
android:pathData="M6,17h3l2,-4L11,7L5,7v6h3zM14,17h3l2,-4L19,7h-6v6h3z"/>
10+
</vector>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
3+
<background android:drawable="@color/ic_quoty_logo_background"/>
4+
<foreground android:drawable="@mipmap/ic_quoty_logo_foreground"/>
5+
</adaptive-icon>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
3+
<background android:drawable="@color/ic_quoty_logo_background"/>
4+
<foreground android:drawable="@mipmap/ic_quoty_logo_foreground"/>
5+
</adaptive-icon>

0 commit comments

Comments
 (0)