-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(SwManager) Container to manage several views
(SWViewModel) to publish changes on a view
- Loading branch information
Showing
6 changed files
with
118 additions
and
75 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
This file was deleted.
Oops, something went wrong.
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,37 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by Ilya on 14.10.20. | ||
// | ||
|
||
import Combine | ||
import SwiftUI | ||
|
||
public class SWViewModel: ObservableObject { | ||
let id: UUID = UUID.init() | ||
@Published var state: ViewState { | ||
didSet { | ||
if state != .center { | ||
self.stateDidChange.send(self) | ||
} | ||
} | ||
} | ||
|
||
@Published var dragOffset: CGSize | ||
let stateDidChange = PassthroughSubject<SWViewModel, Never>() | ||
|
||
init(state: ViewState, size: CGSize) { | ||
self.state = state | ||
self.dragOffset = size | ||
} | ||
|
||
public func goToCenter(){ | ||
DispatchQueue.main.async { | ||
withAnimation { | ||
self.state = .center | ||
self.dragOffset = .zero | ||
} | ||
} | ||
} | ||
} |
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,40 @@ | ||
// | ||
// SwManager.swift | ||
// | ||
// | ||
// Created by Ilya on 14.10.20. | ||
// | ||
|
||
import Combine | ||
|
||
public class SwManager: ObservableObject { | ||
private var views: [SWViewModel] | ||
private var subscriptions = Set<AnyCancellable>() | ||
|
||
public init() { | ||
views = [] | ||
} | ||
|
||
public func hideAllViews() { | ||
self.views.forEach { | ||
$0.goToCenter() | ||
} | ||
} | ||
|
||
public func addView(_ view: SWViewModel) { | ||
views.append(view) | ||
|
||
view.stateDidChange.sink(receiveValue: { vm in | ||
if self.views.count != 0 { | ||
#if DEBUG | ||
print("swiped = \(vm.id.uuidString)") | ||
#endif | ||
self.views.forEach { | ||
if vm.id != $0.id && $0.state != .center{ | ||
$0.goToCenter() | ||
} | ||
} | ||
} | ||
}).store(in: &subscriptions) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.