Skip to content

Commit

Permalink
Add SwiftLint and update source code to match rules
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Pichler committed Mar 28, 2018
1 parent eb7bc50 commit d6f985d
Show file tree
Hide file tree
Showing 13 changed files with 196 additions and 138 deletions.
20 changes: 20 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs
# editorconfig.org

root = true

[*]
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
indent_style = space
indent_size = 4

[{*.{yaml,yml},}]
indent_style = space
indent_size = 2

[*.{diff,md}]
trim_trailing_whitespace = false
8 changes: 8 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
excluded:
- Carthage
# line_length: 120
disabled_rules:
- todo
- line_length
- type_name
- switch_case_alignment
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"editor.quickSuggestions": false
}
20 changes: 20 additions & 0 deletions Audiobook Player.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@
41B2AC8D1D43CCE8005382A9 /* ChaptersViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ChaptersViewController.swift; sourceTree = "<group>"; };
41B2AC8F1D43CD2F005382A9 /* SpeedViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SpeedViewController.swift; sourceTree = "<group>"; };
41B2AC911D441627005382A9 /* MoreViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MoreViewController.swift; sourceTree = "<group>"; };
C35BADE7206989E6007687C1 /* LICENSE */ = {isa = PBXFileReference; lastKnownFileType = text; path = LICENSE; sourceTree = "<group>"; };
C35BADE8206989E6007687C1 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
C35BADE9206989E6007687C1 /* .swiftlint.yml */ = {isa = PBXFileReference; lastKnownFileType = text; path = .swiftlint.yml; sourceTree = "<group>"; };
D367F7671FA2A6F000FEDB37 /* Constants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -117,6 +120,9 @@
418B6CEF1D2707F700F974FB = {
isa = PBXGroup;
children = (
C35BADE8206989E6007687C1 /* README.md */,
C35BADE7206989E6007687C1 /* LICENSE */,
C35BADE9206989E6007687C1 /* .swiftlint.yml */,
418B6CFA1D2707F800F974FB /* Audiobook Player */,
418B6D111D2707F800F974FB /* Audiobook PlayerTests */,
418B6CF91D2707F800F974FB /* Products */,
Expand Down Expand Up @@ -197,6 +203,7 @@
isa = PBXNativeTarget;
buildConfigurationList = 418B6D171D2707F800F974FB /* Build configuration list for PBXNativeTarget "Audiobook Player" */;
buildPhases = (
C35BADEA20698A6D007687C1 /* ShellScript */,
418B6CF41D2707F800F974FB /* Sources */,
418B6CF51D2707F800F974FB /* Frameworks */,
418B6CF61D2707F800F974FB /* Resources */,
Expand Down Expand Up @@ -333,6 +340,19 @@
shellPath = /bin/sh;
shellScript = "/usr/local/bin/carthage copy-frameworks";
};
C35BADEA20698A6D007687C1 /* ShellScript */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "if which swiftlint >/dev/null; then\n swiftlint\nelse\n echo \"warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint\"\nfi";
};
/* End PBXShellScriptBuildPhase section */

/* Begin PBXSourcesBuildPhase section */
Expand Down
46 changes: 23 additions & 23 deletions Audiobook Player/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//
// AppDelegate.swift
// Audiobook Player
// AppDelegate.swift
// Audiobook Player
//
// Created by Gianni Carlo on 7/1/16.
// Copyright © 2016 Tortuga Power. All rights reserved.
// Created by Gianni Carlo on 7/1/16.
// Copyright © 2016 Tortuga Power. All rights reserved.
//

import UIKit
Expand All @@ -13,54 +13,54 @@ import Crashlytics

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
Fabric.with([Crashlytics.self])
let defaults:UserDefaults = UserDefaults.standard


let defaults: UserDefaults = UserDefaults.standard

// Perfrom first launch setup
if !defaults.bool(forKey: UserDefaultsConstants.completedFirstLaunch) {
// Set default settings
defaults.set(true, forKey: UserDefaultsConstants.smartRewindEnabled)

defaults.set(true, forKey: UserDefaultsConstants.completedFirstLaunch)
}

UIApplication.shared.statusBarStyle = .lightContent

try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
//clean leftover sleep timer registry

// clean leftover sleep timer registry
UserDefaults.standard.set(nil, forKey: "sleep_timer")

return true
}

func application(_ app: UIApplication,
open url: URL,
options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
open url: URL,
options: [UIApplicationOpenURLOptionsKey: Any] = [:]) -> Bool {
// This function is called when the app is opened with a audio file url,
// like when receiving files through AirDrop

let fmanager = FileManager.default

let fmanager = FileManager.default
let filename = url.lastPathComponent
let documentsURL = fmanager.urls(for: .documentDirectory, in: .userDomainMask).first!
let destinationURL = documentsURL.appendingPathComponent(filename)

// move file from Inbox to Document folder
do {
try fmanager.moveItem(at: url, to: destinationURL)
//In case the app was already running in background
// In case the app was already running in background
NotificationCenter.default.post(name: Notification.Name.AudiobookPlayer.openURL, object: nil)
} catch {
// TODO: How should this case be handled?
try! fmanager.removeItem(at: url)

return false
}

return true
}

Expand All @@ -80,19 +80,19 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDele

func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

// Check if the app is on the PlayerViewController
guard let navigationVC = UIApplication.shared.keyWindow?.rootViewController!,
navigationVC.childViewControllers.count > 1 else{
navigationVC.childViewControllers.count > 1 else {

return
}

// Notify controller to see if it should ask for review
NotificationCenter.default.post(name: Notification.Name.AudiobookPlayer.requestReview, object: nil)
}

func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

}
51 changes: 25 additions & 26 deletions Audiobook Player/ChaptersViewController.swift
Original file line number Diff line number Diff line change
@@ -1,49 +1,48 @@
//
// ChaptersViewController.swift
// Audiobook Player
// ChaptersViewController.swift
// Audiobook Player
//
// Created by Gianni Carlo on 7/23/16.
// Copyright © 2016 Tortuga Power. All rights reserved.
// Created by Gianni Carlo on 7/23/16.
// Copyright © 2016 Tortuga Power. All rights reserved.
//

import UIKit
import MediaPlayer
import DeckTransition

struct Chapter {
var title:String
var start:Int
var duration:Int
var index:Int
var title: String
var start: Int
var duration: Int
var index: Int
}

class ChaptersViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var vfxBackgroundView: UIVisualEffectView!
var chapterArray:[Chapter]!
var currentChapter:Chapter!

var chapterArray: [Chapter]!
var currentChapter: Chapter!

override func viewDidLoad() {
super.viewDidLoad()
self.vfxBackgroundView.effect = UIBlurEffect(style: .light)

self.currentChapter = PlayerManager.sharedInstance.currentChapter
self.chapterArray = PlayerManager.sharedInstance.chapterArray

self.tableView.tableFooterView = UIView()
self.tableView.reloadData()
}
override var prefersStatusBarHidden : Bool {

override var prefersStatusBarHidden: Bool {
return true
}
override var preferredStatusBarUpdateAnimation : UIStatusBarAnimation {

override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
return .slide
}



@IBAction func didPressClose(_ sender: UIBarButtonItem) {
self.dismiss(animated: true, completion: nil)
}
Expand All @@ -53,38 +52,39 @@ extension ChaptersViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return chapterArray.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ChapterViewCell", for: indexPath) as! ChapterViewCell
let chapter = self.chapterArray[indexPath.row]

cell.titleLabel.text = chapter.title
cell.durationLabel.text = formatTime(chapter.start)
cell.titleLabel.highlightedTextColor = UIColor.black
cell.durationLabel.highlightedTextColor = UIColor.black

if self.currentChapter.index == chapter.index {
tableView.selectRow(at: indexPath, animated: true, scrollPosition: .top)
}

return cell
}
}

extension ChaptersViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let chapter = self.chapterArray[indexPath.row]

self.currentChapter = chapter
self.performSegue(withIdentifier: "selectedChapterSegue", sender: self)
}
}

/**
* Temporal fix to stop dismiss when scrolling up on a long list
* View README.md -> https://github.com/HarshilShah/DeckTransition
* View README.md -> https:// github.com/HarshilShah/DeckTransition
*/
extension ChaptersViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {

if let delegate = transitioningDelegate as? DeckTransitioningDelegate {
if scrollView.contentOffset.y > 0 {
// Normal behaviour if the `scrollView` isn't scrolled to the top
Expand Down Expand Up @@ -113,5 +113,4 @@ extension ChaptersViewController: UIScrollViewDelegate {
class ChapterViewCell: UITableViewCell {
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var durationLabel: UILabel!

}
42 changes: 20 additions & 22 deletions Audiobook Player/Extensions.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//
// Extensions.swift
// Audiobook Player
// Extensions.swift
// Audiobook Player
//
// Created by Gianni Carlo on 3/10/17.
// Copyright © 2017 Tortuga Power. All rights reserved.
// Created by Gianni Carlo on 3/10/17.
// Copyright © 2017 Tortuga Power. All rights reserved.
//

import UIKit
Expand All @@ -13,34 +13,34 @@ extension UIViewController {
func showAlert(_ title: String?, message: String?, style: UIAlertControllerStyle) {
let alert = UIAlertController(title: title, message: message, preferredStyle: style)
let okButton = UIAlertAction(title: "Ok", style: .default, handler: nil)

alert.addAction(okButton)

alert.popoverPresentationController?.sourceView = self.view
alert.popoverPresentationController?.sourceRect = CGRect(x: Double(self.view.bounds.size.width / 2.0), y: Double(self.view.bounds.size.height-45), width: 1.0, height: 1.0)

self.present(alert, animated: true, completion: nil)
}
//utility function to transform seconds to format HH:MM:SS
func formatTime(_ time:Int) -> String {

// utility function to transform seconds to format HH:MM:SS
func formatTime(_ time: Int) -> String {
let hours = Int(time / 3600)

let remaining = Float(time - (hours * 3600))

let minutes = Int(remaining / 60)

let seconds = Int(remaining - Float(minutes * 60))
var formattedTime = String(format:"%02d:%02d", minutes, seconds)

var formattedTime = String(format: "%02d:%02d", minutes, seconds)
if hours > 0 {
formattedTime = String(format:"%02d:"+formattedTime, hours)
formattedTime = String(format: "%02d:" + formattedTime, hours)
}

return formattedTime
}
func presentModal(_ viewController:UIViewController, animated:Bool, completion: (() -> Swift.Void)? = nil){

func presentModal(_ viewController: UIViewController, animated: Bool, completion: (() -> Swift.Void)? = nil) {
let transitionDelegate = DeckTransitioningDelegate()
viewController.transitioningDelegate = transitionDelegate
viewController.modalPresentationStyle = .custom
Expand All @@ -49,13 +49,12 @@ extension UIViewController {
}

extension UINavigationController {
override open var preferredStatusBarStyle : UIStatusBarStyle {
override open var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}

extension Notification.Name {

public struct AudiobookPlayer {
public static let openURL = Notification.Name(rawValue: "com.tortugapower.audiobookplayer.openurl")
public static let requestReview = Notification.Name(rawValue: "com.tortugapower.audiobookplayer.requestreview")
Expand All @@ -70,4 +69,3 @@ extension Notification.Name {
public static let bookChange = Notification.Name(rawValue: "com.tortugapower.audiobookplayer.book.change")
}
}

Loading

0 comments on commit d6f985d

Please sign in to comment.