Skip to content

Commit

Permalink
Add/remove chats to widgets from chat-details
Browse files Browse the repository at this point in the history
  • Loading branch information
zeitschlag committed Dec 22, 2024
1 parent 9e5b2e7 commit c406bba
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
47 changes: 47 additions & 0 deletions deltachat-ios/Controller/ContactDetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,35 @@ class ContactDetailViewController: UITableViewController {
return cell
}()

private lazy var homescreenWidgetCell: ActionCell = {
let cell = ActionCell()

let chatIdsOnHomescreen: [Int]

if #available(iOS 15, *) {
chatIdsOnHomescreen = UserDefaults.shared!
.getChatWidgetEntries()
.filter { $0.accountId == viewModel.context.id }
.compactMap { entry in
switch entry.type {
case .app: return nil
case .chat(let chatId): return chatId
}
}
} else {
chatIdsOnHomescreen = []
}

let isOnHomescreen = chatIdsOnHomescreen.contains(viewModel.chatId)
if isOnHomescreen {
cell.actionTitle = String.localized("ios_remove_from_home_screen")
} else {
cell.actionTitle = String.localized("ios_add_to_home_screen")
}
cell.actionColor = UIColor.systemBlue
return cell
}()

private lazy var shareContactCell: ActionCell = {
let cell = ActionCell()
cell.actionTitle = String.localized("menu_share")
Expand Down Expand Up @@ -220,6 +249,9 @@ class ContactDetailViewController: UITableViewController {
return clearChatCell
case .deleteChat:
return deleteChatCell
case .addToHomescreen:
// only relevant for iOS 15
return homescreenWidgetCell
}
case .sharedChats:
if let cell = tableView.dequeueReusableCell(withIdentifier: ContactCell.reuseIdentifier, for: indexPath) as? ContactCell {
Expand Down Expand Up @@ -381,6 +413,10 @@ class ContactDetailViewController: UITableViewController {
case .deleteChat:
tableView.deselectRow(at: indexPath, animated: false)
showDeleteChatConfirmationAlert()
case .addToHomescreen:
// only relevant for iOS 15
tableView.deselectRow(at: indexPath, animated: true)
toggleChatInHomescreenWidget()
}
}

Expand Down Expand Up @@ -432,6 +468,17 @@ class ContactDetailViewController: UITableViewController {
}
}

private func toggleChatInHomescreenWidget() {
guard #available(iOS 15, *) else { return }

let onHomescreen = viewModel.toggleChatInHomescreenWidget()
if onHomescreen {
homescreenWidgetCell.actionTitle = String.localized("ios_remove_from_home_screen")
} else {
homescreenWidgetCell.actionTitle = String.localized("ios_add_to_home_screen")
}
}

private func updateBlockContactCell() {
blockContactCell.actionTitle = viewModel.contact.isBlocked ? String.localized("menu_unblock_contact") : String.localized("menu_block_contact")
blockContactCell.actionColor = viewModel.contact.isBlocked ? UIColor.systemBlue : UIColor.systemRed
Expand Down
31 changes: 29 additions & 2 deletions deltachat-ios/ViewModel/ContactDetailViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class ContactDetailViewModel {
}

enum ChatAction {
case addToHomescreen
case archiveChat
case showEncrInfo
case blockContact
Expand Down Expand Up @@ -87,6 +88,10 @@ class ContactDetailViewModel {
chatOptions.append(.locations)
}

if #available(iOS 15, *) {
chatActions = [.addToHomescreen]
}

if chatId != 0 {
if !isDeviceTalk {
chatOptions.append(.ephemeralMessages)
Expand All @@ -97,7 +102,7 @@ class ContactDetailViewModel {
chatOptions.append(.shareContact)
}

chatActions = [.archiveChat]
chatActions.append(.archiveChat)
if !isDeviceTalk && !isSavedMessages {
chatActions.append(.showEncrInfo)
chatActions.append(.blockContact)
Expand All @@ -106,7 +111,8 @@ class ContactDetailViewModel {
chatActions.append(.deleteChat)
} else {
chatOptions.append(.startChat)
chatActions = [.showEncrInfo, .blockContact]
chatActions.append(.showEncrInfo)
chatActions.append(.blockContact)
}
}

Expand Down Expand Up @@ -221,4 +227,25 @@ class ContactDetailViewModel {
public func unblockContact() {
context.unblockContact(id: contact.id)
}

@available(iOS 15, *)
func toggleChatInHomescreenWidget() -> Bool {
guard let userDefaults = UserDefaults.shared else { return false }
let allHomescreenChatsIds: [Int] = userDefaults
.getChatWidgetEntries()
.compactMap { entry in
switch entry.type {
case .app: return nil
case .chat(let chatId): return chatId
}
}

if allHomescreenChatsIds.contains(chatId) {
userDefaults.removeChatFromHomescreenWidget(accountId: context.id, chatId: chatId)
return false
} else {
userDefaults.addChatToHomescreenWidget(accountId: context.id, chatId: chatId)
return true
}
}
}

0 comments on commit c406bba

Please sign in to comment.