Skip to content

Commit

Permalink
AndroidX lifecycle upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
joaquim-verges committed Jan 20, 2022
1 parent b9ac2b9 commit f2e2166
Show file tree
Hide file tree
Showing 13 changed files with 129 additions and 88 deletions.
67 changes: 35 additions & 32 deletions buildSrc/src/main/kotlin/Deps.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,60 @@ object Deps {
// Kotlin
const val stdlib = "org.jetbrains.kotlin:kotlin-stdlib-common"
const val coroutines = "org.jetbrains.kotlinx:kotlinx-coroutines-core:${Versions.coroutines}"
const val coroutinesAndroid = "org.jetbrains.kotlinx:kotlinx-coroutines-android:${Versions.coroutines}"
const val coroutineNative = "org.jetbrains.kotlinx:kotlinx-coroutines-core-native:${Versions.coroutines}"
const val coroutinesAndroid =
"org.jetbrains.kotlinx:kotlinx-coroutines-android:${Versions.coroutines}"
const val coroutineNative =
"org.jetbrains.kotlinx:kotlinx-coroutines-core-native:${Versions.coroutines}"

// AndroidX
val lifecycleDeps = listOf(
"androidx.lifecycle:lifecycle-viewmodel-ktx:${Versions.arch_lifecycle_viewmodel}",
"androidx.lifecycle:lifecycle-runtime-ktx:${Versions.arch_lifecycle_runtime}",
"androidx.lifecycle:lifecycle-extensions:${Versions.arch_lifecycle}"
"androidx.lifecycle:lifecycle-viewmodel-ktx:${Versions.arch_lifecycle_viewmodel}",
"androidx.lifecycle:lifecycle-runtime-ktx:${Versions.arch_lifecycle_runtime}",
"androidx.fragment:fragment-ktx:${Versions.fragment}"
)

val androidXUiDeps = listOf(
"androidx.appcompat:appcompat:${Versions.appcompat}",
"androidx.recyclerview:recyclerview:${Versions.recyclerview}",
"androidx.swiperefreshlayout:swiperefreshlayout:${Versions.swiperefresh}",
"androidx.constraintlayout:constraintlayout:${Versions.constraint_layout}",
"com.google.android.material:material:${Versions.material_lib}"
"androidx.appcompat:appcompat:${Versions.appcompat}",
"androidx.recyclerview:recyclerview:${Versions.recyclerview}",
"androidx.swiperefreshlayout:swiperefreshlayout:${Versions.swiperefresh}",
"androidx.constraintlayout:constraintlayout:${Versions.constraint_layout}",
"com.google.android.material:material:${Versions.material_lib}"
)

val androidXNavigationDeps = listOf(
"androidx.navigation:navigation-fragment-ktx:${Versions.navigation}",
"androidx.navigation:navigation-ui-ktx:${Versions.navigation}"
"androidx.navigation:navigation-fragment-ktx:${Versions.navigation}",
"androidx.navigation:navigation-ui-ktx:${Versions.navigation}"
)

val androidXTestDeps = listOf(
"androidx.test:core:${Versions.test_core}",
"androidx.test:runner:${Versions.test_core}",
"androidx.test:rules:${Versions.test_core}",
"androidx.test.ext:junit:${Versions.test_ext}",
"androidx.test.ext:junit-ktx:${Versions.test_ext}",
"androidx.test.espresso:espresso-core:${Versions.test_espresso}",
"com.nhaarman.mockitokotlin2:mockito-kotlin:${Versions.mockito_kotlin}",
"org.robolectric:robolectric:${Versions.robolectric}",
"org.jetbrains.kotlinx:kotlinx-coroutines-test:${Versions.coroutines}"
"androidx.test:core:${Versions.test_core}",
"androidx.test:runner:${Versions.test_core}",
"androidx.test:rules:${Versions.test_core}",
"androidx.test.ext:junit:${Versions.test_ext}",
"androidx.test.ext:junit-ktx:${Versions.test_ext}",
"androidx.test.espresso:espresso-core:${Versions.test_espresso}",
"com.nhaarman.mockitokotlin2:mockito-kotlin:${Versions.mockito_kotlin}",
"org.robolectric:robolectric:${Versions.robolectric}",
"org.jetbrains.kotlinx:kotlinx-coroutines-test:${Versions.coroutines}",
"androidx.fragment:fragment-ktx:${Versions.fragment}"
)

val composeDeps = listOf(
"androidx.compose.runtime:runtime:${Versions.compose}",
"androidx.compose.ui:ui:${Versions.compose}",
"androidx.compose.foundation:foundation-layout:${Versions.compose}",
"androidx.compose.material:material:${Versions.compose}",
"androidx.compose.foundation:foundation:${Versions.compose}",
"androidx.compose.animation:animation:${Versions.compose}",
"androidx.compose.ui:ui-tooling:${Versions.compose}",
"androidx.constraintlayout:constraintlayout-compose:${Versions.compose_constraint_layout}",
"androidx.activity:activity-compose:${Versions.compose_activity}"
"androidx.compose.runtime:runtime:${Versions.compose}",
"androidx.compose.ui:ui:${Versions.compose}",
"androidx.compose.foundation:foundation-layout:${Versions.compose}",
"androidx.compose.material:material:${Versions.compose}",
"androidx.compose.foundation:foundation:${Versions.compose}",
"androidx.compose.animation:animation:${Versions.compose}",
"androidx.compose.ui:ui-tooling:${Versions.compose}",
"androidx.constraintlayout:constraintlayout-compose:${Versions.compose_constraint_layout}",
"androidx.activity:activity-compose:${Versions.compose_activity}"
)

// MPP
val ktorCommonDeps = listOf(
"io.ktor:ktor-client-core:${Versions.ktor}",
"io.ktor:ktor-client-serialization:${Versions.ktor}"
"io.ktor:ktor-client-core:${Versions.ktor}",
"io.ktor:ktor-client-serialization:${Versions.ktor}"
)
const val ktorAndroid = "io.ktor:ktor-client-android:${Versions.ktor}"
const val ktorIOS = "io.ktor:ktor-client-ios:${Versions.ktor}"
Expand Down
11 changes: 6 additions & 5 deletions buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ object Versions {

// android
const val agp = "7.0.2"
const val compileSdk = 30
const val compileSdk = 31
const val minSdk = 21
const val targetSdk = 30
const val targetSdk = 31

// kotlin std
const val kotlin = "1.5.31"
Expand All @@ -18,9 +18,10 @@ object Versions {
const val test_espresso = "3.3.0"

// androidX
const val arch_lifecycle = "2.2.0"
const val arch_lifecycle_runtime = "2.3.0-alpha05"
const val arch_lifecycle_viewmodel = "2.3.0-alpha05"
const val arch_lifecycle = "2.4.0"
const val fragment = "1.4.0"
const val arch_lifecycle_runtime = "2.4.0"
const val arch_lifecycle_viewmodel = "2.4.0"
const val appcompat = "1.1.0"
const val recyclerview = "1.1.0"
const val swiperefresh = "1.1.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,36 @@
package com.joaquimverges.helium.core

import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.CoroutineScope

/**
* Android ViewModel implementation
*/
actual abstract class HeliumViewModel : ViewModel(), LifecycleObserver {
actual abstract class HeliumViewModel : ViewModel(), DefaultLifecycleObserver {
actual val coroutineScope: CoroutineScope = viewModelScope

override fun onResume(owner: LifecycleOwner) {
onResume()
}

override fun onPause(owner: LifecycleOwner) {
onPause()
}

override fun onCleared() {
super.onCleared()
this.onClear()
}

actual open fun onResume() {
}

actual open fun onPause() {
}

actual open fun onClear() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class LogicBlockFactory<T : ViewModel?>(
private val factory: (Class<T>) -> T
) : ViewModelProvider.Factory {

override fun <T : ViewModel?> create(modelClass: Class<T>): T {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
if (clazz.isAssignableFrom(modelClass)) {
@Suppress("UNCHECKED_CAST")
return factory.invoke(clazz) as T
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ import kotlinx.coroutines.CoroutineScope
*/
expect abstract class HeliumViewModel() {
val coroutineScope: CoroutineScope
open fun onResume()
open fun onPause()
open fun onClear()
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,13 @@ import kotlinx.coroutines.MainScope

actual abstract class HeliumViewModel {
actual val coroutineScope: CoroutineScope = MainScope()

actual open fun onResume() {
}

actual open fun onPause() {
}

actual open fun onClear() {
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.joaquimverges.helium.ui.list

import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.OnLifecycleEvent
import com.joaquimverges.helium.core.LogicBlock
import com.joaquimverges.helium.core.event.BlockEvent
Expand Down Expand Up @@ -61,13 +62,16 @@ open class ListLogic<T, E : BlockEvent>(
}.launchInBlock()
}

@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
internal fun refreshIfNeeded() {
if (refreshPolicy.shouldRefresh()) {
loadFirstPage()
}
}

override fun onResume() {
refreshIfNeeded()
}

fun loadFirstPage() {
launchInBlock {
try {
Expand Down
36 changes: 19 additions & 17 deletions samples/demoapp/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.joaquimverges.demoapp">
package="com.joaquimverges.demoapp">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="com.joaquimverges.demoapp.MainActivity">
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name="com.joaquimverges.demoapp.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<action android:name="android.intent.action.MAIN" />

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

<activity android:name="com.joaquimverges.demoapp.SimpleListActivity"/>
<activity android:name="com.joaquimverges.demoapp.ViewPagerActivity"/>
<activity android:name="com.joaquimverges.demoapp.SimpleListActivityRetained"/>
<activity android:name="com.joaquimverges.demoapp.SimpleListFragmentActivityRetained"/>
<activity android:name="com.joaquimverges.demoapp.AdvancedListActivity"/>
<activity android:name="com.joaquimverges.demoapp.CardListActivity"/>
<activity android:name="com.joaquimverges.demoapp.BottomNavActivity"/>
<activity android:name="com.joaquimverges.demoapp.SimpleListActivity" />
<activity android:name="com.joaquimverges.demoapp.ViewPagerActivity" />
<activity android:name="com.joaquimverges.demoapp.SimpleListActivityRetained" />
<activity android:name="com.joaquimverges.demoapp.SimpleListFragmentActivityRetained" />
<activity android:name="com.joaquimverges.demoapp.AdvancedListActivity" />
<activity android:name="com.joaquimverges.demoapp.CardListActivity" />
<activity android:name="com.joaquimverges.demoapp.BottomNavActivity" />
</application>

</manifest>
24 changes: 13 additions & 11 deletions samples/multiplatform_app/android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.joaquimverges.kmp.news.android">
package="com.joaquimverges.kmp.news.android">

<uses-permission android:name="android.permission.INTERNET" />

<application
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="com.joaquimverges.kmp.news.android.MainActivity">
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name="com.joaquimverges.kmp.news.android.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Expand Down
Binary file not shown.
2 changes: 0 additions & 2 deletions samples/newsapp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ dependencies {
implementation "com.github.bumptech.glide:glide:4.11.0"
kapt "com.github.bumptech.glide:compiler:4.9.0"
implementation "com.thoughtbot:expandablecheckrecyclerview:1.4"
debugImplementation "com.squareup.leakcanary:leakcanary-android:1.5.4"
releaseImplementation "com.squareup.leakcanary:leakcanary-android-no-op:1.5.4"
implementation project(':helium-core')
implementation project(':helium-ui')
implementation project(':helium-navigation')
Expand Down
23 changes: 13 additions & 10 deletions samples/newsapp/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jv.news">
xmlns:tools="http://schemas.android.com/tools"
package="com.jv.news">

<uses-permission android:name="android.permission.INTERNET" />

<application
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name="com.jv.news.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Expand Down
7 changes: 0 additions & 7 deletions samples/newsapp/src/main/java/com/jv/news/App.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.jv.news

import android.app.Application
import com.squareup.leakcanary.LeakCanary

/**
* @author joaquim
Expand All @@ -16,11 +15,5 @@ class App : Application() {
override fun onCreate() {
super.onCreate()
context = this
if (!LeakCanary.isInAnalyzerProcess(this)) {
// This process is dedicated to LeakCanary for heap analysis.
// You should not init your app in this process.
return
}
LeakCanary.install(this)
}
}

0 comments on commit f2e2166

Please sign in to comment.