Skip to content

Commit

Permalink
Merge pull request #234 from GuoXiCheng/dev-c
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
GuoXiCheng authored Sep 18, 2024
2 parents 7cace8e + 069e8f7 commit b6912f6
Show file tree
Hide file tree
Showing 14 changed files with 183 additions and 24 deletions.
4 changes: 4 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ android {
}

dependencies {
implementation(libs.converter.scalars)
implementation(libs.retrofit2.converter.gson)
implementation(libs.retrofit)
implementation(libs.okhttp3.okhttp)
implementation(libs.snakeyaml)
implementation(libs.accompanist.drawablepainter)
implementation(libs.androidx.paging.compose)
Expand Down
9 changes: 8 additions & 1 deletion app/src/main/assets/skip_config_v3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,11 @@
- packageName: com.zhihu.android
skipIds:
# fileId=812b304e-63fd-42da-b200-7497a2e635c4&nodeId=21
- id: com.zhihu.android:id/btn_skip
- id: com.zhihu.android:id/btn_skip

# 汽水音乐
- packageName: com.luna.music
skipBounds:
# fileId=ab69a19e-56e8-4983-9656-cdbab1b09682&nodeId=13
- bound: 1244,176,1384,316
resolution: 1440x3200
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.android.skip.data.config

import android.content.Context
import android.graphics.Rect
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.android.skip.data.network.MyApiNetwork
import com.android.skip.dataclass.ConfigLoadSchema
import com.android.skip.dataclass.ConfigReadSchema
import com.android.skip.dataclass.LoadSkipBound
Expand All @@ -13,31 +13,48 @@ import com.android.skip.dataclass.ReadClick
import com.blankj.utilcode.util.ScreenUtils
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.yaml.snakeyaml.Yaml
import java.security.MessageDigest
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class ConfigReadRepository @Inject constructor() {
private val _configHashCode = MutableLiveData<Int>()
val configHashCode: LiveData<Int> = _configHashCode
class ConfigReadRepository @Inject constructor(
private val myApiNetwork: MyApiNetwork
) {
private val _configHashCode = MutableLiveData<String>()
val configHashCode: LiveData<String> = _configHashCode

private lateinit var configReadSchemaList: List<ConfigReadSchema>

fun readConfig(context: Context) {
val yamlContent = context.assets.open("skip_config_v3.yaml").use { input ->
val yaml = Yaml().load<List<ConfigReadSchema>>(input)
yaml
}
suspend fun readConfig() = withContext(Dispatchers.IO) {
val configV3 = myApiNetwork.fetchSkipConfigV3()
val yamlContent = Yaml().load<List<ConfigReadSchema>>(configV3)

val gson = Gson()
val jsonStr = gson.toJson(yamlContent)
val type = object : TypeToken<List<ConfigReadSchema>>() {}.type
configReadSchemaList = gson.fromJson(jsonStr, type)

_configHashCode.postValue(jsonStr.hashCode())
_configHashCode.postValue(md5(jsonStr))
}

// fun readConfig(context: Context) {
// val yamlContent = context.assets.open("skip_config_v3.yaml").use { input ->
// val yaml = Yaml().load<List<ConfigReadSchema>>(input)
// yaml
// }
//
// val gson = Gson()
// val jsonStr = gson.toJson(yamlContent)
// val type = object : TypeToken<List<ConfigReadSchema>>() {}.type
// configReadSchemaList = gson.fromJson(jsonStr, type)
//
// _configHashCode.postValue(md5(jsonStr))
// }

fun handleConfig(): Map<String, ConfigLoadSchema> {
val screenWidth = ScreenUtils.getScreenWidth()
val screenHeight = ScreenUtils.getScreenHeight()
Expand Down Expand Up @@ -110,4 +127,9 @@ class ConfigReadRepository @Inject constructor() {
screenHeight
)
}

private fun md5(input: String): String {
val bytes = MessageDigest.getInstance("MD5").digest(input.toByteArray())
return bytes.joinToString("") { "%02x".format(it) }
}
}
26 changes: 20 additions & 6 deletions app/src/main/java/com/android/skip/data/config/ConfigViewModel.kt
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 app/src/main/java/com/android/skip/data/network/MyApiNetwork.kt
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 app/src/main/java/com/android/skip/data/network/ServiceCreator.kt
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)
}
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>
}
22 changes: 21 additions & 1 deletion app/src/main/java/com/android/skip/ui/about/AboutActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,30 @@ package com.android.skip.ui.about
import android.content.Intent
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.runtime.Composable
import com.android.skip.MyApp
import com.android.skip.R
import com.android.skip.data.config.ConfigViewModel
import com.android.skip.ui.about.config.ConfigVersionButton
import com.android.skip.ui.components.FlatButton
import com.android.skip.ui.components.RowContent
import com.android.skip.ui.components.ScaffoldPage
import com.android.skip.ui.theme.AppTheme
import com.android.skip.ui.webview.WebViewActivity
import com.blankj.utilcode.util.AppUtils
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class AboutActivity : AppCompatActivity() {
private val configViewModel by viewModels<ConfigViewModel>()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
AppTheme {
ScaffoldPage(R.string.about, { finish() },{
ScaffoldPage(R.string.about, { finish() }, {
AboutGithub {
val intent = Intent(MyApp.context, WebViewActivity::class.java).apply {
putExtra("url", R.string.about_github_subtitle)
Expand All @@ -32,6 +40,8 @@ class AboutActivity : AppCompatActivity() {
}
startActivity(intent)
}
AboutAppVersion(AppUtils.getAppVersionName())
ConfigVersionButton(configViewModel)
})
}
}
Expand All @@ -56,4 +66,14 @@ fun AboutDocs(onClick: () -> Unit) {
subTitle = R.string.about_docs_subtitle
)
}, onClick = onClick)
}

@Composable
fun AboutAppVersion(version: String) {
FlatButton(content = {
RowContent(
title = R.string.about_app_version,
subTitle = version
)
})
}
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()
})
}
2 changes: 1 addition & 1 deletion app/src/main/java/com/android/skip/ui/main/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class MainActivity : AppCompatActivity() {
}
}

configViewModel.readConfig(this)
configViewModel.readConfig()
}

override fun onResume() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package com.android.skip.ui.whitelist.list

import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import com.android.skip.dataclass.AppListItem
import com.android.skip.ui.whitelist.WhiteListRepository
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.LogUtils
import javax.inject.Inject
import javax.inject.Singleton

Expand All @@ -26,7 +23,6 @@ class AppListRepository @Inject constructor() {
} else {
appInfos.filterNot { it.isSystem }
}
LogUtils.d(isShowSystem)
val fromIndex = currentPage * pageSize
val toIndex = minOf(fromIndex + pageSize, apps.size)

Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
<string name="about_docs_title">文档</string>
<string name="about_docs_subtitle">https://skip.guoxicheng.top</string>
<string name="about_docs_url">https://skip.guoxicheng.top/guide/intro/what-is-skip</string>
<string name="about_current_version">当前版本号: </string>
<string name="about_app_version">应用版本号</string>
<string name="about_config_version">配置版本号</string>

<string name="toast_start_accessibility_first">请先开启无障碍服务</string>
<string name="toast_save_success">保存成功</string>
Expand Down
Binary file added capture/ab69a19e-56e8-4983-9656-cdbab1b09682.zip
Binary file not shown.
7 changes: 7 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
accompanistDrawablepainter = "0.34.0"
activityCompose = "1.9.0"
agp = "8.5.1"
converterScalars = "2.5.0"
gson = "2.10.1"
hiltAndroidCompiler = "2.51.1"
hiltAndroid = "2.51.1"
Expand All @@ -13,7 +14,9 @@ espressoCore = "3.6.1"
appcompat = "1.7.0"
material = "1.10.0"
activity = "1.9.0"
okhttpVersion = "4.12.0"
pagingRuntime = "3.3.0"
retrofit = "2.9.0"
runtimeAndroid = "1.6.8"
foundationAndroid = "1.6.8"
material3Android = "1.2.1"
Expand All @@ -30,6 +33,7 @@ androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref =
androidx-paging-compose = { module = "androidx.paging:paging-compose", version.ref = "pagingRuntime" }
androidx-paging-runtime = { module = "androidx.paging:paging-runtime", version.ref = "pagingRuntime" }
androidx-ui = { module = "androidx.compose.ui:ui", version.ref = "ui" }
converter-scalars = { module = "com.squareup.retrofit2:converter-scalars", version.ref = "converterScalars" }
gson = { module = "com.google.code.gson:gson", version.ref = "gson" }
hilt-android = { module = "com.google.dagger:hilt-android", version.ref = "hiltAndroid" }
hilt-android-compiler = { module = "com.google.dagger:hilt-android-compiler", version.ref = "hiltAndroidCompiler" }
Expand All @@ -42,6 +46,9 @@ androidx-activity = { group = "androidx.activity", name = "activity", version.re
androidx-runtime-android = { group = "androidx.compose.runtime", name = "runtime-android", version.ref = "runtimeAndroid" }
androidx-foundation-android = { group = "androidx.compose.foundation", name = "foundation-android", version.ref = "foundationAndroid" }
androidx-material3-android = { group = "androidx.compose.material3", name = "material3-android", version.ref = "material3Android" }
okhttp3-okhttp = { module = "com.squareup.okhttp3:okhttp", version.ref = "okhttpVersion" }
retrofit = { module = "com.squareup.retrofit2:retrofit", version.ref = "retrofit" }
retrofit2-converter-gson = { module = "com.squareup.retrofit2:converter-gson", version.ref = "retrofit" }
snakeyaml = { module = "org.yaml:snakeyaml", version.ref = "snakeyaml" }
utilcodex = { module = "com.blankj:utilcodex", version.ref = "utilcodex" }
androidx-runtime-livedata = { group = "androidx.compose.runtime", name = "runtime-livedata", version.ref = "runtimeLivedata" }
Expand Down

0 comments on commit b6912f6

Please sign in to comment.