mirror of https://github.com/oxen-io/session-ios
Fix screen lock handling
parent
86af5f1d99
commit
1a11476b85
@ -0,0 +1,99 @@
|
|||||||
|
import SessionUIKit
|
||||||
|
|
||||||
|
final class ThreadPickerVC : UIViewController, UITableViewDataSource {
|
||||||
|
private var threads: YapDatabaseViewMappings!
|
||||||
|
private var threadViewModelCache: [String:ThreadViewModel] = [:] // Thread ID to ThreadViewModel
|
||||||
|
|
||||||
|
private var threadCount: UInt {
|
||||||
|
threads.numberOfItems(inGroup: TSInboxGroup)
|
||||||
|
}
|
||||||
|
|
||||||
|
private lazy var dbConnection: YapDatabaseConnection = {
|
||||||
|
let result = OWSPrimaryStorage.shared().newDatabaseConnection()
|
||||||
|
result.objectCacheLimit = 500
|
||||||
|
return result
|
||||||
|
}()
|
||||||
|
|
||||||
|
private lazy var tableView: UITableView = {
|
||||||
|
let result = UITableView()
|
||||||
|
result.backgroundColor = .clear
|
||||||
|
result.separatorStyle = .none
|
||||||
|
result.register(SimplifiedConversationCell.self, forCellReuseIdentifier: SimplifiedConversationCell.reuseIdentifier)
|
||||||
|
result.showsVerticalScrollIndicator = false
|
||||||
|
return result
|
||||||
|
}()
|
||||||
|
|
||||||
|
// MARK: Lifecycle
|
||||||
|
override func viewDidLoad() {
|
||||||
|
super.viewDidLoad()
|
||||||
|
// Gradient
|
||||||
|
view.backgroundColor = .clear
|
||||||
|
let gradient = Gradients.defaultBackground
|
||||||
|
view.setGradient(gradient)
|
||||||
|
// Threads
|
||||||
|
dbConnection.beginLongLivedReadTransaction() // Freeze the connection for use on the main thread (this gives us a stable data source that doesn't change until we tell it to)
|
||||||
|
threads = YapDatabaseViewMappings(groups: [ TSInboxGroup ], view: TSThreadDatabaseViewExtensionName) // The extension should be registered at this point
|
||||||
|
threads.setIsReversed(true, forGroup: TSInboxGroup)
|
||||||
|
dbConnection.read { transaction in
|
||||||
|
self.threads.update(with: transaction) // Perform the initial update
|
||||||
|
}
|
||||||
|
// Title
|
||||||
|
let titleLabel = UILabel()
|
||||||
|
titleLabel.text = NSLocalizedString("vc_share_title", comment: "")
|
||||||
|
titleLabel.textColor = Colors.text
|
||||||
|
titleLabel.font = .boldSystemFont(ofSize: Values.veryLargeFontSize)
|
||||||
|
navigationItem.titleView = titleLabel
|
||||||
|
// Table view
|
||||||
|
tableView.dataSource = self
|
||||||
|
view.addSubview(tableView)
|
||||||
|
tableView.pin(to: view)
|
||||||
|
// Reload
|
||||||
|
reload()
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: Table View Data Source
|
||||||
|
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
||||||
|
return Int(threadCount)
|
||||||
|
}
|
||||||
|
|
||||||
|
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
||||||
|
let cell = tableView.dequeueReusableCell(withIdentifier: SimplifiedConversationCell.reuseIdentifier) as! SimplifiedConversationCell
|
||||||
|
cell.threadViewModel = threadViewModel(at: indexPath.row)
|
||||||
|
return cell
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: Updating
|
||||||
|
private func reload() {
|
||||||
|
AssertIsOnMainThread()
|
||||||
|
dbConnection.beginLongLivedReadTransaction() // Jump to the latest commit
|
||||||
|
dbConnection.read { transaction in
|
||||||
|
self.threads.update(with: transaction)
|
||||||
|
}
|
||||||
|
threadViewModelCache.removeAll()
|
||||||
|
tableView.reloadData()
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: Convenience
|
||||||
|
private func thread(at index: Int) -> TSThread? {
|
||||||
|
var thread: TSThread? = nil
|
||||||
|
dbConnection.read { transaction in
|
||||||
|
let ext = transaction.ext(TSThreadDatabaseViewExtensionName) as! YapDatabaseViewTransaction
|
||||||
|
thread = ext.object(atRow: UInt(index), inSection: 0, with: self.threads) as! TSThread?
|
||||||
|
}
|
||||||
|
return thread
|
||||||
|
}
|
||||||
|
|
||||||
|
private func threadViewModel(at index: Int) -> ThreadViewModel? {
|
||||||
|
guard let thread = thread(at: index) else { return nil }
|
||||||
|
if let cachedThreadViewModel = threadViewModelCache[thread.uniqueId!] {
|
||||||
|
return cachedThreadViewModel
|
||||||
|
} else {
|
||||||
|
var threadViewModel: ThreadViewModel? = nil
|
||||||
|
dbConnection.read { transaction in
|
||||||
|
threadViewModel = ThreadViewModel(thread: thread, transaction: transaction)
|
||||||
|
}
|
||||||
|
threadViewModelCache[thread.uniqueId!] = threadViewModel
|
||||||
|
return threadViewModel
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue