From 82c0a5938e0ab2724c78ed61580c1202fd5731bf Mon Sep 17 00:00:00 2001 From: dankito Date: Fri, 16 Oct 2020 16:22:20 +0200 Subject: [PATCH] Extracted extension methods to convert KotlinCharArray --- .../Security/AuthenticationService.swift | 8 +-- .../extensions/SwiftExtensions.swift | 58 ++++++++++++++----- .../CoreDataBankingPersistence.swift | 10 +--- 3 files changed, 45 insertions(+), 31 deletions(-) diff --git a/ui/BankingiOSApp/BankingiOSApp/Security/AuthenticationService.swift b/ui/BankingiOSApp/BankingiOSApp/Security/AuthenticationService.swift index dda1a2dd..a4efcae0 100644 --- a/ui/BankingiOSApp/BankingiOSApp/Security/AuthenticationService.swift +++ b/ui/BankingiOSApp/BankingiOSApp/Security/AuthenticationService.swift @@ -479,13 +479,7 @@ class AuthenticationService { } private func map(_ string: String) -> KotlinCharArray { - let array = KotlinCharArray(size: Int32(string.count)) - - for i in 0 ..< string.count { - array.set(index: Int32(i), value: (string as NSString).character(at: i)) - } - - return array + return string.toKotlinCharArray() } } diff --git a/ui/BankingiOSApp/BankingiOSApp/extensions/SwiftExtensions.swift b/ui/BankingiOSApp/BankingiOSApp/extensions/SwiftExtensions.swift index bd47f793..80c7ff51 100644 --- a/ui/BankingiOSApp/BankingiOSApp/extensions/SwiftExtensions.swift +++ b/ui/BankingiOSApp/BankingiOSApp/extensions/SwiftExtensions.swift @@ -1,5 +1,6 @@ import SwiftUI import CoreData +import BankingUiSwift extension StringProtocol { @@ -69,26 +70,53 @@ extension StringProtocol { extension String { - func localize() -> String { - return NSLocalizedString(self, comment: "") + func localize() -> String { + return NSLocalizedString(self, comment: "") + } + + // TODO: implement passing multiple arguments to localize() +// func localize(_ arguments: CVarArg...) -> String { +// return localize(arguments) +// } +// +// func localize(_ arguments: [CVarArg]) -> String { +// return String(format: NSLocalizedString(self, comment: ""), arguments) +// return String(format: NSLocalizedString(self, comment: ""), getVaList(arguments)) +// } + + func localize(_ arguments: CVarArg) -> String { + return String(format: NSLocalizedString(self, comment: ""), arguments) + } + + + func toKotlinCharArray() -> KotlinCharArray { + let array = KotlinCharArray(size: Int32(self.count)) + + for i in 0 ..< self.count { + array.set(index: Int32(i), value: (self as NSString).character(at: i)) } - // TODO: implement passing multiple arguments to localize() - // func localize(_ arguments: CVarArg...) -> String { - // return localize(arguments) - // } - // - // func localize(_ arguments: [CVarArg]) -> String { - // return String(format: NSLocalizedString(self, comment: ""), arguments) - // return String(format: NSLocalizedString(self, comment: ""), getVaList(arguments)) - // } - - func localize(_ arguments: CVarArg) -> String { - return String(format: NSLocalizedString(self, comment: ""), arguments) - } + return array + } } +extension KotlinCharArray { + + func toString() -> String { + var mapped = [Character]() + + for i in 0 ..< self.size { + if let scalar = Unicode.Scalar(self.get(index: i)) { + mapped.append(Character(scalar)) + } + } + + return String(mapped) + } + +} + extension Optional where Wrapped == String { diff --git a/ui/BankingiOSApp/BankingiOSApp/persistence/CoreDataBankingPersistence.swift b/ui/BankingiOSApp/BankingiOSApp/persistence/CoreDataBankingPersistence.swift index 16f71a9f..48e6ab5e 100644 --- a/ui/BankingiOSApp/BankingiOSApp/persistence/CoreDataBankingPersistence.swift +++ b/ui/BankingiOSApp/BankingiOSApp/persistence/CoreDataBankingPersistence.swift @@ -86,15 +86,7 @@ class CoreDataBankingPersistence: IBankingPersistence, ITransactionPartySearcher } private func map(_ array: KotlinCharArray) -> String { - var mapped = [Character]() - - for i in 0 ..< array.size { - if let scalar = Unicode.Scalar(array.get(index: i)) { - mapped.append(Character(scalar)) - } - } - - return String(mapped) + return array.toString() }