Skip to content
This repository has been archived by the owner on Feb 17, 2024. It is now read-only.

Commit

Permalink
feat: WebView in Compose
Browse files Browse the repository at this point in the history
  • Loading branch information
HuanCheng65 committed Jan 29, 2024
1 parent a758639 commit 6a15cbf
Show file tree
Hide file tree
Showing 14 changed files with 1,538 additions and 36 deletions.
1 change: 0 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ dependencies {
implementation "com.google.accompanist:accompanist-insets-ui:$accompanist_version"
implementation "com.google.accompanist:accompanist-systemuicontroller:$accompanist_version"
implementation "com.google.accompanist:accompanist-placeholder-material:$accompanist_version"
implementation "com.google.accompanist:accompanist-webview:$accompanist_version"

def sketch_version = '3.3.0'
implementation "io.github.panpf.sketch3:sketch:$sketch_version"
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/java/com/huanchengfly/tieba/post/MainActivityV2.kt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ import com.huanchengfly.tieba.post.utils.PickMediasRequest
import com.huanchengfly.tieba.post.utils.QuickPreviewUtil
import com.huanchengfly.tieba.post.utils.ThemeUtil
import com.huanchengfly.tieba.post.utils.TiebaUtil
import com.huanchengfly.tieba.post.utils.compose.LaunchActivityForResult
import com.huanchengfly.tieba.post.utils.compose.LaunchActivityRequest
import com.huanchengfly.tieba.post.utils.isIgnoringBatteryOptimizations
import com.huanchengfly.tieba.post.utils.launchUrl
import com.huanchengfly.tieba.post.utils.newIntentFilter
Expand Down Expand Up @@ -160,6 +162,12 @@ class MainActivityV2 : BaseComposeActivity() {
emitGlobalEvent(GlobalEvent.SelectedImages(it.id, it.uris))
}

private val mLaunchActivityForResultLauncher = registerForActivityResult(
LaunchActivityForResult()
) {
emitGlobalEvent(GlobalEvent.ActivityResult(it.requesterId, it.resultCode, it.intent))
}

private val devicePostureFlow: StateFlow<DevicePosture> by lazy {
WindowInfoTracker.getOrCreate(this)
.windowLayoutInfo(this)
Expand Down Expand Up @@ -390,6 +398,14 @@ class MainActivityV2 : BaseComposeActivity() {
PickMediasRequest(it.id, it.maxCount, it.mediaType)
)
}
onGlobalEvent<GlobalEvent.StartActivityForResult> {
mLaunchActivityForResultLauncher.launch(
LaunchActivityRequest(
it.requesterId,
it.intent
)
)
}
TiebaLiteLocalProvider {
TranslucentThemeBackground {
val navController = rememberNavController()
Expand Down
50 changes: 34 additions & 16 deletions app/src/main/java/com/huanchengfly/tieba/post/arch/GlobalEvent.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.huanchengfly.tieba.post.arch

import android.content.Intent
import android.net.Uri
import android.util.Log
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.rememberCoroutineScope
import com.huanchengfly.tieba.post.utils.PickMediasRequest
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow
Expand All @@ -30,7 +32,7 @@ sealed interface GlobalEvent : UiEvent {

data class SelectedImages(
val id: String,
val images: List<Uri>
val images: List<Uri>,
) : GlobalEvent

data class ReplySuccess(
Expand All @@ -40,6 +42,17 @@ sealed interface GlobalEvent : UiEvent {
val subPostId: Long? = null,
val newSubPostId: Long? = null,
) : GlobalEvent

data class StartActivityForResult(
val requesterId: String,
val intent: Intent,
) : GlobalEvent

data class ActivityResult(
val requesterId: String,
val resultCode: Int,
val intent: Intent?,
) : GlobalEvent
}

private val globalEventSharedFlow: MutableSharedFlow<UiEvent> by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
Expand All @@ -58,27 +71,32 @@ suspend fun emitGlobalEventSuspend(event: UiEvent) {
globalEventSharedFlow.emit(event)
}

inline fun <reified Event : UiEvent> CoroutineScope.onGlobalEvent(
noinline filter: (Event) -> Boolean = { true },
noinline listener: suspend (Event) -> Unit,
): Job {
return launch {
GlobalEventFlow
.filterIsInstance<Event>()
.filter {
filter(it)
}
.cancellable()
.collect {
Log.i("GlobalEvent", "onGlobalEvent: $it")
listener(it)
}
}
}

@Composable
inline fun <reified Event : UiEvent> onGlobalEvent(
coroutineScope: CoroutineScope = rememberCoroutineScope(),
noinline filter: (Event) -> Boolean = { true },
noinline listener: suspend (Event) -> Unit
noinline listener: suspend (Event) -> Unit,
) {
DisposableEffect(filter, listener) {
val job = coroutineScope.launch {
GlobalEventFlow
.filterIsInstance<Event>()
.filter {
filter(it)
}
.cancellable()
.collect {
Log.i("GlobalEvent", "onGlobalEvent: $it")
launch {
listener(it)
}
}
}
val job = coroutineScope.onGlobalEvent(filter, listener)
onDispose {
job.cancel()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static com.huanchengfly.tieba.post.utils.FileUtil.FILE_TYPE_DOWNLOAD;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.ClipData;
import android.content.Intent;
Expand Down Expand Up @@ -305,7 +304,6 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
}
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void onActivityResultAboveL(int requestCode, int resultCode, Intent intent) {
if (requestCode != FILE_CHOOSER_RESULT_CODE || uploadMessageAboveL == null)
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.github.panpf.sketch.compose.AsyncImage
import com.huanchengfly.tieba.post.R
import com.huanchengfly.tieba.post.activities.WebViewActivity
import com.huanchengfly.tieba.post.arch.BaseComposeActivity.Companion.LocalWindowSizeClass
import com.huanchengfly.tieba.post.models.PhotoViewData
import com.huanchengfly.tieba.post.ui.common.windowsizeclass.WindowWidthSizeClass
import com.huanchengfly.tieba.post.ui.page.LocalNavigator
import com.huanchengfly.tieba.post.ui.page.destinations.UserProfilePageDestination
import com.huanchengfly.tieba.post.ui.page.destinations.WebViewPageDestination
import com.huanchengfly.tieba.post.ui.widgets.compose.EmoticonText
import com.huanchengfly.tieba.post.ui.widgets.compose.NetworkImage
import com.huanchengfly.tieba.post.ui.widgets.compose.VoicePlayer
Expand Down Expand Up @@ -183,6 +183,7 @@ data class VideoContentRender(
val widthFraction =
if (LocalWindowSizeClass.current.widthSizeClass == WindowWidthSizeClass.Compact) 1f else 0.5f
val context = LocalContext.current
val navigator = LocalNavigator.current

if (picUrl.isNotBlank()) {
val picModifier = Modifier
Expand Down Expand Up @@ -210,7 +211,9 @@ data class VideoContentRender(
contentDescription = stringResource(id = R.string.desc_video),
modifier = picModifier
.clickable {
WebViewActivity.launch(context, webUrl)
navigator.navigate(
WebViewPageDestination(webUrl)
)
},
contentScale = ContentScale.Crop
)
Expand Down Expand Up @@ -314,7 +317,7 @@ fun PbContentText(
when (annotation.tag) {
"url" -> {
val url = annotation.item
launchUrl(context, url)
launchUrl(context, navigator, url)
}

"user" -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.google.accompanist.placeholder.placeholder
import com.huanchengfly.tieba.post.R
import com.huanchengfly.tieba.post.activities.WebViewActivity
import com.huanchengfly.tieba.post.arch.collectPartialAsState
import com.huanchengfly.tieba.post.arch.pageViewModel
import com.huanchengfly.tieba.post.models.database.Account
Expand All @@ -58,6 +57,7 @@ import com.huanchengfly.tieba.post.ui.page.destinations.HistoryPageDestination
import com.huanchengfly.tieba.post.ui.page.destinations.SettingsPageDestination
import com.huanchengfly.tieba.post.ui.page.destinations.ThreadStorePageDestination
import com.huanchengfly.tieba.post.ui.page.destinations.UserProfilePageDestination
import com.huanchengfly.tieba.post.ui.page.destinations.WebViewPageDestination
import com.huanchengfly.tieba.post.ui.widgets.compose.Avatar
import com.huanchengfly.tieba.post.ui.widgets.compose.ConfirmDialog
import com.huanchengfly.tieba.post.ui.widgets.compose.HorizontalDivider
Expand Down Expand Up @@ -369,9 +369,10 @@ fun UserPage(
icon = ImageVector.vectorResource(id = R.drawable.ic_help_outline_black_24),
text = stringResource(id = R.string.my_info_service_center),
onClick = {
WebViewActivity.launch(
context,
"https://tieba.baidu.com/mo/q/hybrid-main-service/uegServiceCenter?cuid=${CuidUtils.getNewCuid()}&cuid_galaxy2=${CuidUtils.getNewCuid()}&cuid_gid=&timestamp=${System.currentTimeMillis()}&_client_version=11.10.8.6&nohead=1"
navigator.navigate(
WebViewPageDestination(
initialUrl = "https://tieba.baidu.com/mo/q/hybrid-main-service/uegServiceCenter?cuid=${CuidUtils.getNewCuid()}&cuid_galaxy2=${CuidUtils.getNewCuid()}&cuid_gid=&timestamp=${System.currentTimeMillis()}&_client_version=12.52.1.0&nohead=1"
)
)
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ fun AboutPage(
contentColor = ExtendedTheme.colors.text
),
onClick = {
launchUrl(context, "https://github.com/HuanCheng65/TiebaLite")
launchUrl(context, navigator, "https://github.com/HuanCheng65/TiebaLite")
},
modifier = Modifier.fillMaxWidth()
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Scaffold
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
Expand Down Expand Up @@ -62,7 +63,12 @@ fun AccountManagePage(
backgroundColor = Color.Transparent,
topBar = {
TitleCentredToolbar(
title = stringResource(id = R.string.title_account_manage),
title = {
Text(
text = stringResource(id = R.string.title_account_manage),
fontWeight = FontWeight.Bold, style = MaterialTheme.typography.h6
)
},
navigationIcon = {
BackNavigationIcon(onBackPressed = { navigator.navigateUp() })
}
Expand Down Expand Up @@ -179,6 +185,7 @@ fun AccountManagePage(
onClick = {
launchUrl(
context,
navigator,
"https://wappass.baidu.com/static/manage-chunk/change-username.html#/showUsername"
)
},
Expand Down
Loading

0 comments on commit 6a15cbf

Please sign in to comment.