Skip to content

Instantly share code, notes, and snippets.

View ahbou's full-sized avatar
shipping

Ahmed Bouchfaa ahbou

shipping
View GitHub Profile
struct Overlay: View {
@State private var showLive = true
@State private var isAnimationSlowed = false
var body: some View {
VStack {
Spacer()
HStack {
Spacer()
@ahbou
ahbou / ProfileViewController.swift
Last active March 17, 2021 11:35
CoreData discard changes
import UIKit
class ProfileViewController: UIViewController {
/// First create a throwable child context for editing the Profile
private lazy var writeContext: NSManagedObjectContext = {
let result = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
result.automaticallyMergesChangesFromParent = true
result.performAndWait {
// Our main parent context
@ahbou
ahbou / Observable.swift
Created December 9, 2020 16:58
Observable
import Foundation
class Observable<T>: NSObject {
private var subscriptions: Atomic<[WeakBox<Subscription>]> = Atomic([])
fileprivate func fire(_ payload: T) {
let targets = subscriptions.modify { value -> [Subscription] in
value = value.filter { $0.value != nil }
return value.compactMap { $0.value }
@ahbou
ahbou / gist:44edfb02a1facbd3307d06f28996b3d9
Created November 27, 2020 08:53 — forked from zetachang/gist:4111314
Instruction on adding a Acknowledgements Settings.bundle
  • To add Settings.bundle in Xcode. Command N and in Resource, choose Settings Bundle .
  • Open Root.plist in source code, paste the code below to replace it,
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>PreferenceSpecifiers</key>
	
@ahbou
ahbou / URLCacheTest.swift
Created November 12, 2020 08:23 — forked from steipete/URLCacheTest.swift
Using URLCache with download tasks (NSURLCache & NSURLSessionDownloadTask)
import Foundation
import os.log
class URLCacheTest {
let logger = Logger(subsystem: "URLCacheTest", category: "main")
// HTTP HEADERS:
// Date: Wed, 04 Nov 2020 11:13:24 GMT
// Server: Apache
// Strict-Transport-Security: max-age=63072000; includeSubdomains; preload
@ahbou
ahbou / Log.swift
Created June 22, 2020 08:58
Swift logger with emoji
//
// Log.swift
//
//
import Foundation
class Log {
class func msg(message: String,
functionName: String = #function, fileNameWithPath: NSString = #file, lineNumber: Int = #line ) {
//
// UIView+RSKeyboardLayoutGuide.swift
// RSTouchUIKit
//
// Created by Daniel Jalkut on 12/23/18.
//
import UIKit
// Extends UIView to expose a keyboardLayoutGuide property that can be used to tie a view controller's content
@ahbou
ahbou / UIWindow+Extension.swift
Created June 1, 2018 11:00
Animate UIWindow rootViewController switch
extension UIWindow {
func setRootViewController(_ rootViewController: UIViewController?, animated: Bool) {
guard let viewContoller = rootViewController else {
self.rootViewController = rootViewController
return
}
var snapShotView: UIView?
@ahbou
ahbou / HUD.swift
Last active October 28, 2019 09:14
Dead Simple HUD implementation in swift with a title and an Acitivity indicator
import UIKit
class HUD: UIView {
private lazy var backView: UIView = UIView(frame: bounds)
private let activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView(style: .whiteLarge)
private lazy var titleLabel: UILabel = {
let title = UILabel()
title.font = UIFont.boldSystemFont(ofSize: 16)
@ahbou
ahbou / WavPlayer.swift
Created October 12, 2017 08:18
Play .wav file in Swift
let ringtonePath = URL(fileURLWithPath: Bundle.main.path(forResource: "sound", ofType: "wav")!)
do {
let ringtonePlayer = try AVAudioPlayer(contentsOf: ringtonePath)
try AVAudioSession.sharedInstance().setActive(true)
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord)
ringtonePlayer.volume = 1.0
ringtonePlayer.play()
} catch {
print("Failed to initialize audio player \(error.localizedDescription)")