Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option to 'mark all selected chats read' #2226

Merged
merged 2 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 27 additions & 22 deletions deltachat-ios/Controller/ChatListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ class ChatListViewController: UITableViewController {
return button
}()

private lazy var markArchivedReadButton: UIBarButtonItem = {
let button = UIBarButtonItem(title: String.localized("mark_as_read_short"), style: .plain, target: self, action: #selector(markArchivedReadPressed))
private lazy var markReadButton: UIBarButtonItem = {
let button = UIBarButtonItem(title: String.localized("mark_as_read_short"), style: .plain, target: self, action: #selector(markReadPressed))
return button
}()

Expand Down Expand Up @@ -387,8 +387,13 @@ class ChatListViewController: UITableViewController {
}
}

@objc func markArchivedReadPressed() {
dcContext.marknoticedChat(chatId: Int(DC_CHAT_ID_ARCHIVED_LINK))
@objc func markReadPressed() {
if isEditing {
viewModel?.markUnreadSelectedChats(in: tableView.indexPathsForSelectedRows)
setLongTapEditing(false)
} else if isArchive {
dcContext.marknoticedChat(chatId: Int(DC_CHAT_ID_ARCHIVED_LINK))
}
}

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
Expand Down Expand Up @@ -569,13 +574,13 @@ class ChatListViewController: UITableViewController {
}
}

func setLongTapEditing(_ editing: Bool, initialIndexPath: [IndexPath]? = nil) {
func setLongTapEditing(_ editing: Bool, initialIndexPath: IndexPath? = nil) {
setEditing(editing, animated: true)
if editing {
tableView.selectRow(at: initialIndexPath, animated: true, scrollPosition: .none)
addEditingView()
if let viewModel = viewModel {
editingBar.showUnpinning = viewModel.hasOnlyPinnedChatsSelected(in: tableView.indexPathsForSelectedRows) ||
viewModel.hasOnlyPinnedChatsSelected(in: initialIndexPath)
editingBar.showUnpinning = viewModel.hasOnlyPinnedChatsSelected(in: tableView.indexPathsForSelectedRows)
}
} else {
removeEditingView()
Expand Down Expand Up @@ -663,9 +668,9 @@ class ChatListViewController: UITableViewController {
titleView.text = String.localized("chat_archived_label")
if !handleMultiSelectionTitle() {
navigationItem.setLeftBarButton(nil, animated: true)
navigationItem.setRightBarButton(markArchivedReadButton, animated: true)
navigationItem.setRightBarButton(markReadButton, animated: true)
}
updateMarkArchivedReadButton()
updateMarkReadButton()
} else {
titleView.text = DcUtils.getConnectivityString(dcContext: dcContext, connectedString: String.localized("pref_chats"))
if !handleMultiSelectionTitle() {
Expand All @@ -686,19 +691,18 @@ class ChatListViewController: UITableViewController {
return false
}
titleView.accessibilityHint = nil
let cnt = tableView.indexPathsForSelectedRows?.count ?? 1
let cnt = tableView.indexPathsForSelectedRows?.count ?? 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It took me a while to understand what cnt stands for

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leaving vocals out to make names shorter is a thing maybe only ppl understand growing up with 8+3 filename and other restrictions, hard to get rid of this habit obviously :)

titleView.text = String.localized(stringID: "n_selected", parameter: cnt)
navigationItem.setLeftBarButton(cancelButton, animated: true)
navigationItem.setRightBarButton(nil, animated: true)
navigationItem.setRightBarButton(markReadButton, animated: true)
updateMarkReadButton()
return true
}

func handleChatListUpdate() {
if let viewModel = viewModel, viewModel.isEditing {
if let viewModel, viewModel.isEditing {
viewModel.setPendingChatListUpdate()
return
}
if Thread.isMainThread {
} else if Thread.isMainThread {
tableView.reloadData()
handleEmptyStateLabel()
} else {
Expand All @@ -708,13 +712,15 @@ class ChatListViewController: UITableViewController {
self.handleEmptyStateLabel()
}
}
if isArchive {
updateMarkArchivedReadButton()
}
updateMarkReadButton()
}

func updateMarkArchivedReadButton() {
self.markArchivedReadButton.isEnabled = dcContext.getUnreadMessages(chatId: Int(DC_CHAT_ID_ARCHIVED_LINK)) != 0
func updateMarkReadButton() {
if tableView.isEditing {
self.markReadButton.isEnabled = viewModel?.hasAnyUnreadChatSelected(in: tableView.indexPathsForSelectedRows) ?? false
} else if isArchive {
self.markReadButton.isEnabled = dcContext.getUnreadMessages(chatId: Int(DC_CHAT_ID_ARCHIVED_LINK)) != 0
zeitschlag marked this conversation as resolved.
Show resolved Hide resolved
}
}

private func handleEmptyStateLabel() {
Expand Down Expand Up @@ -919,8 +925,7 @@ extension ChatListViewController: ContactCellDelegate {
guard let chatList = viewModel?.chatList else { return }
if chatList.getChatId(index: indexPath.row) != Int(DC_CHAT_ID_ARCHIVED_LINK) {
UIImpactFeedbackGenerator(style: .medium).impactOccurred()
setLongTapEditing(true, initialIndexPath: [indexPath])
tableView.selectRow(at: indexPath, animated: true, scrollPosition: .none)
setLongTapEditing(true, initialIndexPath: indexPath)
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions deltachat-ios/ViewModel/ChatListViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,14 @@ class ChatListViewModel: NSObject {
}
}

func markUnreadSelectedChats(in indexPaths: [IndexPath]?) {
let chatIds = chatIdsFor(indexPaths: indexPaths)
for chatId in chatIds {
dcContext.marknoticedChat(chatId: chatId)
NotificationManager.removeNotificationsForChat(dcContext: dcContext, chatId: chatId)
}
}

func deleteChat(chatId: Int) {
dcContext.deleteChat(chatId: chatId)
NotificationManager.removeNotificationsForChat(dcContext: dcContext, chatId: chatId)
Expand Down Expand Up @@ -261,6 +269,16 @@ class ChatListViewModel: NSObject {
updateChatList(notifyListener: notifyListener)
}

func hasAnyUnreadChatSelected(in indexPaths: [IndexPath]?) -> Bool {
let chatIds = chatIdsFor(indexPaths: indexPaths)
for chatId in chatIds {
if dcContext.getUnreadMessages(chatId: chatId) > 0 {
return true
}
}
return false
}

func hasOnlyPinnedChatsSelected(in indexPaths: [IndexPath]?) -> Bool {
let chatIds = chatIdsFor(indexPaths: indexPaths)
return hasOnlyPinnedChatsSelected(chatIds: chatIds)
Expand Down