Skip to content
This repository was archived by the owner on Jan 29, 2024. It is now read-only.

Commit 7bc8378

Browse files
wrongsahilP1V4HH2CFG
andauthored
[RQLY-262] enhancement: update version dialog (#18)
Co-authored-by: P1V4HH2CFG <[email protected]>
1 parent 952e4f4 commit 7bc8378

File tree

6 files changed

+153
-6
lines changed

6 files changed

+153
-6
lines changed

requestly-android-core/build.gradle

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ android {
1010
defaultConfig {
1111
minSdk rootProject.minSdkVersion
1212
targetSdk rootProject.targetSdkVersion
13-
versionCode 1
14-
versionName "1.0"
13+
versionCode VERSION_CODE.toInteger()
14+
versionName VERSION_NAME
1515

1616
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
17+
buildConfigField("String", "VERSION_NAME", "\"${defaultConfig.versionName}\"")
18+
buildConfigField("int", "VERSION_CODE", "${defaultConfig.versionCode}")
1719
}
1820

1921
buildTypes {
@@ -53,6 +55,8 @@ dependencies {
5355
testImplementation 'junit:junit:4.13.2'
5456
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
5557
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
58+
implementation "com.squareup.retrofit2:retrofit:$retrofitVersion"
59+
implementation "com.squareup.retrofit2:converter-gson:$retrofitVersion"
5660
}
5761

5862
apply from: rootProject.file('gradle/gradle-mvn-push.gradle')
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.requestly.android.core.network
2+
3+
import retrofit2.Retrofit
4+
import retrofit2.converter.gson.GsonConverterFactory
5+
6+
object NetworkManager {
7+
8+
private const val RQ_SERVER_BASE_URL = "https://api.requestly.io/"
9+
10+
private val client: Retrofit by lazy { Retrofit.Builder()
11+
.baseUrl(RQ_SERVER_BASE_URL)
12+
.addConverterFactory(GsonConverterFactory.create())
13+
.build()
14+
}
15+
16+
fun <S> create(serviceClass: Class<S>): S {
17+
return client.create(serviceClass)
18+
}
19+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.requestly.android.core.network
2+
3+
import com.google.gson.annotations.SerializedName
4+
import retrofit2.Call
5+
import retrofit2.http.GET
6+
7+
data class RQSDKVersionInfo(
8+
@SerializedName("versionName") val versionName: String?,
9+
@SerializedName("versionCode") val versionCode: Int?,
10+
@SerializedName("displayText") val displayText: String?,
11+
@SerializedName("ctaText") val ctaText: String?,
12+
@SerializedName("redirectUrl") val redirectUrl: String?)
13+
14+
interface RQSDKVersionUpdateService {
15+
@GET("androidSdk/latestVersion")
16+
fun getLatestVersion(): Call<RQSDKVersionInfo>
17+
}

requestly-android-core/src/main/java/io/requestly/android/core/ui/MainRequestlyActivity.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package io.requestly.android.core.ui
22

3+
import android.content.Intent
4+
import android.net.Uri
35
import android.os.Bundle
46
import android.view.Menu
57
import android.view.MenuInflater
68
import android.view.MenuItem
9+
import android.view.View
10+
import androidx.activity.viewModels
711
import androidx.appcompat.app.AppCompatActivity
812
import androidx.core.view.MenuProvider
913
import androidx.navigation.findNavController
@@ -17,11 +21,14 @@ import io.requestly.android.core.navigation.NavigationFlow
1721
import io.requestly.android.core.navigation.Navigator
1822
import io.requestly.android.core.navigation.ToFlowNavigatable
1923

24+
2025
class MainRequestlyActivity : AppCompatActivity(), ToFlowNavigatable {
2126

2227
private lateinit var binding: ActivityMainRequestlyBinding
2328
private var navigator = Navigator()
2429

30+
private val viewModel: MainRequestlyActivityViewModel by viewModels()
31+
2532
override fun onCreate(savedInstanceState: Bundle?) {
2633
super.onCreate(savedInstanceState)
2734
binding = ActivityMainRequestlyBinding.inflate(layoutInflater)
@@ -45,6 +52,22 @@ class MainRequestlyActivity : AppCompatActivity(), ToFlowNavigatable {
4552
navigator.navController = navController
4653
setupMenu()
4754
handleOnStartNavigation()
55+
56+
binding.updateNotifyStripView.visibility = View.GONE
57+
viewModel.versionUpdateLiveData.observe(this) { data ->
58+
if (data == null) {
59+
binding.updateNotifyStripView.visibility = View.GONE
60+
return@observe
61+
}
62+
binding.updateNotifyStripView.visibility = View.VISIBLE
63+
binding.updateNotifyStripText.text = data.displayText
64+
binding.updateNotifyStripButton.text = data.ctaText
65+
binding.updateNotifyStripButton.setOnClickListener {
66+
val intent: Intent =
67+
Intent(Intent.ACTION_VIEW).setData(Uri.parse(data.url))
68+
startActivity(intent)
69+
}
70+
}
4871
}
4972

5073
override fun onSupportNavigateUp(): Boolean {
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package io.requestly.android.core.ui
2+
3+
import androidx.lifecycle.LiveData
4+
import androidx.lifecycle.MutableLiveData
5+
import androidx.lifecycle.ViewModel
6+
import io.requestly.android.core.BuildConfig
7+
import io.requestly.android.core.network.NetworkManager
8+
import io.requestly.android.core.network.RQSDKVersionInfo
9+
import io.requestly.android.core.network.RQSDKVersionUpdateService
10+
import retrofit2.Call
11+
import retrofit2.Callback;
12+
import retrofit2.Response
13+
14+
data class NewVersionNotifyStripData(val displayText: String, val ctaText: String, val url: String)
15+
16+
class MainRequestlyActivityViewModel : ViewModel() {
17+
18+
private var _versionUpdateLiveData: MutableLiveData<NewVersionNotifyStripData?> =
19+
MutableLiveData()
20+
val versionUpdateLiveData: LiveData<NewVersionNotifyStripData?> = _versionUpdateLiveData
21+
22+
init {
23+
try {
24+
NetworkManager.create(RQSDKVersionUpdateService::class.java)
25+
.getLatestVersion()
26+
.enqueue(object : Callback<RQSDKVersionInfo> {
27+
override fun onResponse(
28+
call: Call<RQSDKVersionInfo>,
29+
response: Response<RQSDKVersionInfo>
30+
) {
31+
val responseBody = response.body()
32+
if (response.isSuccessful &&
33+
responseBody?.versionCode != null &&
34+
responseBody.versionCode > BuildConfig.VERSION_CODE
35+
) {
36+
_versionUpdateLiveData.value = NewVersionNotifyStripData(
37+
displayText = responseBody.displayText ?: "New Version Available",
38+
ctaText = responseBody.ctaText ?: "Update now",
39+
url = responseBody.redirectUrl ?: "https://github.com/requestly/requestly-android-sdk/releases"
40+
)
41+
return
42+
}
43+
44+
_versionUpdateLiveData.value = null
45+
}
46+
47+
override fun onFailure(call: Call<RQSDKVersionInfo>, t: Throwable) {
48+
_versionUpdateLiveData.value = null
49+
}
50+
})
51+
} catch (e: java.lang.Exception) {
52+
_versionUpdateLiveData.value = null
53+
}
54+
}
55+
}

requestly-android-core/src/main/res/layout/activity_main_requestly.xml

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,44 @@
77
tools:context=".ui.MainRequestlyActivity">
88

99
<fragment
10+
android:id="@+id/nav_host_fragment"
11+
android:name="androidx.navigation.fragment.NavHostFragment"
1012
android:layout_width="match_parent"
1113
android:layout_height="0dp"
12-
android:id="@+id/nav_host_fragment"
1314
app:defaultNavHost="true"
14-
app:navGraph="@navigation/main_nav_graph"
15-
android:name="androidx.navigation.fragment.NavHostFragment"
15+
app:layout_constraintBottom_toTopOf="@id/updateNotifyStripView"
1616
app:layout_constraintTop_toTopOf="parent"
17+
app:navGraph="@navigation/main_nav_graph" />
18+
19+
<LinearLayout
20+
android:id="@+id/updateNotifyStripView"
21+
android:layout_width="match_parent"
22+
android:layout_height="wrap_content"
23+
android:gravity="center_vertical"
24+
android:paddingLeft="16dp"
25+
android:paddingRight="16dp"
26+
android:orientation="horizontal"
27+
android:background="@color/cardview_dark_background"
1728
app:layout_constraintBottom_toTopOf="@id/nav_view"
18-
/>
29+
app:layout_constraintLeft_toLeftOf="parent"
30+
app:layout_constraintRight_toRightOf="parent">
31+
32+
<TextView
33+
android:id="@+id/updateNotifyStripText"
34+
android:layout_width="0dp"
35+
android:textColor="@color/white"
36+
android:layout_weight="1"
37+
android:layout_height="wrap_content"
38+
android:layout_marginRight="16dp"
39+
android:layout_gravity="center_vertical"
40+
android:text="New version of SDK is available" />
41+
42+
<Button
43+
android:id="@+id/updateNotifyStripButton"
44+
android:layout_width="wrap_content"
45+
android:layout_height="wrap_content"
46+
android:text="Update Now" />
47+
</LinearLayout>
1948

2049
<com.google.android.material.bottomnavigation.BottomNavigationView
2150
android:id="@+id/nav_view"

0 commit comments

Comments
 (0)