-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #234 from GuoXiCheng/dev-c
Dev
- Loading branch information
Showing
14 changed files
with
183 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 20 additions & 6 deletions
26
app/src/main/java/com/android/skip/data/config/ConfigViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,38 @@ | ||
package com.android.skip.data.config | ||
|
||
import android.content.Context | ||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.Observer | ||
import androidx.lifecycle.ViewModel | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.launch | ||
import androidx.lifecycle.viewModelScope | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class ConfigViewModel @Inject constructor( | ||
private val configReadRepository: ConfigReadRepository, | ||
private val configLoadRepository: ConfigLoadRepository | ||
) : ViewModel() { | ||
var configHashCode: LiveData<Int> = configReadRepository.configHashCode | ||
var configHashCode: LiveData<String> = configReadRepository.configHashCode | ||
|
||
private val observer = Observer<String> { | ||
val configMap = configReadRepository.handleConfig() | ||
configLoadRepository.loadConfig(configMap) | ||
} | ||
|
||
init { | ||
configReadRepository.configHashCode.observeForever{ | ||
val configMap = configReadRepository.handleConfig() | ||
configLoadRepository.loadConfig(configMap) | ||
configReadRepository.configHashCode.observeForever(observer) | ||
} | ||
|
||
// fun readConfig(context: Context) = configReadRepository.readConfig(context) | ||
fun readConfig() { | ||
viewModelScope.launch { | ||
configReadRepository.readConfig() | ||
} | ||
} | ||
|
||
fun readConfig(context: Context) = configReadRepository.readConfig(context) | ||
override fun onCleared() { | ||
super.onCleared() | ||
configReadRepository.configHashCode.removeObserver(observer) | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
app/src/main/java/com/android/skip/data/network/MyApiNetwork.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.android.skip.data.network | ||
|
||
import com.android.skip.data.network.api.MyApiService | ||
import retrofit2.Call | ||
import retrofit2.Callback | ||
import retrofit2.Response | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
import kotlin.coroutines.resume | ||
import kotlin.coroutines.resumeWithException | ||
import kotlin.coroutines.suspendCoroutine | ||
|
||
@Singleton | ||
class MyApiNetwork @Inject constructor() { | ||
private val myApiService = ServiceCreator.create(MyApiService::class.java) | ||
|
||
suspend fun fetchSkipConfigV3() = myApiService.getSkipConfigV3().await() | ||
|
||
private suspend fun <T> Call<T>.await(): T{ | ||
return suspendCoroutine { continuation-> | ||
enqueue(object: Callback<T> { | ||
override fun onResponse(call: Call<T>, response: Response<T>) { | ||
val body = response.body() | ||
if (body != null) continuation.resume(body) | ||
else continuation.resumeWithException(RuntimeException("response body is null")) | ||
} | ||
|
||
override fun onFailure(call: Call<T>, t: Throwable) { | ||
continuation.resumeWithException(t) | ||
} | ||
|
||
}) | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
app/src/main/java/com/android/skip/data/network/ServiceCreator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.android.skip.data.network | ||
|
||
import okhttp3.OkHttpClient | ||
import retrofit2.Retrofit | ||
import retrofit2.converter.gson.GsonConverterFactory | ||
import retrofit2.converter.scalars.ScalarsConverterFactory | ||
|
||
object ServiceCreator { | ||
private const val BASE_URL = "https://skip.guoxicheng.top" | ||
|
||
private val httpClient = OkHttpClient.Builder() | ||
|
||
private val builder = Retrofit.Builder() | ||
.baseUrl(BASE_URL) | ||
.client(httpClient.build()) | ||
.addConverterFactory(ScalarsConverterFactory.create()) | ||
.addConverterFactory(GsonConverterFactory.create()) | ||
|
||
private val retrofit = builder.build() | ||
|
||
fun <T> create(serviceClass: Class<T>): T = retrofit.create(serviceClass) | ||
} |
9 changes: 9 additions & 0 deletions
9
app/src/main/java/com/android/skip/data/network/api/MyApiService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.android.skip.data.network.api | ||
|
||
import retrofit2.Call | ||
import retrofit2.http.GET | ||
|
||
interface MyApiService { | ||
@GET("/skip_config_v3.yaml") | ||
fun getSkipConfigV3(): Call<String> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
app/src/main/java/com/android/skip/ui/about/config/ConfigVersionButton.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.android.skip.ui.about.config | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.livedata.observeAsState | ||
import com.android.skip.R | ||
import com.android.skip.data.config.ConfigViewModel | ||
import com.android.skip.ui.components.FlatButton | ||
import com.android.skip.ui.components.RowContent | ||
|
||
@Composable | ||
fun ConfigVersionButton(configViewModel: ConfigViewModel) { | ||
val configVersion = configViewModel.configHashCode.observeAsState() | ||
|
||
FlatButton(content = { | ||
RowContent( | ||
title = R.string.about_config_version, | ||
subTitle = configVersion.value | ||
) | ||
}, onClick = { | ||
configViewModel.readConfig() | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters