增加批量删除历史消息功能

This commit is contained in:
Fin 2021-05-25 19:13:28 +08:00
parent 6bc79191b5
commit 4ba97d4cca
No known key found for this signature in database
GPG Key ID: CFB59B99D87A7B93
6 changed files with 142 additions and 31 deletions

View File

@ -17,8 +17,37 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
var window: UIWindow?
var syncEngine: SyncEngine?
func setupRealm() {
let groupUrl = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.bark")
let fileUrl = groupUrl?.appendingPathComponent("bark.realm")
let config = Realm.Configuration(
fileURL: fileUrl,
schemaVersion: 12,
migrationBlock: { migration, oldSchemaVersion in
// We havent migrated anything yet, so oldSchemaVersion == 0
if (oldSchemaVersion < 1) {
// Nothing to do!
// Realm will automatically detect new properties and removed properties
// And will update the schema on disk automatically
}
})
// Tell Realm to use this new configuration object for the default Realm
Realm.Configuration.defaultConfiguration = config
//iCloud
syncEngine = SyncEngine(objects: [
SyncObject(type: Message.self)
], databaseScope: .private)
#if DEBUG
let realm = try? Realm()
print("message count: \(realm?.objects(Message.self).count ?? 0)")
#endif
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Realm()
setupRealm()
self.window = UIWindow(frame: UIScreen.main.bounds)
if #available(iOS 13.0, *) {
@ -66,32 +95,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
bar.backIndicatorTransitionMaskImage = UIImage(named: "back")
bar.tintColor = Color.darkText.primary
let groupUrl = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.bark")
let fileUrl = groupUrl?.appendingPathComponent("bark.realm")
let config = Realm.Configuration(
fileURL: fileUrl,
schemaVersion: 12,
migrationBlock: { migration, oldSchemaVersion in
// We havent migrated anything yet, so oldSchemaVersion == 0
if (oldSchemaVersion < 1) {
// Nothing to do!
// Realm will automatically detect new properties and removed properties
// And will update the schema on disk automatically
}
})
// Tell Realm to use this new configuration object for the default Realm
Realm.Configuration.defaultConfiguration = config
//iCloud
syncEngine = SyncEngine(objects: [
SyncObject(type: Message.self)
], databaseScope: .private)
let realm = try? Realm()
print("message count: \(realm?.objects(Message.self).count ?? 0)")
return true
}

View File

@ -70,3 +70,8 @@ setSounds = "You can set different sounds for push notifications";
viewAllSounds = "View all sounds";
service = "Service";
lastHour = "The last hour";
today = "Today";
todayAndYesterday = "Today and yesterday";
allTime = "All time";

View File

@ -70,3 +70,9 @@ setSounds = "可以为推送设置不同的铃声";
viewAllSounds = "查看所有铃声";
service = "服务器";
lastHour = "过去一小时";
today = "今天";
todayAndYesterday = "昨天和今天";
allTime = "所有时间";

View File

@ -41,3 +41,24 @@ extension Date {
return "刚刚"
}
}
extension Date {
static var yesterday: Date { return Date().dayBefore }
static var tomorrow: Date { return Date().dayAfter }
static var lastHour: Date { return Calendar.current.date(byAdding: .hour, value: -1, to: Date())! }
var dayBefore: Date {
return Calendar.current.date(byAdding: .day, value: -1, to: noon)!
}
var dayAfter: Date {
return Calendar.current.date(byAdding: .day, value: 1, to: noon)!
}
var noon: Date {
return Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: self)!
}
var month: Int {
return Calendar.current.component(.month, from: self)
}
var isLastDayOfMonth: Bool {
return dayAfter.month != month
}
}

View File

@ -13,7 +13,21 @@ import RxCocoa
import RxDataSources
import MJRefresh
enum MessageDeleteType {
case lastHour
case today
case todayAndYesterday
case allTime
}
class MessageListViewController: BaseViewController {
let deleteButton: BKButton = {
let btn = BKButton()
btn.setImage(UIImage(named: "baseline_delete_outline_black_24pt"), for: .normal)
btn.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
return btn
}()
let tableView: UITableView = {
let tableView = UITableView()
tableView.separatorStyle = .none
@ -29,6 +43,8 @@ class MessageListViewController: BaseViewController {
override func makeUI() {
self.title = NSLocalizedString("historyMessage")
navigationItem.setRightBarButtonItem(item: UIBarButtonItem(customView: deleteButton))
self.view.addSubview(tableView)
tableView.snp.makeConstraints { (make) in
make.edges.equalToSuperview()
@ -42,11 +58,37 @@ class MessageListViewController: BaseViewController {
return
}
let output = viewModel.transform(input: MessageListViewModel.Input(
loadMore: tableView.mj_footer!.rx.refresh.asDriver(),
itemDelete: tableView.rx.itemDeleted.asDriver(),
itemSelected: tableView.rx.modelSelected(MessageTableViewCellViewModel.self).asDriver()
))
let batchDelete = deleteButton.rx
.tap
.flatMapLatest { Void -> PublishRelay<MessageDeleteType> in
let relay = PublishRelay<MessageDeleteType>()
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
alertController.addAction(UIAlertAction(title: NSLocalizedString("lastHour"), style: .default, handler: { _ in
relay.accept(.lastHour)
}))
alertController.addAction(UIAlertAction(title: NSLocalizedString("today"), style: .default, handler: { _ in
relay.accept(.today)
}))
alertController.addAction(UIAlertAction(title: NSLocalizedString("todayAndYesterday"), style: .default, handler: { _ in
relay.accept(.todayAndYesterday)
}))
alertController.addAction(UIAlertAction(title: NSLocalizedString("allTime"), style: .default, handler: { _ in
relay.accept(.allTime)
}))
alertController.addAction(UIAlertAction(title: NSLocalizedString("cancel"), style: .cancel, handler: nil))
self.navigationController?.present(alertController, animated: true, completion: nil)
return relay
}
let output = viewModel.transform(
input: MessageListViewModel.Input(
loadMore: tableView.mj_footer!.rx.refresh.asDriver(),
itemDelete: tableView.rx.itemDeleted.asDriver(),
itemSelected: tableView.rx.modelSelected(MessageTableViewCellViewModel.self).asDriver(),
delete:batchDelete.asDriver(onErrorDriveWith: .empty())
))
//tableView
output.refreshAction

View File

@ -17,6 +17,7 @@ class MessageListViewModel: ViewModel,ViewModelType {
var loadMore: Driver<Void>
var itemDelete: Driver<IndexPath>
var itemSelected: Driver<MessageTableViewCellViewModel>
var delete: Driver<MessageDeleteType>
}
struct Output {
@ -124,7 +125,40 @@ class MessageListViewModel: ViewModel,ViewModelType {
}
.compactMap { URL(string: $0) } //url
//
input.delete.drive(onNext: {[weak self] type in
guard let strongSelf = self else { return }
var date = Date()
switch type {
case .allTime:
date = Date(timeIntervalSince1970: 0)
case .todayAndYesterday:
date = Date.yesterday
case .today:
date = Date().noon
case .lastHour:
date = Date.lastHour
}
if let realm = try? Realm() {
let messages = realm.objects(Message.self).filter("createDate >= %@", date)
try? realm.write{
for msg in messages{
msg.isDeleted = true
}
}
}
strongSelf.page = 0
let messages = strongSelf.getNextPage()
let cellViewModels = messages.map({ (message) -> MessageTableViewCellViewModel in
return MessageTableViewCellViewModel(message: message)
})
messagesRelay.accept([MessageSection(header: "model", messages: cellViewModels)])
}).disposed(by: rx.disposeBag)
return Output(
messages: messagesRelay.asDriver(onErrorJustReturn: []),