Skip to content

Commit

Permalink
Merge pull request #476 from joeloewi7178/hotfix-zzz
Browse files Browse the repository at this point in the history
Hotfix zzz
  • Loading branch information
joeloewi7178 authored Jul 9, 2024
2 parents 6975f79 + ab2241d commit d948fad
Show file tree
Hide file tree
Showing 15 changed files with 180 additions and 72 deletions.
8 changes: 8 additions & 0 deletions .idea/file.template.settings.xml

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

16 changes: 16 additions & 0 deletions .idea/fileTemplates/includes/File Header.java

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

4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ android {

defaultConfig {
applicationId = "com.joeloewi.croissant"
versionCode = 62
versionName = "1.3.1"
versionCode = 63
versionName = "1.3.2"
targetSdk = 34

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
Expand Down
44 changes: 16 additions & 28 deletions app/src/main/kotlin/com/joeloewi/croissant/di/EntryPoints.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,30 @@
package com.joeloewi.croissant.di

import android.content.Context
import androidx.hilt.work.HiltWorkerFactory
import coil.ImageLoader
import com.joeloewi.croissant.data.di.DefaultDispatcherExecutor
import com.joeloewi.croissant.util.NotificationGenerator
import com.joeloewi.croissant.initializer.CoilInitializer
import com.joeloewi.croissant.initializer.NotificationChannelInitializer
import com.joeloewi.croissant.initializer.WorkManagerInitializer
import dagger.hilt.EntryPoint
import dagger.hilt.InstallIn
import dagger.hilt.android.EntryPointAccessors
import dagger.hilt.components.SingletonComponent
import java.util.concurrent.Executor
import kotlin.reflect.KClass

@EntryPoint
@InstallIn(SingletonComponent::class)
interface InitializerEntryPoint {
fun imageLoader(): ImageLoader
fun hiltWorkerFactory(): HiltWorkerFactory
fun injectCoilInitializer(coilInitializer: CoilInitializer)
fun injectNotificationChannelInitializer(initializer: NotificationChannelInitializer)
fun injectWorkManagerInitializer(workManagerInitializer: WorkManagerInitializer)

@DefaultDispatcherExecutor
fun executor(): Executor
fun notificationGenerator(): NotificationGenerator
}

inline fun <reified EntryPoint : Any> Context.entryPoints(): Lazy<EntryPoint> = EntryPointLazy(
entryPointInterface = EntryPoint::class,
context = this
)

class EntryPointLazy<EntryPoint : Any>(
private val entryPointInterface: KClass<EntryPoint>,
private val context: Context
) : Lazy<EntryPoint> {
private var cached: EntryPoint? = null
override val value: EntryPoint
get() = EntryPointAccessors.fromApplication(context, entryPointInterface.java).also {
cached = it
companion object {
fun resolve(context: Context): InitializerEntryPoint {
val appContext = context.applicationContext ?: throw IllegalStateException(
"applicationContext was not found in InitializerEntryPoint",
)
return EntryPointAccessors.fromApplication(
appContext,
InitializerEntryPoint::class.java,
)
}

override fun isInitialized(): Boolean = cached != null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ import coil.Coil
import coil.ImageLoader
import coil.imageLoader
import com.joeloewi.croissant.di.InitializerEntryPoint
import com.joeloewi.croissant.di.entryPoints
import javax.inject.Inject

class CoilInitializer : Initializer<ImageLoader> {

@set:Inject
internal lateinit var imageLoader: ImageLoader

override fun create(context: Context): ImageLoader {
val initializerEntryPoint: InitializerEntryPoint by context.entryPoints()
val imageLoader = initializerEntryPoint.imageLoader()
InitializerEntryPoint.resolve(context).injectCoilInitializer(this)

Coil.setImageLoader { imageLoader }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package com.joeloewi.croissant.initializer
import android.content.Context
import androidx.startup.Initializer
import com.joeloewi.croissant.di.InitializerEntryPoint
import com.joeloewi.croissant.di.entryPoints
import com.joeloewi.croissant.util.NotificationGenerator
import javax.inject.Inject

class NotificationChannelInitializer : Initializer<Unit> {

@set:Inject
internal lateinit var notificationGenerator: NotificationGenerator

override fun create(context: Context) {
val initializerEntryPoint: InitializerEntryPoint by context.entryPoints()
val notificationGenerator = initializerEntryPoint.notificationGenerator()
InitializerEntryPoint.resolve(context).injectNotificationChannelInitializer(this)

notificationGenerator.createNotificationChannels()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,26 @@
package com.joeloewi.croissant.initializer

import android.content.Context
import androidx.hilt.work.HiltWorkerFactory
import androidx.startup.Initializer
import androidx.work.Configuration
import androidx.work.WorkManager
import com.joeloewi.croissant.data.di.DefaultDispatcherExecutor
import com.joeloewi.croissant.di.InitializerEntryPoint
import com.joeloewi.croissant.di.entryPoints
import java.util.concurrent.Executor
import javax.inject.Inject

class WorkManagerInitializer : Initializer<WorkManager> {

@set:Inject
internal lateinit var hiltWorkerFactory: HiltWorkerFactory

@Inject
@DefaultDispatcherExecutor
lateinit var executor: Executor

override fun create(context: Context): WorkManager {
val initializerEntryPoint: InitializerEntryPoint by context.entryPoints()
val hiltWorkerFactory = initializerEntryPoint.hiltWorkerFactory()
val executor = initializerEntryPoint.executor()
InitializerEntryPoint.resolve(context).injectWorkManagerInitializer(this)

WorkManager.initialize(
context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import android.app.Application
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import androidx.lifecycle.ProcessLifecycleOwner
import androidx.lifecycle.lifecycleScope
import android.util.Log
import androidx.work.ExistingWorkPolicy
import androidx.work.WorkManager
import com.google.firebase.Firebase
Expand All @@ -17,13 +16,13 @@ import com.joeloewi.croissant.util.AlarmScheduler
import com.joeloewi.croissant.worker.AttendCheckInEventWorker
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import javax.inject.Inject

@AndroidEntryPoint
class AlarmReceiver : BroadcastReceiver() {
private val _processLifecycleScope = ProcessLifecycleOwner.get().lifecycleScope
private val _coroutineContext = Dispatchers.IO + CoroutineExceptionHandler { _, throwable ->
Firebase.crashlytics.apply {
log(this@AlarmReceiver.javaClass.simpleName)
Expand All @@ -49,10 +48,15 @@ class AlarmReceiver : BroadcastReceiver() {
@Inject
lateinit var alarmScheduler: AlarmScheduler

@Inject
lateinit var coroutineScope: CoroutineScope

override fun onReceive(p0: Context, p1: Intent) {
Log.d("123123", p1.action.toString())

when (p1.action) {
Intent.ACTION_BOOT_COMPLETED, Intent.ACTION_MY_PACKAGE_REPLACED, AlarmManager.ACTION_SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED -> {
_processLifecycleScope.launch(_coroutineContext) {
coroutineScope.launch(_coroutineContext) {
getAllOneShotAttendanceUseCase().forEach { attendance ->
attendance.runCatching {
alarmScheduler.scheduleCheckInAlarm(
Expand All @@ -65,7 +69,7 @@ class AlarmReceiver : BroadcastReceiver() {
}

RECEIVE_ATTEND_CHECK_IN_ALARM -> {
_processLifecycleScope.launch(_coroutineContext) {
coroutineScope.launch(_coroutineContext) {
val attendanceId = p1.getLongExtra(ATTENDANCE_ID, Long.MIN_VALUE)
val attendanceWithGames = getOneAttendanceUseCase(attendanceId)
val attendance = attendanceWithGames.attendance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@ import android.app.Application
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import androidx.lifecycle.ProcessLifecycleOwner
import androidx.lifecycle.lifecycleScope
import androidx.work.WorkManager
import com.google.firebase.Firebase
import com.google.firebase.crashlytics.crashlytics
import com.joeloewi.croissant.domain.usecase.AttendanceUseCase
import com.joeloewi.croissant.domain.usecase.ResinStatusWidgetUseCase
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import javax.inject.Inject

@AndroidEntryPoint
class MigrationHelper : BroadcastReceiver() {
private val _processLifecycleScope = ProcessLifecycleOwner.get().lifecycleScope
private val _coroutineContext = Dispatchers.IO + CoroutineExceptionHandler { _, throwable ->
Firebase.crashlytics.apply {
log(this@MigrationHelper.javaClass.simpleName)
Expand All @@ -42,10 +40,13 @@ class MigrationHelper : BroadcastReceiver() {
@Inject
lateinit var workManager: WorkManager

@Inject
lateinit var coroutineScope: CoroutineScope

override fun onReceive(p0: Context, p1: Intent) {
when (p1.action) {
Intent.ACTION_MY_PACKAGE_REPLACED -> {
_processLifecycleScope.launch(_coroutineContext) {
coroutineScope.launch(_coroutineContext) {
//because work manager's job can be deferred, cancel check in event worker
//instead of work manager, use alarm manager
getAllOneShotAttendanceUseCase().forEach { attendance ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProvider
import android.content.Context
import android.os.PowerManager
import androidx.lifecycle.ProcessLifecycleOwner
import androidx.lifecycle.lifecycleScope
import androidx.work.ExistingWorkPolicy
import androidx.work.WorkManager
import com.google.firebase.Firebase
Expand All @@ -17,6 +15,7 @@ import com.joeloewi.croissant.worker.RefreshResinStatusWorker
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.async
Expand All @@ -26,7 +25,6 @@ import javax.inject.Inject

@AndroidEntryPoint
class ResinStatusWidgetProvider : AppWidgetProvider() {
private val _processLifecycleScope = ProcessLifecycleOwner.get().lifecycleScope
private val _coroutineContext = Dispatchers.IO + CoroutineExceptionHandler { _, throwable ->
Firebase.crashlytics.apply {
log(this@ResinStatusWidgetProvider.javaClass.simpleName)
Expand All @@ -46,13 +44,16 @@ class ResinStatusWidgetProvider : AppWidgetProvider() {
@Inject
lateinit var workManager: WorkManager

@Inject
lateinit var coroutineScope: CoroutineScope

override fun onUpdate(
context: Context,
appWidgetManager: AppWidgetManager,
appWidgetIds: IntArray
) {
//this method also called when user put widget on home screen
_processLifecycleScope.launch(_coroutineContext) {
coroutineScope.launch(_coroutineContext) {
appWidgetIds.map { appWidgetId ->
async(SupervisorJob() + Dispatchers.IO + CoroutineExceptionHandler { _, _ -> }) {
if (powerManager.isPowerSaveMode && !powerManager.isIgnoringBatteryOptimizationsCompat(
Expand Down Expand Up @@ -89,7 +90,7 @@ class ResinStatusWidgetProvider : AppWidgetProvider() {
}

override fun onDeleted(context: Context, appWidgetIds: IntArray) {
_processLifecycleScope.launch(_coroutineContext) {
coroutineScope.launch(_coroutineContext) {
appWidgetIds.run {
map { appWidgetId ->
async(SupervisorJob() + Dispatchers.IO + CoroutineExceptionHandler { _, _ -> }) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package com.joeloewi.croissant.receiver
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import androidx.lifecycle.ProcessLifecycleOwner
import androidx.lifecycle.lifecycleScope
import com.google.firebase.Firebase
import com.google.firebase.crashlytics.crashlytics
import com.joeloewi.croissant.util.NotificationGenerator
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.UUID
Expand All @@ -18,7 +17,6 @@ import javax.inject.Inject
@AndroidEntryPoint
class TimeZoneChangedReceiver @Inject constructor(
) : BroadcastReceiver() {
private val _processLifecycleScope = ProcessLifecycleOwner.get().lifecycleScope
private val _coroutineContext = Dispatchers.IO + CoroutineExceptionHandler { _, throwable ->
Firebase.crashlytics.apply {
log(this@TimeZoneChangedReceiver.javaClass.simpleName)
Expand All @@ -29,10 +27,13 @@ class TimeZoneChangedReceiver @Inject constructor(
@Inject
lateinit var notificationGenerator: NotificationGenerator

@Inject
lateinit var coroutineScope: CoroutineScope

override fun onReceive(context: Context, intent: Intent) {
when (intent.action) {
Intent.ACTION_TIMEZONE_CHANGED -> {
_processLifecycleScope.launch(_coroutineContext) {
coroutineScope.launch(_coroutineContext) {
with(notificationGenerator) {
safeNotify(
UUID.randomUUID().toString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ import com.skydoves.sandwich.ApiResponse
import retrofit2.http.Body
import retrofit2.http.Header
import retrofit2.http.POST
import retrofit2.http.Query
import java.util.Locale

interface GenshinImpactCheckInService {

@POST("event/sol/sign")
suspend fun attend(
@Query("lang") language: String = Locale.getDefault().toLanguageTag().lowercase(),
@Header("Cookie") cookie: String,
@Body params: Map<String, String> = mapOf(
"act_id" to "e202102251931481",
"lang" to Locale.getDefault().toLanguageTag().lowercase()
)
@Body params: Map<String, String> = mapOf("act_id" to "e202102251931481")
): ApiResponse<AttendanceResponse>
}
Loading

0 comments on commit d948fad

Please sign in to comment.