Skip to content

Commit

Permalink
查看指定分组
Browse files Browse the repository at this point in the history
  • Loading branch information
Finb committed Dec 27, 2024
1 parent a82f658 commit 80f2bda
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 7 deletions.
10 changes: 9 additions & 1 deletion Controller/MessageListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ class MessageListViewController: BaseViewController<MessageListViewModel> {
}
}
}
cell.showGroupMessageAction = { [weak self] group in
let viewModel = MessageListViewModel(sourceType: .group(group))
let controller = MessageListViewController(viewModel: viewModel)
self?.navigationController?.pushViewController(controller, animated: true)
}
cell.cellData = (title, max(0, totalCount - messages.count), messages)
cell.isExpanded = self.expandedGroup.contains(title)
return cell
Expand Down Expand Up @@ -205,6 +210,9 @@ class MessageListViewController: BaseViewController<MessageListViewModel> {
// 标题
output.title
.drive(self.navigationItem.rx.title).disposed(by: rx.disposeBag)

output.groupToggleButtonHidden
.drive((groupButton.customView as! UIButton).rx.isHidden).disposed(by: rx.disposeBag)
}

private func getBatchDeleteDriver() -> Driver<MessageDeleteType> {
Expand Down Expand Up @@ -289,7 +297,7 @@ extension MessageListViewController: UITableViewDelegate {
let action = UIContextualAction(style: .destructive, title: NSLocalizedString("removeMessage")) { [weak self] _, _, actionPerformed in
guard let self else { return }

if let cell = self.tableView.cellForRow(at: indexPath) as? MessageTableViewCell {
if self.tableView.cellForRow(at: indexPath) is MessageTableViewCell {
// 单个消息直接删除,不弹出提示
self.tableView.dataSource?.tableView?(self.tableView, commit: .delete, forRowAt: indexPath)
actionPerformed(true)
Expand Down
41 changes: 37 additions & 4 deletions Controller/MessageListViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ enum MessageListType: Int, Codable {
case group
}

enum MessageSourceType {
/// 全部数据源
case all
/// 只查看某一个分组
case group(String?)
}

class MessageListViewModel: ViewModel, ViewModelType {
struct Input {
/// 刷新
Expand Down Expand Up @@ -48,6 +55,8 @@ class MessageListViewModel: ViewModel, ViewModelType {
var type: Driver<MessageListType>
/// 标题
var title: Driver<String>
/// 群组切换按钮是否隐藏
var groupToggleButtonHidden: Driver<Bool>
}

private static let typeKey = "me.fin.messageListType"
Expand All @@ -63,6 +72,9 @@ class MessageListViewModel: ViewModel, ViewModelType {
}
}

/// 数据源
private var sourceType: MessageSourceType = .all

/// 当前页数
private var page = 0
/// 每页数量
Expand All @@ -73,6 +85,11 @@ class MessageListViewModel: ViewModel, ViewModelType {
/// 全部数据(懒加载)
private var results: Results<Message>?

convenience init(sourceType: MessageSourceType) {
self.init()
self.sourceType = sourceType
}

/// 获取筛选后的全部数据源 (懒加载)
private func getResults(filterGroups: [String?], searchText: String?) -> Results<Message>? {
if let realm = try? Realm() {
Expand Down Expand Up @@ -157,6 +174,10 @@ class MessageListViewModel: ViewModel, ViewModelType {
}

private func getNextPage() -> [MessageListCellItem] {
if case .group = self.sourceType {
// 查看指定分组时,只能按列表查看
return getListNextPage()
}
if type == .list {
return getListNextPage()
}
Expand Down Expand Up @@ -191,9 +212,12 @@ class MessageListViewModel: ViewModel, ViewModelType {
// 刷新操作
let refreshAction = BehaviorRelay<MJRefreshAction>(value: .none)
// 切换群组
let filterGroups: BehaviorRelay<[String?]> = {
if let groups: [String?] = Settings["me.fin.filterGroups"] {
return BehaviorRelay<[String?]>(value: groups)
let filterGroups: BehaviorRelay<[String?]> = { [weak self] in
guard let self = self else {
return BehaviorRelay<[String?]>(value: [])
}
if case .group(let name) = self.sourceType {
return BehaviorRelay<[String?]>(value: [name])
}
return BehaviorRelay<[String?]>(value: [])
}()
Expand Down Expand Up @@ -344,12 +368,21 @@ class MessageListViewModel: ViewModel, ViewModelType {

}).disposed(by: rx.disposeBag)

// 查看指定分组时,隐藏分组切换按钮
let groupToggleButtonHidden = {
if case .group = self.sourceType {
return true
}
return false
}()

return Output(
messages: messagesRelay.asDriver(onErrorJustReturn: []),
refreshAction: refreshAction.asDriver(),
alertMessage: alertMessage,
type: Driver.merge(messageTypeChanged.asDriver(), Driver.just(self.type)),
title: titleRelay.asDriver()
title: titleRelay.asDriver(),
groupToggleButtonHidden: Driver.just(groupToggleButtonHidden)
)
}
}
13 changes: 12 additions & 1 deletion Model/MessageSection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,18 @@ enum MessageListCellItem: Equatable {
case (.message(let l), .message(let r)):
return l.id == r.id && l.dateText == r.dateText
case (.messageGroup(let l, _, let lMessages), .messageGroup(let r, _, let rMessages)):
return l == r && lMessages.first?.dateText == rMessages.first?.dateText
if l != r {
return false
}
if lMessages.first?.dateText != rMessages.first?.dateText {
return false
}
for (lMessage, rMessage) in zip(lMessages, rMessages) {
if lMessage.id != rMessage.id {
return false
}
}
return true
default:
return false
}
Expand Down
10 changes: 9 additions & 1 deletion View/MessageList/MessageTableViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ class MessageGroupTableViewCell: UITableViewCell {
groupHeader.clearAction = newValue
}
}

/// 查看群组所有消息
var showGroupMessageAction: ((_ group: String?) -> Void)? = nil

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
Expand All @@ -162,7 +165,12 @@ class MessageGroupTableViewCell: UITableViewCell {
make.bottom.equalToSuperview().offset(-18)
make.centerX.equalToSuperview()
}


moreView.addGestureRecognizer(UITapGestureRecognizer())
moreView.gestureRecognizers?.first?.rx.event.subscribe(onNext: { [weak self] _ in
self?.showGroupMessageAction?(self?.messages.first?.group)
}).disposed(by: self.rx.disposeBag)

refreshViewState()
}

Expand Down

0 comments on commit 80f2bda

Please sign in to comment.