Skip to content

Commit

Permalink
Add ApplicationContext initializer and getter
Browse files Browse the repository at this point in the history
  • Loading branch information
evowizz committed Mar 14, 2022
1 parent 468923a commit 43cb9df
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
4 changes: 3 additions & 1 deletion app/src/main/java/dev/evowizz/commonlib/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import dev.evowizz.common.hashing.Algorithm
import dev.evowizz.common.hashing.Hashing
import dev.evowizz.common.init.ApplicationContext
import dev.evowizz.common.mosaic.MosaicBuilder
import dev.evowizz.common.mosaic.URLSpanProvider
import dev.evowizz.common.os.AndroidVersion
Expand Down Expand Up @@ -57,7 +58,8 @@ class MainActivity : AppCompatActivity() {
/* Using Int.toDp(...)*/
val toDpValue = 16.toDp(this)

val nBarMode = getNavigationBarMode(this)
val aContext = ApplicationContext.get()
val nBarMode = getNavigationBarMode(aContext)

val aCodename = AndroidVersion.getCodename()
val aIsPreview = AndroidVersion.isPreview()
Expand Down
18 changes: 17 additions & 1 deletion core/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,20 @@
-->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dev.evowizz.common.core" />
xmlns:tools="http://schemas.android.com/tools"
package="dev.evowizz.common.core">

<application>
<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
android:directBootAware="true"
android:exported="false"
tools:node="merge">

<meta-data
android:name="dev.evowizz.common.init.ApplicationContext"
android:value="androidx.startup" />
</provider>
</application>
</manifest>
42 changes: 42 additions & 0 deletions core/src/main/java/dev/evowizz/common/init/ApplicationContext.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2022 Dylan Roussel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dev.evowizz.common.init

import android.annotation.SuppressLint
import android.content.Context
import androidx.startup.Initializer

class ApplicationContext : Initializer<ApplicationContext> {

override fun create(context: Context): ApplicationContext {
_context = context
return this
}

/**
* ApplicationContext initializer does not depend on anything else.
*/
override fun dependencies() = emptyList<Nothing>()

companion object {

@SuppressLint("StaticFieldLeak")
private var _context: Context? = null

fun get() = _context ?: error("Context not initialized.")
}
}

0 comments on commit 43cb9df

Please sign in to comment.