This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? { | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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? | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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) => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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. |
NewerOlder