pEpForiOS/UI/Handshake/ViewModel/HandshakePartnerTableViewCellViewModel.swift
author buff <andreas@pep-project.org>
Wed, 15 May 2019 10:47:37 +0200
branchIOS-1521+IOS-1495
changeset 8620 81e20eeddbe0
parent 8525 70d607f356db
child 8706 4a0b9b1fe61a
permissions -rw-r--r--
IOS-1521 reminder for root of CdCrash
dirk@2079
     1
//
dirk@2084
     2
//  HandshakePartnerTableViewCellViewModel.swift
dirk@2079
     3
//  pEpForiOS
dirk@2079
     4
//
dirk@2079
     5
//  Created by Dirk Zimmermann on 26.04.17.
dirk@2079
     6
//  Copyright © 2017 p≡p Security S.A. All rights reserved.
dirk@2079
     7
//
dirk@2079
     8
dirk@2079
     9
import Foundation
xavier@7644
    10
import pEpIOSToolbox
dirk@2079
    11
import MessageModel
andreas@7908
    12
import PEPObjCAdapterFramework
andreas@7908
    13
import PEPObjCAdapterFramework
dirk@2079
    14
dirk@2084
    15
class HandshakePartnerTableViewCellViewModel {
dirk@2079
    16
    enum ExpandedState {
dirk@2079
    17
        case notExpanded
dirk@2079
    18
        case expanded
dirk@2079
    19
    }
dirk@2079
    20
dirk@2094
    21
    /**
dirk@2094
    22
     The background changes depending on the position in the list, alternating between
dirk@2094
    23
     light and dark.
dirk@2094
    24
     */
dirk@2094
    25
    var backgroundColorDark = true
dirk@2094
    26
dirk@2102
    27
    /** Do we show the trustwords for this identity? */
dirk@2094
    28
    var showTrustwords: Bool {
dirk@4335
    29
        switch partnerColor {
dz@7851
    30
        case .yellow:
dirk@4282
    31
            return true
dirk@4282
    32
        default:
dirk@4282
    33
            return false
dirk@4282
    34
        }
dirk@2102
    35
    }
dirk@2102
    36
dirk@2102
    37
    /** Show the button for start/stop trusting? */
dirk@2102
    38
    var showStopStartTrustButton: Bool {
dz@7859
    39
        return partnerColor == .green || partnerColor == PEPColor.red ||
dz@7860
    40
            partnerRating == .haveNoKey
dirk@2094
    41
    }
dirk@2094
    42
dirk@2080
    43
    var expandedState: ExpandedState
dirk@4277
    44
dirk@4277
    45
    /**
dirk@4277
    46
     The rating of the partner.
dirk@4277
    47
     */
dz@7795
    48
    var partnerRating: PEPRating
dirk@4277
    49
dirk@4277
    50
    /**
dirk@4277
    51
     The color of the partner.
dirk@4277
    52
     */
dz@7807
    53
    var partnerColor: PEPColor
dirk@4277
    54
borja@5653
    55
    var trustwordsLanguage: String{
borja@5653
    56
        didSet{
borja@5653
    57
            updateTrustwords(session: session)
borja@5653
    58
        }
borja@5653
    59
    }
dirk@2151
    60
    var trustwordsFull = false
dirk@2079
    61
dirk@4277
    62
    var ownName: String {
dirk@4277
    63
        return pEpSelf.userName ?? pEpSelf.address
dirk@4277
    64
    }
dirk@2094
    65
dirk@4277
    66
    var partnerName: String {
dirk@4277
    67
        return pEpPartner.userName ?? pEpPartner.address
dirk@2094
    68
    }
dirk@2079
    69
dirk@2087
    70
    var partnerImage = ObservableValue<UIImage>()
dirk@2080
    71
    var trustwords: String?
dirk@2079
    72
dirk@2087
    73
    /**
dirk@2087
    74
     pEp session usable on the main thread.
dirk@2087
    75
     */
dirk@2087
    76
    let session: PEPSession
dirk@2087
    77
xavier@3892
    78
    var isPartnerpEpUser = false
dirk@2112
    79
dirk@2127
    80
    /**
dirk@4277
    81
     Have to store this for some future access from the owning VC.
dirk@4277
    82
     */
dirk@4277
    83
    let ownIdentity: Identity
dirk@4277
    84
dirk@4277
    85
    /**
dirk@2127
    86
     Cache the updated own identity.
dirk@2127
    87
     */
dirk@3349
    88
    let pEpSelf: PEPIdentity
dirk@2127
    89
dirk@2127
    90
    /**
dirk@2127
    91
     Cache the updated partner identity.
dirk@2127
    92
     */
dirk@3349
    93
    var pEpPartner: PEPIdentity
dirk@2127
    94
andreas@4085
    95
    lazy var contactImageTool = IdentityImageTool()
andreas@4085
    96
andreas@4085
    97
    init(ownIdentity: Identity, partner: Identity, session: PEPSession) {
dirk@2080
    98
        self.expandedState = .notExpanded
dirk@2080
    99
        self.trustwordsLanguage = "en"
andreas@2838
   100
        self.session = session
dz@7931
   101
        self.partnerRating = PEPUtil.pEpRating(identity: partner, session: session)
dirk@4591
   102
        self.partnerColor = partnerRating.pEpColor()
dirk@4277
   103
        self.ownIdentity = ownIdentity
dirk@2079
   104
dirk@3984
   105
        pEpSelf = ownIdentity.updatedIdentity(session: session)
dirk@3984
   106
        pEpPartner = partner.updatedIdentity(session: session)
dirk@2127
   107
dirk@4428
   108
        do {
dirk@4428
   109
            isPartnerpEpUser = try session.isPEPUser(pEpPartner).boolValue
dirk@4428
   110
        } catch let err as NSError {
agp@8234
   111
            Log.shared.error("%{public}@", err.localizedDescription)
dirk@4428
   112
            isPartnerpEpUser = false
dirk@4428
   113
        }
andreas@4085
   114
        setPartnerImage(for: partner)
andreas@4085
   115
        updateTrustwords(session: session)
andreas@4085
   116
    }
andreas@4085
   117
andreas@4085
   118
    private func setPartnerImage(`for` partnerIdentity: Identity) {
andreas@4085
   119
        if let cachedContactImage =
andreas@8496
   120
            contactImageTool.cachedIdentityImage(for: IdentityImageTool.IdentityKey(identity: partnerIdentity)) {
andreas@4085
   121
            partnerImage.value = cachedContactImage
andreas@4085
   122
        } else {
andreas@8496
   123
            DispatchQueue.global().async { [weak self] in
andreas@8496
   124
                guard let me = self else {
andreas@8525
   125
                    Log.shared.errorAndCrash("Lost myself")
andreas@8496
   126
                    return
andreas@8620
   127
                } //!!!: needs session (partnerIdentity?)
andreas@8496
   128
                let contactImage = me.contactImageTool.identityImage(for: IdentityImageTool.IdentityKey(identity: partnerIdentity))
andreas@8496
   129
                me.partnerImage.value = contactImage
dirk@2079
   130
            }
dirk@2079
   131
        }
dirk@2127
   132
    }
dirk@2106
   133
andreas@3321
   134
    func updateTrustwords(session: PEPSession = PEPSession()) {
xavier@3892
   135
        if !isPartnerpEpUser,
dirk@3349
   136
            let fprSelf = pEpSelf.fingerPrint,
dirk@3349
   137
            let fprPartner = pEpPartner.fingerPrint {
dirk@2106
   138
            let fprPrettySelf = fprSelf.prettyFingerPrint()
dirk@2106
   139
            let fprPrettyPartner = fprPartner.prettyFingerPrint()
dirk@2106
   140
            self.trustwords =
dirk@4277
   141
                "\(partnerName):\n\(fprPrettyPartner)\n\n" + "\(ownName):\n\(fprPrettySelf)"
dirk@2106
   142
        } else {
dirk@3988
   143
            self.trustwords = determineTrustwords(identitySelf: pEpSelf,
dirk@3988
   144
                                                  identityPartner: pEpPartner)
dirk@2106
   145
        }
dirk@2079
   146
    }
dirk@2087
   147
dirk@3988
   148
    func determineTrustwords(identitySelf: PEPIdentity, identityPartner: PEPIdentity) -> String? {
dirk@4423
   149
        do {
dirk@4423
   150
            return try session.getTrustwordsIdentity1(
dirk@4423
   151
                identitySelf,
dirk@4423
   152
                identity2: identityPartner,
dirk@4423
   153
                language: trustwordsLanguage,
dirk@4423
   154
                full: trustwordsFull)
dirk@4423
   155
        } catch let err as NSError {
agp@8234
   156
            Log.shared.error("%{public}@", err.localizedDescription)
dirk@4423
   157
            return nil
dirk@4423
   158
        }
dirk@2947
   159
    }
dirk@2947
   160
dirk@2147
   161
    func toggleTrustwordsLength() {
dirk@2147
   162
        trustwordsFull = !trustwordsFull
dirk@2147
   163
        updateTrustwords(session: session)
dirk@2147
   164
    }
dirk@2147
   165
dirk@3349
   166
    func invokeTrustAction(action: (PEPIdentity) -> ()) {
dirk@4279
   167
        let theBackup = PEPIdentity(identity: pEpPartner)
dirk@2269
   168
        action(pEpPartner)
dirk@4279
   169
        pEpPartner = theBackup
dirk@4277
   170
dirk@4277
   171
        do {
dirk@4440
   172
            partnerRating = try session.rating(for: pEpPartner).pEpRating
dz@7931
   173
            partnerColor = PEPUtil.pEpColor(pEpRating: partnerRating)
dirk@4277
   174
        } catch let error as NSError {
dirk@4277
   175
            assertionFailure("\(error)")
dirk@4277
   176
        }
dirk@4277
   177
dirk@2269
   178
        updateTrustwords(session: session)
dirk@2087
   179
    }
dirk@2087
   180
dirk@2087
   181
    public func confirmTrust() {
dirk@2087
   182
        invokeTrustAction() { thePartner in
dirk@4243
   183
            do {
dirk@4243
   184
                try session.trustPersonalKey(thePartner)
dirk@4243
   185
            } catch let error as NSError {
dirk@4243
   186
                assertionFailure("\(error)")
dirk@4243
   187
            }
dirk@2087
   188
        }
dirk@2087
   189
    }
dirk@2087
   190
dirk@2087
   191
    public func denyTrust() {
dirk@2087
   192
        invokeTrustAction() { thePartner in
dirk@4244
   193
            do {
dirk@4244
   194
                try session.keyMistrusted(thePartner)
dirk@4244
   195
            } catch let error as NSError {
dirk@4244
   196
                assertionFailure("\(error)")
dirk@4244
   197
            }
dirk@2087
   198
        }
dirk@2087
   199
    }
dirk@2087
   200
dirk@4281
   201
    /**
dirk@4335
   202
     Used for undoing a trust or mistrust.
dirk@4346
   203
     - Note: Since undoLastMistrust is currently not implemented with all consequences,
dirk@4346
   204
     it is not used.
dirk@4281
   205
     */
dirk@4346
   206
    public func resetOrUndoTrustOrMistrust() {
dirk@2087
   207
        invokeTrustAction() { thePartner in
dirk@4269
   208
            do {
dirk@4345
   209
                try session.keyResetTrust(thePartner)
dirk@4269
   210
            } catch let error as NSError {
dirk@4345
   211
                assertionFailure("\(error)")
dirk@4269
   212
            }
dirk@2087
   213
        }
dirk@2087
   214
    }
dirk@2079
   215
}