Skip to content

Instantly share code, notes, and snippets.

View yosshi4486's full-sized avatar

yosshi4486 yosshi4486

View GitHub Profile
@yosshi4486
yosshi4486 / EdgeLines.swift
Created May 3, 2024 11:52
SwiftUI: Partial Border of View
import SwiftUI
struct EdgeLines: Shape {
struct Edge: OptionSet {
static let top = Edge(rawValue: 1 << 0)
static let bottom = Edge(rawValue: 1 << 1)
static let left = Edge(rawValue: 1 << 2)
static let right = Edge(rawValue: 1 << 3)
static let all = Edge([.top, .bottom, .left, .right])
@yosshi4486
yosshi4486 / Polygon.swift
Created February 17, 2024 08:45
Polygon shape considered a corner radius.
/// 多角形のシェープ.
struct Polygon : Shape {
/// 多角形の頂点の数. デフォルト値は`3`
var numberOfVertex: Int
/// 角の半径. デフォルト値は`3`.
var cornerRadius: CGFloat
/// 直線をトップに配置するかどうかのBool値. デフォルト値は`false`.
@yosshi4486
yosshi4486 / ConvertToExtendedSRGB.swift
Last active November 26, 2023 09:35
Swift Extended sRGB Convert methods.
/// Extended Range sRGB, 拡張範囲sRGBの構造体.
///
/// ユーザーがカラーピッカーで選択した色を保存する際、
/// 拡張sRGBに変換して取り回せばDisplay P3やAdobe RGBなどRGB系の別カラースペースの場合でも気にせず取り扱え便利なため中心的に利用する.
struct ExtendedSRGB: Hashable, Codable {
/// 負数と1以上の値も扱える赤の値. 0.0~1.0はsRGBと互換性がある。
var red: CGFloat
/// 負数と1以上の値も扱える緑の値. 0.0~1.0はsRGBと互換性がある。
@yosshi4486
yosshi4486 / OnCommitModifier.swift
Created August 16, 2023 01:45
OnCommitModifier
/// データをコミットすべきタイミングで実行されるModifier
///
/// コミットされるタイミングは下記の3つ:
/// 1. `UIApplication.willTerminateNotification`通知を受け取ったとき
/// 2. `onDissapear`が呼び出されたとき
/// 3. `scenePhase`が`.background`に切り替わったとき
///
/// このModifierは上記の3つをまとめて汎用的に取り扱えるシンタックスシュガーとなっている.
struct OnCommitModifier: ViewModifier {
@yosshi4486
yosshi4486 / UIKitSplitView.swift
Last active July 22, 2023 08:34
An UIViewControllerRepresentable wrapper of UISplitViewController
/// UIKit製のUISplitViewController.
struct UIKitSplitView<Primary, Secondary>: UIViewControllerRepresentable where Primary : View, Secondary : View {
typealias UIViewControllerType = UISplitViewController
/// プライマリPaneのビューを生成するクロージャ
var primary: () -> Primary
/// セカンダリPaneのビューを生成するクロージャ
var secondary: () -> Secondary
@yosshi4486
yosshi4486 / UIKitTableView.swift
Last active July 18, 2023 13:55
UIKitTableView for some cases SwiftUI.List couldn't be applied.
//
// UIKitTableView.swift
//
// Created by yosshi4486 on 2023/07/18.
//
import SwiftUI
private struct MoveActionEnvironmentKey: EnvironmentKey {
@yosshi4486
yosshi4486 / BackForwardList.swift
Last active April 8, 2023 06:23
Swift implementation of a `BackForwardList` which can manage back and forward.
//
// BackForwardList.swift
//
// Created by yosshi4486 on 2023/04/02.
//
import Foundation
/// 進む・戻る管理の可能なリスト
///
@yosshi4486
yosshi4486 / SynchronizingKeyValueStore.swift
Last active October 21, 2023 23:17
Example of syncing key value stores.
//
// SynchronizingKeyValueStore.swift
//
// Created by yosshi4486 on 2022/08/30.
//
import Foundation
/// A key value store that stores a key-value into both local and cloud. Subclass this class and override methods for your application.
///
@yosshi4486
yosshi4486 / NetworkConnectivityManager.swift
Last active May 9, 2022 00:58
Sample use of NWPathMonitor
/// A class that checks and notifies an internect connectivity changes.
class NetworkConnectivityManager {
/// The static singleton instance. You can use this throught `NetworkConnectivityManager.shared`.
///
/// Creating several manager instances doesn't make sense, so we design it as singleton.
static let shared = NetworkConnectivityManager()
/// The static notification name that the notification is post when a network status change. The object is [NWPath.Status](https://developer.apple.com/documentation/network/nwpath/status)
static let networkConnectivityDidChangeNotificationName = Notification.Name(rawValue: "_networkConnectivityDidChangeNotification")
@yosshi4486
yosshi4486 / ViewController.swift
Created November 15, 2021 03:28
Creating an own view subclass that can receive keyboard input.
class CustomInputView: UIView, UIKeyInput {
private let label = UILabel()
override init(frame: CGRect) {
super.init(frame: frame)
label.tintColor = .label
label.font = .preferredFont(forTextStyle: .body, compatibleWith: nil)
label.text = ""