From 8c26e84a8b3e834b74ae52c3dbbdaa9cab0ece12 Mon Sep 17 00:00:00 2001 From: Ratik Sharma Date: Mon, 10 Jun 2024 18:13:20 +0530 Subject: [PATCH] fix(refresh): temp fix wallpaper refresh task --- .../main/kotlin/com/ratik/uttam/UttamApp.kt | 18 ++++- .../ratik/uttam/bg/RefreshWallpaperWorker.kt | 2 +- .../ui/feature/onboarding/OnboardingScreen.kt | 66 +++++++++---------- 3 files changed, 51 insertions(+), 35 deletions(-) diff --git a/app/src/main/kotlin/com/ratik/uttam/UttamApp.kt b/app/src/main/kotlin/com/ratik/uttam/UttamApp.kt index f037eb7d..541722d5 100644 --- a/app/src/main/kotlin/com/ratik/uttam/UttamApp.kt +++ b/app/src/main/kotlin/com/ratik/uttam/UttamApp.kt @@ -1,6 +1,10 @@ package com.ratik.uttam import android.app.Application +import android.app.NotificationChannel +import android.app.NotificationManager +import android.content.Context.NOTIFICATION_SERVICE +import android.os.Build import androidx.hilt.work.HiltWorkerFactory import androidx.work.Configuration import com.ratik.uttam.logging.ReleaseTree @@ -12,11 +16,13 @@ import javax.inject.Inject @HiltAndroidApp class UttamApp : Application(), Configuration.Provider { - @Inject lateinit var workerFactory: HiltWorkerFactory + @Inject + lateinit var workerFactory: HiltWorkerFactory override fun onCreate() { super.onCreate() initLogging() + createNotificationChannel() } private fun initLogging() { @@ -26,4 +32,14 @@ class UttamApp : Application(), Configuration.Provider { override val workManagerConfiguration: Configuration get() = Configuration.Builder().setWorkerFactory(workerFactory).build() + + private fun createNotificationChannel() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + val importance = NotificationManager.IMPORTANCE_DEFAULT + val channel = NotificationChannel("Uttam", "General", importance) + val notificationManager: NotificationManager = + getSystemService(NOTIFICATION_SERVICE) as NotificationManager + notificationManager.createNotificationChannel(channel) + } + } } diff --git a/app/src/main/kotlin/com/ratik/uttam/bg/RefreshWallpaperWorker.kt b/app/src/main/kotlin/com/ratik/uttam/bg/RefreshWallpaperWorker.kt index d9304d43..63eb8e08 100644 --- a/app/src/main/kotlin/com/ratik/uttam/bg/RefreshWallpaperWorker.kt +++ b/app/src/main/kotlin/com/ratik/uttam/bg/RefreshWallpaperWorker.kt @@ -33,7 +33,7 @@ constructor( var workResult = Result.failure() photoRepo.fetchRandomPhoto().collect { photo -> val builder = - NotificationCompat.Builder(appContext, "UttamChannel") + NotificationCompat.Builder(appContext, "Uttam") .setSmallIcon(R.drawable.ic_stat_uttam) .setContentTitle(appContext.getString(R.string.wallpaper_notif_title)) .setContentText( diff --git a/app/src/main/kotlin/com/ratik/uttam/ui/feature/onboarding/OnboardingScreen.kt b/app/src/main/kotlin/com/ratik/uttam/ui/feature/onboarding/OnboardingScreen.kt index 0aadfc9e..2bdf4112 100644 --- a/app/src/main/kotlin/com/ratik/uttam/ui/feature/onboarding/OnboardingScreen.kt +++ b/app/src/main/kotlin/com/ratik/uttam/ui/feature/onboarding/OnboardingScreen.kt @@ -89,8 +89,7 @@ internal fun OnboardingScreen( val context = LocalContext.current val displayMetrics = context.resources.displayMetrics - val state by - rememberFlowOnLifecycle(flow = viewModel.state).collectAsState(OnboardingState.initialState) + val state by rememberFlowOnLifecycle(flow = viewModel.state).collectAsState(OnboardingState.initialState) val pagerState = rememberPagerState(pageCount = { state.onboardingSteps.size }) @@ -111,13 +110,14 @@ internal fun OnboardingScreen( is Home -> navigateToHome() } } + else -> Ignored } } Column( - modifier = - Modifier.fillMaxSize() + modifier = Modifier + .fillMaxSize() .background(OnboardingBackground) .navigationBarsPadding() .padding(horizontal = SpacingLarge), @@ -136,25 +136,27 @@ internal fun OnboardingScreen( if (isLastPage) { UttamText.Body( text = "Done", - modifier = - Modifier.align(CenterEnd).padding(vertical = SpacingXXSmall).clickable { - // enqueueDailyRefreshRequest(context) + modifier = Modifier + .align(CenterEnd) + .padding(vertical = SpacingXXSmall) + .clickable { + enqueueDailyRefreshRequest(context) - viewModel.onViewAction( - FinishOnboarding( - deviceHeight = displayMetrics.heightPixels, - deviceWidth = displayMetrics.widthPixels, - ), - ) - }, + viewModel.onViewAction( + FinishOnboarding( + deviceHeight = displayMetrics.heightPixels, + deviceWidth = displayMetrics.widthPixels, + ), + ) + }, ) } else { Icon( painter = painterResource(id = R.drawable.ic_arrow_right), contentDescription = stringResource(R.string.content_desc_right_arrow), tint = White, - modifier = - Modifier.align(CenterEnd) + modifier = Modifier + .align(CenterEnd) .padding(vertical = SpacingXXXSmall, horizontal = SpacingSmall) .clickable { val currentStep = state.onboardingSteps[state.currentStepIndex] @@ -184,8 +186,8 @@ private fun PageIndicator(modifier: Modifier = Modifier, pagerState: PagerState) repeat(pagerState.pageCount) { iteration -> val color = if (pagerState.currentPage == iteration) Color.Black else ColorPrimaryVariant Box( - modifier = - Modifier.padding(SpacingXXXXXSmall) + modifier = Modifier + .padding(SpacingXXXXXSmall) .clip(CircleShape) .background(color) .size(IconXXXXSmall), @@ -240,33 +242,31 @@ private fun getOnboardingMessage(context: Context, onboardingStep: OnboardingSte } private fun enqueueDailyRefreshRequest(context: Context) { - val repeatInterval = TimeUnit.HOURS.toMillis(5) - val initialDelay = calculateInitialDelayToMorning() + val repeatInterval = TimeUnit.HOURS.toMillis(2) + val initialDelay = TimeUnit.HOURS.toMillis(2) val workRequest = PeriodicWorkRequest.Builder(RefreshWallpaperWorker::class.java, repeatInterval, MILLISECONDS) .setInitialDelay(initialDelay, MILLISECONDS) .build() - WorkManager.getInstance(context) - .enqueueUniquePeriodicWork( - RefreshWallpaperWorker::class.java.simpleName, - ExistingPeriodicWorkPolicy.UPDATE, - workRequest, - ) + WorkManager.getInstance(context).enqueueUniquePeriodicWork( + "RatikUttamRefresh", + ExistingPeriodicWorkPolicy.CANCEL_AND_REENQUEUE, + workRequest, + ) } private fun calculateInitialDelayToMorning(): Long { val now = Calendar.getInstance() // Set the target time to 7 AM today - val target = - Calendar.getInstance().apply { - set(Calendar.HOUR_OF_DAY, 7) - set(Calendar.MINUTE, 0) - set(Calendar.SECOND, 0) - set(Calendar.MILLISECOND, 0) - } + val target = Calendar.getInstance().apply { + set(Calendar.HOUR_OF_DAY, 7) + set(Calendar.MINUTE, 0) + set(Calendar.SECOND, 0) + set(Calendar.MILLISECOND, 0) + } // If the target time is before now, set it to 7 AM tomorrow if (target.before(now)) {