Skip to content

Instantly share code, notes, and snippets.

@hassanvfx
hassanvfx / fetchObject.swift
Created January 29, 2024 15:18
Generic URLSession call
// Generic function to fetch and decode JSON into a Codable object
func fetchDecodedObject<T: Codable>(from urlString: String, completion: @escaping (Result<T, NetworkError>) -> Void) {
// Step 1: Create a URL instance
guard let url = URL(string: urlString) else {
completion(.failure(.badURL))
return
}
// Step 2: Create a URLSession
let session = URLSession.shared
@hassanvfx
hassanvfx / RouletteWheelView.swift
Created January 2, 2024 06:19
swiftui rouletter wheel
import SwiftUI
struct RouletteWheel: View {
let segments: [String]
var onResultSelected: (String) -> Void
@State private var rotationAngle: Double = 0
@State private var isSpinning: Bool = false
var body: some View {
ZStack {
@hassanvfx
hassanvfx / output_text.py
Created July 10, 2023 14:15
This script extract only the text from the twitter scraper
import csv
import sys
import os
import re
def remove_urls(text):
url_pattern = re.compile(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+')
return url_pattern.sub(r'', text)
def read_csv(filename, output_filename):
@hassanvfx
hassanvfx / QRCodeScanner.swift
Created June 23, 2023 02:04
SwiftUI QRCode Scanner
import SwiftUI
import AVFoundation
struct QRCodeScannerView: UIViewControllerRepresentable {
var handleQRCode: (String) -> Void
func makeUIViewController(context: Context) -> QRCodeScannerViewController {
let viewController = QRCodeScannerViewController()
viewController.handleQRCode = handleQRCode
@hassanvfx
hassanvfx / Quizz.swift
Created February 19, 2023 02:56
Quizz-sample
//
// QuizzView.swift
// TwinChatAI
//
// Created by Eon Fluxor on 2/18/23.
//
import Foundation
import SwiftUI
@hassanvfx
hassanvfx / convert2xcframework.sh
Created January 4, 2023 07:58
Convert Framework into XCFramework Xcode 14
#! /bin/bash
find "." -name '*.framework' -type d | while read -r FRAMEWORK
do
echo "-----------------------"
FRAMEWORK_NAME=$(echo "$FRAMEWORK" | sed 's/\.\/\(.*\)\.framework/\1/')
FRAMEWORK_BUNDLE_NAME=$(/usr/libexec/PlistBuddy -c "Print CFBundleName" "$FRAMEWORK/Resources/Info.plist") || exit
XCFRAMEWORK_ROOT="./_$FRAMEWORK_NAME.xcframework"
@hassanvfx
hassanvfx / cameraView.swift
Created January 4, 2023 05:24
Basic Camera Display / Buffer Analysis in SwiftUI
import SwiftUI
import AVFoundation
struct CameraView: View {
// Set up the AVCaptureSession and AVCaptureVideoPreviewLayer
let captureSession = AVCaptureSession()
let previewLayer = AVCaptureVideoPreviewLayer()
let cameraDelegate = CameraDelegate()
import UIKit
public class FactoryCache {
public typealias MakerClosure<Item> = ()->Item
typealias CacheType = NSCache<NSString, InstanceWrapper>
struct CacheWrapper{
var storage: CacheType = NSCache()
}
@hassanvfx
hassanvfx / largeTitleView.swift
Last active March 10, 2020 22:20
Access LargeTitleView in swift
var largeTitleView:UIView{
if let navBarSubviews = navigationController?.navigationBar.subviews {
for subView in navBarSubviews where NSStringFromClass(type(of: subView)).contains("LargeTitle"){
return subView
}
}
assert(false, "A large title view is expected on viewDidAppear")
return UIView()
}
@hassanvfx
hassanvfx / gist:02feb9f75d451810e1e3ec4a20131e9d
Created February 11, 2020 21:34
Check if the current application state is dark mode.
UIApplication.shared.windows.first?.traitCollection.userInterfaceStyle