Skip to content

Commit

Permalink
remove default params value from Cache interface
Browse files Browse the repository at this point in the history
  • Loading branch information
bandeapart committed Jan 17, 2025
1 parent a37c566 commit 5325183
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ fun EditorInnerPage(
Unit
}

val saveLock = Cache.getOrDefaultByType(Cache.Key.editorPageSaveLockPrefix+ Cache.keySeparator+editorPageShowingFilePath.value, default= Mutex())
val saveLock = remember(editorPageShowingFilePath.value) { Cache.getOrDefaultByType(generateKeyForSaveLock(editorPageShowingFilePath.value), default = Mutex(), saveDefaultWhenNoKey = true) }
// val saveLock = Mutex() //其实这样也行,不过根据路径创建锁更严谨,跨页面也适用,比如如果首页的Editor正在保存,然后打开子页面,这时子页面必须等首页保存完成,但如果用这个和页面生命周期一样的锁,就无法实现那种效果了,但和页面生命周期一样的锁其实也够用

// val isEdited = rememberSaveable{ mutableStateOf(false) }
Expand Down Expand Up @@ -1444,3 +1444,11 @@ private fun getBackHandler(

return backHandlerOnBack
}

/**
* generate cache key for save lock, you can use this key get lock obj from Cache for saving file
* @return a cache key, e.g. "editor_save_lock:/path/to/file"
*/
private fun generateKeyForSaveLock(filePath:String):String {
return Cache.Key.editorPageSaveLockPrefix + Cache.keySeparator + filePath
}
2 changes: 1 addition & 1 deletion app/src/main/java/com/catpuppyapp/puppygit/utils/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,7 @@ fun<T> getFirstOrNullThenRemove(list:MutableList<T>):T? {
}

/**
* return pair: onlyforthisfolder and viewAndSort
* return pair: onlyForThisFolder and viewAndSort
*/
fun getViewAndSortForPath(path:String, settings:AppSettings) :Pair<Boolean, DirViewAndSort> {
val folderViewSort = settings.files.dirAndViewSort_Map[path]
Expand Down
20 changes: 10 additions & 10 deletions app/src/main/java/com/catpuppyapp/puppygit/utils/cache/Cache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ object Cache:CacheStoreImpl(){
// val changeListInnerPage_RequirePull = "cliprpul";
// val changeListInnerPage_RequirePush = "cliprpus";
// val changeListInnerPage_RequireSync = "cliprsyn";
val changeListInnerPage_requireDoActFromParent = "cliprdafp";
val repoTmpStatusPrefix = "repo_tmp_status" // prefix+keyseparator+repoId,例如 "repo_tmp_status:a29388d9988"
const val changeListInnerPage_requireDoActFromParent = "cliprdafp";
const val repoTmpStatusPrefix = "repo_tmp_status" // prefix+keyseparator+repoId,例如 "repo_tmp_status:a29388d9988"

val editorPageSaveLockPrefix = "editor_save_lock"
const val editorPageSaveLockPrefix = "editor_save_lock"

val commitList_fullOidKey = "commitList_fullOidKey"
val commitList_shortBranchNameKey = "commitList_shortBranchNameKey"
const val commitList_fullOidKey = "commitList_fullOidKey"
const val commitList_shortBranchNameKey = "commitList_shortBranchNameKey"

val diffScreen_underRepoPathKey = "diffScreen_underRepoPathKey"
val diffScreen_diffableItemListKey = "diffScreen_diffableItemListKey"
const val diffScreen_underRepoPathKey = "diffScreen_underRepoPathKey"
const val diffScreen_diffableItemListKey = "diffScreen_diffableItemListKey"

val fileHistory_fileRelativePathKey = "fileHistory_fileRelativePathKey"
val subPageEditor_filePathKey = "subPageEditor_filePathKey"
val treeToTreeChangeList_titleDescKey = "treeToTreeChangeList_titleDescKey"
const val fileHistory_fileRelativePathKey = "fileHistory_fileRelativePathKey"
const val subPageEditor_filePathKey = "subPageEditor_filePathKey"
const val treeToTreeChangeList_titleDescKey = "treeToTreeChangeList_titleDescKey"
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ interface CacheStore { // TODO定时删除
fun set(key:String, value:Any):Any?
fun get(key:String):Any?

fun getOrDefault(key:String, default:Any, saveDefaultWhenNoKey:Boolean = true):Any
fun getOrDefault(key:String, default:Any, saveDefaultWhenNoKey:Boolean):Any

fun<T> getByType(key:String):T?
fun<T:Any> getOrDefaultByType(key:String, default:T, saveDefaultWhenNoKey:Boolean = true):T
fun<T:Any> getOrDefaultByType(key:String, default:T, saveDefaultWhenNoKey:Boolean):T

fun<T> getByTypeThenDel(key:String):T?

Expand All @@ -23,7 +23,7 @@ interface CacheStore { // TODO定时删除
fun getThenDel(key:String):Any?

//让newKey指向oldKey的数据,然后从map删除oldKey
fun updateKey(oldKey:String, newKey:String, requireDelOldKey:Boolean=false)
fun updateKey(oldKey:String, newKey:String, requireDelOldKey:Boolean)

//清空缓存
fun clear()
Expand Down

0 comments on commit 5325183

Please sign in to comment.