IOS-1542 Collapse create/update code for servers. IOS-1542
authorDirk Zimmermann <dz@pep.security>
Thu, 25 Apr 2019 16:25:40 +0200
branchIOS-1542
changeset 837873a67e61d662
parent 8377 0ed9a236fa32
child 8379 f19109e055f9
IOS-1542 Collapse create/update code for servers.
pEpForiOS/Models/VerifiableAccount/VerifiableAccount.swift
     1.1 --- a/pEpForiOS/Models/VerifiableAccount/VerifiableAccount.swift	Thu Apr 25 16:25:40 2019 +0200
     1.2 +++ b/pEpForiOS/Models/VerifiableAccount/VerifiableAccount.swift	Thu Apr 25 16:25:40 2019 +0200
     1.3 @@ -176,45 +176,23 @@
     1.4  
     1.5              let cdAccount = findOrCreateAccount(context: moc, identity: cdIdentity)
     1.6  
     1.7 -            var imapServer: CdServer?
     1.8 -            if let theServer = cdAccount.imapCdServer {
     1.9 -                update(server: theServer,
    1.10 -                       address: addressImap,
    1.11 -                       port: portIMAP,
    1.12 -                       serverType: .imap,
    1.13 -                       authMethod: authMethod,
    1.14 -                       trusted: trustedImapServer,
    1.15 -                       transport: transportIMAP)
    1.16 -                imapServer = theServer
    1.17 -            }
    1.18 +            let theImapServer = update(
    1.19 +                server: cdAccount.imapCdServer ?? CdServer.create(context: moc),
    1.20 +                address: addressImap,
    1.21 +                port: portIMAP,
    1.22 +                serverType: .imap,
    1.23 +                authMethod: authMethod,
    1.24 +                trusted: trustedImapServer,
    1.25 +                transport: transportIMAP)
    1.26  
    1.27 -            let theImapServer = imapServer ?? createServer(context: moc,
    1.28 -                                                           address: addressImap,
    1.29 -                                                           port: portIMAP,
    1.30 -                                                           serverType: .imap,
    1.31 -                                                           authMethod: authMethod,
    1.32 -                                                           trusted: trustedImapServer,
    1.33 -                                                           transport: transportIMAP)
    1.34 -
    1.35 -            var smtpServer: CdServer?
    1.36 -            if let theServer = cdAccount.smtpCdServer {
    1.37 -                update(server: theServer,
    1.38 -                       address: addressSmtp,
    1.39 -                       port: portSMTP,
    1.40 -                       serverType: .smtp,
    1.41 -                       authMethod: authMethod,
    1.42 -                       trusted: false,
    1.43 -                       transport: transportSMTP)
    1.44 -                smtpServer = theServer
    1.45 -            }
    1.46 -
    1.47 -            let theSmtpServer = smtpServer ?? createServer(context: moc,
    1.48 -                                                           address: addressSmtp,
    1.49 -                                                           port: portSMTP,
    1.50 -                                                           serverType: .smtp,
    1.51 -                                                           authMethod: authMethod,
    1.52 -                                                           trusted: false,
    1.53 -                                                           transport: transportSMTP)
    1.54 +            let theSmtpServer = update(
    1.55 +                server: cdAccount.smtpCdServer ?? CdServer.create(context: moc),
    1.56 +                address: addressSmtp,
    1.57 +                port: portSMTP,
    1.58 +                serverType: .smtp,
    1.59 +                authMethod: authMethod,
    1.60 +                trusted: false,
    1.61 +                transport: transportSMTP)
    1.62  
    1.63              let credentialsImap = theImapServer.credentials ?? createCredentials(
    1.64                  context: moc,
    1.65 @@ -339,31 +317,13 @@
    1.66              return credentials
    1.67      }
    1.68  
    1.69 -    private func createServer(context: NSManagedObjectContext,
    1.70 -                              address: String,
    1.71 -                              port: UInt16,
    1.72 -                              serverType: Server.ServerType,
    1.73 -                              authMethod: AuthMethod?,
    1.74 -                              trusted: Bool,
    1.75 -                              transport: ConnectionTransport) -> CdServer {
    1.76 -        let server = CdServer.create(context: context)
    1.77 -        update(server: server,
    1.78 -               address: address,
    1.79 -               port: port,
    1.80 -               serverType: serverType,
    1.81 -               authMethod: authMethod,
    1.82 -               trusted: trusted,
    1.83 -               transport: transport)
    1.84 -        return server
    1.85 -    }
    1.86 -
    1.87      private func update(server: CdServer,
    1.88                          address: String,
    1.89                          port: UInt16,
    1.90                          serverType: Server.ServerType,
    1.91                          authMethod: AuthMethod?,
    1.92                          trusted: Bool,
    1.93 -                        transport: ConnectionTransport) {
    1.94 +                        transport: ConnectionTransport) -> CdServer {
    1.95          server.address = address
    1.96          server.port = NSNumber.init(value: port)
    1.97          server.authMethod = authMethod?.rawValue
    1.98 @@ -371,6 +331,8 @@
    1.99          server.trusted = trusted
   1.100          server.transport = transport.toServerTransport()
   1.101          server.serverType = serverType
   1.102 +
   1.103 +        return server
   1.104      }
   1.105  
   1.106      // MARK: - Legacy