Skip to content

Commit

Permalink
🎨 模块层级遗留问题整理
Browse files Browse the repository at this point in the history
  • Loading branch information
985892345 committed Dec 27, 2024
1 parent 21a2624 commit 3104be6
Show file tree
Hide file tree
Showing 44 changed files with 139 additions and 1,104 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.cyxbs.components.account.api

import com.cyxbs.components.account.api.utils.Value
import io.reactivex.rxjava3.core.Observable

interface IUserService {
Expand Down Expand Up @@ -35,18 +34,13 @@ interface IUserService {
*
* 旧模块中推荐转换为 Flow 然后配合生命周期
* ```
* // build.gradle.kts 需要先依赖
* dependCoroutinesRx3()
*
* // 使用例子如下
* IAccountService::class.impl
* .getUserService()
* .observeStuNumEvent()
* .asFlow() // asFlow() 将 Observable 装换为 Flow
* .onEach {
* it.nullUnless {
* initFragment()
* }
* // ...
* }.launchIn(lifecycleScope)
* ```
*
Expand All @@ -62,25 +56,21 @@ interface IUserService {
* .observeOn(Schedulers.io()) // 注意:你需要使用 observeOn 才能切换线程,subscribeOn 无法切换发送源的线程
* .switchMap { value ->
* // switchMap 可以在上游发送新的数据时自动关闭上一次数据生成的 Observable
* value.nullUnless(Observable.never()) {
* if (stuNum.isEmpty()) Observable.never()
* else LessonDataBase.INSTANCE.getStuLessonDao() // 数据库
* .observeAllLesson(stuNum) // 观察数据库的数据变动,这是 Room 的响应式编程
* .distinctUntilChanged() // 必加,因为 Room 每次修改都会回调,所以需要加这个去重
* if (it.isEmpty()) Observable.just(emptyList()) else {
* LessonDataBase.INSTANCE.getStuLessonDao() // 数据库
* .observeAllLesson(stuNum) // 观察数据库的数据变动,这是 Room 的响应式编程
* .distinctUntilChanged() // 必加,因为 Room 每次修改都会回调,所以需要加这个去重
* .doOnSubscribe {
* getLesson(stuNum, isNeedOldList).safeSubscribeBy() // 在开始订阅时请求一次云端数据
* getLesson(stuNum, isNeedOldList).safeSubscribeBy()
* }.map { StuResult(stuNum, it) }
* .subscribeOn(Schedulers.io())
* }
* }
* ```
*
* ### 3、为什么使用 Value 包裹 ?
* 因为 Rxjava 不允许数据为空值,所以使用 Value 包裹了一层
*
* - 更多注意事项请看 [observeStuNumEvent]
*/
fun observeStuNumState(): Observable<Value<String>>
fun observeStuNumState(): Observable<String>

/**
* 观察学号的改变(事件)
Expand All @@ -89,5 +79,5 @@ interface IUserService {
*
* ## 更多注意事项请看 [observeStuNumState]
*/
fun observeStuNumEvent(): Observable<Value<String>>
fun observeStuNumEvent(): Observable<String>
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,13 @@ interface IUserStateService {
*
* 旧模块中推荐转换为 Flow 然后配合生命周期,旧模块的使用方式:
* ```
* // build.gradle.kts 需要先依赖
* dependCoroutinesRx3()
*
* // 使用例子如下
* IAccountService::class.impl
* .getUserService()
* .observeStuNumEvent()
* .asFlow() // asFlow() 将 Observable 装换为 Flow
* .onEach {
* it.nullUnless {
* initFragment()
* }
* // ...
* }.launchIn(lifecycleScope) // 这里请注意 Fragment 中要使用 viewLifecycleOwner.lifecycleScope
* ```
*
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import com.cyxbs.components.account.api.IUserTokenService
import com.cyxbs.components.account.bean.LoginParams
import com.cyxbs.components.account.bean.RefreshParams
import com.cyxbs.components.account.bean.TokenWrapper
import com.cyxbs.components.account.utils.UserInfoEncryption
import com.cyxbs.components.account.api.utils.Value
import com.cyxbs.components.utils.utils.secret.Secret
import com.cyxbs.components.utils.extensions.Value
import com.cyxbs.components.account.bean.ErrorMsg
import com.cyxbs.components.account.bean.UserInfo
import com.cyxbs.pages.login.api.ILoginService
Expand Down Expand Up @@ -43,7 +43,7 @@ internal class AccountService : IAccountService {
private val mUserService: IUserService = UserService()
private val mUserStateService: IUserStateService = UserStateService()
private val mUserTokenSerVice: IUserTokenService = UserTokenSerVice()
private val mUserInfoEncryption = UserInfoEncryption()
private val mUserInfoEncryption = Secret()

private var user: UserInfo? = null
@Volatile
Expand Down Expand Up @@ -118,21 +118,19 @@ internal class AccountService : IAccountService {

// 发送学号给下游
fun emitStuNum(stuNum: String?) {
val value = Value(
if (stuNum == null || stuNum.isBlank()) null else stuNum
)
val value = stuNum ?: ""
stuNumState.onNext(value)
stuNumEvent.onNext(value)
}

private val stuNumState = BehaviorSubject.create<Value<String>>()
private val stuNumEvent = PublishSubject.create<Value<String>>()
private val stuNumState = BehaviorSubject.create<String>()
private val stuNumEvent = PublishSubject.create<String>()

override fun observeStuNumState(): Observable<Value<String>> {
override fun observeStuNumState(): Observable<String> {
return stuNumState.distinctUntilChanged()
}

override fun observeStuNumEvent(): Observable<Value<String>> {
override fun observeStuNumEvent(): Observable<String> {
return stuNumEvent.distinctUntilChanged()
}
}
Expand Down
4 changes: 2 additions & 2 deletions cyxbs-components/base/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ plugins {
id("manager.composeLib")
}

useARouter(false) // lib_base 模块不包含实现类,不需要处理注解
useDataBinding(false) // lib_base 模块只依赖 DataBinding 但不开启 DataBinding
useARouter(false) // base 模块不包含实现类,不需要处理注解
useDataBinding(false) // base 模块只依赖 DataBinding 但不开启 DataBinding

dependencies {
implementation(projects.cyxbsComponents.init)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import io.reactivex.rxjava3.disposables.Disposable
/**
* 实现该接口,即代表该类支持自动关闭 Rxjava
*
* ## 为什么不放到 lib_utils 中?
* 如果放到 lib_utils 中,在只依赖 lib_base 的时候会出现无法继承 BaseActivity 的情况
* ## 为什么不放到 utils 中?
* 如果放到 utils 中,在只依赖 base 的时候会出现无法继承 BaseActivity 的情况
*
* 所以要求 base 类实现的接口尽量不要放在其他模块内
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import com.cyxbs.components.utils.extensions.appContext
/**
* Toast 工具类接口
*
* ## 为什么不放到 lib_utils 中?
* 如果放到 lib_utils 中,在值依赖 lib_base 的时候会出现无法继承 BaseActivity 的情况
* ## 为什么不放到 utils 中?
* 如果放到 utils 中,在值依赖 base 的时候会出现无法继承 BaseActivity 的情况
*
* 所以要求 base 类实现的接口尽量不要放在其他模块内
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<!--视觉要的若隐若现阴影的 endColor,在 drawable/config_layer_looming_shadow 中使用-->
<color name="config_layer_looming_shadow_end_color">#1D1D1D</color>

<!--lib_base 中公用的 BaseDialog 的按钮颜色-->
<!--base 中公用的 BaseDialog 的按钮颜色-->
<color name="config_choose_dialog_btn_positive">#4A44E4</color>
<color name="config_choose_dialog_btn_negative">#CC5A5A5A</color>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<!--视觉要的若隐若现阴影的 endColor,在 drawable/config_layer_looming_shadow 中使用-->
<color name="config_layer_looming_shadow_end_color">#FBFCFF</color>

<!--lib_base 中公用的 BaseDialog 的按钮颜色-->
<!--base 中公用的 BaseDialog 的按钮颜色-->
<color name="config_choose_dialog_btn_positive">#4A44E4</color>
<color name="config_choose_dialog_btn_negative">#C3D4EE</color>

Expand Down
2 changes: 1 addition & 1 deletion cyxbs-components/init/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# api_init
# init
该模块主要用于应用初始化加载的 SPI 注入

## 使用教程
Expand Down
2 changes: 1 addition & 1 deletion cyxbs-components/utils/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ plugins {
id("manager.composeLib")
}

useARouter(false) // lib_utils 模块不包含实现类,不需要处理注解
useARouter(false) // utils 模块不包含实现类,不需要处理注解

kotlin {
sourceSets {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,43 @@
package com.cyxbs.components.utils.extensions

import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract

/**
* .
*
* @author 985892345
* 2023/3/1 14:53
*/

@OptIn(ExperimentalContracts::class)
inline fun String?.ifNull(action: () -> Unit): Boolean {
contract { callsInPlace(action, InvocationKind.EXACTLY_ONCE) }
return if (this != null) false else {
action.invoke()
true
}
}

@OptIn(ExperimentalContracts::class)
inline fun String?.ifNullOrEmpty(action: () -> Unit): Boolean {
contract { callsInPlace(action, InvocationKind.EXACTLY_ONCE) }
return if (!this.isNullOrEmpty()) false else {
action.invoke()
true
}
}

@OptIn(ExperimentalContracts::class)
inline fun String?.ifNullOrBlank(action: () -> Unit): Boolean {
contract { callsInPlace(action, InvocationKind.EXACTLY_ONCE) }
return if (!this.isNullOrBlank()) false else {
action.invoke()
true
}
}

/**
* 遍历每行并替换
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.cyxbs.components.account.utils
package com.cyxbs.components.utils.utils.secret

/**
* Stripped-down version of the SHA1PRNG provided by the Crypto provider.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.cyxbs.components.account.utils
package com.cyxbs.components.utils.utils.secret

import android.util.Base64
import java.nio.charset.StandardCharsets

/**
* @author Haruue Icymoon haruue@caoyue.com.cn
*/
class UserInfoEncryption {
class Secret {

private var isSupportEncrypt = true

init {
Expand All @@ -18,34 +19,33 @@ class UserInfoEncryption {
}
}

fun encrypt(json: String): String {
if (!isSupportEncrypt) return json
fun encrypt(input: String): String {
if (!isSupportEncrypt) return input
return try {
Base64.encodeToString(
SerialAESEncryptor.encrypt(json.toByteArray(StandardCharsets.UTF_8)),
SerialAESEncryptor.encrypt(input.toByteArray(StandardCharsets.UTF_8)),
Base64.DEFAULT
)
} catch (e: Exception) {
e.printStackTrace()
json
input
}
}

fun decrypt(base64Encrypted: String): String {
if (base64Encrypted == "") return ""
if (!isSupportEncrypt) return base64Encrypted
fun decrypt(input: String): String {
if (!isSupportEncrypt) return input
return try {
String(
SerialAESEncryptor.decrypt(
Base64.decode(
base64Encrypted,
input,
Base64.DEFAULT
)
), StandardCharsets.UTF_8
)
} catch (e: Exception) {
e.printStackTrace()
""
input
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.cyxbs.components.account.utils
package com.cyxbs.components.utils.utils.secret

import android.os.Build
import com.cyxbs.components.account.utils.InsecureSHA1PRNGKeyDerivator.Companion.deriveInsecureKey
import com.cyxbs.components.utils.utils.secret.InsecureSHA1PRNGKeyDerivator.Companion.deriveInsecureKey
import java.nio.charset.StandardCharsets
import javax.crypto.Cipher
import javax.crypto.spec.IvParameterSpec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ object AffairRepository {
.getUserService()
.observeStuNumState()
.observeOn(Schedulers.io())
.switchMap { value ->
.switchMap {
// 使用 switchMap 可以停止之前学号的订阅
value.nullUnless(Observable.just(emptyList())) {
if (it.isEmpty()) Observable.just(emptyList()) else {
AffairDao.observeAffair(it)
.distinctUntilChanged()
.doOnSubscribe {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ object StuLessonRepository {
.getUserService()
.observeStuNumState()
.observeOn(Schedulers.io())
.switchMap { value ->
.switchMap { stuNum ->
// 使用 switchMap 可以停止之前学号的订阅
value.nullUnless(Observable.just(emptyList())) { stuNum ->
if (stuNum.isEmpty()) Observable.just(emptyList()) else {
if (ILessonService.isUseLocalSaveLesson) {
LessonDataBase.stuLessonDao
.observeLesson(stuNum)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ object LinkRepository {
.getUserService()
.observeStuNumState()
.observeOn(Schedulers.io())
.switchMap { value ->
.switchMap {
// 使用 switchMap 可以停止之前学号的订阅
value.nullUnless(Observable.just(LinkStuEntity.NULL)) {
if (it.isEmpty()) Observable.just(LinkStuEntity.NULL) else {
mLinkStuDB.observeLinkStu(it) // 然后观察数据库
.distinctUntilChanged() // 必加,因为 Room 每次修改都会回调,所以需要加个这个去重
.doOnSubscribe {
Expand Down
Loading

0 comments on commit 3104be6

Please sign in to comment.