Skip to content

Instantly share code, notes, and snippets.

Avatar

Michael Long hmlongco

View GitHub Profile
View SwiftUI-Basic-Navigation.swift
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"))
@hmlongco
hmlongco / MultipleAsyncAPIs.swift
Last active Apr 30, 2019
FlatMapping Multiple Asynchronous API calls using Swift 5's new Result type
View MultipleAsyncAPIs.swift
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) }
@hmlongco
hmlongco / AsyncAPIs.swift
Last active Apr 30, 2019
Asynchronous API handling using GCD and Swift 5's new Result type
View AsyncAPIs.swift
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)
View CommodityPicker.dart
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) {
View VariadicDisposeBagExample2.swift
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 / VariadicDisposeBag1.swift
Last active Feb 8, 2019
Variadic DisposeBag Example 1
View VariadicDisposeBag1.swift
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()
View StructStateViewModel.swift
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 / EnumeratedStateViewModel.swift
Last active Jan 29, 2019
EnumeratedStateViewModel
View EnumeratedStateViewModel.swift
class EnumeratedStateViewModel {
public let state: Observable<State>
enum State: Equatable {
case initial
case loading
case loaded(UIImage)
case empty(String)
case error(String)
You can’t perform that action at this time.