Browse Source

Merge pull request #530 from deltachat/pin-chats

pin chats
bjoern 5 years ago
parent
commit
9b899973ea
2 changed files with 32 additions and 8 deletions
  1. 19 8
      deltachat-ios/Controller/ChatListController.swift
  2. 13 0
      deltachat-ios/DC/Wrapper.swift

+ 19 - 8
deltachat-ios/Controller/ChatListController.swift

@@ -193,6 +193,14 @@ extension ChatListController: UITableViewDataSource, UITableViewDelegate {
             } else {
                 cell.setBackupImage(name: chat.name, color: chat.color)
             }
+
+            if chat.visibility == DC_CHAT_VISIBILITY_PINNED {
+                cell.backgroundColor = DcColors.deaddropBackground
+                cell.contentView.backgroundColor = DcColors.deaddropBackground
+            } else {
+                cell.backgroundColor = DcColors.contactCellBackgroundColor
+                cell.contentView.backgroundColor = DcColors.contactCellBackgroundColor
+            }
         }
 
         cell.setVerified(isVerified: chat.isVerified)
@@ -256,21 +264,24 @@ extension ChatListController: UITableViewDataSource, UITableViewDelegate {
             // see https://forums.developer.apple.com/thread/115030
         }
 
-        var title = String.localized("archive")
-        if showArchive {
-            title = String.localized("unarchive")
-        }
-        let archive = UITableViewRowAction(style: .destructive, title: title) { [unowned self] _, _ in
+        let archive = UITableViewRowAction(style: .destructive, title: String.localized(showArchive ? "unarchive" : "archive")) { [unowned self] _, _ in
             self.dcContext.archiveChat(chatId: chatId, archive: !self.showArchive)
         }
         archive.backgroundColor = UIColor.lightGray
 
-        let delete = UITableViewRowAction(style: .destructive, title: String.localized("delete")) { [unowned self] _, _ in
+        let chat = dcContext.getChat(chatId: chatId)
+        let pinned = chat.visibility==DC_CHAT_VISIBILITY_PINNED
+        let pin = UITableViewRowAction(style: .destructive, title: String.localized(pinned ? "unpin" : "pin")) { [unowned self] _, _ in
+            self.dcContext.setChatVisibility(chatId: chatId, visibility: pinned ? DC_CHAT_VISIBILITY_NORMAL : DC_CHAT_VISIBILITY_PINNED)
+        }
+        pin.backgroundColor = UIColor.systemGreen
+
+        let delete = UITableViewRowAction(style: .normal, title: String.localized("delete")) { [unowned self] _, _ in
             self.showDeleteChatConfirmationAlert(chatId: chatId)
         }
-        delete.backgroundColor = UIColor.red
+        delete.backgroundColor = UIColor.systemRed
 
-        return [archive, delete]
+        return [archive, pin, delete]
     }
 
     func getDeaddropCell(_ tableView: UITableView) -> ContactCell {

+ 13 - 0
deltachat-ios/DC/Wrapper.swift

@@ -35,6 +35,10 @@ class DcContext {
         dc_add_address_book(mailboxPointer, contactString)
     }
 
+    func getChat(chatId: Int) -> DcChat {
+        return DcChat(id: chatId)
+    }
+
     func getChatlist(flags: Int32, queryString: String?, queryId: Int) -> DcChatlist {
         let chatlistPointer = dc_get_chatlist(contextPointer, flags, queryString, UInt32(queryId))
         let chatlist = DcChatlist(chatListPointer: chatlistPointer)
@@ -70,6 +74,10 @@ class DcContext {
         dc_set_chat_visibility(contextPointer, UInt32(chatId), Int32(archive ? DC_CHAT_VISIBILITY_ARCHIVED : DC_CHAT_VISIBILITY_NORMAL))
     }
 
+    func setChatVisibility(chatId: Int, visibility: Int32) {
+        dc_set_chat_visibility(contextPointer, UInt32(chatId), visibility)
+    }
+
     func marknoticedChat(chatId: Int) {
         dc_marknoticed_chat(self.contextPointer, UInt32(chatId))
     }
@@ -436,6 +444,7 @@ class DcChatlist {
 class DcChat {
     var chatPointer: OpaquePointer?
 
+    // use DcContext.getChat() instead of calling the constructor directly
     init(id: Int) {
         if let p = dc_get_chat(mailboxPointer, UInt32(id)) {
             chatPointer = p
@@ -475,6 +484,10 @@ class DcChat {
         return Int(dc_chat_get_visibility(chatPointer)) == DC_CHAT_VISIBILITY_ARCHIVED
     }
 
+    var visibility: Int32 {
+        return dc_chat_get_visibility(chatPointer)
+    }
+
     var isUnpromoted: Bool {
         return Int(dc_chat_is_unpromoted(chatPointer)) != 0
     }