Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Activity Indicator Demo: Dynamic start/stop activity button title #1958

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class ActivityIndicatorDemoController: DemoTableViewController {
return UITableViewCell()
}

cell.setup(action1Title: row.title)
cell.setup(action1Title: isAnimating ? "Stop Activity" : "Start Activity")
cell.action1Button.addTarget(self,
action: #selector(startStopActivity),
for: .touchUpInside)
Expand Down Expand Up @@ -191,7 +191,7 @@ class ActivityIndicatorDemoController: DemoTableViewController {
case .hidesWhenStopped:
return "Hides when stopped"
case .startStopActivity:
return "Start / Stop activity"
return ""
case .demoOfSize:
return ""
}
Expand Down Expand Up @@ -235,6 +235,13 @@ class ActivityIndicatorDemoController: DemoTableViewController {

@objc private func startStopActivity() {
isAnimating.toggle()

guard let section: Int = ActivityIndicatorDemoSection.allCases.firstIndex(of: .settings),
let row: Int = ActivityIndicatorDemoSection.settings.rows.firstIndex(of: .startStopActivity) else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: we avoid declaring types for inline variables unless strictly needed. See the specific reference in our style guide at Type Inference | swift-guide (microsoft.github.io)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the linked guide, it would still make sense to declare types in such cases. Here Int is not directly used as literal. It is the result of a function firstIndex on another type. So for quick understanding, I think it is helpful to have the type Int specified here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The guide is consistent on this point:

Use type annotation as little as possible when declaring a constant or a variable; instead let the compiler infer the variable type.

Let the compiler infer the type of a constant or variable whenever possible.

The primary exception to this policy is for stored properties; local variables should almost never need a type declared, particularly when they are storing the result of a well-understood method like firstIndex.

return
}
// Reloading row to update the button title
tableView.reloadRows(at: [.init(row: row, section: section)], with: .automatic)
}
}

Expand Down