Skip to content

Instantly share code, notes, and snippets.

View anivaros's full-sized avatar

Vasily Anisimov anivaros

View GitHub Profile
import _Concurrency
import Combine
import Dispatch
import Foundation
// MARK: General
struct SomeError: Error {}
extension AnyPublisher {
@buh
buh / AuthenticationStateExample.swift
Last active December 14, 2023 09:42
Example for a Finite-State Machine
/// The authentication state.
enum AuthenticationState: StateType {
// A list of events.
enum Event {
case userSignIn(email: String, password: String)
case accessTokenReceived(AccessToken)
case userReceived(User)
case userSignedOut
}
@gdavis
gdavis / ThreadSafe.swift
Last active October 21, 2024 13:49
ThreadSafe is a property wrapper that can be used to control atomic access to the underlying property while allowing concurrent access to reading the value.
//
// ThreadSafe.swift
// GDICore
//
// Created by Grant Davis on 1/2/21.
// Updated to support `_modify` accessor on 12/5/21.
//
// Copyright © 2021 Grant Davis Interactive, LLC. All rights reserved.
//
import Foundation
@networkextension
networkextension / actor_dl_imge.swift
Created June 11, 2021 07:56
actor async/await download imge Swift 5.5
extension UIImage {
@available(iOS 15, *)
var thumbnail: UIImage? {
get async {
let size = CGSize(width: 80, height: 40)
return await self.byPreparingThumbnail(ofSize: size)
}
}
}
enum FetchError:Error{
@gtrabanco
gtrabanco / S00-backnifi.sh
Last active February 20, 2023 09:50
Script to do a backup of unifi or protect files with udm
#!/usr/bin/env sh
{ # Ensure all script is loaded
#
# Backup unifi or protect configuration files to a remote server
# You should install first the public key remotely.
#
# To do ssh authentication if you have a Cloud Key Gen2, it is like any other
# debian. Just use "ssh-keygen" and "ssh-copy-id"
#
@atomicbird
atomicbird / NSPersistentContainer+extension.swift
Created May 22, 2020 21:39
Back up and restore Core Data persistent stores
//
// NSPersistentContainer+extension.swift
// CDMoveDemo
//
// Created by Tom Harrington on 5/12/20.
// Copyright © 2020 Atomic Bird LLC. All rights reserved.
//
import Foundation
import CoreData
@mecid
mecid / Calendar.swift
Last active November 28, 2024 14:25
SwiftUI Calendar view using LazyVGrid
import SwiftUI
extension Calendar {
func generateDates(
inside interval: DateInterval,
matching components: DateComponents
) -> [Date] {
var dates: [Date] = []
dates.append(interval.start)
@zntfdr
zntfdr / firebase-iOS-breakdown.swift
Last active September 24, 2024 06:29
Firebase iOS Version breakdown
// How to:
// 1. Open the Firebase Analytics Dashboard
// 2. Scroll to bottom, where you see the "Users by Device model" widget
// 3. Click "View device models" in that widget (this opens the "Tech details" Firebase Analytics page)
// 4. Above the table shown in the new page, click on the “Device model” drop down menu and select “OS with Version”
// 5. Make sure to select “OS with version” and not “OS Version”
// 6. On the top right corner of the page, click on the “Share this report” icon (next to the date)
// 7. Click “Download file” on the new side bar, then “Download CSV"
// 8. Open the file and select the iOS/Android breakdown raw data
// 9. Replace the sample data in this script with your data
@mjm
mjm / ManagedObjectChangesPublisher.swift
Created November 3, 2019 20:41
Observe changes to a Core Data fetch request with Combine
import Combine
import CoreData
extension NSManagedObjectContext {
func changesPublisher<Object: NSManagedObject>(for fetchRequest: NSFetchRequest<Object>)
-> ManagedObjectChangesPublisher<Object>
{
ManagedObjectChangesPublisher(fetchRequest: fetchRequest, context: self)
}
}
@Amzd
Amzd / UIKitTabView.swift
Last active January 2, 2025 02:53
UIKitTabView. SwiftUI tab bar view that respects navigation stacks when tabs are switched (unlike the TabView implementation)
/// An iOS style TabView that doesn't reset it's childrens navigation stacks when tabs are switched.
public struct UIKitTabView: View {
private var viewControllers: [UIHostingController<AnyView>]
private var selectedIndex: Binding<Int>?
@State private var fallbackSelectedIndex: Int = 0
public init(selectedIndex: Binding<Int>? = nil, @TabBuilder _ views: () -> [Tab]) {
self.viewControllers = views().map {
let host = UIHostingController(rootView: $0.view)
host.tabBarItem = $0.barItem