|
@@ -7,31 +7,39 @@ import androidx.activity.ComponentActivity
|
|
|
import androidx.activity.compose.rememberLauncherForActivityResult
|
|
|
import androidx.activity.compose.setContent
|
|
|
import androidx.activity.result.contract.ActivityResultContracts
|
|
|
-import androidx.compose.foundation.layout.Column
|
|
|
-import androidx.compose.foundation.layout.fillMaxWidth
|
|
|
-import androidx.compose.foundation.layout.padding
|
|
|
+import androidx.compose.foundation.layout.*
|
|
|
+import androidx.compose.material.*
|
|
|
import androidx.compose.material.Text
|
|
|
+import androidx.compose.material.icons.Icons
|
|
|
+import androidx.compose.material.icons.filled.Add
|
|
|
+import androidx.compose.material.icons.filled.Settings
|
|
|
import androidx.compose.material3.*
|
|
|
-import androidx.compose.runtime.Composable
|
|
|
-import androidx.compose.runtime.mutableStateOf
|
|
|
-import androidx.compose.runtime.remember
|
|
|
+import androidx.compose.material3.Icon
|
|
|
+import androidx.compose.material3.IconButton
|
|
|
+import androidx.compose.runtime.*
|
|
|
+import androidx.compose.ui.Alignment
|
|
|
import androidx.compose.ui.Modifier
|
|
|
import androidx.compose.ui.platform.LocalContext
|
|
|
-import androidx.compose.ui.unit.dp
|
|
|
+import androidx.compose.ui.tooling.preview.Preview
|
|
|
import androidx.compose.ui.viewinterop.AndroidView
|
|
|
import androidx.lifecycle.LifecycleCoroutineScope
|
|
|
import androidx.lifecycle.lifecycleScope
|
|
|
import com.compdfkit.core.document.CPDFDocument
|
|
|
-import com.compdfkit.core.document.CPDFSdk
|
|
|
-import com.compdfkit.ui.reader.ReaderView
|
|
|
+import com.compdfkit.ui.reader.CPDFReaderView
|
|
|
+import com.compdfkit.ui.reader.IReaderViewCallback
|
|
|
+import com.convenient.android.common.utils.ToastUtil
|
|
|
+import com.convenient.android.lib.ui.sample.read.setting.BottomSettingView
|
|
|
import com.convenient.android.lib.ui.theme.SampleTheme
|
|
|
import com.pdf.base.function.onVerifyDocument
|
|
|
+import kotlinx.coroutines.launch
|
|
|
|
|
|
/**
|
|
|
* @author: hubowen
|
|
|
* @date: 2022/9/20
|
|
|
* @description:
|
|
|
*/
|
|
|
+var readerView: CPDFReaderView? = null
|
|
|
+
|
|
|
class ReaderActivity : ComponentActivity() {
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
super.onCreate(savedInstanceState)
|
|
@@ -40,48 +48,111 @@ class ReaderActivity : ComponentActivity() {
|
|
|
readerPage(lifecycleScope)
|
|
|
}
|
|
|
}
|
|
|
- Log.e("AAAA", "${CPDFSdk.getSDKInitialResult()}")
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
|
|
|
-@OptIn(ExperimentalMaterial3Api::class)
|
|
|
+@Preview(showSystemUi = true)
|
|
|
@Composable
|
|
|
-fun readerPage(life: LifecycleCoroutineScope) {
|
|
|
+fun MainPagePreview() {
|
|
|
+ SampleTheme {
|
|
|
+ readerPage(null)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+@OptIn(ExperimentalMaterial3Api::class, ExperimentalMaterialApi::class)
|
|
|
+@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter", "CoroutineCreationDuringComposition")
|
|
|
+@Composable
|
|
|
+fun readerPage(life: LifecycleCoroutineScope?) {
|
|
|
val context = LocalContext.current
|
|
|
|
|
|
- val currentDocument = remember { mutableStateOf<CPDFDocument?>(null) }
|
|
|
+ val coroutineScope = rememberCoroutineScope()
|
|
|
+
|
|
|
+ var currentDocument by remember { mutableStateOf<CPDFDocument?>(null) }
|
|
|
|
|
|
val choosePDFFileLauncher = rememberLauncherForActivityResult(contract = ActivityResultContracts.OpenMultipleDocuments(), onResult = {
|
|
|
//第一个文件
|
|
|
- it[0].apply {
|
|
|
- life.onVerifyDocument(context, this, "", "", { document ->
|
|
|
- currentDocument.value = document
|
|
|
- }, {
|
|
|
- Log.e("AAAA", "初始化失败")
|
|
|
- })
|
|
|
+ when (it.isNotEmpty()) {
|
|
|
+ true -> it[0].apply {
|
|
|
+ life?.onVerifyDocument(context, this, "", "", { document ->
|
|
|
+ currentDocument = document
|
|
|
+ }, {
|
|
|
+ Log.e("AAAA", "初始化失败")
|
|
|
+ })
|
|
|
+ }
|
|
|
+ else -> ToastUtil.showToast(context, "初始化失败")
|
|
|
}
|
|
|
})
|
|
|
|
|
|
- Column {
|
|
|
- SmallTopAppBar(title = {
|
|
|
- Text(text = "ReadView", style = MaterialTheme.typography.titleMedium)
|
|
|
- }, colors = TopAppBarDefaults.smallTopAppBarColors(
|
|
|
- containerColor = MaterialTheme.colorScheme.primary.copy(alpha = .1F)
|
|
|
- ))
|
|
|
-
|
|
|
- ElevatedButton(modifier = Modifier
|
|
|
- .fillMaxWidth()
|
|
|
- .padding(horizontal = 16.dp), onClick = {
|
|
|
- choosePDFFileLauncher.launch(arrayOf("*/*"))
|
|
|
+ val sheetState = rememberModalBottomSheetState(initialValue = ModalBottomSheetValue.Hidden)
|
|
|
+ ModalBottomSheetLayout(
|
|
|
+ modifier = Modifier.fillMaxSize(),
|
|
|
+ sheetState = sheetState,
|
|
|
+ sheetContent = {
|
|
|
+ BottomSettingView()
|
|
|
}) {
|
|
|
- Text(text = "选择文件")
|
|
|
- }
|
|
|
+ Column {
|
|
|
+ Scaffold(
|
|
|
+ modifier = Modifier.fillMaxSize(),
|
|
|
+ topBar = {
|
|
|
+ SmallTopAppBar(
|
|
|
+ title = {
|
|
|
+ Text(text = "ReadView", style = androidx.compose.material3.MaterialTheme.typography.titleMedium)
|
|
|
+ },
|
|
|
+ colors = TopAppBarDefaults.smallTopAppBarColors(
|
|
|
+ containerColor = androidx.compose.material3.MaterialTheme.colorScheme.primary.copy(alpha = .1F)
|
|
|
+ ),
|
|
|
+ actions = {
|
|
|
+ IconButton(onClick = {
|
|
|
+ coroutineScope.launch {
|
|
|
+ sheetState.show()
|
|
|
+ }
|
|
|
+ }) {
|
|
|
+ Icon(Icons.Filled.Settings, null)
|
|
|
+ }
|
|
|
+ IconButton(onClick = {
|
|
|
+ choosePDFFileLauncher.launch(arrayOf("*/*"))
|
|
|
+ }) {
|
|
|
+ Icon(Icons.Filled.Add, null)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ )
|
|
|
+ }
|
|
|
+ ) {
|
|
|
+ Column(modifier = Modifier
|
|
|
+ .fillMaxWidth()
|
|
|
+ .wrapContentHeight(Alignment.CenterVertically)) {
|
|
|
+ //ReaderView展示位置
|
|
|
+ AndroidView(factory = { context ->
|
|
|
+ CPDFReaderView(context).apply {
|
|
|
+ readerView = this
|
|
|
+ //监听设置
|
|
|
+ setReaderViewCallback(object : IReaderViewCallback {
|
|
|
+ override fun onTapMainDocArea() {
|
|
|
+ Log.e("AAAA", "onTapMainDocArea")
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onMoveToChild(p0: Int) {
|
|
|
+ }
|
|
|
|
|
|
- AndroidView(factory = { context ->
|
|
|
- ReaderView(context).apply {
|
|
|
- pdfDocument = currentDocument.value
|
|
|
+ override fun onEndScroll() {
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onScrolling() {
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onRecordLastJumpPageNum(p0: Int) {
|
|
|
+ Log.e("AAAA", "onRecordLastJumpPageNum")
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }, update = { readerView ->
|
|
|
+ readerView.pdfDocument = currentDocument
|
|
|
+ readerView.reloadPages()
|
|
|
+ }, modifier = Modifier
|
|
|
+ .fillMaxWidth()
|
|
|
+ .fillMaxHeight())
|
|
|
+ }
|
|
|
}
|
|
|
- })
|
|
|
+ }
|
|
|
}
|
|
|
}
|