Skip to content

Commit

Permalink
Merge pull request #515 from RedrockMobile/guoxiangrui/feature/projec…
Browse files Browse the repository at this point in the history
…t_hierarchy_opt

compose重构登录页,desktop和wasm同步支持
  • Loading branch information
985892345 authored Jan 6, 2025
2 parents 3b4e449 + d196590 commit 09ee77f
Show file tree
Hide file tree
Showing 94 changed files with 4,717 additions and 1,117 deletions.
1 change: 0 additions & 1 deletion build-logic/manager/src/main/kotlin/manager.app.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import org.jetbrains.compose.desktop.application.dsl.TargetFormat
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.targets.js.webpack.KotlinWebpackConfig
import rule.ModuleNamespaceCheckRule

plugins {
Expand Down
15 changes: 14 additions & 1 deletion build-logic/plugin/kmp/src/main/kotlin/kmp.compose.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ kotlin {
implementation(compose.materialIconsExtended)
implementation(libsEx.`compose-navigation`)
implementation(libsEx.`compose-lifecycle`)
implementation(libsEx.`compose-constraintLayout`)
}

androidMain.dependencies {
implementation(compose.preview)
implementation(libsEx.`androidx-activity-compose`)
implementation(libsEx.`compose-activity`)
}

if (Multiplatform.enableDesktop(project)) {
Expand All @@ -41,6 +42,18 @@ plugins.withId("com.android.base") {
dependencies {
add("debugImplementation", compose.uiTooling)
}
configurations.getByName("androidMainImplementation") {
// 目前第三方的 constraintlayout 在安卓上的实现与 constraintlayout-core 存在依赖冲突
// 所以这里 exclude 掉对应的 -android 依赖,然后下面在安卓上单独依赖官方的 constraintlayout-compose
exclude(group = "tech.annexflow.compose", module = "constraintlayout-compose-multiplatform-android")
}
kotlin {
sourceSets {
androidMain.dependencies {
implementation(libsEx.`compose-constraintLayout-android`)
}
}
}
}

composeCompiler {
Expand Down
15 changes: 4 additions & 11 deletions cyxbs-applications/test/src/desktopMain/kotlin/CyxbsDesktopApp.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Text
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.launchApplication
import androidx.compose.ui.window.rememberWindowState
import com.cyxbs.components.utils.coroutine.runApp
import com.cyxbs.pages.login.ui.LoginPage
import com.g985892345.provider.cyxbsmobile.cyxbsapplications.test.TestKtProviderInitializer

/**
Expand All @@ -22,7 +17,7 @@ import com.g985892345.provider.cyxbsmobile.cyxbsapplications.test.TestKtProvider
fun main() = runApp {
TestKtProviderInitializer.tryInitKtProvider()
launchApplication {
val width = 1000
val width = 800
val height = 600
Window(
onCloseRequest = ::exitApplication,
Expand All @@ -31,11 +26,9 @@ fun main() = runApp {
// resizable = false,
) {
remember {
// this.window.minimumSize = java.awt.Dimension(width, height)
}
Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
Text(text = "桌上重邮", fontSize = 20.sp)
this.window.minimumSize = java.awt.Dimension(360, 600)
}
LoginPage()
}
}
}
15 changes: 2 additions & 13 deletions cyxbs-applications/test/src/wasmJsMain/kotlin/CyxbsWasmJsApp.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.ComposeViewport
import com.cyxbs.pages.login.ui.LoginPage
import com.g985892345.provider.cyxbsmobile.cyxbsapplications.test.TestKtProviderInitializer
import kotlinx.browser.document

Expand All @@ -23,12 +18,6 @@ fun main() {
ComposeViewport(
viewportContainer = document.getElementById("compose")!!,
) {
MaterialTheme(
typography = createTypography(),
) {
Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
Text(text = "网上重邮", fontSize = 20.sp)
}
}
LoginPage()
}
}
66 changes: 0 additions & 66 deletions cyxbs-applications/test/src/wasmJsMain/kotlin/Font.kt

This file was deleted.

4 changes: 2 additions & 2 deletions cyxbs-applications/test/src/wasmJsMain/resources/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
flex-direction: column;
}
#compose {
flex: 1; /* 剩余空间 */
width: 100%; /* 横向铺满 */
height: 100%;
width: 100%;
}
</style>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,16 @@ abstract class BaseApp : Application() {
mInitialManager.init()
}

//隐私策略同意了
fun privacyAgree() {
mInitialManager.privacyAgree()
// 隐私策略同意,在登录后调用
fun tryPrivacyAgree() {
mInitialManager.tryPrivacyAgree()
}
//没同意
fun privacyDenied() {
mInitialManager.privacyDenied()

// 取消同意隐私策略,用于重新登录
fun cancelPrivacyAgree() {
mInitialManager.cancelPrivacyAgree()
}

private fun initActivityManger() {
registerActivityLifecycleCallbacks(
object : ActivityLifecycleCallbacksImpl {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package com.cyxbs.components.base.ui

import android.annotation.SuppressLint
import android.content.pm.ActivityInfo
import android.graphics.Color
import android.os.Bundle
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.annotation.CallSuper
import androidx.annotation.IdRes
import androidx.annotation.LayoutRes
Expand Down Expand Up @@ -93,19 +93,25 @@ abstract class BaseActivity : AppCompatActivity, BaseUi {
* 是否沉浸式状态栏
*
* ## 注意
* 沉浸式后,状态栏不会再有东西占位,界面会默认上移,
* 可以给布局加上 android:fitsSystemWindows=true (但不建议给根布局加,一般是给第二个布局加),
* 不同布局该属性效果不同,请给合适的布局添加
*
* ## 比如
* - 大部分情况下是给第二层布局添加 fitsSystemWindows=true,因为最外层布局需要提供背景给状态栏,而第二层布局需要下移状态栏
* - 如果你使用了 BottomSheet,那么大概率需要给 BottomSheet 加上 fitsSystemWindows=true。
* (注意: CoordinatorLayout 设置 fitsSystemWindows 无效,但可以在外面包一层 FrameLayout,给它加上 fitsSystemWindows,具体可以看 main 模块里面的课表写法)
* -
* 沉浸式后,状态栏和导航栏不会再有东西占位,界面会默认上移,
* 可以使用 ViewCompat.setOnApplyWindowInsetsListener(View) 给对应的 View 进行状态栏或者导航栏偏移
*
* 但注意:OnApplyWindowInsets 给父布局消耗后子布局就不会回调
* 如果在 Fragment 场景,则可以使用:
* ```
* requireActivity().window.decorView.doOnAttach {
* val statusBarsInsets = WindowInsetsCompat.toWindowInsetsCompat(it.rootWindowInsets)
* .getInsets(WindowInsetsCompat.Type.statusBars())
* view.setPadding(statusBarsInsets.left, statusBarsInsets.top, statusBarsInsets.right, statusBarsInsets.bottom)
* }
* ```
* 来获取状态栏高度然后单独设置 padding
*
* 详细可看:https://juejin.cn/post/7395866692772085800
*/
protected open val isCancelStatusBar: Boolean
protected open val enableEdgeToEdge: Boolean
get() = true

/**
* 是否处于转屏或异常重建后的 Activity 状态
*/
Expand All @@ -116,31 +122,24 @@ abstract class BaseActivity : AppCompatActivity, BaseUi {
@SuppressLint("SourceLockedOrientationActivity")
override fun onCreate(savedInstanceState: Bundle?) {
mIsActivityRebuilt = savedInstanceState != null
if (enableEdgeToEdge) { // 沉浸式状态栏
configEdgeToEdge()
}
super.onCreate(savedInstanceState)
if (isPortraitScreen) { // 锁定竖屏
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
}

if (isCancelStatusBar) { // 沉浸式状态栏
cancelStatusBar()
}
}

private fun cancelStatusBar() {
private fun configEdgeToEdge() {
enableEdgeToEdge()
val window = this.window
val decorView = window.decorView

// 这是 Android 做了兼容的 Compat 包
// 注意,使用了下面这个方法后,状态栏不会再有东西占位,
// 可以给根布局加上 android:fitsSystemWindows=true
// 不同布局该属性效果不同,请给合适的布局添加
WindowCompat.setDecorFitsSystemWindows(window, false)
val windowInsetsController = WindowCompat.getInsetsController(window, decorView)
// 如果你要白色的状态栏字体,请在你直接的 Activity 中单独设置 isAppearanceLightStatusBars,这里不提供方法
windowInsetsController.isAppearanceLightStatusBars = isDaytimeMode()
window.statusBarColor = Color.TRANSPARENT //把状态栏颜色设置成透明
}

/**
* 替换 Fragment 的正确用法。
* 如果不按照正确方式使用,会造成 ViewModel 失效,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,11 @@ import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import com.cyxbs.components.init.appApplication
import com.cyxbs.components.base.utils.RxjavaLifecycle
import com.cyxbs.components.base.utils.ToastUtils
import io.reactivex.rxjava3.disposables.CompositeDisposable
import io.reactivex.rxjava3.disposables.Disposable
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.launch
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext

/**
*
Expand Down Expand Up @@ -96,7 +89,7 @@ import kotlin.coroutines.EmptyCoroutineContext
*
* # 更多封装请往父类和接口查看
*/
abstract class BaseViewModel : ViewModel(), RxjavaLifecycle, ToastUtils {
actual abstract class BaseViewModel : CommonBaseViewModel(), RxjavaLifecycle {

val appContext: Context
get() = appApplication
Expand All @@ -108,13 +101,6 @@ abstract class BaseViewModel : ViewModel(), RxjavaLifecycle, ToastUtils {
super.onCleared()
mDisposables.clear()
}

/**
* 开启协程并收集 Flow
*/
protected fun <T> Flow<T>.collectLaunch(action: suspend (value: T) -> Unit): Job = launch {
collect{ action.invoke(it) }
}

/**
* 返回一个缓存值为 0 表示事件的 SharedFlow,不会因为 Activity 重建而出现数据倒灌问题
Expand All @@ -132,17 +118,6 @@ abstract class BaseViewModel : ViewModel(), RxjavaLifecycle, ToastUtils {
}
return sharedFlow
}


protected fun launch(
context: CoroutineContext = EmptyCoroutineContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
block: suspend CoroutineScope.() -> Unit
): Job = viewModelScope.launch(context, start, block)





/**
* 实现 [RxjavaLifecycle] 的方法,用于带有生命周期的调用
Expand Down
Loading

0 comments on commit 09ee77f

Please sign in to comment.