-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from thecraftman/add-m1-support
Add M1 Mac support & Optimize app startup by deferring initialization of non-critical components
- Loading branch information
Showing
6 changed files
with
146 additions
and
122 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
app/src/main/java/com/mayokunadeniyi/instantweather/App.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// App.kt | ||
override fun onCreate() { | ||
super.onCreate() | ||
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { | ||
val deferredComponentInitializer = DeferredComponentInitializer(this) | ||
deferredComponentInitializer.initialize() | ||
} else { | ||
initializeComponents() | ||
} | ||
} | ||
|
||
private fun initializeComponents() { | ||
initWorkManager() | ||
initCrashlytics() | ||
initAnalytics() | ||
} | ||
|
||
// DeferredComponentInitializer.kt | ||
class DeferredComponentInitializer(private val app: Application) { | ||
|
||
fun initialize() { | ||
val componentInitializer = ComponentInitializer(app) | ||
val initializeMessage = when { | ||
isColdStart() -> "Deferred initialization from cold start" | ||
else -> "Deferred initialization from warm start" | ||
} | ||
WorkManager.getInstance(app) | ||
.beginUniqueWork( | ||
"DeferredInitialization", | ||
ExistingWorkPolicy.KEEP, | ||
OneTimeWorkRequestBuilder<DeferredWorker>() | ||
.setInputData(workDataOf(DeferredWorker.KEY_INITIALIZE to initializeMessage)) | ||
.build() | ||
) | ||
.enqueue() | ||
} | ||
|
||
private fun isColdStart(): Boolean { | ||
return ProcessLifecycleOwner.get().lifecycle.currentState == Lifecycle.State.INITIALIZED | ||
} | ||
|
||
private class DeferredWorker( | ||
context: Context, | ||
workerParams: WorkerParameters | ||
) : Worker(context, workerParams) { | ||
override fun doWork(): Result { | ||
val initializeMessage = inputData.getString(KEY_INITIALIZE) | ||
initializeMessage?.let { | ||
Log.d("DeferredInit", it) | ||
} | ||
ComponentInitializer(applicationContext).initialize() | ||
return Result.success() | ||
} | ||
|
||
companion object { | ||
const val KEY_INITIALIZE = "KEY_INITIALIZE" | ||
} | ||
} | ||
|
||
private class ComponentInitializer(private val app: Application) { | ||
fun initialize() { | ||
initCrashlytics() | ||
initAnalytics() | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
.../main/java/com/mayokunadeniyi/instantweather/initializers/DeferredComponentInitializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.mayokunadeniyi.instantweather.initializers | ||
|
||
import android.app.Application | ||
import android.content.Context | ||
import android.os.Build | ||
import android.util.Log | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.lifecycle.ProcessLifecycleOwner | ||
import androidx.work.ExistingWorkPolicy | ||
import androidx.work.OneTimeWorkRequestBuilder | ||
import androidx.work.WorkManager | ||
import androidx.work.Worker | ||
import androidx.work.WorkerParameters | ||
import androidx.work.workDataOf | ||
|
||
class DeferredComponentInitializer(private val app: Application) { | ||
|
||
fun initialize() { | ||
val componentInitializer = ComponentInitializer(app) | ||
val initializeMessage = when { | ||
isColdStart() -> "Deferred initialization from cold start" | ||
else -> "Deferred initialization from warm start" | ||
} | ||
WorkManager.getInstance(app) | ||
.beginUniqueWork( | ||
"DeferredInitialization", | ||
ExistingWorkPolicy.KEEP, | ||
OneTimeWorkRequestBuilder<DeferredWorker>() | ||
.setInputData(workDataOf(DeferredWorker.KEY_INITIALIZE to initializeMessage)) | ||
.build() | ||
) | ||
.enqueue() | ||
} | ||
|
||
private fun isColdStart(): Boolean { | ||
return ProcessLifecycleOwner.get().lifecycle.currentState == Lifecycle.State.INITIALIZED | ||
} | ||
|
||
private class DeferredWorker( | ||
context: Context, | ||
workerParams: WorkerParameters | ||
) : Worker(context, workerParams) { | ||
override fun doWork(): Result { | ||
val initializeMessage = inputData.getString(KEY_INITIALIZE) | ||
initializeMessage?.let { | ||
Log.d("DeferredInit", it) | ||
} | ||
ComponentInitializer(applicationContext).initialize() | ||
return Result.success() | ||
} | ||
|
||
companion object { | ||
const val KEY_INITIALIZE = "KEY_INITIALIZE" | ||
} | ||
} | ||
|
||
private class ComponentInitializer(private val app: Application) { | ||
fun initialize() { | ||
initCrashlytics() | ||
initAnalytics() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters