Skip to content

Instantly share code, notes, and snippets.

View hmlongco's full-sized avatar

Michael Long hmlongco

View GitHub Profile
@hmlongco
hmlongco / WithTableViewExtension.swift
Last active August 1, 2021 18:15
The tableview extension
extension UITableView {
func dequeueCell<Cell:UITableViewCell>(_ id: String, for indexPath: IndexPath, configure: (_ cell: Cell) -> Void) -> UITableViewCell {
let cell = dequeueReusableCell(withIdentifier: id, for: indexPath)
if let cell = cell as? Cell {
configure(cell)
}
return cell
}
@hmlongco
hmlongco / WithTableView.swift
Created July 31, 2021 15:07
With in tableviews
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch section {
case 0:
return tableView.dequeueCell(CELLID, for: indexPath) { (_ cell: MyTableViewCell) in
cell.configure(viewModel.config((for: indexPath.section))
}
case 1:
...
}
}
struct MainMenuView: View {
@EnvironmentObject var menu: MenuService
var body: some View {
NavigationView {
List {
if menu.sections.isEmpty {
Text("Loading...")
.foregroundColor(.gray)
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 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 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) }
@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 / 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 / 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 / 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()