Skip to content

Instantly share code, notes, and snippets.

@SlappyAUS
SlappyAUS / DateSnippets.swift
Created December 15, 2020 07:54
Date Snippets #swift #date
private func setDateFromSelection() {
let monthNumber = getMonthNumberFromName(name: month)
let timeComponents = Calendar.current.dateComponents(
[.hour, .minute, .second, .nanosecond], from: selectedDate)
let dateComponents = DateComponents(
year: year,
month: monthNumber,
day: day,
hour: timeComponents.hour,
@SlappyAUS
SlappyAUS / DateUtil.swift
Last active December 15, 2020 07:53
DateUtils #swift #date
//
// DateUtil.swift
// DrinkSum
//
// Created by Greg Eales on 5/11/20.
//
import Foundation
struct DateUtil {
static func getModifiedDate(date: Date, currentDate: Date) -> String {
@SlappyAUS
SlappyAUS / EnumAsImages.swift
Created December 6, 2020 00:00
Enums as Images #swift #enum
enum SFSymbols {
static let location = UIImage(systemName: "mappin.and.ellipse")
static let repos = UIImage(systemName: "folder")
static let gists = UIImage(systemName: "text.alignleft")
static let followers = UIImage(systemName: "heart")
static let following = UIImage(systemName: "person.2")
}
@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:
@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 / 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 / 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 / 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 / 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 / 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.