Skip to content

Commit

Permalink
Android wip
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierPineau committed Oct 25, 2023
1 parent a16df04 commit 253c946
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 21 deletions.
13 changes: 13 additions & 0 deletions androidApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ android {
resources.srcDirs("../shared/src/commonMain/resources")
}
}

flavorDimensions += "environment"

productFlavors {
create("ci") {
dimension = "environment"
applicationIdSuffix = ".ci"
}

create("store") {
dimension = "environment"
}
}
}

dependencies {
Expand Down
4 changes: 3 additions & 1 deletion androidApp/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@
<uses-permission android:name="android.permission.INTERNET" />

<application
android:name=".app.AndroidApplication"
android:allowBackup="true"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name="com.mirego.kmp.boilerplate.MainActivity"
android:name="com.mirego.kmp.boilerplate.app.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.mirego.kmp.boilerplate.app

import android.app.Application
import com.mirego.kmp.boilerplate.app.bootstrap.AndroidBootstrap
import com.mirego.kmp.boilerplate.bootstrap.Bootstrapper

class AndroidApplication : Application() {
val bootstrapper = Bootstrapper()

override fun onCreate() {
super.onCreate()
bootstrapper.initDependencies(AndroidBootstrap(this))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.mirego.kmp.boilerplate.app

import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.WindowCompat
import com.mirego.kmp.boilerplate.app.ui.application.ApplicationView
import com.mirego.kmp.boilerplate.bootstrap.Bootstrapper
import com.mirego.kmp.boilerplate.trikot.viewmodels.declarative.compose.getInitialViewModel
import com.mirego.kmp.boilerplate.viewmodel.application.ApplicationViewModel

class MainActivity : AppCompatActivity() {
private val bootstrapper: Bootstrapper
get() = (applicationContext as AndroidApplication).bootstrapper

private val viewModel: ApplicationViewModel by lazy {
getInitialViewModel {
bootstrapper.applicationViewModel()
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
WindowCompat.setDecorFitsSystemWindows(window, false)
setContent {
ApplicationView(applicationViewModel = viewModel)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.mirego.kmp.boilerplate.app.bootstrap

import android.content.Context
import com.mirego.kmp.boilerplate.app.resources.AndroidImageProvider
import com.mirego.kmp.boilerplate.bootstrap.AppEnvironment
import com.mirego.kmp.boilerplate.bootstrap.Bootstrap
import com.mirego.kmp.boilerplate.common.BuildConfig
import com.mirego.trikot.kword.android.AndroidKWord
import com.mirego.trikot.viewmodels.declarative.configuration.TrikotViewModelDeclarative
import java.util.Locale

class AndroidBootstrap(context: Context) : Bootstrap {

override val appInformation = AppInformationImpl(context)

override val environment = when (BuildConfig.FLAVOR.lowercase()) {
"ci" -> AppEnvironment.DEV
"store" -> AppEnvironment.PRODUCTION
else -> AppEnvironment.PRODUCTION
}

init {
if (Locale.getDefault().language.lowercase() == "fr") {
AndroidKWord.setCurrentLanguageCode("fr")
} else {
AndroidKWord.setCurrentLanguageCode("en")
}

TrikotViewModelDeclarative.initialize(
imageProvider = AndroidImageProvider()
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.mirego.kmp.boilerplate.app.bootstrap

import android.content.Context
import com.mirego.kmp.boilerplate.bootstrap.AppInformation
import com.mirego.kmp.boilerplate.common.BuildConfig
import com.mirego.kmp.boilerplate.model.Language
import com.mirego.kmp.boilerplate.model.Locale

class AppInformationImpl(context: Context) : AppInformation {
override val locale: Locale = Locale(
if (java.util.Locale.getDefault().language.lowercase() == "fr") Language.FRENCH else Language.ENGLISH,
java.util.Locale.getDefault().country
)

override val versionNumber: String = "${BuildConfig.VERSION_NAME}.${BuildConfig.VERSION_CODE}"
override val diskCachePath: String = context.cacheDir.toString()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.mirego.kmp.boilerplate.app.resources

import android.content.Context
import com.mirego.trikot.viewmodels.declarative.configuration.VMDImageProvider
import com.mirego.trikot.viewmodels.declarative.properties.VMDImageResource

class AndroidImageProvider : VMDImageProvider {
override fun resourceIdForResource(resource: VMDImageResource, context: Context) = null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.mirego.kmp.boilerplate.app.ui.application

import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import com.mirego.kmp.boilerplate.app.ui.root.RootView
import com.mirego.kmp.boilerplate.viewmodel.application.ApplicationViewModel
import com.mirego.trikot.viewmodels.declarative.compose.extensions.observeAsState

@Composable
fun ApplicationView(applicationViewModel: ApplicationViewModel) {
val viewModel: ApplicationViewModel by applicationViewModel.observeAsState()
RootView(rootViewModel = viewModel.rootViewModel)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.mirego.kmp.boilerplate.app.ui.root

import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import com.mirego.kmp.boilerplate.viewmodel.root.RootViewModel
import com.mirego.trikot.viewmodels.declarative.compose.extensions.observeAsState

@Composable
fun RootView(rootViewModel: RootViewModel) {
val viewModel: RootViewModel by rootViewModel.observeAsState()
Text(text = "Hi")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.mirego.kmp.boilerplate.trikot.viewmodels.declarative.compose

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStoreOwner
import com.mirego.trikot.viewmodels.declarative.viewmodel.VMDViewModel

/**
* Use this method to get the initial VMDViewModel. This will make sure that it survives Activity recreation by wrapping it in a androidx.lifecycle.ViewModel
*/
@Suppress("UNCHECKED_CAST")
fun <VMD : VMDViewModel> ViewModelStoreOwner.getInitialViewModel(factory: () -> VMD): VMD =
ViewModelProvider(
viewModelStore,
ViewModelProviderFactory(factory)
)[ViewModelWrapper::class.java].wrappedViewModel as VMD

@Suppress("UNCHECKED_CAST")
private class ViewModelProviderFactory<VMD : VMDViewModel>(private val factory: () -> VMD) : ViewModelProvider.Factory {
override fun <VM : ViewModel> create(modelClass: Class<VM>): VM {
return ViewModelWrapper(factory()) as VM
}
}

class ViewModelWrapper<VMD : VMDViewModel>(val wrappedViewModel: VMD) : ViewModel()
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ enum class Language {
ENGLISH,
FRENCH;

fun toMulesoftLang() = when (this) {
ENGLISH -> "en-CA"
FRENCH -> "fr-CA"
}

fun toLangCode() = when (this) {
ENGLISH -> "en"
FRENCH -> "fr"
Expand Down

0 comments on commit 253c946

Please sign in to comment.