Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Commit ab69aa1

Browse files
author
Eduard Ablekimov
committed
Rewritten to Kotlin. Replaced Rx with Coroutines. Added tests
1 parent cb8af8d commit ab69aa1

31 files changed

Lines changed: 707 additions & 744 deletions

.idea/gradle.xml

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ dependencies {
4444

4545
// Anko
4646
implementation "org.jetbrains.anko:anko-sdk15:$ankoVersion" // So here it's 15 too
47+
implementation "org.jetbrains.anko:anko-sdk21-coroutines:$ankoVersion"
4748

4849
// Anko libs matching support libs
4950
implementation "org.jetbrains.anko:anko-appcompat-v7:${ankoVersion}"
Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
package com.ackee.versionupdatehandler.setup
22

3+
import android.annotation.SuppressLint
34
import android.os.Bundle
45
import androidx.appcompat.app.AppCompatActivity
56
import com.ackee.versionupdatehandler.R
7+
import com.ackee.versioupdatehandler.VersionFetcher
68
import com.ackee.versioupdatehandler.VersionStatusResolver
79
import com.ackee.versioupdatehandler.model.BasicVersionsConfiguration
810
import com.ackee.versioupdatehandler.model.DialogSettings
911
import com.ackee.versioupdatehandler.model.VersionStatus
10-
import io.reactivex.Single
12+
import com.ackee.versioupdatehandler.model.VersionsConfiguration
1113
import org.jetbrains.anko.*
14+
import org.jetbrains.anko.sdk21.coroutines.onClick
1215
import java.util.Random
1316

1417
/**
1518
* Activity with samples
1619
*/
1720
class MainActivity : AppCompatActivity() {
1821

22+
@SuppressLint("SetTextI18n")
1923
override fun onCreate(savedInstanceState: Bundle?) {
2024
super.onCreate(savedInstanceState)
2125
scrollView {
2226
verticalLayout {
2327
padding = dip(16)
2428
button {
2529
text = "Not mandatory update"
26-
setOnClickListener {
30+
onClick {
2731
checkWithVersion(13)
2832
}
2933
}.lparams(width = matchParent) {
@@ -32,7 +36,7 @@ class MainActivity : AppCompatActivity() {
3236

3337
button {
3438
text = "Mandatory update"
35-
setOnClickListener {
39+
onClick {
3640
checkWithVersion(8)
3741
}
3842
}.lparams(width = matchParent) {
@@ -41,15 +45,13 @@ class MainActivity : AppCompatActivity() {
4145

4246
button {
4347
text = "Customized dialog"
44-
setOnClickListener {
45-
VersionStatusResolver({
46-
Single.just(BasicVersionsConfiguration(10, 15))
47-
}).checkVersionStatusAndOpenDefault(8, supportFragmentManager, DialogSettings.Builder()
48-
.title("My custom title")
49-
.messageRes(R.string.update_dialog_message)
50-
.positiveButton("Yaay")
51-
.negativeButton("Never")
52-
.build()
48+
onClick {
49+
getDefaultStatusResolver().checkVersionStatusAndOpenDefault(8, supportFragmentManager, DialogSettings.Builder()
50+
.title("My custom title")
51+
.messageRes(R.string.update_dialog_message)
52+
.positiveButton("Yaay")
53+
.negativeButton("Never")
54+
.build()
5355
)
5456
}
5557
}.lparams(width = matchParent) {
@@ -58,16 +60,12 @@ class MainActivity : AppCompatActivity() {
5860

5961
button {
6062
text = "Custom UI"
61-
setOnClickListener {
62-
VersionStatusResolver({
63-
Single.just(BasicVersionsConfiguration(10, 15))
64-
}).checkVersionStatus(if (Random().nextInt() % 2 == 0) 8 else 12)
65-
.subscribe({
66-
when (it) {
67-
VersionStatus.UPDATE_AVAILABLE -> toast("Update is available")
68-
VersionStatus.UPDATE_REQUIRED -> toast("Mandatory update is available")
69-
}
70-
}, Throwable::printStackTrace)
63+
onClick {
64+
when (getDefaultStatusResolver().checkVersionStatus(if (Random().nextInt() % 2 == 0) 8 else 12)) {
65+
VersionStatus.UPDATE_AVAILABLE -> toast("Update is available")
66+
VersionStatus.UPDATE_REQUIRED -> toast("Mandatory update is available")
67+
VersionStatus.UP_TO_DATE -> toast("Up to date")
68+
}
7169
}
7270
}.lparams(width = matchParent) {
7371
bottomMargin = dip(16)
@@ -76,10 +74,16 @@ class MainActivity : AppCompatActivity() {
7674
}
7775
}
7876

79-
private fun checkWithVersion(version: Int) {
77+
private suspend fun checkWithVersion(version: Int) {
8078
// replace fetcher with some real one
81-
VersionStatusResolver({
82-
Single.just(BasicVersionsConfiguration(10, 15))
83-
}).checkVersionStatusAndOpenDefault(version, supportFragmentManager)
79+
getDefaultStatusResolver().checkVersionStatusAndOpenDefault(version, supportFragmentManager)
80+
}
81+
82+
private fun getDefaultStatusResolver(minimalVersion: Int = 10, currentVersion: Int = 15): VersionStatusResolver {
83+
return VersionStatusResolver(object : VersionFetcher {
84+
override suspend fun fetch(): VersionsConfiguration {
85+
return BasicVersionsConfiguration(10, 15)
86+
}
87+
})
8488
}
8589
}

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22

33
buildscript {
4-
ext.kotlin_version = '1.3.50'
4+
ext.kotlin_version = '1.3.72'
55

66
repositories {
77
jcenter()
88
google()
99
}
1010
dependencies {
11-
classpath 'com.android.tools.build:gradle:3.5.1'
11+
classpath 'com.android.tools.build:gradle:4.0.0'
1212
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1313
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
1414
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0'

firebase-fetcher/build.gradle

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
apply plugin: 'com.android.library'
2+
apply plugin: 'kotlin-android'
23

34
android {
45
compileSdkVersion 29
@@ -17,10 +18,17 @@ android {
1718
}
1819
}
1920

21+
repositories {
22+
mavenCentral()
23+
}
24+
2025
dependencies {
21-
testImplementation 'junit:junit:4.12'
22-
implementation 'com.google.firebase:firebase-config:19.0.3'
26+
testImplementation 'junit:junit:4.13'
27+
implementation 'com.google.firebase:firebase-config:19.1.4'
2328
implementation project(':status-resolver')
29+
implementation "androidx.core:core-ktx:+"
30+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
31+
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.3.3'
2432
}
2533

2634
ext {

firebase-fetcher/src/main/java/com/ackee/versionupdatehandler/FirebaseVersionFetcher.java

Lines changed: 0 additions & 69 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.ackee.versionupdatehandler
2+
3+
import com.ackee.versioupdatehandler.VersionFetcher
4+
import com.ackee.versioupdatehandler.model.BasicVersionsConfiguration
5+
import com.ackee.versioupdatehandler.model.VersionsConfiguration
6+
import com.google.firebase.remoteconfig.FirebaseRemoteConfig
7+
import kotlinx.coroutines.tasks.await
8+
9+
/**
10+
* Class that fetches version configuration from Firebase Remote.
11+
*/
12+
class FirebaseVersionFetcher constructor(
13+
private val cacheExpiration: Int = 3600,
14+
private val minimalAttributeName: String = MINIMAL_VERSION,
15+
private val currentAttributeName: String = CURRENT_VERSION
16+
) : VersionFetcher {
17+
18+
override suspend fun fetch(): VersionsConfiguration {
19+
val remoteConfig = FirebaseRemoteConfig.getInstance().apply {
20+
setDefaultsAsync(DEFAULTS)
21+
}
22+
val isDevMode = FirebaseRemoteConfig.getInstance().info.configSettings.isDeveloperModeEnabled
23+
remoteConfig.fetch((if (isDevMode) 0 else cacheExpiration).toLong()).await()
24+
remoteConfig.activate().await()
25+
26+
val minimalVersion = remoteConfig.getLong(minimalAttributeName)
27+
val currentVersion = remoteConfig.getLong(currentAttributeName)
28+
return BasicVersionsConfiguration(minimalVersion, currentVersion)
29+
}
30+
31+
companion object {
32+
const val MINIMAL_VERSION = "minimal_version_android"
33+
const val CURRENT_VERSION = "current_version_android"
34+
35+
val DEFAULTS: Map<String, Any> by lazy {
36+
mapOf(
37+
MINIMAL_VERSION to -1,
38+
CURRENT_VERSION to -1
39+
)
40+
}
41+
}
42+
}

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ org.gradle.jvmargs=-Xmx1536m
1111
# This option should only be used with decoupled projects. More details, visit
1212
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
1313
# org.gradle.parallel=true
14-
VERSION_NAME=1.1.0
14+
VERSION_NAME=1.2.0
1515
VERSION_CODE=1
1616
GROUP=cz.ackee.updatechecker
1717
BINTRAY_REPO=version-update-handler
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Wed Oct 16 14:22:47 CEST 2019
1+
#Tue Jun 09 15:27:04 YEKT 2020
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip

readme.MD

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@
33
# Android Version Status Resolver and Fetcher
44

55
## Purpose of library
6-
Many applications have need to tell their users to update current application because they have outdated version. Google Play doesnt support any API for that so we need to use custom solution. This library will compare current application version number to the versions that are stored in remote locations (Api request/Firebase Remote Config, ...) and show the dialog to the user with prompt to update their app.
6+
Many applications have need to tell their users to update current application because they have outdated version. Google Play doesn't support any API for that so we need to use custom solution. This library will compare current application version number to the versions that are stored in remote locations (Api request/Firebase Remote Config, ...) and show the dialog to the user with prompt to update their app.
77

88

9-
This library is based on RxJava2
9+
This library is based on Kotlin Coroutines
1010
## Usage
1111
The simplest way for use this library is
1212
```kotlin
1313
val versionFetcher = ...
1414
val versionResolver = VersionStatusResolver(versionFetcher)
15-
versionResolver.checkVersionStatusAndShowDefault(BuildConfig.VERSION_CODE, getSupportFragmentManager())
15+
versionResolver.checkVersionStatusAndOpenDefault(BuildConfig.VERSION_CODE, getSupportFragmentManager())
1616
```
1717

1818
this will check versions and show update dialog with default texts. If you want to modify strings in dialog and maybe package name, you can via DialogSettings.Builder() class.
1919

2020
```kotlin
2121
val versionFetcher = ...
2222
val versionResolver = VersionStatusResolver(versionFetcher)
23-
versionResolver.checkVersionStatusAndShowDefault(BuildConfig.VERSION_CODE, getSupportFragmentManager(),
23+
versionResolver.checkVersionStatusAndOpenDefault(BuildConfig.VERSION_CODE, getSupportFragmentManager(),
2424
DialogSettings.Builder()
2525
.title("My custom title")
2626
.messageRes(R.string.update_dialog_message)

0 commit comments

Comments
 (0)