Skip to content

Instantly share code, notes, and snippets.

View hmlongco's full-sized avatar

Michael Long hmlongco

View GitHub Profile
@hmlongco
hmlongco / EnumeratedStateViewModel.swift
Last active January 29, 2019 16:40
EnumeratedStateViewModel
class EnumeratedStateViewModel {
public let state: Observable<State>
enum State: Equatable {
case initial
case loading
case loaded(UIImage)
case empty(String)
case error(String)
@hmlongco
hmlongco / StructStateViewModel.swift
Last active January 29, 2019 16:39
StructStateViewModel
class StructStateViewModel {
public let state: Observable<State>
struct State {
var id: String?
var loading = false
var image: UIImage?
var empty: String?
var error: String?
@hmlongco
hmlongco / VariadicDisposeBag1.swift
Last active February 8, 2019 17:34
Variadic DisposeBag Example 1
class MVVMViewController: UIViewController {
@IBOutlet weak var firstNameLabel: UILabel!
@IBOutlet weak var lastNameLabel: UILabel!
@IBOutlet weak var addressLabel: UILabel!
@IBOutlet weak var cityLabel: UILabel!
@IBOutlet weak var stateLabel: UILabel!
@IBOutlet weak var zipLabel: UILabel!
private var viewModel = MyViewModel()
@hmlongco
hmlongco / VariadicDisposeBagExample2.swift
Created February 8, 2019 17:30
Variadic DisposeBag Example 2
class RxSwiftViewController: UIViewController {
@IBOutlet weak var firstNameLabel: UILabel!
@IBOutlet weak var lastNameLabel: UILabel!
@IBOutlet weak var addressLabel: UILabel!
@IBOutlet weak var cityLabel: UILabel!
@IBOutlet weak var stateLabel: UILabel!
@IBOutlet weak var zipLabel: UILabel!
private var viewModel = MyViewModel()
@hmlongco
hmlongco / CommodityPicker.dart
Created April 23, 2019 13:18
Flutter List Picker
import 'package:flutter/material.dart';
import 'package:test_app/models/commodities_model.dart';
class CommodityPicker extends StatelessWidget {
CommodityPicker(this.commodityMap, this.selected);
final Map<String, Commodity> commodityMap; // VM
final String selected; // VM
@override
Widget build(BuildContext context) {
@hmlongco
hmlongco / AsyncAPIs.swift
Last active April 30, 2019 14:55
Asynchronous API handling using GCD and Swift 5's new Result type
func load() {
DispatchQueue.global(qos: .utility).async {
let result = self.makeAPICall()
DispatchQueue.main.async {
switch result {
case let .success(data):
print(data)
case let .failure(error):
print(error)
@hmlongco
hmlongco / MultipleAsyncAPIs.swift
Last active April 30, 2019 12:54
FlatMapping Multiple Asynchronous API calls using Swift 5's new Result type
func simulateAnotherCall(_ param: String?) -> Result<String?, NWError> {
return makeAPICall()
}
func load() {
DispatchQueue.global(qos: .utility).async {
// make first api call and flatMap second and third api calls
let result = self.apiTest()
.flatMap { self.simulateAnotherCall($0) }
.flatMap { self.simulateAnotherCall($0) }
struct BasicNavigationView : View {
let users: [User] = User.users
var body: some View {
NavigationView {
List(users) { user in
NavigationButton(destination: BasicNavigationDetailView(user: user)) {
Text(user.name)
}
}
.navigationBarTitle(Text("Users"))
struct A11yModifier: ViewModifier {
///A label that is visible to UI tests AND VoiceOver
let label: String?
///An identifier that is visible to UI tests NOT VoicOver
let identifier: String?
///The required function that returns our View with the necessary accessibility modifiers
func body(content: Content) -> some View {
content
.conditionalModifier(label != nil) {
$0.accessibility(label: Text(label!))
struct MainMenuView: View {
@EnvironmentObject var menu: MenuService
var body: some View {
NavigationView {
List {
if menu.sections.isEmpty {
Text("Loading...")
.foregroundColor(.gray)