Skip to content

Commit

Permalink
perf: 优化应用市场模式标记更新后的数据传递
Browse files Browse the repository at this point in the history
  • Loading branch information
xz-dev committed Jul 28, 2020
1 parent e3b3fcc commit 7bf5817
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ android {
minSdkVersion 21
targetSdkVersion 29
versionCode 45
versionName "0.1.2-rc.1"
versionName "0.1.2-rc.2"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
25 changes: 25 additions & 0 deletions app/src/main/java/net/xzos/upgradeall/data/AppUiDataManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ object AppUiDataManager {
// 列表中所有的 APP
private val appListLiveDataMap: MutableMap<Int, MutableLiveData<List<BaseApp>>> = mutableMapOf()

private val applicationObserverFunMap: MutableMap<Applications, ObserverFun<Unit>> = mutableMapOf()

init {
AppManager.observeForever<Unit>(fun(_) {
refreshAllAppListMap()
Expand All @@ -54,9 +56,32 @@ object AppUiDataManager {
}
}
}
setApplicationObserverMap(needUpdateAppList.filterIsInstance<Applications>())
needUpdateAppListLiveData.setValueBackground(list)
}

private fun setApplicationObserverMap(newList: List<Applications>) {
val oldList = applicationObserverFunMap.keys.toList()
if (oldList == newList) return
val delList = oldList.filter {
!newList.contains(it)
}
for (applications in delList) {
applications.removeObserver(applicationObserverFunMap.remove(applications)!!)
}
val addList = newList.filter {
!oldList.contains(it)
}
for (applications in addList) {
val observerFun = fun(_: Unit) {
refreshNeedUpdateAppList()
}.also {
applicationObserverFunMap[applications] = it
}
applications.observeForever(observerFun)
}
}

private fun refreshAllAppListMap() {
allAppListLiveData.setValueBackground(AppManager.apps)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ private const val DEFAULT_TAG = "NULL"

interface Informer {

fun notifyChanged() {
notifyChanged(DEFAULT_TAG, Unit)
}

fun notifyChanged(tag: String) {
notifyChanged(tag, Unit)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ object AppManager : Informer {
}

private fun notifyChange() {
notifyChanged(Unit)
notifyChanged()
}

private fun initApp() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ class Applications(override val appDatabase: AppDatabase,
if (appStatus == Updater.INVALID_APP && app is App) {
markInvalidApp(app)
}
notifyChanged(Unit) // 通知应用列表改变
notifyChanged() // 通知应用列表改变
checkUpdateStatusChanged()
})
private val otherUpdateControl = UpdateControl(applicationsUtils.excludeApps, fun(app, appStatus) {
if ((appStatus == Updater.APP_OUTDATED || appStatus == Updater.APP_LATEST) && app is App) {
markValidApp(app)
notifyChanged(Unit)
notifyChanged()
}
checkUpdateStatusChanged()
})
Expand Down

0 comments on commit 7bf5817

Please sign in to comment.