Skip to content

Instantly share code, notes, and snippets.

@SlappyAUS
SlappyAUS / hideKeyboard.swift
Last active November 18, 2020 08:05
Hide Keyboard #swift #swiftui #ui
// Add to View Struct
@State private var editing = false
// TextField Definition
TextField("", text: dailyIntakeGoalString) { (change) in
self.editing = true
} onCommit: {}
// Attach to Parent View
.gesture(TapGesture().onEnded { _ in
@SlappyAUS
SlappyAUS / watchkit_lists.swift
Created November 22, 2020 09:17
WatchKit Lists #watchkit #ui #swiftui #ios #swift
//
// ContentView.swift
// FirstWatch WatchKit Extension
//
// Created by Greg Eales on 22/11/20.
//
import SwiftUI
struct Person: Identifiable, Equatable {
@SlappyAUS
SlappyAUS / WatchkitButtonStyle.swift
Created November 24, 2020 01:59
Watchkit Button Style #swiftui #watchos
struct ContentView: View {
@State private var EnrouteText = "Enroute"
var body: some View {
List {
Button(action: {
self.EnrouteText = getCurrentTime()
}) {
Text(EnrouteText)
}
.buttonStyle(BtnStyle(bgColor: .red, fgColor: .white))
@SlappyAUS
SlappyAUS / PublishedData.swift
Last active November 29, 2020 09:17
Pub/Sub Property Sample #swift #combine
// The list of drinks consumed.
// Because this is @Published property,
// Combine notifies any observers when a change occurs.
@Published public var currentDrinks = [Drink]()
// A sink that is also called whenever the currentDrinks array changes.
var updateSink: AnyCancellable!
// Add a subscriber to currentDrinks that responds whenever currentDrinks changes.
@SlappyAUS
SlappyAUS / CustomQueues.swift
Created November 29, 2020 09:22
Custom Queues #swift #async #queue
// A background queue used to save and load the model data.
private var background = DispatchQueue(label: "Background Queue",
qos: .userInitiated)
// Save the data on a background queue.
background.async { [unowned self] in
do {
try data.write(to: self.getDataURL(), options: [.atomic])
@SlappyAUS
SlappyAUS / PropertyEncoder.swift
Created November 29, 2020 09:26
Property Encoder #swift #data #binary #plist
// Returns the URL for the plist file that stores the drink data.
private func getDataURL() throws -> URL {
// Get the URL for the app's document directory.
let fileManager = FileManager.default
let documentDirectory = try fileManager.url(for: .documentDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: false)
// Append the file name to the directory.
@SlappyAUS
SlappyAUS / HealthKitContorller.swift
Created November 29, 2020 09:28
HealthKit Controller Sample #swift #healthkit
/*
See LICENSE folder for this sample’s licensing information.
Abstract:
A data controller that manages reading and writing data from the HealthKit store.
*/
import Foundation
import HealthKit
@SlappyAUS
SlappyAUS / ArrayFiltering.swift
Last active December 27, 2020 02:26
Array Filtering #swift #array #filter
// Filter array to only the drinks in the last 24 hours.
private func filterDrinks(drinks: [Drink]) -> [Drink] {
// The current date and time.
let endDate = Date()
// The date and time 24 hours ago.
let startDate = endDate.addingTimeInterval(-24.0 * 60.0 * 60.0)
// return an array of drinks with a date parameter between
// the start and end dates.
@SlappyAUS
SlappyAUS / ConfirmationTextField.swift
Created December 2, 2020 06:08
ConfirmationTextField #swift #swiftui #io
struct ConfirmationTextField: View {
@State private var showtextFieldToolbar = false
let keyboardType: UIKeyboardType
@Binding var text: String
var body: some View {
TextField("", text: $text)
{ isChanged in
if isChanged {
showtextFieldToolbar = true
@SlappyAUS
SlappyAUS / AttributedEnums.swift
Created December 5, 2020 23:55
Attributed Enums #swift #enums
enum SocialMediaPlatofrm {
case twitter(followers: Int)
case youtube(subscribers: Int)
case instagram
case linkedin
}
func getSponshorshipEligibility(for platofrm: SocialMediaPlatform) {
switch platform {
case .twitter(let followers) where followers > 10_000: