Skip to content

Instantly share code, notes, and snippets.

View imthath-m's full-sized avatar
💭
developmentMode

Mohammed Imthathullah imthath-m

💭
developmentMode
View GitHub Profile
@imthath-m
imthath-m / SwiftUIRendering.swift
Last active January 21, 2023 14:04
Sample View to demonstrate how often SwiftUI redraws its views.
import SwiftUI
struct SampleView: View {
var texts = ["SwiftUI", "A new declarative UI framework", "Let us try to understand how SwiftUI renders its views"]
@State var selected = 0
@State var textFieldValue = ""
@State var isSwitchOn = false
@imthath-m
imthath-m / Imitable.swift
Last active February 25, 2020 13:46
Conform to this protocol to easily deep copy reference types in Swift.
import Foundation
public protocol Imitable: Codable {
var copy: Self? { get }
}
extension Imitable {
public var copy: Self? {
guard let data = try? JSONEncoder().encode(self) else { return nil }
return try? JSONDecoder().decode(Self.self, from: data)
@imthath-m
imthath-m / ComparingCodables.swift
Created February 25, 2020 14:09
Provide default implemenation for Equatable classes conforming to Codable
import Foundation
public extension Equatable where Self: Codable {
static func ==(lhs: Self, rhs: Self) -> Bool {
lhs.jsonData == rhs.jsonData
}
}
public extension Person: Equatable { }
import SwiftUI
// import Introspect
public struct PasscodeField: View {
var maxDigits: Int = 4
var label = "Enter One Time Password"
@State var pin: String = ""
@State var showPin = false
@objc class SuperClassA: NSObject {
public required override init() { }
public static var shared: SuperClassA {
if let instance = _shared {
return instance
}
return instance
@imthath-m
imthath-m / 0_UIKitTabView.swift
Created April 8, 2020 02:02 — forked from Amzd/UIKitTabView.swift
SwiftUI tab bar view that respects navigation stacks when tabs are switched (unlike the TabView implementation)
import SwiftUI
/// An iOS style TabView that doesn't reset it's childrens navigation stacks when tabs are switched.
struct UIKitTabView: View {
var viewControllers: [UIHostingController<AnyView>]
@State var selectedIndex: Int = 0
init(_ views: [Tab]) {
self.viewControllers = views.map {
let host = UIHostingController(rootView: $0.view)
class UserSettings: ObservableObject {
@Published var score = 0
}
struct ContentView: View {
@EnvironmentObject var settings: UserSettings
var body: some View {
NavigationView {
VStack {
class UserSettings: ObservableObject {
private init() { }
static let shared = UserSettings()
@Published var score = 0
}
@available(iOS 14.0, *)
public struct EnumMenuPicker<T: Hashable & CaseIterable, V: View>: View {
@Binding var selected: T
var title: String? = nil
let mapping: (T) -> V
public var body: some View {
if let existingTitle = title {
@available (iOS 14.0, *)
public struct MenuPicker<T, V: View>: View {
@Binding var selected: Int
var array: [T]
var title: String?
let mapping: (T) -> V
public init(selected: Binding<Int>, array: [T], title: String? = nil,
mapping: @escaping (T) -> V) {