-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from Infomaniak/ikMailShareExtension
Moved core code form ikMail share extension.
- Loading branch information
Showing
7 changed files
with
301 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
Sources/InfomaniakCore/Asynchronous/SendableDictionary.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
Infomaniak Core - iOS | ||
Copyright (C) 2023 Infomaniak Network SA | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import Foundation | ||
|
||
/// A thread safe Dictionary wrapper that does not require `await`. Conforms to Sendable. | ||
/// | ||
/// Useful when dealing with UI. | ||
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) | ||
public final class SendableDictionary<T: Hashable, U>: @unchecked Sendable { | ||
let lock = DispatchQueue(label: "com.infomaniak.core.SendableDictionary.lock") | ||
private(set) var content = [T: U]() | ||
|
||
public init() { | ||
// META: keep SonarCloud happy | ||
} | ||
|
||
public var values: Dictionary<T, U>.Values { | ||
var buffer: Dictionary<T, U>.Values! | ||
lock.sync { | ||
buffer = content.values | ||
} | ||
return buffer | ||
} | ||
|
||
public func value(for key: T) -> U? { | ||
var buffer: U? | ||
lock.sync { | ||
buffer = content[key] | ||
} | ||
return buffer | ||
} | ||
|
||
public func setValue(_ value: U?, for key: T) { | ||
lock.sync { | ||
content[key] = value | ||
} | ||
} | ||
|
||
@discardableResult | ||
public func removeValue(forKey key: T) -> U? { | ||
var buffer: U? | ||
lock.sync { | ||
buffer = content.removeValue(forKey: key) | ||
} | ||
return buffer | ||
} | ||
|
||
/// Bracket get / set pattern | ||
public subscript(_ key: T) -> U? { | ||
get { | ||
value(for: key) | ||
} | ||
set { | ||
setValue(newValue, for: key) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
Infomaniak kDrive - iOS App | ||
Copyright (C) 2023 Infomaniak Network SA | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import Foundation | ||
|
||
/// Represents `any` (ie. all of them not the type) curried closure, of arbitrary type. | ||
public typealias CurriedClosure<Input, Output> = (Input) -> Output | ||
|
||
/// A closure that take no argument and return nothing, but technically curried. | ||
/// | ||
/// Calling this closure type requires the use of `Void` which is, in swift, an empty tuple `()`. | ||
public typealias SimpleClosure = CurriedClosure<Void, Void> | ||
|
||
/// Append a SimpleClosure to another one | ||
/// | ||
/// This allows you to append / prepend a closure to another one with the + operator. | ||
/// This is not function composition. | ||
public func + (_ lhs: @escaping SimpleClosure, _ rhs: @escaping SimpleClosure) -> SimpleClosure { | ||
let closure: SimpleClosure = { _ in | ||
lhs(()) | ||
rhs(()) | ||
} | ||
return closure | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
Infomaniak Core - iOS | ||
Copyright (C) 2023 Infomaniak Network SA | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import InfomaniakCore | ||
import XCTest | ||
|
||
public final class CountedFulfillmentTestExpectation: XCTestExpectation { | ||
private(set) var currentFulfillmentCount = 0 | ||
|
||
override public func fulfill() { | ||
currentFulfillmentCount += 1 | ||
super.fulfill() | ||
} | ||
} | ||
|
||
final class UTCurriedClosure: XCTestCase { | ||
func testAppendToClosure() { | ||
// GIVEN | ||
let expectationA = CountedFulfillmentTestExpectation(description: "Closure is called") | ||
let expectationB = CountedFulfillmentTestExpectation(description: "Closure is called") | ||
let expectations = [expectationA, expectationB] | ||
|
||
let a: SimpleClosure = { _ in | ||
XCTAssertEqual(expectationA.currentFulfillmentCount, 0, "expectationA should not be fulfilled") | ||
XCTAssertEqual(expectationB.currentFulfillmentCount, 0, "expectationB should not be fulfilled") | ||
expectationA.fulfill() | ||
} | ||
|
||
let b: SimpleClosure = { _ in | ||
XCTAssertEqual(expectationA.currentFulfillmentCount, 1, "expectationA should be fulfilled") | ||
XCTAssertEqual(expectationB.currentFulfillmentCount, 0, "expectationB should not be fulfilled") | ||
expectationB.fulfill() | ||
} | ||
|
||
// WHEN | ||
let computation = a + b | ||
computation(()) | ||
|
||
// THEN | ||
wait(for: expectations, timeout: 10.0) | ||
} | ||
|
||
func testAppendToClosure_3chain() { | ||
// GIVEN | ||
let expectationA = CountedFulfillmentTestExpectation(description: "Closure is called") | ||
let expectationB = CountedFulfillmentTestExpectation(description: "Closure is called") | ||
let expectationC = CountedFulfillmentTestExpectation(description: "Closure is called") | ||
let expectations = [expectationA, expectationB, expectationC] | ||
|
||
let a: SimpleClosure = { _ in | ||
XCTAssertEqual(expectationA.currentFulfillmentCount, 0, "expectationA should not be fulfilled") | ||
XCTAssertEqual(expectationB.currentFulfillmentCount, 0, "expectationB should not be fulfilled") | ||
XCTAssertEqual(expectationC.currentFulfillmentCount, 0, "expectationC should not be fulfilled") | ||
expectationA.fulfill() | ||
} | ||
|
||
let b: SimpleClosure = { _ in | ||
XCTAssertEqual(expectationA.currentFulfillmentCount, 1, "expectationA should be fulfilled") | ||
XCTAssertEqual(expectationB.currentFulfillmentCount, 0, "expectationB should not be fulfilled") | ||
XCTAssertEqual(expectationC.currentFulfillmentCount, 0, "expectationC should not be fulfilled") | ||
expectationB.fulfill() | ||
} | ||
|
||
let c: SimpleClosure = { _ in | ||
XCTAssertEqual(expectationA.currentFulfillmentCount, 1, "expectation should be fulfilled") | ||
XCTAssertEqual(expectationB.currentFulfillmentCount, 1, "expectation should be fulfilled") | ||
XCTAssertEqual(expectationC.currentFulfillmentCount, 0, "expectationC should not be fulfilled") | ||
expectationC.fulfill() | ||
} | ||
|
||
// WHEN | ||
let computation = a + b + c | ||
computation(()) | ||
|
||
// THEN | ||
wait(for: expectations, timeout: 10.0) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters