Skip to content

Commit

Permalink
Use rating control in the Main ViewController
Browse files Browse the repository at this point in the history
  • Loading branch information
DipanshKhandelwal committed Oct 20, 2019
1 parent fbe0ccb commit eaf1d0f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
18 changes: 6 additions & 12 deletions food-tracker-app/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" alignment="top" spacing="8" translatesAutoresizingMaskIntoConstraints="NO" id="NuG-Lf-rXT">
<rect key="frame" x="20" y="64" width="374" height="534.5"/>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" alignment="center" spacing="8" translatesAutoresizingMaskIntoConstraints="NO" id="NuG-Lf-rXT">
<rect key="frame" x="20" y="64" width="374" height="496.5"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Meal Name" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="uNi-bg-8bh">
<rect key="frame" x="0.0" y="0.0" width="86.5" height="20.5"/>
<rect key="frame" x="144" y="0.0" width="86.5" height="20.5"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
Expand All @@ -29,15 +29,8 @@
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits" returnKeyType="done" enablesReturnKeyAutomatically="YES"/>
</textField>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="cum-gC-Me4">
<rect key="frame" x="0.0" y="70.5" width="151" height="30"/>
<state key="normal" title="Set Default Label Text"/>
<connections>
<action selector="setDefaultLabelText:" destination="BYZ-38-t0r" eventType="touchUpInside" id="tfA-QK-75e"/>
</connections>
</button>
<imageView clipsSubviews="YES" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" placeholderIntrinsicWidth="320" placeholderIntrinsicHeight="320" image="Default" translatesAutoresizingMaskIntoConstraints="NO" id="LLN-ga-dNp">
<rect key="frame" x="0.0" y="108.5" width="374" height="374"/>
<rect key="frame" x="0.0" y="70.5" width="374" height="374"/>
<gestureRecognizers/>
<constraints>
<constraint firstAttribute="width" secondItem="LLN-ga-dNp" secondAttribute="height" multiplier="1:1" id="KWd-OH-XmV"/>
Expand All @@ -47,7 +40,7 @@
</connections>
</imageView>
<stackView opaque="NO" contentMode="scaleToFill" spacing="8" translatesAutoresizingMaskIntoConstraints="NO" id="RbP-JF-Oo7" customClass="RatingControl" customModule="food_tracker_app" customModuleProvider="target">
<rect key="frame" x="0.0" y="490.5" width="252" height="44"/>
<rect key="frame" x="61" y="452.5" width="252" height="44"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="number" keyPath="starCount">
<integer key="value" value="5"/>
Expand All @@ -74,6 +67,7 @@
<outlet property="mealNameLabel" destination="uNi-bg-8bh" id="5rP-pi-mYt"/>
<outlet property="nameTextField" destination="twI-5p-iym" id="ego-jW-u8a"/>
<outlet property="photoImageView" destination="LLN-ga-dNp" id="C40-Mp-m7L"/>
<outlet property="ratingControl" destination="RbP-JF-Oo7" id="P5W-zb-dVA"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
Expand Down
28 changes: 27 additions & 1 deletion food-tracker-app/RatingControl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ import UIKit
let highlightedStar = UIImage(named:"highlightedStar", in: bundle, compatibleWith: self.traitCollection)

// add new buttons
for _ in 0..<starCount {
for index in 0..<starCount {
// Create the button
let button = UIButton()
// Set the button images
Expand All @@ -91,6 +91,9 @@ import UIKit
button.heightAnchor.constraint(equalToConstant: starSize.height).isActive = true
button.widthAnchor.constraint(equalToConstant: starSize.width).isActive = true

// Set the accessibility label
button.accessibilityLabel = "Set \(index + 1) star rating"

// Setup the button action
button.addTarget(self, action: #selector(RatingControl.ratingButtonTapped(button:)), for: .touchUpInside)

Expand All @@ -108,6 +111,29 @@ import UIKit
for (index, button) in ratingButtons.enumerated() {
// If the index of a button is less than the rating, that button should be selected.
button.isSelected = index < rating

// Set the hint string for the currently selected star
let hintString: String?
if rating == index + 1 {
hintString = "Tap to reset the rating to zero."
} else {
hintString = nil
}

// Calculate the value string
let valueString: String
switch (rating) {
case 0:
valueString = "No rating set."
case 1:
valueString = "1 star set."
default:
valueString = "\(rating) stars set."
}

// Assign the hint string and value string
button.accessibilityHint = hintString
button.accessibilityValue = valueString
}
}

Expand Down
4 changes: 1 addition & 3 deletions food-tracker-app/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class ViewController: UIViewController, UITextFieldDelegate, UIImagePickerContro
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var mealNameLabel: UILabel!
@IBOutlet weak var photoImageView: UIImageView!
@IBOutlet weak var ratingControl: RatingControl!

override func viewDidLoad() {
super.viewDidLoad()
Expand Down Expand Up @@ -53,9 +54,6 @@ class ViewController: UIViewController, UITextFieldDelegate, UIImagePickerContro
}

//MARK: Actions
@IBAction func setDefaultLabelText(_ sender: UIButton) {
mealNameLabel.text = "Default Text"
}

@IBAction func selectImageFromPhotoLibrary(_ sender: UITapGestureRecognizer) {
// Hide the keyboard.
Expand Down

0 comments on commit eaf1d0f

Please sign in to comment.