Skip to content

Commit

Permalink
fix(refresh): temp fix wallpaper refresh task
Browse files Browse the repository at this point in the history
  • Loading branch information
libhide committed Jun 10, 2024
1 parent 6a891e4 commit 8c26e84
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 35 deletions.
18 changes: 17 additions & 1 deletion app/src/main/kotlin/com/ratik/uttam/UttamApp.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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() {
Expand All @@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 })

Expand All @@ -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),
Expand All @@ -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]
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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)) {
Expand Down

0 comments on commit 8c26e84

Please sign in to comment.