Commit 03a68214 authored by wanglei's avatar wanglei

...处理数据刷新

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