Skip to content

Instantly share code, notes, and snippets.

View fatbobman's full-sized avatar

东坡肘子 fatbobman

View GitHub Profile
@fatbobman
fatbobman / keyboardAvoiding.swift
Created May 13, 2024 03:49
keyboardAvoiding for List
// by klaytonb
// https://forums.developer.apple.com/forums/thread/699111?answerId=740437022#740437022
import Combine
import SwiftUI
public extension Publishers {
static var keyboardHeight: AnyPublisher<CGFloat, Never> {
let willShow = NotificationCenter.default.publisher(for: UIApplication.keyboardWillShowNotification)
.map { $0.keyboardHeight }
let willHide = NotificationCenter.default.publisher(for: UIApplication.keyboardWillHideNotification)
@fatbobman
fatbobman / myContainerRelativeFrame.swift
Last active May 9, 2024 02:02
A forked version of ContainerRelativeFrame
import Combine
import Foundation
import SwiftUI
import UIKit
extension UIView {
fileprivate func findRelevantContainer() -> ContainerType? {
var responder: UIResponder? = self
while let currentResponder = responder {
import SwiftUI
import TipKit
struct Tip1: ShowTip {
var title: Text = Text("Step1 Tips")
@Parameter
static var show: Bool = false
var rules: [Rule] {
@fatbobman
fatbobman / dynamicHeightSheet.swift
Created April 5, 2024 06:59
a dynamic sheet demo
import SwiftUI
struct ContentView: View {
@State var show = false
@State var height: CGFloat = 250
var body: some View {
List {
Button("Pop Sheet") {
height = 250
show.toggle()
@fatbobman
fatbobman / noteExtension.swift
Created March 16, 2024 13:40
ModelExtensionDemo
import Foundation
import SwiftData
extension Note {
/// 根据给定的筛选条件生成相应的谓词,用于筛选笔记。
public static func predicateFor(_ filter: NotePredicate) -> Predicate<Note>? {
var result: Predicate<Note>?
switch filter {
case let .filterRootNoteByName(name):
result = #Predicate<Note> { $0.name == name && $0.parent == nil }
@fatbobman
fatbobman / CompoundPredicate.swift
Created February 5, 2024 12:46 — forked from NoahKamara/CompoundPredicate.swift
Combining New Swift Predicates
import Foundation
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
/// Allows you to use an existing Predicate as a ``StandardPredicateExpression``
struct VariableWrappingExpression<T>: StandardPredicateExpression {
let predicate: Predicate<T>
let variable: PredicateExpressions.Variable<T>
func evaluate(_ bindings: PredicateBindings) throws -> Bool {
// resolve the variable
@fatbobman
fatbobman / disableNavigationViewGesture.swift
Created November 7, 2022 07:43
避免在 dismiss Sheet 后快速返回 NavigationView 上层视图导致的应用锁死
/*
当在 NavigationView 非根视图中显示 sheet 时,在 dismiss Sheet 后快速用手势( 从左向右 )返回上层视图会导致整个 UI 锁死,
应用从此无法响应。写了个 ViewModifer ,来避免这种情况
*/
public extension Notification.Name {
static let disableNavigationViewGesture = Notification.Name("disableNavigationViewGesture")
}
public struct NavigationViewGestureMaskModifier: ViewModifier {
@State private var enable = false
@fatbobman
fatbobman / InfiniteScrollChart.swift
Created November 7, 2022 03:59 — forked from beader/InfiniteScrollChart.swift
Infinite Scrollable Bar Chart using Swift Charts
//
// InfiniteScrollChart.swift
// ChartsGallery
//
// Created by beader on 2022/11/3.
//
import SwiftUI
import Charts
@fatbobman
fatbobman / adaptiveSheet.swift
Created October 22, 2022 00:53
Adaptive Height Sheet
import SwiftUI
struct ContentView: View {
@State var show = false
var body: some View {
VStack {
Button("Pop Sheet") { show.toggle() }
}
.adaptiveSheet(isPresent: $show) { SheetView() }
}
@fatbobman
fatbobman / FreeSpaceViewController.swift
Created October 8, 2022 00:14 — forked from kylehowells/FreeSpaceViewController.swift
UIViewController subclass to show both the Settings app displayed free space, and the real free space.
//
// FreeSpaceViewController.swift
// Free Space
//
// Created by Kyle Howells on 04/05/2022.
//
import UIKit
class FreeSpaceViewController: UIViewController {