-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(app): 保持应用最后使用的输入模式状态持久化,支持应用重启后仍能获取之前的状态
- Loading branch information
Showing
6 changed files
with
83 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// | ||
// InputModeCache.swift | ||
// Fire | ||
// | ||
// Created by qwertyyb on 2023/11/7. | ||
// | ||
import Defaults | ||
|
||
class InputModeCache { | ||
let capacity = 100 | ||
private var cache: [String: InputMode] = [:] | ||
private var keys: [String] = [] | ||
|
||
private init() { | ||
loadFromUserDefaults() | ||
} | ||
|
||
func get(_ key: String) -> InputMode? { | ||
if let value = cache[key] { | ||
updateKeyOrder(key) | ||
return value | ||
} | ||
return nil | ||
} | ||
|
||
func put(_ key: String, _ value: InputMode) { | ||
if cache[key] == nil { | ||
if keys.count >= capacity { | ||
let oldestKey = keys.removeFirst() | ||
cache[oldestKey] = nil | ||
} | ||
keys.append(key) | ||
} else { | ||
updateKeyOrder(key) | ||
} | ||
cache[key] = value | ||
saveToUserDefaults() | ||
} | ||
|
||
private func updateKeyOrder(_ key: String) { | ||
if let index = keys.firstIndex(of: key) { | ||
keys.remove(at: index) | ||
keys.append(key) | ||
saveToUserDefaults() | ||
} | ||
} | ||
|
||
private func saveToUserDefaults() { | ||
Defaults[.keepAppInputMode_keys] = keys | ||
Defaults[.keepAppInputMode_cache] = cache | ||
} | ||
|
||
private func loadFromUserDefaults() { | ||
keys = Defaults[.keepAppInputMode_keys] | ||
cache = Defaults[.keepAppInputMode_cache] | ||
} | ||
|
||
static let shared = InputModeCache() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters