Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add M1 Mac support & Optimize app startup by deferring initialization of non-critical components #56

Merged
merged 8 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 0 additions & 111 deletions .gitignore

This file was deleted.

16 changes: 7 additions & 9 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,9 @@ android {
}
}

android {
sourceSets {
getByName("test").java.srcDir("src/sharedTest/java")
getByName("androidTest").java.srcDir("src/sharedTest/java")
}
sourceSets {
getByName("test").java.srcDir("src/sharedTest/java")
getByName("androidTest").java.srcDir("src/sharedTest/java")
}

hilt {
Expand All @@ -107,11 +105,11 @@ android {
}

compileOptions {
sourceCompatibility(Config.javaVersion)
targetCompatibility(Config.javaVersion)
sourceCompatibility = Config.javaVersion
targetCompatibility = Config.javaVersion
}

tasks.withType().all {
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions {
jvmTarget = Config.javaVersion.toString()
}
Expand Down Expand Up @@ -162,7 +160,7 @@ dependencies {
// Weather Image
implementation(Utils.weatherImage)

// CalenderView
// CalendarView
implementation(Utils.calendarView)

// Google Play Services
Expand Down
8 changes: 6 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<application
android:name="com.mayokunadeniyi.instantweather.InstantWeatherApplication"
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/instant_weather_new"
android:label="@string/app_name"
Expand All @@ -16,18 +17,21 @@
android:usesCleartextTraffic="true"
android:theme="@style/AppTheme"
tools:targetApi="m">
<activity android:name="com.mayokunadeniyi.instantweather.ui.MainActivity"

<activity android:name=".ui.MainActivity"
android:theme="@style/SplashScreenTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<provider
android:name="androidx.work.impl.WorkManagerInitializer"
android:authorities="${applicationId}.workmanager-init"
tools:node="remove" />

</application>

</manifest>
67 changes: 67 additions & 0 deletions app/src/main/java/com/mayokunadeniyi/instantweather/App.kt
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()
}
}
}
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()
}
}
}
3 changes: 3 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ android.enableJetifier=true
kotlin.code.style=official
#Enable incremental annotation processing
kapt.incremental.apt=true
# m1 chip support
org.gradle.jvmargs=-Xmx1536M -XX:MaxMetaspaceSize=512m -XX:+UseG1GC -Dfile.encoding=UTF-8 -Dapple.awt.UIElement=true

Loading