How to reload data and view using rxswift and mvvm pattern - swift

This time, I am implementing the screen with RxSwift / MVVM.
It's too difficult to implement RxSwift as an MVVM.
What I wanted to ask you was to enter the list screen and get the data.
Then it went into the detail screen and changed specific data.
And if it go to the list screen, it have to update the data.
I think I can put data in the viewwillappear() of the view controller, and I do not know how to implement the renewal in the view Model, and I do not know if it is right to do this in functional programming like rx.
I defined the viewmodel as follows.
The store.getListEventWinning() method is a function that fits data and is delivered in the form of Observable.
Binding was done in view controller as below.

You don't give a lot of detail, despite the code you posted. I will address your specific comment, "What I wanted to ask you was to enter the list screen and get the data. Then it went into the detail screen and changed specific data. And if it go to the list screen, it have to update the data."
Using my CLE library (which you can install with cocoapods or SPM) doing this is quite simple.
let change = changeButton.rx.tap
.flatMapFirst(presentScene(animated: true) {
DetailViewController.scene { $0.connect() }
})
Here is a complete example that you can run yourself to see how it works. To run the below, just add XIB files for the two view controllers, and hook up the outlets.
import Cause_Logic_Effect
import RxCocoa
import RxSwift
final class MainViewController: UIViewController {
#IBOutlet var addButton: UIButton!
#IBOutlet var tableView: UITableView!
let disposeBag = DisposeBag()
}
final class DetailViewController: UIViewController {
#IBOutlet var saveButton: UIButton!
#IBOutlet var nameField: UITextField!
let disposeBag = DisposeBag()
}
extension MainViewController {
func connect() {
let initial = Observable<[String]>.just([]) // This could be a network request
let addName = addButton.rx.tap
.flatMapFirst(presentScene(animated: true) {
DetailViewController().scene { $0.connect() }
})
let state = mainVieweModel(initial: initial, addName: addName)
.share(replay: 1)
state
.bind(to: tableView.rx.items(cellIdentifier: "Cell", cellType: UITableViewCell.self)) { _, name, cell in
cell.textLabel?.text = name
}
.disposed(by: disposeBag)
}
}
func mainVieweModel(initial: Observable<[String]>, addName: Observable<String>) -> Observable<[String]> {
enum Input {
case initial([String])
case add(String)
}
return Observable.merge(
initial.map { Input.initial($0) },
addName.map { Input.add($0) }
)
.scan(into: [String]()) { state, input in
switch input {
case let .initial(value):
state = value
case let .add(text):
state.append(text)
}
}
}
extension DetailViewController {
func connect() -> Observable<String> {
return saveButton.rx.tap
.withLatestFrom(nameField.rx.text.orEmpty)
.take(1)
}
}
The app delegate looks like this:
import Cause_Logic_Effect
import UIKit
#main
final class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = {
let result = UIWindow(frame: UIScreen.main.bounds)
result.rootViewController = MainViewController().configure { $0.connect() }
result.makeKeyAndVisible()
return result
}()
return true
}
}

Related

Using UIApplicationDelegateAdaptor to get callbacks from userDidAcceptCloudKitShareWith not working

I'm trying to get notified when userDidAcceptCloudKitShareWith gets called. Traditionally this was called in the App Delegate but since I am building an iOS 14+ using App as my root object. I couldn't find any documentation out yet as far as how to add userDidAcceptCloudKitShareWith to my App class, so I am using UIApplicationDelegateAdaptor to use an App Delegate class, however it doesn't seem like userDidAcceptCloudKitShareWith is ever getting called?
import SwiftUI
import CloudKit
// Our observable object class
class ShareDataStore: ObservableObject {
static let shared = ShareDataStore()
#Published var didRecieveShare = false
#Published var shareInfo = ""
}
#main
struct SocialTestAppApp: App {
#StateObject var shareDataStore = ShareDataStore.shared
#UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView().environmentObject(shareDataStore)
}
}
}
class AppDelegate: NSObject, UIApplicationDelegate {
let container = CKContainer(identifier: "iCloud.com.TestApp")
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
print("did finish launching called")
return true
}
func application(_ application: UIApplication, userDidAcceptCloudKitShareWith cloudKitShareMetadata: CKShare.Metadata) {
print("delegate callback called!! ")
acceptShare(metadata: cloudKitShareMetadata) { result in
switch result {
case .success(let recordID):
print("successful share!")
ShareDataStore.shared.didRecieveShare = true
ShareDataStore.shared.shareInfo = recordID.recordName
case .failure(let error):
print("failure in share = \(error)")
}
} }
func acceptShare(metadata: CKShare.Metadata,
completion: #escaping (Result<CKRecord.ID, Error>) -> Void) {
// Create a reference to the share's container so the operation
// executes in the correct context.
let container = CKContainer(identifier: metadata.containerIdentifier)
// Create the operation using the metadata the caller provides.
let operation = CKAcceptSharesOperation(shareMetadatas: [metadata])
var rootRecordID: CKRecord.ID!
// If CloudKit accepts the share, cache the root record's ID.
// The completion closure handles any errors.
operation.perShareCompletionBlock = { metadata, share, error in
if let _ = share, error == nil {
rootRecordID = metadata.rootRecordID
}
}
// If the operation fails, return the error to the caller.
// Otherwise, return the record ID of the share's root record.
operation.acceptSharesCompletionBlock = { error in
if let error = error {
completion(.failure(error))
} else {
completion(.success(rootRecordID))
}
}
// Set an appropriate QoS and add the operation to the
// container's queue to execute it.
operation.qualityOfService = .utility
container.add(operation)
}
}
Updated based on Asperi's Answer:
import SwiftUI
import CloudKit
class ShareDataStore: ObservableObject {
static let shared = ShareDataStore()
#Published var didRecieveShare = false
#Published var shareInfo = ""
}
#main
struct athlyticSocialTestAppApp: App {
#StateObject var shareDataStore = ShareDataStore.shared
#UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
let sceneDelegate = MySceneDelegate()
var body: some Scene {
WindowGroup {
ContentView().environmentObject(shareDataStore)
.withHostingWindow { window in
sceneDelegate.originalDelegate = window.windowScene.delegate
window.windowScene.delegate = sceneDelegate
}
}
}
}
class MySceneDelegate: NSObject, UIWindowSceneDelegate {
let container = CKContainer(identifier: "iCloud.com...")
var originalDelegate: UIWindowSceneDelegate?
var window: UIWindow?
func sceneWillEnterForeground(_ scene: UIScene) {
print("scene is active")
}
func sceneWillResignActive(_ scene: UIScene) {
print("scene will resign active")
}
// forward all other UIWindowSceneDelegate/UISceneDelegate callbacks to original, like
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
originalDelegate?.scene!(scene, willConnectTo: session, options: connectionOptions)
}
func windowScene(_ windowScene: UIWindowScene, userDidAcceptCloudKitShareWith cloudKitShareMetadata: CKShare.Metadata) {
print("delegate callback called!! ")
acceptShare(metadata: cloudKitShareMetadata) { result in
switch result {
case .success(let recordID):
print("successful share!")
ShareDataStore.shared.didRecieveShare = true
ShareDataStore.shared.shareInfo = recordID.recordName
case .failure(let error):
print("failure in share = \(error)")
}
}
}
}
extension View {
func withHostingWindow(_ callback: #escaping (UIWindow?) -> Void) -> some View {
self.background(HostingWindowFinder(callback: callback))
}
}
struct HostingWindowFinder: UIViewRepresentable {
var callback: (UIWindow?) -> ()
func makeUIView(context: Context) -> UIView {
let view = UIView()
DispatchQueue.main.async { [weak view] in
self.callback(view?.window)
}
return view
}
func updateUIView(_ uiView: UIView, context: Context) {
}
}
In Scene-based application the userDidAcceptCloudKitShareWith callback is posted to Scene delegate, but in SwiftUI 2.0 App-based application the scene delegate is used by SwiftUI itself to provide scenePhase events, but does not provide native way to handle topic callback.
The possible approach to solve this is to find a window and inject own scene delegate wrapper, which will handle userDidAcceptCloudKitShareWith and forward others to original SwiftUI delegate (to keep standard SwiftUI events working).
Here is a couple of demo snapshots based on https://stackoverflow.com/a/63276688/12299030 window access (however you can use any other preferable way to get window)
#main
struct athlyticSocialTestAppApp: App {
#StateObject var shareDataStore = ShareDataStore.shared
#UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
let sceneDelegate = MySceneDelegate()
var body: some Scene {
WindowGroup {
ContentView().environmentObject(shareDataStore)
.withHostingWindow { window in
sceneDelegate.originalDelegate = window?.windowScene.delegate
window?.windowScene.delegate = sceneDelegate
}
}
}
}
class MySceneDelegate : NSObject, UIWindowSceneDelegate {
var originalDelegate: UISceneDelegate?
func windowScene(_ windowScene: UIWindowScene, userDidAcceptCloudKitShareWith cloudKitShareMetadata: CKShareMetadata) {
// your code here
}
// forward all other UIWindowSceneDelegate/UISceneDelegate callbacks to original, like
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
originalDelegate?.scene(scene, willConnectTo: session, options: connectionOptions)
}
}
Check out this question that has a lot of useful things to check across several possible answers:
CloudKit CKShare userDidAcceptCloudKitShareWith Never Fires on Mac App
Be sure to add the CKSharingSupported key to your info.plist, and then try putting the userDidAcceptCloudKitShareWith in different places using the answers in the above link (where you put it will depend on what kind of app you're building).

VIPER architecture using Swift to store data in presenter

So I'm setting up a simple VIPER architecture in Swift.
The Interactor gets some data from an API, and passes the data to the presenter that then passes formatted data to the view.
The presenter will process the data, and just count the number of objects that are downloaded. To do so I have stored a var in the presenter. The question is should I store data in the presenter?
Interactor:
class Interactor {
weak var presenter: Presenter?
func getData() {
ClosureDataManager.shared.fetchBreaches(withURLString: baseUrl + breachesExtensionURL, completion: { [weak self] result in
guard let self = self else { return }
switch result {
case .failure(let error):
print(error)
case .success(let breaches):
self.presenter?.dataDidFetch(breaches: breaches)
self.presenter?.dataNumberDidFetch(number: breaches.count)
}
})
}
}
Presenter:
class Presenter {
var wireframe: Wireframe?
var view: ViewController?
var interactor: Interactor?
var dataDownloaded = 0
func viewDidLoad() {
print ("presenter vdl")
}
func loadData() {
interactor?.getData()
}
func dataDidFetch(breaches: [BreachModel]) {
view?.dataReady()
}
func showDetail(with text: String, from view: UIViewController) {
wireframe?.pushToDetail(with: text, from: view)
}
func dataNumberDidFetch(number: Int) {
dataDownloaded += number
view?.showData(number: String(dataDownloaded) )
}
}
View (ViewController)
protocol dataViewProtocol {
func showData(number: String)
}
class ViewController: UIViewController, dataViewProtocol {
#IBOutlet weak var showDetailButton: UIButton!
#IBOutlet weak var dataLabel: UILabel!
// weak here means it won't work
var presenter: Presenter?
#IBAction func buttonPressAction(_ sender: UIButton) {
presenter?.loadData()
}
#IBAction func buttonShowDetailAction(_ sender: UIButton) {
presenter?.showDetail(with: "AAA", from: self)
}
func dataReady() {
showDetailButton.isEnabled = true
}
func showData(number: String) {
dataLabel.text = number
}
override func viewDidLoad() {
super.viewDidLoad()
Wireframe.createViewModule(view: self)
presenter?.viewDidLoad()
}
}
Router (Wireframe)
class Wireframe {
static func createViewModule (view: ViewController) {
let presenterInst = Presenter()
view.presenter = presenterInst
view.presenter?.wireframe = Wireframe()
view.presenter?.view = view
view.presenter?.interactor = Interactor()
view.presenter?.interactor?.presenter = presenterInst
}
}
So should the presenter be used to store the number of objects downloaded?
What have you tried I've implemented the var, as shown above. This is a minimum example of the problem.
What resources have you used I've looked on StackOverflow, and Googled the issue. I can't find an answer, but know I could store the data in the view but I think this is incorrect. I could store the number of data in the Interactor, but this also doesn't seem right. It all seems...to violate separation of concerns...
I won't do your homework / use a different architecture / You should use protocols / Why is there a single protocol in your implementation This isn't homework, it is for my own self - study. There may be other architectures that can be used to do this (and coding to protocols is good practice) but this is about storing a variable in the presenter. I want to know if I should store the variable in the presenter, using VIPER and using Swift. Comments about trivia around the question are seldom helpful if they are about variable names, or the like.
What is the question? I want to know if I can store the number of downloaded data items in the presenter.

RxSwift Observe changes on model and Make request

I'm trying to learn RxSwift concept and got stuck somewhere unfortunately. There is two different screen connected to my TabBarController. On my SettingsViewController, I'm getting two string values and creating a model, On TransactionListViewController, I need to observe changes on and make a new request to fill list.
On parent tab bar controller, I have a Variable and when didLoadCall I'm subscribing this model with wallet.asObservable().subscribe
On SettingViewController when user presses the login button I'm trying to change UserModel with this code:
if let tabBar = parent?.parent as? TransactionTabBarController{
Observable.just(wallet).bind(to: tabBar.wallet)
}
I realized that onNext function for wallet.asObservable().subscribe is calling.
There is also another wallet model on my TransactionListViewController,
on viewDidLoad function I'm running this code:
wallet.asObservable().subscribe(onNext: { (wallet) in
APIClient.getTransaction(address: wallet.walletAddress)
.observeOn(MainScheduler.instance)
.subscribe(onNext: { (model) in
self.changeModels(items: model.result)
.bind(to: self.transactionTableView.rx.items(dataSource: self.dataSource))
.disposed(by: self.disposeBag)
})
.disposed(by: self.disposeBag)}, onError: nil, onCompleted: nil, onDisposed: nil)
.disposed(by: disposeBag)
I tried to set wallet on TabBar's onNext function and I got crush couple of times on TransactionListViewController.
Can anyone help me with that?
Sadly, your code sample is inscrutable. However, it seems as though you are asking how to transmit data between two view controllers that are connected through a tab bar view controller. Below is one way you could go about doing it...
In order to use this code, you only need to assign a function to TabBarController.logic which takes a TabBarController.Inputs as an input parameter and returns a TabBarController.Outputs. You could make this assignment in the AppDelegate.
The key thing to note in this code is that every ViewController subclass has a struct Inputs, a struct Outputs and a var logic in it.
The Inputs has all the UI elements that a user can input to (e.g., Buttons and TextFields,) and the Outputs has all the UI elements that the user can see (e.g., Label text, isHidden flags.)
The logic var is a closure that contains all the logic for that view controller. Note that it can be assigned to. That means that you can develop and test the logic independently of the view controller and you can provide a view controller with a different logic object if necessary depending on context.
For somewhat more complex example code that uses a Coordinator instead of embedding code in the container view controller, see this repo: https://github.com/danielt1263/RxEarthquake
class TabBarController: UITabBarController {
struct Inputs {
let login: Observable<Void>
}
struct Outputs {
let transactions: Observable<[Transaction]>
}
var logic: (Inputs) -> Outputs = { _ in fatalError("Forgot to set logic.") }
private let bag = DisposeBag()
override func viewDidLoad() {
super.viewDidLoad()
let settings = children[0] as! SettingsViewController
let transactionList = children[1] as! TransactionListViewController
let login = PublishSubject<Void>()
let outputs = logic(Inputs(login: login.asObservable()))
let bag = self.bag
settings.logic = { inputs in
inputs.login
.bind(to: login)
.disposed(by: bag)
return SettingsViewController.Outputs()
}
transactionList.logic = { inputs in
return TransactionListViewController.Outputs(transactions: outputs.transactions)
}
}
}
class SettingsViewController: UIViewController {
struct Inputs {
let login: Observable<Void>
}
struct Outputs {
}
var logic: (Inputs) -> Outputs = { _ in fatalError("Forgot to set logic.") }
private let bag = DisposeBag()
#IBOutlet weak var login: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
_ = logic(Inputs(login: login.rx.tap.asObservable()))
}
}
class TransactionListViewController: UIViewController {
struct Inputs {
}
struct Outputs {
let transactions: Observable<[Transaction]>
}
var logic: (Inputs) -> Outputs = { _ in fatalError("Forgot to set logic.") }
private let bag = DisposeBag()
#IBOutlet weak var transactionTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
let output = logic(Inputs())
let dataSource = MyDataSource()
output.transactions
.bind(to: transactionTableView.rx.items(dataSource: dataSource))
.disposed(by: bag)
}
}

How to update variable in MVVM?

I am trying to use MVVM. I am going to VC2 from VC1. I am updating the viewModel.fromVC = 1, but the value is not updating in the VC2.
Here is what I mean:
There is a viewModel, in it there is a var fromVC = Int(). Now, in vc1, I am calling the viewModel as
let viewModel = viewModel().
Now, on the tap of button, I am updating the viewModel.fromVC = 8. And, moving to the next screen. In the next screen, when I print fromVC then I get the value as 0 instead of 8.
This is how the VC2 looks like
class VC2 {
let viewModel = viewModel()
func abc() {
print(viewModel.fromVC)
}
}
Now, I am calling abc() in viewDidLoad and the fromVC is printed as 0 instead of 8. Any help?
For the MVVM pattern you need to understand that it's a layer split in 2 different parts: Inputs & Outputs.
Int terms of inputs, your viewModel needs to catch every event from the viewController, and for the Outputs, this is the way were the viewModel will send data (correctly formatted) to the viewController.
So basically, if we have a viewController like this:
final class HomeViewController: UIViewController {
// MARK: - Outlets
#IBOutlet private weak var titleLabel: UILabel!
// MARK: - View life cycle
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: - Actions
#IBAction func buttonTouchUp(_ sender: Any) {
titleLabel.text = "toto"
}
}
We need to extract the responsibilities to a viewModel, since the viewController is handling the touchUp event, and owning the data to bring to th label.
By Extracting this, you will keep the responsibility correctly decided and after all, you'll be able to test your viewModel correctly 🙌
So how to do it? Easy, let's take a look to our futur viewModel:
final class HomeViewModel {
// MARK: - Private properties
private let title: String
// MARK: - Initializer
init(title: String) {
self.title = title
}
// MARK: - Outputs
var titleText: ((String) -> Void)?
// MARK: - Inputs
func viewDidLoad() {
titleText?("")
}
func buttonDidPress() {
titleText?(title)
}
}
So now, by doing this, you are keeping safe the different responsibilities, let's see how to bind our viewModel to our previous viewController :
final class HomeViewController: UIViewController {
// MARK: - public var
var viewModel: HomeViewModel!
// MARK: - Outlets
#IBOutlet private weak var titleLabel: UILabel!
// MARK: - View life cycle
override func viewDidLoad() {
super.viewDidLoad()
bind(to: viewModel)
viewModel.viewDidLoad()
}
// MARK: - Private func
private func bind(to viewModel: HomeViewModel) {
viewModel.titleText = { [weak self] title in
self?.titleLabel.text = title
}
}
// MARK: - Actions
#IBAction func buttonTouchUp(_ sender: Any) {
viewModel.buttonDidPress()
}
}
So one thing is missing, you'll asking me "but how to initialise our viewModel inside the viewController?"
Basically you should once again extract responsibilities, you could have a Screens layer which would have the responsibility to create the view like this:
final class Screens {
// MARK: - Properties
private let storyboard = UIStoryboard(name: StoryboardName, bundle: Bundle(for: Screens.self))
// MARK: - Home View Controller
func createHomeViewController(with title: String) -> HomeViewController {
let viewModel = HomeViewModel(title: title)
let viewController = storyboard.instantiateViewController(withIdentifier: "Home") as! HomeViewController
viewController.viewModel = viewModel
return viewController
}
}
And finally do something like this:
let screens = Screens()
let homeViewController = screens.createHomeViewController(with: "Toto")
But the main subject was to bring the possibility to test it correctly, so how to do it? very easy!
import XCTest
#testable import mvvmApp
final class HomeViewModelTests: XCTestCase {
func testGivenAHomeViewModel_WhenViewDidLoad_titleLabelTextIsEmpty() {
let viewModel = HomeViewModel(title: "toto")
let expectation = self.expectation("Returned title")
viewModel.titleText = { title in
XCTAssertEqual(title, "")
expectation.fulfill()
}
viewModel.viewDidLoad()
waitForExpectations(timeout: 1.0, handler: nil)
}
func testGivenAHomeViewModel_WhenButtonDidPress_titleLabelTextIsCorrectlyReturned() {
let viewModel = HomeViewModel(title: "toto")
let expectation = self.expectation("Returned title")
var counter = 0
viewModel.titleText = { title in
if counter == 1 {
XCTAssertEqual(title, "toto")
expectation.fulfill()
}
counter += 1
}
viewModel.viewDidLoad()
viewModel.buttonDidPress()
waitForExpectations(timeout: 1.0, handler: nil)
}
}
And that's it 💪

swift share information between two classes

I have a tabbed view app that I am working on right now I am wondering if there's any way I can share the information from my FirstViewController.swift to my SecondViewController.swift? Since I know swift doesnt support multiple class inheritance, is there a way that I can use the variables and informations I have on my FirstViewController in my SecondViewController?
You'll need a model outside the context of both your view controllers to store data, and then a way to populate those view controllers with that data and provide a reference to the model to make changes from the view controllers. You could this by creating a new model object that conforms to UITabBarColtronnerDelegate, which will allow you access to a view controller after it is selected. Keep things decoupled by abstracting these relationships into protocols, which then allows your implementations to vary independently as your app scales up.
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
let applicationModel = ApplicationModel()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if let tabBarController = window?.rootViewController as? UITabBarController {
tabBarController.delegate = applicationModel
}
return true
}
}
protocol TabbedViewControllerDelegate {
var message: String { set get }
}
class ApplicationModel: NSObject, UITabBarControllerDelegate, TabbedViewControllerDelegate {
var message = "Hello World!"
func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
if var tabbedViewController = viewController as? TabbedViewController {
tabbedViewController.message = message
tabBarController.delegate = self
}
}
}
protocol TabbedViewController {
var message: String { set get }
var delegate: TabbedViewControllerDelegate? { get set }
}
class FirstViewController: UIViewController, TabbedViewController {
var delegate: TabbedViewControllerDelegate?
var message: String = "" {
didSet {
println( "FirstViewController populated with message: \(message)" )
}
}
#IBAction func buttonPressed( sender: AnyObject? ) {
self.delegate?.message = "Updated message from FirstViewController"
}
}
class SecondViewController: UIViewController, TabbedViewController {
var delegate: TabbedViewControllerDelegate?
var message: String = "" {
didSet {
println( "SecondViewController populated with message: \(message)" )
}
}
#IBAction func buttonPressed( sender: AnyObject? ) {
self.delegate?.message = "Updated message from SecondViewController"
}
}