Skip to content

Instantly share code, notes, and snippets.

View davidseek's full-sized avatar
💭
He/Him

David Seek davidseek

💭
He/Him
View GitHub Profile
@davidseek
davidseek / hoteFinal.swift
Created February 8, 2021 03:20
hoteFinal.swift
class Hotel {
private let smallRoomsCount: Int
private let mediumRoomsCount: Int
private let largeRoomsCount: Int
private var smallRooms = [String: Dog]()
private var mediumRooms = [String: Dog]()
private var largeRooms = [String: Dog]()
// MARK: - Public
@davidseek
davidseek / hotelOptimized.swift
Last active February 8, 2021 03:28
hotelOptimized.swift
class Hotel {
// MARK: - Public
// Another major improvement in our design,
// is to clearly define the desired access level of a function.
// We mark this function as public, as we want
// consumers to be able to access this function.
public func checkIn(_ dog: Dog) -> String? {
@davidseek
davidseek / hotelWithMethods.swift
Last active February 8, 2021 20:37
hotelWithMethods.swift
// Again, same class as before.
class Hotel {
// We now have the returned `roomID` optional.
// It's possible, that there are no rooms available anymore,
// so we need to make nil a valid option.
func checkIn(_ dog: Dog) -> String? {
// We want to return a roomID so each user
// is able to retrieve their dog using the ID.
@davidseek
davidseek / hotelWithAttributes.swift
Created February 8, 2021 03:16
hotelWithAttributes.swift
// This is not a new Hotel class.
// It's the same class as above.
// I'm just not going to add every line of code to every snippet.
// This way it's easier for you to see,
// what lines of code I'm talking about.
class Hotel {
// We have 3 individual integers,
// that reflect the available rooms in total
let smallRoomsCount: Int
@davidseek
davidseek / dog.swift
Created February 8, 2021 03:15
dog.swift
// Since we're iOS engineers,
// we might want to use a
// light weight type like a struct
struct Dog {
// A dog object needs an id for identification
let uid: String
// And important for out hotel, we need to know the size.
// We could use an integer, but that's not very easy to read.
// A String is a bad idea because mistakes can easily occur.
// That's why we want to use a dedicated enum.
@davidseek
davidseek / hotel.swift
Created February 8, 2021 03:15
hotel.swift
// I'm creating a hotel class
class Hotel {
// And I'm giving it the following defintions for some functions
// One function to check a dog in that returns a roomID
func checkIn(_ dog: Dog) -> String
// And one function that checks the dog out.
func checkOut(_ dog: Dog) -> Dog?
}
@davidseek
davidseek / human.swift
Last active February 8, 2021 21:45
human.swift
class Human {
// The eat function returns an indicator,
// whether or not the food has been eaten.
// Our human has free choice.
func eat(_ food: Food) -> Bool {
// But they're a simple person,
// that eats everything that's edible.
return food.isEdible
}
}
@davidseek
davidseek / setNotificationsTrigger.ts
Last active January 28, 2021 22:05
Push Notificaiton setNotificationsTrigger
/**
* This function will be called every day at 8AM pacific time.
* Scheduled as CRON job in cloud scheduler.
*/
// This function create an https API that we can call using a GET request.
export const setNotificationsTrigger = functions.https.onRequest(async (_, response) => {
// We're using a try catch block to take advantage of the
// very nice `await` syntax, while also handling errors properly.
@davidseek
davidseek / notifyUser.ts
Last active January 28, 2021 21:45
Push Notification notifyUser
/**
* Function to send a Push Notification using Firebase Messaging.
* @param userID The user to receive the push message.
* @param taskCount The amount of open tasks.
* @returns Returns the message ID or undefined in case of failure.
*/
function notifyUser(userID: string, taskCount: number): Promise<string | undefined> {
// Again we're just initiating a new Promise.
// A closure that can succeed and fail.
return new Promise(async (resolve, reject) => {
@davidseek
davidseek / getFormatted.ts
Last active January 28, 2021 21:33
Push Notifications Lodash
// Import statement to use lodash
const _ = require('lodash')
/**
* Converts the array to an array per userID
* @param tasks Tasks to convert
* @returns a dictionary of [userID: [Task]]
*/
function getFormatted(tasks: Task[]): { [userID: string]: Task[]; } {
// Use lodash function groupBy.