Skip to content

Instantly share code, notes, and snippets.

View lalkrishna's full-sized avatar
🤩

Lal Krishna lalkrishna

🤩
View GitHub Profile
@lalkrishna
lalkrishna / MenuItem.swift
Created January 17, 2023 07:50
MenuBuilder
protocol MenuItem { }
struct MenuItemSeparator: MenuItem { }
struct MenuItemModel: MenuItem {
let icon: UIImage?
let title: String
// var instance: Instantiatable.Type
fileprivate var instance: ViewControllerConvertable
@lalkrishna
lalkrishna / Instantiatable.swift
Created January 17, 2023 07:33
instantiate viewcontroller using identifier.
protocol Instantiatable {
static var storyboard: Storyboard { get }
static func instantiate() -> Self
}
extension Instantiatable where Self: UIViewController {
static func instantiate() -> Self {
// swiftlint:disable force_cast
UIStoryboard(storyboard).instantiateViewController(withIdentifier: String(describing: Self.self)) as! Self
}
@lalkrishna
lalkrishna / CurrencyFormatter.swift
Created January 11, 2023 09:50
amount formatter based on user's locale.
extension Double {
func formattedAsCurrency(_ currencySymbol: String? = nil) -> String {
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.locale = Locale(identifier: "id_ID")
if let currencySymbol {
formatter.currencySymbol = currencySymbol
}
let price = NSNumber(value: self)
@lalkrishna
lalkrishna / TableView+Extensions.swift
Last active August 5, 2022 05:02
Tableview Extensions for Making dequeuing easier.
extension UITableView {
func register<T: UITableViewCell>(_ cellClass: T.Type) {
register(T.self, forCellReuseIdentifier: String(describing: T.self))
}
func reusableCell<T: UITableViewCell>(for indexPath: IndexPath) -> T {
// swiftlint:disable force_cast
dequeueReusableCell(withIdentifier: String(describing: T.self), for: indexPath) as! T
}
@lalkrishna
lalkrishna / HTMLBuilder.swift
Last active March 11, 2022 06:37
Build AttributedString from HTML String content. Fonts can be linked locally.
//
// HTMLBuilder.swift
// HTMLBuilder
//
// Created by Lal Krishna on 11/03/22.
// Copyright © 2022 Lal Krishna. All rights reserved.
//
import Foundation
import UIKit
import Foundation
import Alamofire
import UIKit
typealias ResponseType = Decodable
typealias AFResponse<Response: Decodable> = Alamofire.DataResponse<ApiResponse<Response>, AFError>
struct AFNetwork: NetworkService {
static func cancelAllRequests() {
@lalkrishna
lalkrishna / throttledSearch.swift
Created August 30, 2021 12:15
Throttled / Incremental search on Swift
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
guard !searchText.isEmpty else { searchQuery(); return }
NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(searchQuery), object: nil)
perform(#selector(searchQuery), with: nil, afterDelay: 0.4)
}
@objc func searchQuery() {
guard let searchText = searchBar.text else { return }
// Call API
}
protocol UnknownCase: RawRepresentable, CaseIterable where RawValue: Equatable & Codable {
static var unknownCase: Self { get }
}
extension UnknownCase {
init(rawValue: RawValue) {
let value = Self.allCases.first { $0.rawValue == rawValue }
self = value ?? Self.unknownCase
}
@lalkrishna
lalkrishna / AppContainer.swift
Created January 26, 2021 16:05
Dependency injection using Property wrappers (like @EnvironmentObject in SwiftUI). Core code from: https://medium.com/@anuragajwani/dependency-injection-in-ios-and-swift-using-property-wrappers-f411117cfdcf
/* SET */
Container.shared.register(type: User.self, User())
Container.shared.set(User(), forKey: "SecondUser")
/* GET */
let sameUser = Container.shared.resolve(User.self)
let secondUser = Container.shared.get("SecondUser", of: User.self)
/* REMOVE */
$user.deregister() // This will remove "User" from factory.
@lalkrishna
lalkrishna / xcframework_generate.md
Created December 16, 2020 19:42
Create xcframework instead of Universal(fat) frameworks.

Advantages of xcframework

  • Support for multiple platforms in the same framework.
  • Support for Swift, Objective-C and C.
  • Support for dynamic frameworks and static libraries.
  • Support for Swift Package Manager.
  • End of fat binaries.

Steps to create Aggregate target:

  1. Open Current framework project