Skip to content

Instantly share code, notes, and snippets.

View auramagi's full-sized avatar

Mike Apurin auramagi

View GitHub Profile
@auramagi
auramagi / ReadMore.swift
Last active June 8, 2024 06:23
SwiftUI read more component. Requires iOS 16.
import SwiftUI
struct ContentView: View {
var text = "long text"
@State private var lines = 3.0
var body: some View {
ScrollView {
ReadMore(lineLimit: Int(lines)) {
import MediaPlayer
import SwiftUI
struct ContentView: View {
@State var sliderValue: Float = 0
@StateObject var observer = VolumeObserver()
var body: some View {
VStack {
@auramagi
auramagi / Effect.swift
Last active August 25, 2023 07:49
Swift Concurrency + ViewModel action handling
enum Effect {
/// Action completed synchronously
case none
/// Action produced a task
case task(Task<Void, Never>)
/// Notify about completion in a closure
func onCompletion(_ completion: @escaping () -> Void) {
switch self {
@auramagi
auramagi / AlertPresentationWindow.swift
Last active December 27, 2022 07:23
Global alert handler for SwiftUI
import SwiftUI
private struct AlertPresentationWindow: View {
@ObservedObject var service: ErrorStateService
var body: some View {
Color.clear
.alert(
service.alerts.first?.title ?? "Error",
isPresented: .init(
import SwiftUI
struct BlobbySymbol<Primary: View, Alternative: View>: Animatable, View {
private var progress: Double = 0
var animatableData: Double {
get { progress }
set { progress = newValue }
}
// RE:
// https://gist.github.com/ole/4a8b5fe49d53c79b34e22b3401872093
// https://mobile.twitter.com/olebegemann/status/1572163020695928832
import SwiftUI
struct Item: Identifiable {
var id: UUID = .init()
var country: String
var population: String
@auramagi
auramagi / StateObjectBackportTest.swift
Created June 29, 2022 04:12
Manual testing code for `@StateObject` backport
import Combine
import SwiftUI
import SwiftUIBackports
@main
struct BackportTestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
@auramagi
auramagi / WaterfallLayout.swift
Created June 27, 2022 10:52
Waterfall Layout with SwiftUI Layout protocol
import SwiftUI
struct ContentView: View {
typealias Item = (Color, CGFloat)
@State private var columnCount = 3
@State private var isWaterfall = true
@State var items: [Item] = [
@auramagi
auramagi / CoreTextShape.swift
Last active June 27, 2022 11:16
Use Core Text in SwiftUI
import SwiftUI
struct ContentView: View {
var body: some View {
CoreTextShape(
string: "Hello YUMEMI.swift!",
font: .boldSystemFont(ofSize: 42)
)
.border(.purple)
.padding()
@auramagi
auramagi / LayoutAdaptor.swift
Created June 11, 2022 12:43
Drive UIView frame layout with SwiftUI Layout protocol
// The main layout logic
import SwiftUI
final class LayoutAdaptor: UIView {
var layout: (any Layout)? {
didSet { setNeedsLayout() }
}
override func layoutSubviews() {