GithubHelp home page GithubHelp logo

tipit's Introduction

Pre-work - TipIt

TipIt is a tip calculator application for iOS.

Submitted by: Satoru Sasozaki

Time spent: 40 hours spent in total

User Stories

The following required functionality is complete:

  • User can enter a bill amount, choose a tip percentage, and see the tip and total values.
  • Settings page to change the default tip percentage.

The following optional features are implemented:

  • UI animations
  • Remembering the bill amount across app restarts (if <10mins)
  • Using locale-specific currency and currency thousands separators.
  • Making sure the keyboard is always visible and the bill amount is always the first responder. This way the user doesn't have to tap anywhere to use this app. Just launch the app and start typing.
  • Add color theme

The following additional features are implemented:

  • Tip View Controller
  • Add icons in the navigation bar and the split labels
  • Add labels that tells user the total amount split by 2-4 people
  • Add Save button in the navigation bar to save the current bill to bill record
  • Show an alert view when the save button is clicked
  • Setting View Controller
  • Add a static table view with grouped cells
  • Add UISlider to change tip amounts
  • Add a switch to change the theme color
  • Add a cell to go to the bill record table view
  • Additional Screens
  • Add history table view controller to show the bills that users have saved
  • Add detail view controller to show the selected bill in detail in history table view

Video Walkthrough

Here's a walkthrough of implemented user stories:

Basic feature and animation:

Change currency symbol:

Change theme color:

Change tip percents with UISlider:

Save the current bill and see the detail:

GIF created with LiceCap.

Notes

Describe any challenges encountered while building the app.

Used struct and subscript operator to customize what to return when an item at a specified index of an array is about to return.

####Issue

I had UserManager class that had billRecords property of type [[String:String]]. When the property gets accessed, it go get [[String:String]] object in NSUserDefault and return it. userManger.billRecords returns an Array object that has [billKey: bill, tipPercentKey: tipPercent, totalKey: total, dateKey, date]. UserManager also had public static properties of key to dictionary. In order for Client class of UserManager to get the dictionary value, it had to do like userManager.billRecords[0][UserManager.billKey]. This could cause the client class to put an invalid random key to a dictionary item from billRecords. Client class used to able to do like billRecords[0]["invalidKey"].

class UserManger: NSObject {
	static let billKey = "billKey"
	static let tipPercentKey = "tipPercentKey"
	...
	
	var billRecords: [[String:String]] {
		get {
			return userDefault.objectForKey("billRecordsKey") as! [[String:String]]
		}
}

// In client of UserManger
let user = UserManager()
let billRecord = userManger!.billRecords[0]
let bill = billRecord[UserManager.billKey] // can be billRecord["invalidKey"]

####Challenge In order to make a client class of UserManger not able to put random keys in a dictionary item in billRecords array, I made a model object called BillRecord to wrap the dictionary items in billRecords, and then try to make UserManager return a BillRecord object when BillRecords[index] is executed. With this way client class can get values that are used to in a dictionary with like billRecord[index].bill but not billRecord[index]["key"]. What I wanted to do is to make billRecords of type [[String:String]] return an object of type BillRecord.

class BillRecord: NSObject {
    // keys
    static let billRecordKey = "billRecord"
    static let tipPercentRecordKey = "tipPercentRecord"
    static let totalRecordKey = "totalRecord"
    static let dateRecordKey = "dateRecord"
    
    let bill: String?
    let tipPercent: String?
    let total: String?
    let date: String?
    
    override init() {
        bill = ""
        tipPercent = ""
        total = ""
        date = ""
        super.init()
    }
    
    // init with an item from [[String:String]]
    init(rawRecord: [String:String]) {
        self.bill = rawRecord[BillRecord.billRecordKey]
        self.tipPercent = rawRecord[BillRecord.tipPercentRecordKey]
        self.total = rawRecord[BillRecord.totalRecordKey]
        self.date = rawRecord[BillRecord.dateRecordKey]
    }
}

But getter in an Array type computed property can only return the whole array and I cannot specify what to do when an index is specified like billRecords[0]. billRecords[0] returns an object of type [String:String] and there is no way to make billRecords return BillRecord object by using an Array property.

####Solution I used struct and subscript operator to make billRecords[index] return BillRecord object but not [String:String] object. subscript allows you to customize the behavior when an item at a specified index of collection object is about to return.

// UserManager.swift
struct BillRecords {
    
    let ud: NSUserDefaults?
    var count: Int
    
    init(ud: NSUserDefaults) {
        self.ud = ud
        if let records = ud.objectForKey(UserManager.recordKey) {
            let records = records as! [[String:String]]
            count = records.count
        } else {
            count = 0
        }
    }
    
    subscript (i: Int) -> BillRecord {
        mutating get {
            if let records = ud?.objectForKey(UserManager.recordKey) {
                let records = records as! [[String:String]]
                count = records.count
                return BillRecord(rawRecord: records[i])
            } else {
                let records: [[String:String]] = []
                count = records.count
                ud?.setObject(records, forKey: UserManager.recordKey)
                return BillRecord()
            }
        }
    }
}

// Client class of UserManager
let records = userManager.records
let record = records[0]
let bill = record.bill 

After making an instance of BillRecords a public property of UserManager, billRecords[index] became able to return BillRecord object and then, client class can now get values by billRecord.bill without caring about the keys like billRecord[UserManager.billKey] as it used to be before. I give a least privilege to a client class of UserManger and the client class would never fail to get values from billRecord.

License

Copyright [2016] [Satoru Sasozaki]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

tipit's People

Contributors

satorusasozaki avatar

Watchers

 avatar

tipit's Issues

Thanks!

Great work!! This exercise is intended in part to give you an introduction to the general rhythm of this course. The course is entirely project-based with an app being assigned each week and then due the following week. Each project builds on the last to help each engineer learn the practical elements of iOS development and best practices as quickly as possible. We also do a code review for each submitted project once the program begins.

Great to see you were able to complete a large number of optional features to your app already. Your project looks great. As you can probably tell, the optional features available on each project are a great vehicle for exploring the iOS framework deeper.

We'll be following up with you again shortly to outline the next steps in the admissions process.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.