Commit 03a68214 authored by wanglei's avatar wanglei

...处理数据刷新

parent d040b8f7
......@@ -88,6 +88,16 @@
android:screenOrientation="portrait"
tools:ignore="DiscouragedApi,LockedOrientationActivity" />
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713" />
......
package com.base.pdfoneread.ui
import android.app.Application
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
class MyApplication : Application() {
companion object {
lateinit var appContext: MyApplication
//全局协程作用域
val appScope = CoroutineScope(Dispatchers.IO + SupervisorJob())
}
override fun onCreate() {
......
......@@ -21,14 +21,19 @@ import com.base.pdfoneread.ui.views.DialogCallBack
import com.base.pdfoneread.ui.views.DocumentDialog.showDocumentHomeMoreDialog
import com.base.pdfoneread.ui.views.PdfDialog.showPdfHomeMoreDialog
import com.base.pdfoneread.utils.BarUtils
import com.base.pdfoneread.utils.LogEx
import com.base.pdfoneread.utils.PdfBoxUtils.checkPdfEncryption
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import java.io.File
class DocumentActivity : BaseActivity<ActivityDocumentBinding>(), DialogCallBack {
private val TAG = "DocumentActivity"
var type: String = ""
override val binding: ActivityDocumentBinding by lazy {
......@@ -141,6 +146,14 @@ class DocumentActivity : BaseActivity<ActivityDocumentBinding>(), DialogCallBack
initData()
}
initPreData()
lifecycleScope.launch {
isRefreshOver.collectLatest { value ->
LogEx.logDebug(TAG, "value=$value")
initData()
//刷新结束不再监听
cancel()
}
}
}
@SuppressLint("NotifyDataSetChanged")
......@@ -192,15 +205,8 @@ class DocumentActivity : BaseActivity<ActivityDocumentBinding>(), DialogCallBack
else -> getGlobalAllList()
}
if (list.isEmpty()) {
binding.swipeRefreshLayout.isRefreshing = true
} else {
binding.llEmpty.visibility = View.GONE
adapter?.submitList(getRvAdList(list))
}
if (isRefreshing.get()) {
initData()
}
runOnUI(list)
}
......@@ -214,7 +220,11 @@ class DocumentActivity : BaseActivity<ActivityDocumentBinding>(), DialogCallBack
TYPE_PPT -> getPptDocument(context)
else -> getAllDocument(context)
}
launch(Dispatchers.Main) {
runOnUI(list)
}
private fun runOnUI(list: List<DocumentBean>) {
lifecycleScope.launch(Dispatchers.Main) {
if (list.isEmpty()) {
binding.llEmpty.visibility = View.VISIBLE
adapter?.submitList(listOf())
......@@ -224,7 +234,6 @@ class DocumentActivity : BaseActivity<ActivityDocumentBinding>(), DialogCallBack
}
binding.swipeRefreshLayout.isRefreshing = false
}
}
//region DialogCallBack
......
......@@ -21,19 +21,24 @@ import com.base.pdfoneread.bean.DocumentBean.Companion.TYPE_PPT
import com.base.pdfoneread.bean.DocumentBean.Companion.TYPE_WORD
import com.base.pdfoneread.bean.DocumentBean.Companion.deepCopy
import com.base.pdfoneread.bean.MediaBean
import com.base.pdfoneread.ui.MyApplication.Companion.appScope
import com.base.pdfoneread.utils.AssetUtils.readByteArrayFromAsset
import com.base.pdfoneread.utils.LogEx
import com.base.pdfoneread.utils.PdfBoxUtils.checkPdfEncryption
import com.base.pdfoneread.utils.SpStringUtils
import com.base.pdfoneread.utils.SpStringUtils.BOOKMARK_KEY
import com.base.pdfoneread.utils.getMediaFile
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.util.concurrent.CopyOnWriteArrayList
import java.util.concurrent.atomic.AtomicBoolean
var isRefreshing: AtomicBoolean = AtomicBoolean(false)
var isRefreshOver: MutableSharedFlow<Boolean> = MutableSharedFlow()
/**
* 全局数据数据
......@@ -53,7 +58,6 @@ var GlobalExcelList = CopyOnWriteArrayList<DocumentBean>()
var GlobalPptList = CopyOnWriteArrayList<DocumentBean>()
fun cleanGlobalData() {
isRefreshing.compareAndSet(false, true)
GlobalPdfList.clear()
GlobalWordList.clear()
GlobalExcelList.clear()
......@@ -201,7 +205,7 @@ fun getAllDocument(context: Context): MutableList<DocumentBean> {
new.add(0, pdfDemo)
GlobalPdfList.add(0, pdfDemo)
}
isRefreshing.compareAndSet(true, false)
appScope.launch { isRefreshOver.emit(true) }
return new
}
......@@ -241,6 +245,7 @@ fun getPdfDocument(context: Context): MutableList<DocumentBean> {
new.add(0, pdfDemo)
GlobalPdfList.add(0, deepCopy(pdfDemo))
}
appScope.launch { isRefreshOver.emit(true) }
return new
}
......@@ -266,6 +271,7 @@ fun getWordDocument(context: Context): MutableList<DocumentBean> {
new.add(0, wordDemo)
GlobalWordList.add(0, deepCopy(wordDemo))
}
appScope.launch { isRefreshOver.emit(true) }
return new
}
......@@ -292,6 +298,7 @@ fun getExcelDocument(context: Context): MutableList<DocumentBean> {
new.add(0, excelDemo)
GlobalExcelList.add(0, deepCopy(excelDemo))
}
appScope.launch { isRefreshOver.emit(true) }
return new
}
......@@ -317,6 +324,7 @@ fun getPptDocument(context: Context): MutableList<DocumentBean> {
new.add(0, pptDemo)
GlobalPptList.add(0, deepCopy(pptDemo))
}
appScope.launch { isRefreshOver.emit(true) }
return new
}
......
......@@ -39,8 +39,10 @@ import com.base.pdfoneread.ui.document.GlobalWordList
import com.base.pdfoneread.ui.document.getAllDocument
import com.base.pdfoneread.ui.document.getGlobalAllList
import com.base.pdfoneread.utils.LogEx
import com.base.pdfoneread.utils.PermissionUtils.checkStorePermission
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.concurrent.atomic.AtomicBoolean
class HomeFragment() : Fragment() {
......@@ -51,7 +53,7 @@ class HomeFragment() : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
LogEx.logDebug(TAG,"onCreate")
LogEx.logDebug(TAG, "onCreate")
}
override fun onCreateView(
......@@ -122,17 +124,21 @@ class HomeFragment() : Fragment() {
override fun onDestroyView() {
super.onDestroyView()
LogEx.logDebug(TAG,"onDestroyView")
LogEx.logDebug(TAG, "onDestroyView")
}
override fun onDestroy() {
super.onDestroy()
LogEx.logDebug(TAG,"onDestroy")
LogEx.logDebug(TAG, "onDestroy")
}
fun refreshData(where: String = "") {
private var isRefreshing: AtomicBoolean = AtomicBoolean(false)
private fun refreshData(where: String = "") {
if (isRefreshing.get()) return
LogEx.logDebug(TAG, "refreshData where=$where")
isRefreshing.compareAndSet(false, true)
kotlin.runCatching {
lifecycleScope.launch(Dispatchers.IO) {
getAllDocument(requireContext())
......@@ -155,12 +161,20 @@ class HomeFragment() : Fragment() {
adapter.submitList(groupList)
binding.swipeRefreshLayout.isRefreshing = false
isRefreshing.compareAndSet(true, false)
}
}
}
}
override fun onResume() {
super.onResume()
if (requireContext().checkStorePermission()) {
refreshData()
}
}
fun changeUIByMain() {
val activity = requireActivity() as MainActivity?
......
......@@ -47,18 +47,12 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
changeHomeRvIcon()
if (!checkStorePermission()) {
showStoragePermission(launcher) {
refreshHomeData()
}
showStoragePermission(launcher) {}
}
}
private fun refreshHomeData() {
val fragment = getNavCurrentFragment()
if (fragment is HomeFragment) {
fragment.refreshData()
}
override fun onResume() {
super.onResume()
}
override fun initListener() {
......
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<cache-path
name="cache_files"
path="." />
<external-path
name="extern_files"
path="." />
<files-path
name="files"
path="." />
</paths>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment