Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete example update from Natasha #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions MVVMProtocolExperiment/MinionModeViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,26 @@

import UIKit

struct MinionModeViewModel: SwitchWithTextCellDataSource {
var title = "Minion Mode!!!"
var switchOn = true
struct MinionModeViewModel: SwitchWithTextViewPresentable {

}

extension MinionModeViewModel {
var text: String { return "Minion mode" }
var textColor: UIColor { return .black }
var font: UIFont { return .systemFont(ofSize: 17)}
}

extension MinionModeViewModel: SwitchWithTextCellDelegate {
extension MinionModeViewModel {
var switchOn: Bool { return false}
var switchColor: UIColor { return .yellow }

func onSwitchTogleOn(_ on: Bool) {
func onSwitchToggleOn(_ on: Bool) {
if on {
print("The Minions are here to stay!")
} else {
print("The Minions went out to play!")
print("The minion is on")
}
else {
print("The minion is off")
}
}

var switchColor: UIColor {
return .yellow
}
}
17 changes: 9 additions & 8 deletions MVVMProtocolExperiment/SettingsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import UIKit

class SettingsViewController: UITableViewController {

enum Setting: Int {
case minionMode
// other settings here
Expand All @@ -18,17 +18,17 @@ class SettingsViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}

// MARK: - Table view data source

override func tableView(_ tableView: UITableView,
numberOfRowsInSection section: Int) -> Int
numberOfRowsInSection section: Int) -> Int
{
return 1
}

override func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath) -> UITableViewCell
cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
if let setting = Setting(rawValue: indexPath.row) {
switch setting {
Expand All @@ -37,12 +37,13 @@ class SettingsViewController: UITableViewController {

// this is where the magic happens!
let viewModel = MinionModeViewModel()
cell.configure(withDataSource: viewModel, delegate: viewModel)
cell.configure(presenter: viewModel)

return cell
}
}

return tableView.dequeueReusableCell(withIdentifier: "defaultCell", for: indexPath)
}

}
76 changes: 38 additions & 38 deletions MVVMProtocolExperiment/SwitchWithTextTableViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,74 +8,74 @@

import UIKit

protocol SwitchWithTextCellDataSource {
var title: String { get }
var switchOn: Bool { get }
protocol TextPresentable {
var text: String { get }
var textColor: UIColor { get }
var font: UIFont { get }
}

protocol SwitchWithTextCellDelegate {
func onSwitchTogleOn(_ on: Bool)

protocol SwitchPresentable {
var switchOn: Bool { get }
var switchColor: UIColor { get }
var textColor: UIColor { get }
var font: UIFont { get }
func onSwitchToggleOn(_ on: Bool)
}

extension SwitchWithTextCellDelegate {

extension SwitchPresentable {
var switchColor: UIColor {
return .purple
}

var textColor: UIColor {
return .black
return UIColor.yellow
}
}

protocol ImagePresentable {
var imageName: String { get }
}

protocol TextFieldPresentable {
var placeholder: String { get }
var text: String { get }

var font: UIFont {
return .systemFont(ofSize: 17)
}
func onTextFieldDidEndEditing(textField: UITextField)
}

class SwitchWithTextTableViewCell: UITableViewCell {
typealias SwitchWithTextViewPresentable = TextPresentable & SwitchPresentable

class SwitchWithTextTableViewCell: UITableViewCell {

@IBOutlet fileprivate weak var label: UILabel!
@IBOutlet fileprivate weak var switchToggle: UISwitch!

fileprivate var dataSource: SwitchWithTextCellDataSource?
fileprivate var delegate: SwitchWithTextCellDelegate?

fileprivate var delegate: SwitchWithTextViewPresentable?

override func awakeFromNib() {
super.awakeFromNib()
}

func configure(withDataSource dataSource: SwitchWithTextCellDataSource, delegate: SwitchWithTextCellDelegate?) {
self.dataSource = dataSource
self.delegate = delegate
func configure(presenter: SwitchWithTextViewPresentable) {
delegate = presenter

label.text = dataSource.title
switchToggle.isOn = dataSource.switchOn
// color option added!
switchToggle.onTintColor = delegate?.switchColor
//configure UI component
label.text = presenter.text
switchToggle.isOn = presenter.switchOn
}

@IBAction func onSwitchToggle(_ sender: UISwitch) {
delegate?.onSwitchTogleOn(sender.isOn)
delegate?.onSwitchToggleOn(sender.isOn)
}
}

// BEFORE
// BEFORE
//class SwitchWithTextTableViewCell: UITableViewCell {
//
//
// @IBOutlet private weak var label: UILabel!
// @IBOutlet private weak var switchToggle: UISwitch!
//
//
// typealias onSwitchToggleHandlerType = (switchOn: Bool) -> Void
// private var onSwitchToggleHandler: onSwitchToggleHandlerType?
//
//
// override func awakeFromNib() {
// super.awakeFromNib()
// }
//
//
// func configure(withTitle title: String,
// switchOn: Bool,
// switchColor: UIColor = .purpleColor(),
Expand All @@ -85,10 +85,10 @@ class SwitchWithTextTableViewCell: UITableViewCell {
// switchToggle.on = switchOn
// // color option added!
// switchToggle.onTintColor = switchColor
//
//
// self.onSwitchToggleHandler = onSwitchToggleHandler
// }
//
//
// @IBAction func onSwitchToggle(sender: UISwitch) {
// onSwitchToggleHandler?(switchOn: sender.on)
// }
Expand Down