Skip to content

Instantly share code, notes, and snippets.

Avatar

Kaname Noto notoroid

View GitHub Profile
View 13_push_notification_testbed.swift
import SwiftUI
import UserNotifications
#if os(iOS)
class AppDelegate : UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
registerForPushNotifications()
UNUserNotificationCenter.current().delegate = self
return true
}
View 12_location-authorization.swift
import SwiftUI
import Foundation
import CoreLocation
import Combine
class LocationManager: ObservableObject {
enum LocationManagerError: Error, LocalizedError {
case deniedLocation; case restrictedLocation; case unknown
var errorDescription: String? {
switch self {
View 11_simple-combine-framework-testbed.swift
import SwiftUI
import Combine
// original code for CancellerWrapper was posted on @Cockscomb cockscomb's blog post. https://cockscomb.hatenablog.com/entry/2021/09/26/103128
class CancellerWrapper<Wrapped> {
var value: Wrapped
init(_ value: Wrapped) { self.value = value }
}
class SimpleModel: UIResponder, ObservableObject {
@notoroid
notoroid / 10_watchos-interval-background-task.swift
Last active Dec 22, 2021
watchOS8.0 interval background task sample.
View 10_watchos-interval-background-task.swift
import SwiftUI
struct RandomFox: Equatable, Codable { let image: String; let link: String }
class ExtensionDelegate: NSObject, ObservableObject, WKExtensionDelegate {
var foregroundDataTask: URLSessionDataTask?; var backgroundDataTask: URLSessionDataTask?
var timer: Timer?
let url = URL(string: "https://randomfox.ca/floof/")!
@Published var randomFox: RandomFox? = nil
// utilities
func taskRandomFox(completionHandler: @escaping (Error?) -> Void) -> URLSessionDataTask { // DataTask create function
View 09_list-sample-using-caseIterable-protocol.swift
import SwiftUI
//
// ContentView.swift
// ListSampleUsingCaseIterable
//
// Created by 能登 要 on 2021/04/14.
//
import SwiftUI
View 08_test-mode-alternate-icon.swift
import SwiftUI
import UIKit
// Debug.xcconfig
// OTHER_SWIFT_FLAGS = $(inherited) "-D" "DEBUG"
// DebugStage.xcconfig
// OTHER_SWIFT_FLAGS = $(inherited) "-D" "DEBUG" "-D" "STAGE"
// Release.xcconfig
View 07_textfield.swift
import SwiftUI
enum TextFieldTypes {
case defaultStyle
case customStyle
}
struct ContentView: View {
@State var myfield = ""
@ScaledMetric(relativeTo: .body) var imageSize = CGFloat(2)
View 06_settings-bundle-app-storage.swift
struct ContentView: View {
@AppStorage(wrappedValue: false, "enabled_preference") var enabled_preference
var body: some View {
NavigationView {
Text("The toolbar is displayed in conjunction with the switch On/Off in the Settings application - Third Party Application Settings - Sample Application Settings.\n\nNote: The AppStorage value will revert to its initial value after each app transfer after the build, so build carefully.")
.padding()
.toolbar {
ToolbarItemGroup(placement: .bottomBar) {
if enabled_preference {
Spacer()
@notoroid
notoroid / 05_mechanical-arm.swift
Created Feb 14, 2021
Sample of using SwifUI's rotationEffect modifier.
View 05_mechanical-arm.swift
import SwiftUI
enum MechanicalArm {
static let colors: [Color] = [Color(UIColor(red: 0.349, green: 0.671, blue: 0.914, alpha: 1.000)) /*.red, .green, .yellow, .blue, .yellow*/]
static let manipulatorJointEdge = CGFloat(16.5); static let manipulatorRootEdge = CGFloat(37.5)
static let manipulatorWidth = CGFloat(9); static let manipulatorFirstLength = CGFloat(60); static let manipulatorOtherLength = CGFloat(37.5)
static let armLength = CGFloat(105); static let armWidth = CGFloat(37.5); static let armJointEdge = CGFloat(16.5)
static let standWidth = CGFloat(75); static let standHeighth = CGFloat(60)
static let defaultArmAngle = Double(0); static let defaultWristAngle = Double(-45); static let defaultFingerAngle = Double(40)
}
View 04_curved-tabbar.swift
import SwiftUI
struct TopFrameView: Shape {
func path(in rect: CGRect) -> Path {
let bezierPath = UIBezierPath()
bezierPath.move(to: CGPoint(x: 9.16, y: 0.04))
let curvePoints = [(CGPoint(x: 22.13, y: 1.8), CGPoint(x: 13.75, y: 0.17), CGPoint(x: 18.98, y: 0.6)), (CGPoint(x: 36.89, y: 10.14), CGPoint(x: 28.69, y: 4.3), CGPoint(x: 31.15, y: 4.93)), (CGPoint(x: 46.73, y: 22.65), CGPoint(x: 42.63, y: 15.35), CGPoint(x: 44.27, y: 18.48)), (CGPoint(x: 58.2, y: 34.32), CGPoint(x: 49.19, y: 26.81), CGPoint(x: 52.47, y: 30.78)), (CGPoint(x: 70.5, y: 37.65), CGPoint(x: 63.94, y: 37.86), CGPoint(x: 70.5, y: 37.65)), (CGPoint(x: 82.8, y: 34.32), CGPoint(x: 70.5, y: 37.65), CGPoint(x: 77.06, y: 37.86)), (CGPoint(x: 94.27, y: 22.65), CGPoint(x: 88.53, y: 30.78), CGPoint(x: 91.81, y: 26.81)), (CGPoint(x: 104.11, y: 10.14), CGPoint(x: 96.73, y: 18.48), CGPoint(x: 98.37, y: 15.35)), (CGPoint(x: 118.87, y: 1.8), CGPoint(x: 109.85, y: 4.93), CGPoint(x: 112.31, y: 4.3)), (CGPoint(x: 140.79, y: 0.12)