Skip to content

Instantly share code, notes, and snippets.

View DhavalDobariya86's full-sized avatar

Dhaval Dobariya DhavalDobariya86

View GitHub Profile
@DhavalDobariya86
DhavalDobariya86 / Coordinator.swift
Created June 10, 2020 21:09
Coordinator protocol
protocol Coordinator: class {
var childCoordinators: [Coordinator]? { get set }
func start() -> UIViewController
var finished: (() -> Void)? { get set }
}
@DhavalDobariya86
DhavalDobariya86 / ViewModel.swift
Last active June 9, 2020 21:08
ViewModel protocol
import Foundation
/// This protocol defines basic responsibility for `ViewModel`
protocol ViewModel {
/// This method is called when `ViewModel` updates itself with `new` data
var didUpdate: (() -> Void)? { get set }
/// This method is called when `ViewModel` changes its state
/// - isLoading: Loading state of ViewModel
/// - loadingMessage: Loading messages to display while ViewModel is loading
@DhavalDobariya86
DhavalDobariya86 / CountryListCoordinatorTests.swift
Created June 7, 2020 18:43
MVVM-C Coordinator Unit Tests
import XCTest
@testable import MVVMCDemo
class CountryListCoordinatorTests: XCTestCase {
private let sut = CountryListCoordinator(requestManager: LocalRequestManager())
func testStart() {
XCTAssertTrue(sut.start() is CountryListViewController)
}
@DhavalDobariya86
DhavalDobariya86 / CountryListViewModelTests.swift
Last active June 9, 2020 21:11
MVVM-C ViewModel Unit Tests
import XCTest
@testable import MVVMCDemo
class CountryListViewModelTests: XCTestCase {
func testEndpoint() {
let requestManager = LocalRequestManager()
let sut = CountryListViewModel(requestManager: requestManager)
sut.searchFor(keyword: "test")
import Foundation
import UIKit
final class CountryListCoordinator: Coordinator {
private enum LocalizedStrings {
static let okTitle = "Ok"
static let errorTitle = "Error"
static let errorMessage = "Could not search country for the keyword."
}
import Foundation
import UIKit
typealias CountryListViewModelProtocol = CountryListDatasource & ViewModel & Searchable
protocol CountryListDatasource {
var screenTitle: String { get }
var numberOfSections: Int { get }
func numberOfRows(in section: Int) -> Int
func itemForRow(at indexPath: IndexPath) -> CountryListItemRepresentable
import Foundation
private struct CountryListEndpoint: EndpointReprsentable {
var httpMethod: HTTPMethod { .get }
var urlPath: URLPath { .name }
let pathComponent: String?
}
@DhavalDobariya86
DhavalDobariya86 / Country.swift
Last active June 7, 2020 18:53
MVVM-C Model
import Foundation
struct Country: Codable, Equatable, CountryListItemRepresentable, CountryDetailRepresentable {
let name: String
let capital: String
let region: String
let subregion: String
let timezones: [String]
let borders: [String]
@DhavalDobariya86
DhavalDobariya86 / Codable+NestedJSON.swift
Last active April 11, 2020 20:35
Codable to decode required properties only from deeply nested JSON
import UIKit
struct Employee: Decodable {
let name: String
let city: String
enum CodingKeys: String, CodingKey {
case name
case city
case contactInformation
@DhavalDobariya86
DhavalDobariya86 / Codable+OptionalFields.swift
Created April 11, 2020 20:27
Codable to handle optional properties from JSON
import UIKit
struct Employee: Codable {
let name: String
let phoneNumber: String?
}
let jsonDataWithPhoneNumber: Data = """
{
"name": "Eric",