Skip to content

Commit 52cc36b

Browse files
Merge pull request #56 from thecraftman/add-m1-support
Add M1 Mac support & Optimize app startup by deferring initialization of non-critical components
2 parents 9c5564d + 20b408a commit 52cc36b

File tree

6 files changed

+146
-122
lines changed

6 files changed

+146
-122
lines changed

.gitignore

-111
This file was deleted.

app/build.gradle.kts

+7-9
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,9 @@ android {
8383
}
8484
}
8585

86-
android {
87-
sourceSets {
88-
getByName("test").java.srcDir("src/sharedTest/java")
89-
getByName("androidTest").java.srcDir("src/sharedTest/java")
90-
}
86+
sourceSets {
87+
getByName("test").java.srcDir("src/sharedTest/java")
88+
getByName("androidTest").java.srcDir("src/sharedTest/java")
9189
}
9290

9391
hilt {
@@ -107,11 +105,11 @@ android {
107105
}
108106

109107
compileOptions {
110-
sourceCompatibility(Config.javaVersion)
111-
targetCompatibility(Config.javaVersion)
108+
sourceCompatibility = Config.javaVersion
109+
targetCompatibility = Config.javaVersion
112110
}
113111

114-
tasks.withType().all {
112+
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
115113
kotlinOptions {
116114
jvmTarget = Config.javaVersion.toString()
117115
}
@@ -162,7 +160,7 @@ dependencies {
162160
// Weather Image
163161
implementation(Utils.weatherImage)
164162

165-
// CalenderView
163+
// CalendarView
166164
implementation(Utils.calendarView)
167165

168166
// Google Play Services

app/src/main/AndroidManifest.xml

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
<uses-permission android:name="android.permission.INTERNET"/>
77
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
88
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
9+
910
<application
10-
android:name="com.mayokunadeniyi.instantweather.InstantWeatherApplication"
11+
android:name=".App"
1112
android:allowBackup="true"
1213
android:icon="@mipmap/instant_weather_new"
1314
android:label="@string/app_name"
@@ -16,18 +17,21 @@
1617
android:usesCleartextTraffic="true"
1718
android:theme="@style/AppTheme"
1819
tools:targetApi="m">
19-
<activity android:name="com.mayokunadeniyi.instantweather.ui.MainActivity"
20+
21+
<activity android:name=".ui.MainActivity"
2022
android:theme="@style/SplashScreenTheme">
2123
<intent-filter>
2224
<action android:name="android.intent.action.MAIN" />
2325

2426
<category android:name="android.intent.category.LAUNCHER" />
2527
</intent-filter>
2628
</activity>
29+
2730
<provider
2831
android:name="androidx.work.impl.WorkManagerInitializer"
2932
android:authorities="${applicationId}.workmanager-init"
3033
tools:node="remove" />
34+
3135
</application>
3236

3337
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// App.kt
2+
override fun onCreate() {
3+
super.onCreate()
4+
5+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
6+
val deferredComponentInitializer = DeferredComponentInitializer(this)
7+
deferredComponentInitializer.initialize()
8+
} else {
9+
initializeComponents()
10+
}
11+
}
12+
13+
private fun initializeComponents() {
14+
initWorkManager()
15+
initCrashlytics()
16+
initAnalytics()
17+
}
18+
19+
// DeferredComponentInitializer.kt
20+
class DeferredComponentInitializer(private val app: Application) {
21+
22+
fun initialize() {
23+
val componentInitializer = ComponentInitializer(app)
24+
val initializeMessage = when {
25+
isColdStart() -> "Deferred initialization from cold start"
26+
else -> "Deferred initialization from warm start"
27+
}
28+
WorkManager.getInstance(app)
29+
.beginUniqueWork(
30+
"DeferredInitialization",
31+
ExistingWorkPolicy.KEEP,
32+
OneTimeWorkRequestBuilder<DeferredWorker>()
33+
.setInputData(workDataOf(DeferredWorker.KEY_INITIALIZE to initializeMessage))
34+
.build()
35+
)
36+
.enqueue()
37+
}
38+
39+
private fun isColdStart(): Boolean {
40+
return ProcessLifecycleOwner.get().lifecycle.currentState == Lifecycle.State.INITIALIZED
41+
}
42+
43+
private class DeferredWorker(
44+
context: Context,
45+
workerParams: WorkerParameters
46+
) : Worker(context, workerParams) {
47+
override fun doWork(): Result {
48+
val initializeMessage = inputData.getString(KEY_INITIALIZE)
49+
initializeMessage?.let {
50+
Log.d("DeferredInit", it)
51+
}
52+
ComponentInitializer(applicationContext).initialize()
53+
return Result.success()
54+
}
55+
56+
companion object {
57+
const val KEY_INITIALIZE = "KEY_INITIALIZE"
58+
}
59+
}
60+
61+
private class ComponentInitializer(private val app: Application) {
62+
fun initialize() {
63+
initCrashlytics()
64+
initAnalytics()
65+
}
66+
}
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.mayokunadeniyi.instantweather.initializers
2+
3+
import android.app.Application
4+
import android.content.Context
5+
import android.os.Build
6+
import android.util.Log
7+
import androidx.lifecycle.Lifecycle
8+
import androidx.lifecycle.ProcessLifecycleOwner
9+
import androidx.work.ExistingWorkPolicy
10+
import androidx.work.OneTimeWorkRequestBuilder
11+
import androidx.work.WorkManager
12+
import androidx.work.Worker
13+
import androidx.work.WorkerParameters
14+
import androidx.work.workDataOf
15+
16+
class DeferredComponentInitializer(private val app: Application) {
17+
18+
fun initialize() {
19+
val componentInitializer = ComponentInitializer(app)
20+
val initializeMessage = when {
21+
isColdStart() -> "Deferred initialization from cold start"
22+
else -> "Deferred initialization from warm start"
23+
}
24+
WorkManager.getInstance(app)
25+
.beginUniqueWork(
26+
"DeferredInitialization",
27+
ExistingWorkPolicy.KEEP,
28+
OneTimeWorkRequestBuilder<DeferredWorker>()
29+
.setInputData(workDataOf(DeferredWorker.KEY_INITIALIZE to initializeMessage))
30+
.build()
31+
)
32+
.enqueue()
33+
}
34+
35+
private fun isColdStart(): Boolean {
36+
return ProcessLifecycleOwner.get().lifecycle.currentState == Lifecycle.State.INITIALIZED
37+
}
38+
39+
private class DeferredWorker(
40+
context: Context,
41+
workerParams: WorkerParameters
42+
) : Worker(context, workerParams) {
43+
override fun doWork(): Result {
44+
val initializeMessage = inputData.getString(KEY_INITIALIZE)
45+
initializeMessage?.let {
46+
Log.d("DeferredInit", it)
47+
}
48+
ComponentInitializer(applicationContext).initialize()
49+
return Result.success()
50+
}
51+
52+
companion object {
53+
const val KEY_INITIALIZE = "KEY_INITIALIZE"
54+
}
55+
}
56+
57+
private class ComponentInitializer(private val app: Application) {
58+
fun initialize() {
59+
initCrashlytics()
60+
initAnalytics()
61+
}
62+
}
63+
}

gradle.properties

+3
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@ android.enableJetifier=true
2121
kotlin.code.style=official
2222
#Enable incremental annotation processing
2323
kapt.incremental.apt=true
24+
# m1 chip support
25+
org.gradle.jvmargs=-Xmx1536M -XX:MaxMetaspaceSize=512m -XX:+UseG1GC -Dfile.encoding=UTF-8 -Dapple.awt.UIElement=true
26+

0 commit comments

Comments
 (0)