#40: Add UserSessionStoreProtocol.

Only log out of the specific account.
Add tests for the keychain controller.
Expand test coverage.
PR comments
This commit is contained in:
Doug
2022-06-15 10:10:22 +01:00
committed by Doug
parent abd6a9c7c0
commit 8653f1dd6b
17 changed files with 261 additions and 78 deletions
@@ -0,0 +1,64 @@
//
// Bundle.swift
// ElementX
//
// Created by Ismail on 15.04.2022.
// Copyright © 2022 Element. All rights reserved.
//
import Foundation
public extension Bundle {
/// Returns the real app bundle.
/// Can also be used in app extensions.
static let app: Bundle = {
let bundle = main
if bundle.bundleURL.pathExtension == "appex" {
// Peel off two directory levels - MY_APP.app/PlugIns/MY_APP_EXTENSION.appex
let url = bundle.bundleURL.deletingLastPathComponent().deletingLastPathComponent()
if let otherBundle = Bundle(url: url) {
return otherBundle
}
}
return bundle
}()
/// Get an lproj language bundle from the main app bundle.
/// - Parameter language: The language to try to load.
/// - Returns: The lproj bundle if found otherwise nil.
@objc static func lprojBundle(for language: String) -> Bundle? {
guard let lprojURL = Bundle.app.url(forResource: language, withExtension: "lproj") else { return nil }
return Bundle(url: lprojURL)
}
/// Preferred app language for translations. Takes the highest priority in translations. The priority list for translations:
/// - `Bundle.elementLanguage`
/// - `Locale.preferredLanguages`
/// - `Bundle.elementFallbackLanguage`
static var elementLanguage: String? {
didSet {
preferredLanguages = calculatePreferredLanguages()
}
}
/// Preferred fallback language for translations. Only used for strings not translated neither to `elementLanguage` nor to one of the user's preferred languages.
static var elementFallbackLanguage: String? {
didSet {
preferredLanguages = calculatePreferredLanguages()
}
}
/// Preferred languages in the priority order.
static private(set) var preferredLanguages: [String] = calculatePreferredLanguages()
private static func calculatePreferredLanguages() -> [String] {
var set = Set<String>()
return ([Bundle.elementLanguage] +
Locale.preferredLanguages +
[Bundle.elementFallbackLanguage])
.compactMap { $0 }
.filter { set.insert($0).inserted }
}
}
@@ -0,0 +1,34 @@
//
// Copyright 2021 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import SwiftUI
extension String {
/// Returns the string as an `AttributedString` with the specified character tinted in a different color.
/// - Parameters:
/// - character: The character to be tinted.
/// - color: The color to tint the character. Defaults to the accent color.
/// - Returns: An `AttributedString`.
func tinting(_ character: Character, color: Color = .accentColor) -> AttributedString {
var string = AttributedString(self)
let characterView = string.characters
for index in characterView.indices where characterView[index] == character {
string[index..<characterView.index(after: index)].foregroundColor = color
}
return string
}
}