From e1dfc481ebb8ef2601be43ea7177fff916888b90 Mon Sep 17 00:00:00 2001 From: Martin Kim Dung-Pham Date: Sun, 27 Dec 2020 16:04:49 +0100 Subject: [PATCH] Random improvements while browsing the code --- Books/Books/AccountView.swift | 2 +- Books/Books/ContentView.swift | 11 ++++++----- Books/Books/LoanRow.swift | 4 ++-- Books/Books/SignedInContainerView.swift | 2 +- .../View Models/AuthenticationViewModel.swift | 8 ++++---- Books/Books/View Models/LoanViewModel.swift | 2 +- Books/Books/Views/LoanDetailView.swift | 2 ++ .../XCUIApplication+LaunchOptions.swift | 9 ++++++++- .../XCUIApplication+launchOptions.swift | 9 ++++++++- .../LibraryCore/API/RequestBuilder.swift | 18 +++++++++--------- .../AuthenticationManagerStub.swift | 6 +++--- .../Decoding/FlamingoAccountParser.swift | 12 ++++++------ .../Decoding/ItemDetailsParser.swift | 5 +++-- .../Extensions/ProcessInfo+Testing.swift | 4 +--- .../LibraryCore/Network/NetworkClient.swift | 2 +- .../Network/SensitiveDataProcessor.swift | 2 +- .../LibraryCoreTests/Mocks/KeychainMock.swift | 2 +- .../PublicLibraryScraperTests.swift | 2 +- .../Tests/LibraryCoreTests/TestHelper.swift | 4 ++-- .../LibraryCoreTests/XCTestManifests.swift | 2 +- README.md | 2 ++ 21 files changed, 64 insertions(+), 46 deletions(-) diff --git a/Books/Books/AccountView.swift b/Books/Books/AccountView.swift index 6103d18..7482172 100644 --- a/Books/Books/AccountView.swift +++ b/Books/Books/AccountView.swift @@ -13,7 +13,7 @@ struct AccountView : View { @ObservedObject var authenticationViewModel: AuthenticationViewModel @Environment(\.presentationMode) var presentationMode var body: some View { - return VStack { + VStack { Button(action: { self.presentationMode.wrappedValue.dismiss() DispatchQueue.main.asyncAfter(deadline: .now() + 0.34) { diff --git a/Books/Books/ContentView.swift b/Books/Books/ContentView.swift index 8577839..9b725b8 100644 --- a/Books/Books/ContentView.swift +++ b/Books/Books/ContentView.swift @@ -13,9 +13,10 @@ import Combine struct ContentView : View { @ObservedObject var authenticationViewModel: AuthenticationViewModel var body: some View { - view(for: authenticationViewModel.authenticated).onAppear() { - self.authenticationViewModel.attemptAutomaticAuthentication() - } + view(for: authenticationViewModel.state) + .onAppear() { + self.authenticationViewModel.attemptAutomaticAuthentication() + } } func view(for state: AuthenticationState) -> AnyView { @@ -51,7 +52,7 @@ struct ContentView : View { } func signInView() -> some View { - return SignInView(authentication: authenticationViewModel) + SignInView(authentication: authenticationViewModel) } } @@ -61,7 +62,7 @@ struct ActivityIndicator: UIViewRepresentable { let style: UIActivityIndicatorView.Style func makeUIView(context: UIViewRepresentableContext) -> UIActivityIndicatorView { - return UIActivityIndicatorView(style: style) + UIActivityIndicatorView(style: style) } func updateUIView(_ uiView: UIActivityIndicatorView, context: UIViewRepresentableContext) { diff --git a/Books/Books/LoanRow.swift b/Books/Books/LoanRow.swift index 67c7449..6104713 100644 --- a/Books/Books/LoanRow.swift +++ b/Books/Books/LoanRow.swift @@ -13,7 +13,7 @@ struct LoanRow : View { var loanViewModel: LoanViewModel var body: some View { - return VStack { + VStack { loanViewModel.loan.map({ loan in NavigationLink(destination: LoanDetailView(loanViewModel: loanViewModel)) { VStack(alignment: .leading) { @@ -53,7 +53,7 @@ struct LoanRow : View { #if DEBUG struct LoanRow_Previews : PreviewProvider { static var previews: some View { - return Group { + Group { LoanRow(loanViewModel: loanViewModels[0]) .environment(\.colorScheme, .dark) .previewLayout(.fixed(width: 300, height: 100)) diff --git a/Books/Books/SignedInContainerView.swift b/Books/Books/SignedInContainerView.swift index 3028f66..54965e3 100644 --- a/Books/Books/SignedInContainerView.swift +++ b/Books/Books/SignedInContainerView.swift @@ -19,7 +19,7 @@ struct SignedInContainerView : View { authentication.loansViewModel.map({ LoansView(loansViewModel: $0) }) .navigationBarTitle(Text("BTLB")) .navigationBarItems(trailing: Button(action: { - self.showingAccount.toggle() + showingAccount.toggle() }) { Image(systemName: "person.crop.circle").imageScale(Image.Scale.large) .accessibility(identifier: "Account") diff --git a/Books/Books/View Models/AuthenticationViewModel.swift b/Books/Books/View Models/AuthenticationViewModel.swift index 1c4f773..03a15f7 100644 --- a/Books/Books/View Models/AuthenticationViewModel.swift +++ b/Books/Books/View Models/AuthenticationViewModel.swift @@ -20,7 +20,7 @@ class AuthenticationViewModel: ObservableObject { } var accountViewModel: AccountViewModel - @Published var authenticated: AuthenticationState = .authenticating { + @Published var state: AuthenticationState = .authenticating { didSet { self.handleAuthenticationUpdate(account: self.accountViewModel.account) } @@ -39,11 +39,11 @@ class AuthenticationViewModel: ObservableObject { .sink(receiveCompletion: { completion in switch completion { case .failure(let authenticationError): - self.authenticated = .authenticationError(authenticationError) + self.state = .authenticationError(authenticationError) case .finished: () } }) { authenticated in - self.authenticated = authenticated + self.state = authenticated } } @@ -72,7 +72,7 @@ class AuthenticationViewModel: ObservableObject { return } - if case AuthenticationState.authenticationComplete(.authenticated) = self.authenticated { + if case AuthenticationState.authenticationComplete(.authenticated) = self.state { self.loansViewModel = LoansViewModel(account: account, authenticationManager: authenticationManager) } } diff --git a/Books/Books/View Models/LoanViewModel.swift b/Books/Books/View Models/LoanViewModel.swift index 5b1edaf..cc268d4 100644 --- a/Books/Books/View Models/LoanViewModel.swift +++ b/Books/Books/View Models/LoanViewModel.swift @@ -13,7 +13,7 @@ import LibraryCore public class LoanViewModel: ObservableObject, Hashable { public static func == (lhs: LoanViewModel, rhs: LoanViewModel) -> Bool { - return lhs.loan?.signature == rhs.loan?.signature + lhs.loan?.signature == rhs.loan?.signature } public func hash(into hasher: inout Hasher) { diff --git a/Books/Books/Views/LoanDetailView.swift b/Books/Books/Views/LoanDetailView.swift index 6ecb3ec..c9eb66f 100644 --- a/Books/Books/Views/LoanDetailView.swift +++ b/Books/Books/Views/LoanDetailView.swift @@ -251,7 +251,9 @@ struct LoanDetailView_Previews: PreviewProvider { loan.material = "Material" loan.materialName = "Buch Erwachsene" loan.isReserved = false + let viewModel = LoanViewModel(loan: loan) + return LoanDetailView(loanViewModel: viewModel) .environment(\.colorScheme, .light) } diff --git a/Books/BooksUITests/Support/XCUIApplication+LaunchOptions.swift b/Books/BooksUITests/Support/XCUIApplication+LaunchOptions.swift index 555b7ff..3823e76 100644 --- a/Books/BooksUITests/Support/XCUIApplication+LaunchOptions.swift +++ b/Books/BooksUITests/Support/XCUIApplication+LaunchOptions.swift @@ -21,7 +21,7 @@ extension XCUIApplication { /// /// ``` /// app = XCUIApplication() - /// app.launch(options: [.stub(.networkRequests, in: self), .clean]) + /// app.launch(options: [.stub(.networkRequests, in: self), .cleanKeychain]) /// ``` /// /// - Parameter options: the options to apply. @@ -54,6 +54,10 @@ enum StubbingSubject { } extension XCUIApplication { + + /// Adds a launch option to the launch of the application under test. + /// + /// - Parameter option: The option to add func addOption(_ option: LaunchOption) { let processInfo = ProcessInfo() @@ -69,6 +73,9 @@ extension XCUIApplication { } } + /// Removes a launch option of the application under test. + /// + /// - Parameter option: The option to remove func removeOption(_ option: LaunchOption) { switch option { diff --git a/Books/BooksUITests/Support/XCUIApplication+launchOptions.swift b/Books/BooksUITests/Support/XCUIApplication+launchOptions.swift index 555b7ff..3823e76 100644 --- a/Books/BooksUITests/Support/XCUIApplication+launchOptions.swift +++ b/Books/BooksUITests/Support/XCUIApplication+launchOptions.swift @@ -21,7 +21,7 @@ extension XCUIApplication { /// /// ``` /// app = XCUIApplication() - /// app.launch(options: [.stub(.networkRequests, in: self), .clean]) + /// app.launch(options: [.stub(.networkRequests, in: self), .cleanKeychain]) /// ``` /// /// - Parameter options: the options to apply. @@ -54,6 +54,10 @@ enum StubbingSubject { } extension XCUIApplication { + + /// Adds a launch option to the launch of the application under test. + /// + /// - Parameter option: The option to add func addOption(_ option: LaunchOption) { let processInfo = ProcessInfo() @@ -69,6 +73,9 @@ extension XCUIApplication { } } + /// Removes a launch option of the application under test. + /// + /// - Parameter option: The option to remove func removeOption(_ option: LaunchOption) { switch option { diff --git a/LibraryCore/Sources/LibraryCore/API/RequestBuilder.swift b/LibraryCore/Sources/LibraryCore/API/RequestBuilder.swift index e2c3fbc..828cd4d 100644 --- a/LibraryCore/Sources/LibraryCore/API/RequestBuilder.swift +++ b/LibraryCore/Sources/LibraryCore/API/RequestBuilder.swift @@ -121,15 +121,15 @@ final class RequestBuilder { /// - returns: An optional `URLRequest` for the given body and SOAP action private func request(with body: Data?, path: String, action: String) -> URLRequest? { - return URLRequest.request(method: "POST", - host: "zones.buecherhallen.de", - path: path, - body: body, - headers: ["Content-Type": "text/xml; charset=utf-8", - "SOAPAction": "http://bibliomondo.com/websevices/\(action)", - "Accept": "*/*", - "Accept-Language": "en-us", - "Accept-Encoding": "br, gzip, deflate"]) + URLRequest.request(method: "POST", + host: "zones.buecherhallen.de", + path: path, + body: body, + headers: ["Content-Type": "text/xml; charset=utf-8", + "SOAPAction": "http://bibliomondo.com/websevices/\(action)", + "Accept": "*/*", + "Accept-Language": "en-us", + "Accept-Encoding": "br, gzip, deflate"]) } /** diff --git a/LibraryCore/Sources/LibraryCore/Authentication/Authentication Manager/AuthenticationManagerStub.swift b/LibraryCore/Sources/LibraryCore/Authentication/Authentication Manager/AuthenticationManagerStub.swift index 0e6cc77..730f4f9 100644 --- a/LibraryCore/Sources/LibraryCore/Authentication/Authentication Manager/AuthenticationManagerStub.swift +++ b/LibraryCore/Sources/LibraryCore/Authentication/Authentication Manager/AuthenticationManagerStub.swift @@ -12,16 +12,16 @@ import Foundation /// An Authentication Manager stub public class AuthenticationManagerStub: AuthenticationManager { - public var authenticated: AuthenticationState = .authenticating + public var state: AuthenticationState = .authenticating public var error: NSError? = nil public var stubbedSessionIdentifier: String? = nil public override func authenticateAccount(username: String?, password: String?) { - authenticatedSubject.send(authenticated) + authenticatedSubject.send(state) } override func sessionIdentifier(for accountIdentifier: String) -> String? { - return stubbedSessionIdentifier + stubbedSessionIdentifier } } diff --git a/LibraryCore/Sources/LibraryCore/Decoding/FlamingoAccountParser.swift b/LibraryCore/Sources/LibraryCore/Decoding/FlamingoAccountParser.swift index 53b2516..c9d58f5 100644 --- a/LibraryCore/Sources/LibraryCore/Decoding/FlamingoAccountParser.swift +++ b/LibraryCore/Sources/LibraryCore/Decoding/FlamingoAccountParser.swift @@ -24,10 +24,10 @@ class FlamingoCharge: Equatable { } static func ==(lhs: FlamingoCharge, rhs: FlamingoCharge) -> Bool { - return lhs.reason == rhs.reason && - lhs.date == rhs.date && - lhs.debit == rhs.debit && - lhs.credit == rhs.credit + lhs.reason == rhs.reason && + lhs.date == rhs.date && + lhs.debit == rhs.debit && + lhs.credit == rhs.credit } var debugDescription: String { @@ -45,8 +45,8 @@ class FlamingoAccount { init?(identifier: String?, name: String?, email: String?, charges: [FlamingoCharge] = []) { guard let identifier = identifier, - let name = name, let email = email else { - return nil + let name = name, let email = email else { + return nil } self.identifier = identifier diff --git a/LibraryCore/Sources/LibraryCore/Decoding/ItemDetailsParser.swift b/LibraryCore/Sources/LibraryCore/Decoding/ItemDetailsParser.swift index 9035dff..231c33d 100644 --- a/LibraryCore/Sources/LibraryCore/Decoding/ItemDetailsParser.swift +++ b/LibraryCore/Sources/LibraryCore/Decoding/ItemDetailsParser.swift @@ -19,13 +19,14 @@ class FlamingoInfoPair: Equatable { } static func ==(lhs: FlamingoInfoPair, rhs: FlamingoInfoPair) -> Bool { - return lhs.title == rhs.title && lhs.content == rhs.content + lhs.title == rhs.title && lhs.content == rhs.content } } extension FlamingoInfoPair: CustomDebugStringConvertible { + var debugDescription: String { - return "FlamingoInfoPair(title: \"\(title)\", content: \"\(content)\")" + "FlamingoInfoPair(title: \"\(title)\", content: \"\(content)\")" } } diff --git a/LibraryCore/Sources/LibraryCore/Extensions/ProcessInfo+Testing.swift b/LibraryCore/Sources/LibraryCore/Extensions/ProcessInfo+Testing.swift index f601e7d..2977a0c 100644 --- a/LibraryCore/Sources/LibraryCore/Extensions/ProcessInfo+Testing.swift +++ b/LibraryCore/Sources/LibraryCore/Extensions/ProcessInfo+Testing.swift @@ -10,8 +10,6 @@ import Foundation public extension ProcessInfo { /// Returns `true` if the `ProcessInfo` contains a `THE_STUBBORN_NETWORK_UI_TESTING` environment variable. var isUITesting: Bool { - get { - return environment["THE_STUBBORN_NETWORK_UI_TESTING"] != nil - } + environment["THE_STUBBORN_NETWORK_UI_TESTING"] != nil } } diff --git a/LibraryCore/Sources/LibraryCore/Network/NetworkClient.swift b/LibraryCore/Sources/LibraryCore/Network/NetworkClient.swift index 643e6ed..fc0d40b 100644 --- a/LibraryCore/Sources/LibraryCore/Network/NetworkClient.swift +++ b/LibraryCore/Sources/LibraryCore/Network/NetworkClient.swift @@ -34,7 +34,7 @@ public class NetworkClient { } func dataTask(with request: URLRequest, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask { - return session.dataTask(with: request, completionHandler: completionHandler) + session.dataTask(with: request, completionHandler: completionHandler) } } diff --git a/LibraryCore/Sources/LibraryCore/Network/SensitiveDataProcessor.swift b/LibraryCore/Sources/LibraryCore/Network/SensitiveDataProcessor.swift index b325f4e..5a9b77a 100644 --- a/LibraryCore/Sources/LibraryCore/Network/SensitiveDataProcessor.swift +++ b/LibraryCore/Sources/LibraryCore/Network/SensitiveDataProcessor.swift @@ -36,7 +36,7 @@ struct SensitiveDataProcessor: BodyDataProcessor { } func dataForDeliveringResponseBody(data: Data?, of request: URLRequest) -> Data? { - return data + data } } diff --git a/LibraryCore/Tests/LibraryCoreTests/Mocks/KeychainMock.swift b/LibraryCore/Tests/LibraryCoreTests/Mocks/KeychainMock.swift index 47763e7..f42a3d9 100644 --- a/LibraryCore/Tests/LibraryCoreTests/Mocks/KeychainMock.swift +++ b/LibraryCore/Tests/LibraryCoreTests/Mocks/KeychainMock.swift @@ -22,7 +22,7 @@ class KeychainMock: TestableKeychainProvider { } func password(for account: String) -> String? { - return addedKeychainItems[account] + addedKeychainItems[account] } func deletePassword(of account: String) { diff --git a/LibraryCore/Tests/LibraryCoreTests/PublicLibraryScraperTests.swift b/LibraryCore/Tests/LibraryCoreTests/PublicLibraryScraperTests.swift index e61f8a1..619eff2 100644 --- a/LibraryCore/Tests/LibraryCoreTests/PublicLibraryScraperTests.swift +++ b/LibraryCore/Tests/LibraryCoreTests/PublicLibraryScraperTests.swift @@ -61,7 +61,7 @@ class PublicLibraryScraperTests: XCTestCase { let loanDetailRequestB = try XCTUnwrap(RequestBuilder.default.itemDetailsRequest(itemIdentifier: "T01684642X")) stubbornNetwork.stub(request: loanDetailRequestB, data: publicLoanDetailResponseBody) scraper.loans(account, authenticationManager: AuthenticationManager.stubbed({ (manager) in - manager.authenticated = .authenticationComplete(.authenticated) + manager.state = .authenticationComplete(.authenticated) manager.stubbedSessionIdentifier = "abc" })) { (error, loans) -> (Void) in XCTAssertEqual(loans.count, 2) diff --git a/LibraryCore/Tests/LibraryCoreTests/TestHelper.swift b/LibraryCore/Tests/LibraryCoreTests/TestHelper.swift index 365592d..25c8c24 100644 --- a/LibraryCore/Tests/LibraryCoreTests/TestHelper.swift +++ b/LibraryCore/Tests/LibraryCoreTests/TestHelper.swift @@ -22,10 +22,10 @@ final class TestHelper { } static var keychainMock: TestableKeychainProvider { - return KeychainMock() + KeychainMock() } static func accountStub() -> AccountModel { - return AccountModel() + AccountModel() } } diff --git a/LibraryCore/Tests/LibraryCoreTests/XCTestManifests.swift b/LibraryCore/Tests/LibraryCoreTests/XCTestManifests.swift index c93b738..58a5b6c 100644 --- a/LibraryCore/Tests/LibraryCoreTests/XCTestManifests.swift +++ b/LibraryCore/Tests/LibraryCoreTests/XCTestManifests.swift @@ -2,7 +2,7 @@ import XCTest #if !canImport(ObjectiveC) public func allTests() -> [XCTestCaseEntry] { - return [ + [ testCase(LibraryCoreTests.allTests), ] } diff --git a/README.md b/README.md index b9435fd..9b81f2a 100644 --- a/README.md +++ b/README.md @@ -11,3 +11,5 @@ For the Xcode UITests it comes handy to prepare the keyboard in advance so that the monkey can type properly: `defaults write com.apple.iphonesimulator ConnectHardwareKeyboard -bool false` + +[![Build Status](https://travis-ci.com/q231950/Books.svg?branch=master)](https://travis-ci.com/q231950/Books)