Skip to content

Commit

Permalink
IMVVM foundational types
Browse files Browse the repository at this point in the history
  • Loading branch information
neakor committed Sep 16, 2022
1 parent 86e8d88 commit e795e9d
Show file tree
Hide file tree
Showing 13 changed files with 1,006 additions and 1 deletion.
31 changes: 31 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Forked from https://github.com/github/gitignore/blob/master/Swift.gitignore

# MacOS
*.DS_Store

## User settings
xcuserdata/

## Obj-C/Swift specific
*.hmap

## App packaging
*.ipa
*.dSYM.zip
*.dSYM

# Swift Package Manager
#
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
Packages/
.build/

# Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata
# hence it is not needed unless you have added a package configuration file to your project
.swiftpm

build/

# Xcode, since the project is generated using SPM.
*.xcodeproj
*.xcworkspace
14 changes: 14 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"pins" : [
{
"identity" : "swift-collections",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-collections.git",
"state" : {
"revision" : "48254824bb4248676bf7ce56014ff57b142b77eb",
"version" : "1.0.2"
}
}
],
"version" : 2
}
33 changes: 33 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// swift-tools-version: 5.6

import PackageDescription

let package = Package(
name: "IMVVM",
platforms: [
.iOS(.v13),
],
products: [
.library(
name: "IMVVM",
targets: ["IMVVM"]
),
],
dependencies: [
.package(url: "https://github.com/apple/swift-collections.git", from: "1.0.2"),
],
targets: [
.target(
name: "IMVVM",
dependencies: [
.product(name: "OrderedCollections", package: "swift-collections"),
]
),
.testTarget(
name: "IMVVMTests",
dependencies: [
"IMVVM",
]
),
]
)
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# imvvm
# iMVVMFoundation
The foundational library for base MVVM+Interactor and corresponding utility implementations.
91 changes: 91 additions & 0 deletions Sources/IMVVM/AnyCancellable+Interactor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// MIT License
//
// Copyright (c) 2022 Yi Wang
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import Combine

extension AnyCancellable {
/// When the interactor deinits, the subscription is cancelled.
///
/// This function provides the utility to manage Combine subscriptions inside a `Interactor` implementation. For
/// example:
///
/// class MyInteractor: Interactor {
/// func buttonDidTap() {
/// somePublisher
/// .sink { ... }
/// .cancelOnDeinit(of: self)
/// }
/// }
///
/// - Note: Because this function causes the given interactor to stongly retain the subscription, this means the
/// subscription itself should not strongly retain the interactor. Otherwise a retain cycle would occur causing
/// memory leaks.
///
/// This function is thread-safe. Invocations of this function to the same interactor instance can be performed on
/// the different threads.
///
/// This function can only be invoked after the given interactor has loaded. This is done via the interactor's
/// `viewDidAppear` function. Generally speaking, this interactor should be bound to the lifecycle of a `View`.
/// See `ViewLifecycleObserver` for more details.
///
/// - Parameters:
/// - interactor: The interactor to bind the subscription's lifecycle to.
public func cancelOnDeinit<InteractorType: Interactor>(of interactor: InteractorType) {
if !interactor.isLoaded {
fatalError("\(interactor) has not been loaded")
}
interactor.deinitCancelBag.store(self)
}

/// When the interactor's view disappears, the subscription is cancelled.
///
/// This function provides the utility to manage Combine subscriptions inside a `Interactor` implementation. For
/// example:
///
/// class MyInteractor: Interactor {
/// func buttonDidTap() {
/// somePublisher
/// .sink { ... }
/// .cancelOnViewDidDisappear(of: self)
/// }
/// }
///
/// - Note: Because this function causes the given interactor to stongly retain the subscription, this means the
/// subscription itself should not strongly retain the interactor. Otherwise a retain cycle would occur causing
/// memory leaks.
///
/// This function is thread-safe. Invocations of this function to the same interactor instance can be performed on
/// the different threads.
///
/// This function can only be invoked after the given interactor has received notification that its view has
/// appeared. This is done via the interactor's `viewDidAppear` function. Generally speaking, this interactor
/// should be bound to the lifecycle of a `View`. See `ViewLifecycleObserver` for more details.
///
/// - Parameters:
/// - interactor: The interactor to bind the subscription's lifecycle to.
public func cancelOnViewDidDisappear<InteractorType: Interactor>(of interactor: InteractorType) {
if !interactor.hasViewAppeared {
fatalError("\(interactor)'s view has not appeared")
}
interactor.viewAppearanceCancelBag.store(self)
}
}
68 changes: 68 additions & 0 deletions Sources/IMVVM/CancellableBuilder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// MIT License
//
// Copyright (c) 2022 Yi Wang
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import Combine

/// A result builder that allows a variadic number of `AnyCancellable` to be collected into an array.
@resultBuilder
public enum CancellableBuilder {
public static func buildBlock(_ components: [AnyCancellable]...) -> [AnyCancellable] {
components.reduce(into: [], +=)
}

public static func buildExpression(_ expression: Void) -> [AnyCancellable] {
[]
}

public static func buildExpression(_ expression: AnyCancellable) -> [AnyCancellable] {
[expression]
}

/// Convert regular cancellables to AnyCancellable to dispose on deinit.
public static func buildExpression(_ expression: any Cancellable) -> [AnyCancellable] {
[AnyCancellable(expression.cancel)]
}

public static func buildExpression(_ expression: [AnyCancellable]) -> [AnyCancellable] {
expression
}

public static func buildEither(first component: [AnyCancellable]) -> [AnyCancellable] {
component
}

public static func buildEither(second component: [AnyCancellable]) -> [AnyCancellable] {
component
}

public static func buildArray(_ components: [[AnyCancellable]]) -> [AnyCancellable] {
components.reduce(into: [], +=)
}

public static func buildOptional(_ component: [AnyCancellable]?) -> [AnyCancellable] {
if let component = component {
return component
} else {
return []
}
}
}
Loading

0 comments on commit e795e9d

Please sign in to comment.