Show online status

pull/26/head
Niels Andriesse 6 years ago
parent 35f2354327
commit e81a074efc

@ -306,6 +306,7 @@
[avatarView autoPinLeadingToSuperviewMargin];
[avatarView autoSetDimension:ALDimensionWidth toSize:kLargeAvatarSize];
[avatarView autoSetDimension:ALDimensionHeight toSize:kLargeAvatarSize];
avatarView.contactID = OWSIdentityManager.sharedManager.identityKeyPair.hexEncodedPublicKey;
if (!localProfileAvatarImage) {
UIImage *cameraImage = [UIImage imageNamed:@"settings-avatar-camera"];

@ -61,8 +61,6 @@ public class ConversationHeaderView: UIStackView {
let avatarView = ConversationAvatarImageView(thread: thread, diameter: 36, contactsManager: contactsManager)
self.avatarView = avatarView
// remove default border on avatarView
avatarView.layer.borderWidth = 0
titleLabel = UILabel()
titleLabel.textColor = Theme.navbarTitleColor

@ -360,7 +360,8 @@ NS_ASSUME_NONNULL_BEGIN
self.avatarView.image = nil;
return;
}
self.avatarView.contactID = thread.contactIdentifier;
[self.avatarView updateOnlineStatusIndicator];
self.avatarView.image = [OWSAvatarBuilder buildImageForThread:thread.threadRecord diameter:self.avatarSize];
}

@ -908,6 +908,7 @@ const CGFloat kIconViewLength = 24;
[avatarView autoPinLeadingToSuperviewMargin];
[avatarView autoSetDimension:ALDimensionWidth toSize:kLargeAvatarSize];
[avatarView autoSetDimension:ALDimensionHeight toSize:kLargeAvatarSize];
avatarView.contactID = self.thread.contactIdentifier;
UIView *threadNameView = [UIView containerView];
[threadInfoView addSubview:threadNameView];

@ -6,30 +6,37 @@ import UIKit
@objc
public class AvatarImageView: UIImageView {
private let shadowLayer = CAShapeLayer()
@objc var contactID: String = ""
public init() {
super.init(frame: .zero)
self.configureView()
self.initialize()
}
override init(frame: CGRect) {
super.init(frame: frame)
self.configureView()
self.initialize()
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.configureView()
self.initialize()
}
override init(image: UIImage?) {
super.init(image: image)
self.configureView()
self.initialize()
}
func configureView() {
func initialize() {
// Loki: Used to indicate a contact's online status
layer.borderWidth = 3
// Loki: Observe online status changes
NotificationCenter.default.addObserver(self, selector: #selector(handleOnlineStatusChangedNotification), name: .contactOnlineStatusChanged, object: nil)
// Set up UI
self.autoPinToSquareAspectRatio()
self.layer.minificationFilter = .trilinear
@ -44,6 +51,9 @@ public class AvatarImageView: UIImageView {
override public func layoutSubviews() {
self.layer.cornerRadius = self.frame.size.width / 2
// Loki: Update the online status indicator if needed
updateOnlineStatusIndicator()
// Inner shadow.
// This should usually not be visible; it is used to distinguish
// profile pics from the background if they are similar.
@ -63,6 +73,25 @@ public class AvatarImageView: UIImageView {
self.shadowLayer.shadowOpacity = 0.15
self.shadowLayer.shadowOffset = .zero
}
deinit {
NotificationCenter.default.removeObserver(self)
}
@objc private func handleOnlineStatusChangedNotification(_ notification: Notification) {
let contactID = notification.object as! String
guard contactID == self.contactID else { return }
updateOnlineStatusIndicator()
}
@objc func updateOnlineStatusIndicator() {
let peerInfo = LokiP2PManager.getInfo(for: contactID)
let isOnline = peerInfo?.isOnline ?? false
let color: UIColor = isOnline ? .ows_green : .ows_gray75
let currentUserID = OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey
let isCurrentUser = (contactID == currentUserID)
layer.borderColor = isCurrentUser ? UIColor.clear.cgColor : color.cgColor
}
}
/// Avatar View which updates itself as necessary when the profile, contact, or group picture changes.
@ -100,6 +129,7 @@ public class ConversationAvatarImageView: AvatarImageView {
super.init(frame: .zero)
if recipientId != nil {
self.contactID = recipientId! // Loki
NotificationCenter.default.addObserver(self, selector: #selector(handleOtherUsersProfileChanged(notification:)), name: NSNotification.Name(rawValue: kNSNotificationName_OtherUsersProfileDidChange), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handleSignalAccountsChanged(notification:)), name: NSNotification.Name.OWSContactsManagerSignalAccountsDidChange, object: nil)

@ -76,9 +76,9 @@ import PromiseKit
LokiP2PManager.markOffline(destination)
if lokiMessage.isPing {
Logger.warn("[Loki] Failed to ping \(destination); marking contact as offline.")
if let nsError = error as? NSError {
nsError.isRetryable = false
throw nsError
if let error = error as? NSError {
error.isRetryable = false
throw error
} else {
throw error
}

@ -8,12 +8,12 @@
private static let offlinePingTime = 2 * kMinuteInterval
/// A p2p state struct
internal struct PeerInfo {
var address: String
var port: UInt16
var isOnline: Bool
var timerDuration: Double
var pingTimer: Timer? = nil
public struct PeerInfo {
public var address: String
public var port: UInt16
public var isOnline: Bool
public var timerDuration: Double
public var pingTimer: Timer? = nil
}
/// Our p2p address
@ -77,7 +77,7 @@
///
/// - Parameter pubKey: The contact hex pubkey
/// - Returns: The P2P Details or nil if they don't exist
internal static func getInfo(for hexEncodedPublicKey: String) -> PeerInfo? {
public static func getInfo(for hexEncodedPublicKey: String) -> PeerInfo? {
return peerInfo[hexEncodedPublicKey]
}
@ -163,6 +163,8 @@
info.isOnline = isOnline
peerInfo[pubKey] = info
NotificationCenter.default.post(name: .contactOnlineStatusChanged, object: pubKey)
}
}

@ -0,0 +1,4 @@
public extension Notification.Name {
public static let contactOnlineStatusChanged = Notification.Name("contactOnlineStatusChanged")
}
Loading…
Cancel
Save