Skip to content

Commit

Permalink
Modified template
Browse files Browse the repository at this point in the history
  • Loading branch information
Geza Miklo committed Oct 20, 2022
1 parent e5bd8e2 commit 4c205d3
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,15 @@
//
// Created ___FULLUSERNAME___ on ___DATE___.
// Copyright © ___YEAR___ ___ORGANIZATIONNAME___. All rights reserved.
// Generated using MVVM Module Generator by Mohamad Kaakati
// https://github.com/Kaakati/MVVM-Template-Generator
// Generated using MVVM Module Generator
//

/// ___VARIABLE_productName:identifier___ Model
struct ___VARIABLE_productName:identifier___ {
typealias didFetchSuccess = (___VARIABLE_productName:identifier___)->()
typealias didFailWithError = (Error?) -> ()

// Your Model Structure
struct ___VARIABLE_productName:identifier___Model: Codable {
var id : Int?
var name : Int?

func didFetch(withSuccess: @escaping didFetchSuccess, withError: @escaping didFailWithError) {
withSuccess(___VARIABLE_productName:identifier___())
withError(nil)
enum CodingKeys: String, CodingKey {
case id, name
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
import UIKit

protocol ___VARIABLE_productName:identifier___UIDelegate {
func uiDidSelect(object: ___VARIABLE_productName:identifier___)
func didCommit(action: ___VARIABLE_productName:identifier___ViewModel.Action)
}

class ___VARIABLE_productName:identifier___UI : UIView {
var delegate: ___VARIABLE_productName:identifier___UIDelegate!

var object : ___VARIABLE_productName:identifier___?
var object : ___VARIABLE_productName:identifier___Model?
var cellIdentifier = "___VARIABLE_productName:identifier___CellId"

lazy var tableView : UITableView = {
Expand Down Expand Up @@ -77,8 +77,11 @@ extension ___VARIABLE_productName:identifier___UI: UITableViewDataSource {
}

extension ___VARIABLE_productName:identifier___UI: UITableViewDelegate {
func didCommit(action: ___VARIABLE_productName:identifier___ViewModel.Action) {
delegate?.didCommit(action: action)
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
delegate?.uiDidSelect(object: self.object!)
delegate?.didCommit(action: .didSelect(entity: ___VARIABLE_productName:identifier___Model))
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,50 @@
//

import Foundation
import RxSwift
import RxCocoa

protocol ___VARIABLE_productName:identifier___ViewModelProtocol {
func fetchData()
func didReceiveUISelect(object: ___VARIABLE_productName:identifier___)
}

class ___VARIABLE_productName:identifier___ViewModel {
var view : ___VARIABLE_productName:identifier___ViewProtocol!
var object = ___VARIABLE_productName:identifier___()
class ___VARIABLE_productName:identifier___ViewModel: APIRequestStateDelegate {
enum Action {
case didSelect(entity: ___VARIABLE_productName:identifier___Model)
}
let entity = BehaviorRelay<___VARIABLE_productName:identifier___Model?>(value: nil)
let action = PublishSubject<Action>()
let isLoading = BehaviorRelay(value: false)
let viewNeedsReload = PublishSubject<Bool>(value: true)

init() {
action
.asDriver()
.drive(onNext: { [weak self] action in
guard let strongSelf = self else {
return
}
switch action {
case .didSelect(let model):
strongSelf.modelSelected(model)
}
})
.disposed(by: disposeBag)
}

func fetchData() {
object.didFetch(withSuccess: { (success) in
self.view.viewWillPresent(data: success)
}) { (err) in
debugPrint("Error happened", err as Any)
}
isLoading.accept(true)
APIRequest<APIResponseDecodable<___VARIABLE_productName:identifier___>>(request: APIEndpoints.get___VARIABLE_productName:identifier___.request(), stateDelegate: self).rx.fetch()
.subscribe(onNext: { [weak self] response in
guard let strongSelf = self else {
return
}
strongSelf.entity.accept(response.value)
})
.disposed(by: disposeBag)
}

func entitySelected(_ entity: ___VARIABLE_productName:identifier___) {
debugPrint("Did receive UI object", entity)
}

func didReceiveUISelect(object: ___VARIABLE_productName:identifier___) {
debugPrint("Did receive UI object", object)
func requestStateChanged(_ request: any APIRequestProtocol, state: APIRequestState) {
isLoading.accept(state == .fetching)
}
}
}

0 comments on commit 4c205d3

Please sign in to comment.