-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Synchronous Feedback Loop (breaking change) (#51)
* Synchronous Feedback Loop. * Deprecate `Feedback.init(deriving:effects:)`. * Add a text input example. * Introduce a standalone `FeedbackLoop` type. Less state copes in the Property implementation. * Restore docs for `Feedback.custom`. * Rename file and address suggestions * Make init public as all our convenance extensions calling it * Add backwards compatibility mode via retaining sheduler * Add backwards compatibility support via introducing FeedbackLoop.Feedback * Fix Example app * Restore old system tests * Reducer to be `(inout State, Event) -> Void` Co-authored-by: sergdort <[email protected]>
- Loading branch information
Showing
10 changed files
with
758 additions
and
11 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 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,83 @@ | ||
import UIKit | ||
import ReactiveSwift | ||
import ReactiveCocoa | ||
import ReactiveFeedback | ||
|
||
|
||
class TextInputViewController: UIViewController { | ||
let viewModel = TextInputViewModel() | ||
let textView = UITextView() | ||
let inputToolbar = UIToolbar(frame: UIScreen.main.bounds) | ||
let characterCountLabel = UILabel() | ||
|
||
override var inputAccessoryView: UIView? { | ||
inputToolbar.frame.size = inputToolbar.sizeThatFits(UIScreen.main.bounds.size) | ||
return inputToolbar | ||
} | ||
|
||
override func loadView() { | ||
self.view = textView | ||
|
||
textView.font = UIFont.preferredFont(forTextStyle: .title3) | ||
textView.alwaysBounceVertical = true | ||
textView.keyboardDismissMode = .interactive | ||
|
||
if #available(iOS 11.0, *) { | ||
textView.contentInsetAdjustmentBehavior = .always | ||
} else { | ||
self.automaticallyAdjustsScrollViewInsets = true | ||
} | ||
} | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
viewModel.state.start() | ||
|
||
textView.reactive.continuousTextValues | ||
.take(duringLifetimeOf: self) | ||
.observeValues { [viewModel] in viewModel.textDidChange($0) } | ||
|
||
textView.reactive.text <~ viewModel.state.producer | ||
characterCountLabel.reactive.text <~ viewModel.state.producer | ||
.map { "\($0.count) characters" } | ||
inputToolbar.setItems([UIBarButtonItem(customView: characterCountLabel)], animated: false) | ||
} | ||
|
||
override func viewDidAppear(_ animated: Bool) { | ||
super.viewDidAppear(animated) | ||
|
||
textView.becomeFirstResponder() | ||
} | ||
} | ||
|
||
final class TextInputViewModel { | ||
let state: FeedbackLoop<String, Event> | ||
private let (text, textObserver) = Signal<String, Never>.pipe() | ||
|
||
init() { | ||
self.state = FeedbackLoop<String, Event>( | ||
initial: "Lorem ipsum ", | ||
reduce: TextInputViewModel.reduce, | ||
feedbacks: [.custom { [text] (state, consumer) in | ||
text.producer.map(Event.update).enqueue(to: consumer).start() | ||
}] | ||
) | ||
} | ||
|
||
func textDidChange(_ text: String) { | ||
textObserver.send(value: text) | ||
} | ||
} | ||
|
||
extension TextInputViewModel { | ||
static func reduce(state: inout String, event: Event) { | ||
switch event { | ||
case let .update(text): | ||
state = text | ||
} | ||
} | ||
|
||
enum Event { | ||
case update(String) | ||
} | ||
} |
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,19 @@ | ||
import Foundation | ||
|
||
public class FeedbackEventConsumer<Event> { | ||
struct Token: Equatable { | ||
let value: UUID | ||
|
||
init() { | ||
value = UUID() | ||
} | ||
} | ||
|
||
func process(_ event: Event, for token: Token) { | ||
fatalError("This is an abstract class. You must subclass this and provide your own implementation") | ||
} | ||
|
||
func dequeueAllEvents(for token: Token) { | ||
fatalError("This is an abstract class. You must subclass this and provide your own implementation") | ||
} | ||
} |
Oops, something went wrong.