Skip to content

Commit 9bf706e

Browse files
committed
update login agreement
1 parent 0bda283 commit 9bf706e

File tree

7 files changed

+87
-5
lines changed

7 files changed

+87
-5
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package io.agora.flat.common.version
2+
3+
import com.google.gson.Gson
4+
import io.agora.flat.common.android.LanguageManager
5+
import io.agora.flat.data.AppEnv
6+
import io.agora.flat.data.model.Agreement
7+
import io.agora.flat.data.model.AgreementsResp
8+
import io.agora.flat.di.NetworkModule
9+
import kotlinx.coroutines.Dispatchers
10+
import kotlinx.coroutines.withContext
11+
import okhttp3.OkHttpClient
12+
import okhttp3.Request
13+
import javax.inject.Inject
14+
15+
class AgreementFetcher @Inject constructor(
16+
@NetworkModule.NormalOkHttpClient private val client: OkHttpClient,
17+
private val appEnv: AppEnv,
18+
private val gson: Gson = Gson() // Facilitates testing by allowing injection.
19+
) {
20+
suspend fun fetchAgreement(): Agreement? = withContext(Dispatchers.IO) {
21+
try {
22+
val request = Request.Builder().url(appEnv.agreementsUrl).build()
23+
client.newCall(request).execute().use { response ->
24+
if (response.isSuccessful) {
25+
val respJson = response.body?.string()
26+
if (respJson != null) {
27+
val env = appEnv.getEnv()
28+
val lang = LanguageManager.currentLocale().language
29+
val agreementsResp = gson.fromJson(respJson, AgreementsResp::class.java)
30+
return@withContext agreementsResp.data[env]?.get(lang)
31+
}
32+
}
33+
null
34+
}
35+
} catch (e: Exception) {
36+
null
37+
}
38+
}
39+
}

app/src/main/java/io/agora/flat/data/AppEnv.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ class AppEnv @Inject constructor(@ApplicationContext context: Context) {
184184

185185
val showIcp get() = currentEnvItem.showIcp
186186

187+
val agreementsUrl get() = "https://flat-storage.oss-cn-hangzhou.aliyuncs.com/versions/latest/stable/android/agreements.json"
188+
187189
data class EnvItem(
188190
val agoraAppId: String,
189191
val serviceUrl: String,
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package io.agora.flat.data.model
2+
3+
data class AgreementsResp(
4+
val data: Map<String, Map<String, Agreement>>
5+
)
6+
7+
data class Agreement(
8+
val title: String,
9+
val message: String,
10+
)

app/src/main/java/io/agora/flat/di/NetworkModule.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import dagger.hilt.InstallIn
77
import dagger.hilt.android.qualifiers.ApplicationContext
88
import dagger.hilt.components.SingletonComponent
99
import io.agora.flat.BuildConfig
10+
import io.agora.flat.common.version.AgreementFetcher
1011
import io.agora.flat.common.version.VersionChecker
1112
import io.agora.flat.data.AppEnv
1213
import io.agora.flat.data.AppKVCenter
@@ -135,4 +136,13 @@ object NetworkModule {
135136
): VersionChecker {
136137
return VersionChecker(client, appKVCenter, context.getAppVersion(), appEnv.versionCheckUrl)
137138
}
139+
140+
@Provides
141+
@Singleton
142+
fun providerAgreementFetcher(
143+
@NormalOkHttpClient client: OkHttpClient,
144+
appEnv: AppEnv
145+
): AgreementFetcher {
146+
return AgreementFetcher(client, appEnv)
147+
}
138148
}

app/src/main/java/io/agora/flat/ui/activity/LoginActivity.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,14 @@ private fun LoginArea(state: LoginUiState, modifier: Modifier, actioner: (LoginU
362362
LoginAgreement(
363363
modifier = Modifier.padding(horizontal = 24.dp),
364364
checked = agreementChecked,
365-
onCheckedChange = { agreementChecked = it },
365+
onCheckedChange = { target ->
366+
// force show agreement dialog when not checked
367+
if (target) {
368+
showAgreement = true
369+
} else {
370+
agreementChecked = target
371+
}
372+
},
366373
)
367374
}
368375

@@ -382,6 +389,7 @@ private fun LoginArea(state: LoginUiState, modifier: Modifier, actioner: (LoginU
382389

383390
if (showAgreement) {
384391
AgreementDialog(
392+
agreement = state.agreement,
385393
onAgree = {
386394
agreementChecked = true
387395
showAgreement = false

app/src/main/java/io/agora/flat/ui/activity/login/LoginViewModel.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package io.agora.flat.ui.activity.login
33
import androidx.lifecycle.viewModelScope
44
import dagger.hilt.android.lifecycle.HiltViewModel
55
import io.agora.flat.common.android.CallingCodeManager
6+
import io.agora.flat.common.version.AgreementFetcher
67
import io.agora.flat.data.AppEnv
78
import io.agora.flat.data.AppKVCenter
89
import io.agora.flat.data.LoginConfig
10+
import io.agora.flat.data.model.Agreement
911
import io.agora.flat.data.model.PhoneOrEmailInfo
1012
import io.agora.flat.data.onFailure
1113
import io.agora.flat.data.onSuccess
@@ -25,12 +27,14 @@ class LoginViewModel @Inject constructor(
2527
private val userRepository: UserRepository,
2628
private val appKVCenter: AppKVCenter,
2729
private val appEnv: AppEnv,
30+
private val agreementFetcher: AgreementFetcher,
2831
) : BaseAccountViewModel() {
2932
private val loading = ObservableLoadingCounter()
3033
private val messageManager = UiMessageManager()
3134

3235
private var _state = MutableStateFlow(
3336
LoginUiState(
37+
agreement = null,
3438
loginConfig = appEnv.loginConfig,
3539
inputState = PhoneOrEmailInfo(
3640
cc = CallingCodeManager.getDefaultCC(),
@@ -77,6 +81,13 @@ class LoginViewModel @Inject constructor(
7781
)
7882
}
7983
}
84+
85+
viewModelScope.launch {
86+
val fetchAgreement = agreementFetcher.fetchAgreement()
87+
if (fetchAgreement != null) {
88+
_state.value = _state.value.copy(agreement = fetchAgreement)
89+
}
90+
}
8091
}
8192

8293
fun needBindPhone(): Boolean {
@@ -163,4 +174,5 @@ data class LoginUiState(
163174
val message: UiMessage? = null,
164175

165176
val loginConfig: LoginConfig = LoginConfig(),
177+
val agreement: Agreement? = null,
166178
)

app/src/main/java/io/agora/flat/ui/compose/FlatAgreement.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import androidx.compose.ui.unit.dp
1212
import androidx.compose.ui.window.Dialog
1313
import io.agora.flat.Constants
1414
import io.agora.flat.R
15+
import io.agora.flat.data.model.Agreement
1516
import io.agora.flat.ui.theme.Shapes
1617

1718
@Composable
@@ -55,7 +56,7 @@ fun GlobalAgreementDialog(onAgree: () -> Unit, onRefuse: () -> Unit) {
5556
}
5657

5758
@Composable
58-
internal fun AgreementDialog(onAgree: () -> Unit, onRefuse: () -> Unit) {
59+
internal fun AgreementDialog(agreement: Agreement? = null, onAgree: () -> Unit, onRefuse: () -> Unit) {
5960
Dialog(onDismissRequest = onRefuse) {
6061
Surface(
6162
Modifier.widthIn(max = 400.dp),
@@ -64,7 +65,7 @@ internal fun AgreementDialog(onAgree: () -> Unit, onRefuse: () -> Unit) {
6465
Column(Modifier.padding(horizontal = 24.dp, vertical = 20.dp)) {
6566
FlatTextTitle(stringResource(R.string.login_agreement_dialog_title))
6667
FlatNormalVerticalSpacer()
67-
LoginAgreementDialogMessage()
68+
LoginAgreementDialogMessage(agreement)
6869
FlatNormalVerticalSpacer()
6970
Row {
7071
FlatSmallSecondaryTextButton(stringResource(R.string.refuse), Modifier.weight(1f)) { onRefuse() }
@@ -78,8 +79,8 @@ internal fun AgreementDialog(onAgree: () -> Unit, onRefuse: () -> Unit) {
7879

7980

8081
@Composable
81-
private fun LoginAgreementDialogMessage() {
82-
val text = stringResource(R.string.login_agreement_dialog_message)
82+
private fun LoginAgreementDialogMessage(agreement: Agreement?) {
83+
var text = agreement?.message ?: stringResource(R.string.login_agreement_dialog_message)
8384
val items = listOf(
8485
ClickableItem(stringResource(R.string.agreement_global_privacy), "privacy", Constants.URL.Privacy),
8586
ClickableItem(stringResource(R.string.agreement_global_service), "service", Constants.URL.Service)

0 commit comments

Comments
 (0)