Skip to content

Commit

Permalink
Enforce swiftformat on quickstarts (#1208)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulb777 authored Jul 2, 2021
1 parent bd2eb9d commit 70e424c
Show file tree
Hide file tree
Showing 118 changed files with 1,864 additions and 1,516 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: check

on:
pull_request:
push:
branches: master

jobs:
check:
runs-on: macos-latest
env:
MINT_PATH: ${{ github.workspace }}/mint
steps:
- uses: actions/checkout@v2

- name: Cache Mint packages
uses: actions/cache@v1
with:
path: ${{ env.MINT_PATH }}
key: ${{ runner.os }}-mint-${{ hashFiles('**/Mintfile') }}
restore-keys: ${{ runner.os }}-mint-

- name: Install mint
run: brew install mint

- name: Bootstrap mint with swiftformat
run: mint bootstrap

- name: Check
run: scripts/style.sh test-only
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ xcuserdata/
GoogleService-Info.plist
Secrets
Secrets.tar

# Mint package manager
Mint
1 change: 1 addition & 0 deletions Mintfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nicklockwood/[email protected]
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ To add a sample app to a Firebase project, use the bundleID from the Xcode proje
Download the generated `GoogleService-Info.plist` file, and copy it to the root
directory of the sample you wish to run.

### Code Formatting

To ensure that the code is formatted consistently, run the script
[./scripts/style.sh](https://github.com/firebase/quickstart-ios/blob/master/scripts/style.sh)
before creating a PR.

GitHub Actions will verify that any code changes are done in a style compliant
way. Install `mint` and `swiftformat`:

```console
brew install mint
mint bootstrap
./scripts/style.sh
```

- [A/B Testing](abtesting/README.md) ![build](https://github.com/firebase/quickstart-ios/actions/workflows/abtesting.yml/badge.svg)
- [Admob](admob/README.md) ![build](https://github.com/firebase/quickstart-ios/actions/workflows/admob.yml/badge.svg)
- [Analytics](analytics/README.md) ![build](https://github.com/firebase/quickstart-ios/actions/workflows/analytics.yml/badge.svg)
Expand Down
5 changes: 3 additions & 2 deletions abtesting/ABTestingExample/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication
.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
return true
}
Expand Down
49 changes: 23 additions & 26 deletions abtesting/ABTestingExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ extension RemoteConfigFetchStatus {
}

class ViewController: UIViewController, UITableViewDataSource {

@IBOutlet var tableView: UITableView!

var colorScheme: ColorScheme = .light {
Expand All @@ -49,28 +48,28 @@ class ViewController: UIViewController, UITableViewDataSource {

override func viewDidLoad() {
super.viewDidLoad()
self.tableView.dataSource = self
self.tableView.tableFooterView = UIView()
tableView.dataSource = self
tableView.tableFooterView = UIView()

#if DEBUG
NotificationCenter.default.addObserver(self,
selector: #selector(printInstallationsID),
name: .InstallationIDDidChange,
object: nil)
NotificationCenter.default.addObserver(self,
selector: #selector(printInstallationsID),
name: .InstallationIDDidChange,
object: nil)
#endif
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
Installations.installations().authTokenForcingRefresh(true) { (token, error) in
Installations.installations().authTokenForcingRefresh(true) { token, error in
if let error = error {
print("Error fetching token: \(error)")
return
}
guard let token = token else { return }
print("Installation auth token: \(token.authToken)")
}
RemoteConfig.remoteConfig().fetch(withExpirationDuration: 0) { (status, error) in
RemoteConfig.remoteConfig().fetch(withExpirationDuration: 0) { status, error in
if let error = error {
print("Error fetching config: \(error)")
}
Expand All @@ -85,7 +84,7 @@ class ViewController: UIViewController, UITableViewDataSource {
}

func setAppearance() {
RemoteConfig.remoteConfig().activate() { activated, error in
RemoteConfig.remoteConfig().activate { activated, error in
let configValue = RemoteConfig.remoteConfig()["color_scheme"]
print("Config value: \(configValue.stringValue ?? "null")")
DispatchQueue.main.async {
Expand All @@ -100,13 +99,13 @@ class ViewController: UIViewController, UITableViewDataSource {

@objc func printInstallationsID() {
#if DEBUG
Installations.installations().installationID { (identifier, error) in
if let error = error {
print("Error fetching installations ID: \(error)")
} else if let identifier = identifier {
print("Remote installations ID: \(identifier)")
Installations.installations().installationID { identifier, error in
if let error = error {
print("Error fetching installations ID: \(error)")
} else if let identifier = identifier {
print("Remote installations ID: \(identifier)")
}
}
}
#endif
}

Expand All @@ -118,12 +117,11 @@ class ViewController: UIViewController, UITableViewDataSource {

func switchToColorScheme(_ scheme: ColorScheme) {
switch scheme {

case .light:
navigationController?.navigationBar.barTintColor = ViewController.lightColors.primary
navigationController?.navigationBar.barStyle = .`default`
navigationController?.navigationBar.barStyle = .default
navigationController?.navigationBar.titleTextAttributes = [
NSAttributedString.Key.foregroundColor: UIColor.black
NSAttributedString.Key.foregroundColor: UIColor.black,
]
tableView.separatorColor = .gray
tableView.backgroundColor = UIColor(red: 0.94, green: 0.94, blue: 0.94, alpha: 1)
Expand All @@ -132,7 +130,7 @@ class ViewController: UIViewController, UITableViewDataSource {
navigationController?.navigationBar.barTintColor = ViewController.darkColors.primary
navigationController?.navigationBar.barStyle = .black
navigationController?.navigationBar.titleTextAttributes = [
NSAttributedString.Key.foregroundColor: UIColor.white
NSAttributedString.Key.foregroundColor: UIColor.white,
]
tableView.separatorColor = .lightGray
tableView.backgroundColor = ViewController.darkColors.secondary
Expand All @@ -142,13 +140,13 @@ class ViewController: UIViewController, UITableViewDataSource {
}

static let darkColors = (
primary: UIColor(red: 0x61 / 0xff, green: 0x61 / 0xff, blue: 0x61 / 0xff, alpha: 1),
secondary: UIColor(red: 0x42 / 0xff, green: 0x42 / 0xff, blue: 0x42 / 0xff, alpha: 1)
primary: UIColor(red: 0x61 / 0xFF, green: 0x61 / 0xFF, blue: 0x61 / 0xFF, alpha: 1),
secondary: UIColor(red: 0x42 / 0xFF, green: 0x42 / 0xFF, blue: 0x42 / 0xFF, alpha: 1)
)

static let lightColors = (
primary: UIColor(red: 0xff / 0xff, green: 0xc1 / 0xff, blue: 0x07 / 0xff, alpha: 1),
secondary: UIColor(red: 0xff / 0xff, green: 0xc1 / 0xff, blue: 0x07 / 0xff, alpha: 1)
primary: UIColor(red: 0xFF / 0xFF, green: 0xC1 / 0xFF, blue: 0x07 / 0xFF, alpha: 1),
secondary: UIColor(red: 0xFF / 0xFF, green: 0xC1 / 0xFF, blue: 0x07 / 0xFF, alpha: 1)
)

// MARK: - UITableViewDataSource
Expand All @@ -157,7 +155,7 @@ class ViewController: UIViewController, UITableViewDataSource {
("Getting Started with Firebase", "An Introduction to Firebase"),
("Google Firestore", "Powerful Querying and Automatic Scaling"),
("Analytics", "Simple App Insights"),
("Remote Config", "Parameterize App Behavior")
("Remote Config", "Parameterize App Behavior"),
]

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
Expand All @@ -183,5 +181,4 @@ class ViewController: UIViewController, UITableViewDataSource {

return cell
}

}
3 changes: 1 addition & 2 deletions abtesting/ABTestingExampleTests/ABTestingExampleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@
import XCTest
@testable import ABTestingExample

class ABTestingExampleTests: XCTestCase {
}
class ABTestingExampleTests: XCTestCase {}
28 changes: 13 additions & 15 deletions abtesting/ABTestingExampleUITests/ABTestingExampleUITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,17 @@
import XCTest

class ABTestingExampleUITests: XCTestCase {

override func setUp() {
super.setUp()
XCUIApplication().launch()
}

override func tearDown() {
super.tearDown()
}

func testExample() {
XCTAssertTrue(XCUIApplication().navigationBars["Firenotes"].exists,
"Firenotes is missing from the navigation bar")
}

override func setUp() {
super.setUp()
XCUIApplication().launch()
}

override func tearDown() {
super.tearDown()
}

func testExample() {
XCTAssertTrue(XCUIApplication().navigationBars["Firenotes"].exists,
"Firenotes is missing from the navigation bar")
}
}
8 changes: 5 additions & 3 deletions admob/AdMobExampleSwift/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@ import GoogleMobileAds

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

internal func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
internal func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication
.LaunchOptionsKey: Any]?) -> Bool {
// Use Firebase library to configure APIs
FirebaseApp.configure()
// Initialize the Google Mobile Ads SDK.
GADMobileAds.sharedInstance().start(completionHandler: nil)
return true
}
// [END firebase_config]

// [END firebase_config]
}
14 changes: 8 additions & 6 deletions admob/AdMobExampleSwift/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ let kInterstitialAdUnitID = "ca-app-pub-3940256099942544/4411468910"
// Makes ViewController available to Objc classes.
@objc(ViewController)
class ViewController: UIViewController, GADFullScreenContentDelegate {
@IBOutlet weak var bannerView: GADBannerView!
@IBOutlet var bannerView: GADBannerView!
var interstitial: GADInterstitialAd?
@IBOutlet weak var interstitialButton: UIButton!
@IBOutlet var interstitialButton: UIButton!

override func viewDidLoad() {
super.viewDidLoad()
Expand All @@ -52,7 +52,8 @@ class ViewController: UIViewController, GADFullScreenContentDelegate {
func createAndLoadInterstitial() {
GADInterstitialAd.load(
withAdUnitID: kInterstitialAdUnitID,
request: GADRequest()) { (ad, error) in
request: GADRequest()
) { ad, error in
if let error = error {
// For more fine-grained error handling, take a look at the values in GADErrorCode.
print("Error loading ad: \(error)")
Expand All @@ -67,13 +68,14 @@ class ViewController: UIViewController, GADFullScreenContentDelegate {
createAndLoadInterstitial()
}

func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
func ad(_ ad: GADFullScreenPresentingAd,
didFailToPresentFullScreenContentWithError error: Error) {
print("Error presenting ad: \(error)")
}

@IBAction func didTapInterstitialButton(_ sender: AnyObject) {
self.interstitial?.present(fromRootViewController: self)
interstitial?.present(fromRootViewController: self)
}

}

// [END firebase_interstitial_example]
7 changes: 3 additions & 4 deletions admob/AdMobExampleSwiftTests/AdMobExampleSwiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ import XCTest
@testable import AdMobExampleSwift

class AdMobExampleSwiftTests: XCTestCase {

// Important: create a variable for the view controller outside of setup(). Otherwise the variable is not available to the test functions
var vcToTest: ViewController!

override func setUp() {
super.setUp()
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
guard let vc = storyboard.instantiateViewController(withIdentifier: "ViewController") as? ViewController else {
guard let vc = storyboard
.instantiateViewController(withIdentifier: "ViewController") as? ViewController else {
return
}
vcToTest = vc
let _ = vcToTest.view
_ = vcToTest.view
}

func testBannerView() {
Expand All @@ -45,5 +45,4 @@ class AdMobExampleSwiftTests: XCTestCase {
func testInterstitial() {
XCTAssertNotNil(vcToTest.interstitial, "Should create interstitial.")
}

}
32 changes: 15 additions & 17 deletions admob/AdMobExampleSwiftUITests/AdMobExampleSwiftUITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,24 @@
import XCTest

class AdMobExampleSwiftUITests: XCTestCase {
override func setUp() {
// Put setup code here. This method is called before the invocation of each test method in the class.

override func setUp() {
// Put setup code here. This method is called before the invocation of each test method in the class.
// In UI tests it is usually best to stop immediately when a failure occurs.
continueAfterFailure = false

// In UI tests it is usually best to stop immediately when a failure occurs.
continueAfterFailure = false
// UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.
XCUIApplication().launch()

// UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.
XCUIApplication().launch()
// In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
}

// In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
}

override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}

func testExample() {
// Use recording to get started writing UI tests.
// Use XCTAssert and related functions to verify your tests produce the correct results.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}

func testExample() {
// Use recording to get started writing UI tests.
// Use XCTAssert and related functions to verify your tests produce the correct results.
}
}
2 changes: 1 addition & 1 deletion analytics/AnalyticsExample/AnalyticsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ final class AnalyticsView: UIScrollView {

let backgroundLabel = buildLabel(
text: "Set user properties when a user \nselects their favorite season ↑ or \n" +
"preferred temperature units ↓",
"preferred temperature units ↓",
textColor: .secondaryLabel, numberOfLines: 3, textAlignment: .center
)
addSubview(backgroundLabel)
Expand Down
Loading

1 comment on commit 70e424c

@patooworld
Copy link

Choose a reason for hiding this comment

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

brew install mint
mint bootstrap
./scripts/style.sh

Please sign in to comment.