Skip to content

Instantly share code, notes, and snippets.

View iamazhar's full-sized avatar
🤖
The world deserves secure, on-device AI

Azhar Anwar iamazhar

🤖
The world deserves secure, on-device AI
View GitHub Profile
@iamazhar
iamazhar / SharedContainerHelper.swift
Created August 8, 2023 18:12
Share data between app clip and app using this shared container helper
import Foundation
public class SharedContainerHelper {
private static var appGroupIdentifier: String?
/// Set the app group identifier to enable sharing data between app clip and main app.
/// - Parameter groupIdentifier: The app group identifier.
public static func configure(withAppGroup groupIdentifier: String) {
appGroupIdentifier = groupIdentifier
@iamazhar
iamazhar / UserDefaults.swift
Last active June 29, 2023 22:42
User Defaults helper that provides core operations
public class UserDefaultsHelper {
/// Use this generic func to encode and save an Codable object to UserDefaults
/// - Parameters:
/// - value: Any Codable type
/// - key: The corresponding key
/// - Returns: true if it is saved successfully, else false
@discardableResult
public static func save<T: Codable>(customValue value: T, withKey key: String) -> Bool {
if let encoded = try? JSONEncoder().encode(value) {
@iamazhar
iamazhar / Logger.swift
Created October 12, 2022 10:08
Logger wrapper on print statement
import Foundation
public enum LoggerLevel {
case info
case debug
case warning
case error
}
final class Logger {
@iamazhar
iamazhar / UserDefaultsHelper.swift
Created October 12, 2022 09:59
User Defaults helper with generics
import Foundation
public final class UserDefaultsHelper {
/// Use this generic func to encode and save an Codable object to UserDefaults
/// - Parameters:
/// - value: Any Codable type
/// - key: The corresponding key
/// - Returns: true if it is saved successfully, else false
@discardableResult
var body: some View {
Text("She sells sea shells on the sea shore. She sells sea shells on the sea shore")
.truncationMode(.head)
}
//1
let image = UIImage(named: "swifty")!
let swifty = Bird(name: "Swifty", rarity: .veryRare, image: image)
//2
let viewModel = BirdViewModel(bird: swifty)
//3
let frame = CGRect(x: 0, y: 0, width: 300, height: 450)
let view = BirdView(frame: frame)
// MARK: - View
public class BirdView: UIView {
public let imageView: UIImageView
public let nameLabel: UILabel
public let purchaseFeeLabel: UILabel
public override init(frame: CGRect) {
var imageViewFrame = CGRect(x: 0, y: 16, width: frame.width, height: frame.width/2)
imageView = UIImageView(frame: imageViewFrame)
// MARK: - Bird View Model
public class BirdViewModel {
//1
private let bird: Bird
public init(bird: Bird) {
self.bird = bird
}
import PlaygroundSupport
import UIKit
public class Bird {
public enum Rarity {
case common
case uncommon
case rare
case veryRare
}
func displayContent(urlString: String) -> Void {
guard let url = URL(string: urlString) else{return}
let hideScriptURL = Bundle.main.path(forResource: "hideSections", ofType: "js")
do{
//Remove Content Blocks
let scriptContent = try String(contentsOfFile: hideScriptURL!, encoding: String.Encoding.utf8)
let hideSectionsScript = WKUserScript(source: scriptContent, injectionTime: .atDocumentStart, forMainFrameOnly: true)
let controller = WKUserContentController()
controller.addUserScript(hideSectionsScript)
let config = WKWebViewConfiguration()