Bläddra i källkod

Merge pull request #359 from deltachat/message_counter

Message counter
björn petersen 5 år sedan
förälder
incheckning
079ec66684

+ 4 - 0
deltachat-ios.xcodeproj/project.pbxproj

@@ -82,6 +82,7 @@
 		3060119C22DDE24000C1CE6F /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 3060119E22DDE24000C1CE6F /* Localizable.strings */; };
 		306011B622E5E7FB00C1CE6F /* Localizable.stringsdict in Resources */ = {isa = PBXBuildFile; fileRef = 306011B422E5E7FB00C1CE6F /* Localizable.stringsdict */; };
 		30A4D9AE2332672700544344 /* QrInviteViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30A4D9AD2332672600544344 /* QrInviteViewController.swift */; };
+		30F9B9EC235F2116006E7ACF /* MessageCounter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30F9B9EB235F2116006E7ACF /* MessageCounter.swift */; };
 		6795F63A82E94FF7CD2CEC0F /* Pods_deltachat_iosTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2F7009234DB9408201A6CDCB /* Pods_deltachat_iosTests.framework */; };
 		7070FB9B2101ECBB000DC258 /* GroupNameController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7070FB9A2101ECBB000DC258 /* GroupNameController.swift */; };
 		7092474120B3869500AF8799 /* ContactDetailViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7092474020B3869500AF8799 /* ContactDetailViewController.swift */; };
@@ -287,6 +288,7 @@
 		306011C822E5E83100C1CE6F /* zh-Hant-TW */ = {isa = PBXFileReference; lastKnownFileType = text.plist.stringsdict; name = "zh-Hant-TW"; path = "zh-Hant-TW.lproj/Localizable.stringsdict"; sourceTree = "<group>"; };
 		306011C922E5E83500C1CE6F /* uk */ = {isa = PBXFileReference; lastKnownFileType = text.plist.stringsdict; name = uk; path = uk.lproj/Localizable.stringsdict; sourceTree = "<group>"; };
 		30A4D9AD2332672600544344 /* QrInviteViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QrInviteViewController.swift; sourceTree = "<group>"; };
+		30F9B9EB235F2116006E7ACF /* MessageCounter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageCounter.swift; sourceTree = "<group>"; };
 		6241BE1534A653E79AD5D01D /* Pods_deltachat_ios.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_deltachat_ios.framework; sourceTree = BUILT_PRODUCTS_DIR; };
 		7070FB9A2101ECBB000DC258 /* GroupNameController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GroupNameController.swift; sourceTree = "<group>"; };
 		7092474020B3869500AF8799 /* ContactDetailViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContactDetailViewController.swift; sourceTree = "<group>"; };
@@ -705,6 +707,7 @@
 				AE728F14229D5C390047565B /* PhotoPickerAlertAction.swift */,
 				AE52EA18229EB53C00C586C9 /* ContactDetailHeader.swift */,
 				AE9DAF0E22C278C6004C9591 /* ChatTitleView.swift */,
+				30F9B9EB235F2116006E7ACF /* MessageCounter.swift */,
 			);
 			path = View;
 			sourceTree = "<group>";
@@ -1113,6 +1116,7 @@
 				7092474120B3869500AF8799 /* ContactDetailViewController.swift in Sources */,
 				300C50A1234BDAB800F8AE22 /* TextMediaMessageSizeCalculator.swift in Sources */,
 				AE18F292228C17BC0007B1BE /* PortSettingsController.swift in Sources */,
+				30F9B9EC235F2116006E7ACF /* MessageCounter.swift in Sources */,
 				305961F12346125100C80F33 /* ContactMessageCell.swift in Sources */,
 				AE851AD0227DF50900ED86F0 /* GroupChatDetailViewController.swift in Sources */,
 				305961D12346125100C80F33 /* Bundle+Extensions.swift in Sources */,

+ 7 - 1
deltachat-ios/Controller/ChatListController.swift

@@ -142,8 +142,13 @@ extension ChatListController: UITableViewDataSource, UITableViewDelegate {
 
         let chat = DcChat(id: chatId)
         let summary = chatList.getSummary(index: row)
+        let unreadMessages = dcContext.getUnreadMessages(chatId: chatId)
+
+        cell.nameLabel.attributedText = (unreadMessages > 0) ?
+            NSAttributedString(string: chat.name, attributes: [ .font: UIFont.systemFont(ofSize: 16, weight: .bold) ]) :
+            NSAttributedString(string: chat.name, attributes: [ .font: UIFont.systemFont(ofSize: 16, weight: .medium) ])
+
 
-        cell.nameLabel.text = chat.name
         if let img = chat.profileImage {
             cell.resetBackupImage()
             cell.setImage(img)
@@ -163,6 +168,7 @@ extension ChatListController: UITableViewDataSource, UITableViewDelegate {
 
         cell.emailLabel.text = result
         cell.setTimeLabel(summary.timestamp)
+        cell.setUnreadMessageCounter(unreadMessages)
         cell.setDeliveryStatusIndicator(summary.state)
 
         return cell

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

@@ -105,6 +105,10 @@ class DcContext {
         let vStr = value ? "1" : "0"
         setConfig(key, vStr)
     }
+
+    func getUnreadMessages(chatId: Int) -> Int {
+        return Int(dc_get_fresh_msg_cnt(contextPointer, UInt32(chatId)))
+    }
 }
 
 class DcConfig {

+ 1 - 1
deltachat-ios/Extensions/UIView+Extensions.swift

@@ -129,7 +129,7 @@ internal extension UIView {
         return heightAnchor.constraint(equalToConstant: height)
     }
 
-    func constraintWitdthTo(_ width: CGFloat) -> NSLayoutConstraint {
+    func constraintWidthTo(_ width: CGFloat) -> NSLayoutConstraint {
        return  widthAnchor.constraint(equalToConstant: width)
     }
 

+ 11 - 1
deltachat-ios/View/ContactCell.swift

@@ -77,6 +77,11 @@ class ContactCell: UITableViewCell {
         return view
     }()
 
+    private let unreadMessageCounter: MessageCounter = {
+        let view = MessageCounter(count: 0, size: 20)
+        return view
+    }()
+
     var darkMode: Bool = false {
         didSet {
             if darkMode {
@@ -138,6 +143,7 @@ class ContactCell: UITableViewCell {
 
         bottomLineStackView.addArrangedSubview(emailLabel)
         bottomLineStackView.addArrangedSubview(deliveryStatusIndicator)
+        bottomLineStackView.addArrangedSubview(unreadMessageCounter)
 
         contentView.addSubview(myStackView)
         myStackView.leadingAnchor.constraint(equalTo: avatar.trailingAnchor, constant: margin).isActive = true
@@ -185,6 +191,10 @@ class ContactCell: UITableViewCell {
         setColor(color)
     }
 
+    func setUnreadMessageCounter(_ count: Int) {
+        unreadMessageCounter.setCount(count)
+    }
+
     func setDeliveryStatusIndicator(_ status: Int) {
         var indicatorImage: UIImage?
         switch Int32(status) {
@@ -203,7 +213,7 @@ class ContactCell: UITableViewCell {
         default:
             break
         }
-        if indicatorImage != nil {
+        if indicatorImage != nil && unreadMessageCounter.isHidden {
             deliveryStatusIndicator.isHidden = false
         } else {
             deliveryStatusIndicator.isHidden = true

+ 69 - 0
deltachat-ios/View/MessageCounter.swift

@@ -0,0 +1,69 @@
+//
+//  MessageCounter.swift
+//  deltachat-ios
+//
+//  Created by Macci on 22.10.19.
+//  Copyright © 2019 Jonas Reinsch. All rights reserved.
+//
+
+import Foundation
+import UIKit
+
+class MessageCounter: UIView {
+
+    private let minSize: CGFloat
+    private var widthConstraint: NSLayoutConstraint?
+    private let padding: CGFloat = 4
+
+    private var label: UILabel = {
+        let label = UILabel()
+        label.adjustsFontSizeToFitWidth = true
+        label.textAlignment = NSTextAlignment.center
+        label.textColor = UIColor.white
+        label.translatesAutoresizingMaskIntoConstraints = false
+        return label
+    }()
+
+    convenience init(count: Int, size: CGFloat) {
+        self.init(size: size)
+    }
+
+    init(size: CGFloat) {
+        self.minSize = size
+        super.init(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
+        let radius = size / 2
+        layer.cornerRadius = radius
+        translatesAutoresizingMaskIntoConstraints = false
+        self.backgroundColor = UIColor.red
+        addConstraint(constraintHeightTo(size))
+        widthConstraint = constraintWidthTo(size)
+        addConstraint(widthConstraint!)
+        addSubview(label)
+        let labelConstraints = [ label.constraintAlignLeadingTo(self),
+                                 label.constraintAlignTrailingTo(self),
+                                 label.constraintCenterYTo(self) ]
+        self.addConstraints(labelConstraints)
+        isHidden = true
+    }
+
+    required init?(coder _: NSCoder) {
+        fatalError("init(coder:) has not been implemented")
+    }
+
+    public func setCount(_ msgNo: Int) {
+        let countString = NSAttributedString(string: String(msgNo), attributes: [.font: UIFont.systemFont(ofSize: 12)])
+        let countStringSize = countString.width(considering: minSize) + padding
+        if countStringSize > minSize {
+            removeConstraint(widthConstraint!)
+            widthConstraint = constraintWidthTo(countStringSize)
+            addConstraint(widthConstraint!)
+        } else if frame.width > minSize {
+            removeConstraint(widthConstraint!)
+            widthConstraint = constraintWidthTo(minSize)
+            addConstraint(widthConstraint!)
+        }
+        label.attributedText = countString
+        isHidden = (msgNo <= 0)
+    }
+
+}