Skip to content

Commit

Permalink
Refactor link checker in search bar
Browse files Browse the repository at this point in the history
  • Loading branch information
BalestraPatrick committed May 10, 2016
1 parent 5aa3379 commit beebf7e
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 28 deletions.
4 changes: 4 additions & 0 deletions RealmVideo.xcodeproj/project.pbxproj
Expand Up @@ -18,6 +18,7 @@
AE5248651CDBB05B003B7454 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = AE5248631CDBB05B003B7454 /* Main.storyboard */; };
AE5248671CDBB05B003B7454 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = AE5248661CDBB05B003B7454 /* Assets.xcassets */; };
AE52486A1CDBB05B003B7454 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = AE5248681CDBB05B003B7454 /* LaunchScreen.storyboard */; };
AE91EF121CE1DA9400EDBB3C /* LinkValidator.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE91EF111CE1DA9400EDBB3C /* LinkValidator.swift */; };
AEB1B9FB1CDC02360037D310 /* WelcomeViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEB1B9FA1CDC02360037D310 /* WelcomeViewController.swift */; };
/* End PBXBuildFile section */

Expand All @@ -35,6 +36,7 @@
AE5248661CDBB05B003B7454 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
AE5248691CDBB05B003B7454 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
AE52486B1CDBB05B003B7454 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
AE91EF111CE1DA9400EDBB3C /* LinkValidator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LinkValidator.swift; sourceTree = "<group>"; };
AEB1B9FA1CDC02360037D310 /* WelcomeViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WelcomeViewController.swift; sourceTree = "<group>"; };
B9390D3F06C240F7CEC986F9 /* Pods-RealmVideo.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RealmVideo.release.xcconfig"; path = "Pods/Target Support Files/Pods-RealmVideo/Pods-RealmVideo.release.xcconfig"; sourceTree = "<group>"; };
CDCCADFBC735B17A3EE39727 /* Pods_RealmVideo.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_RealmVideo.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -90,6 +92,7 @@
isa = PBXGroup;
children = (
AE4854871CE1CB95006ADC0E /* SlidePosition.swift */,
AE91EF111CE1DA9400EDBB3C /* LinkValidator.swift */,
);
name = Utils;
sourceTree = "<group>";
Expand Down Expand Up @@ -270,6 +273,7 @@
files = (
AE01EFB01CDD2D6D00F200D3 /* BrowseVideosViewController.swift in Sources */,
AE01EFB41CDD479300F200D3 /* Video.swift in Sources */,
AE91EF121CE1DA9400EDBB3C /* LinkValidator.swift in Sources */,
AEB1B9FB1CDC02360037D310 /* WelcomeViewController.swift in Sources */,
AE5248621CDBB05B003B7454 /* RealmVideoViewController.swift in Sources */,
AE5248601CDBB05B003B7454 /* AppDelegate.swift in Sources */,
Expand Down
4 changes: 2 additions & 2 deletions RealmVideo/Base.lproj/Main.storyboard
Expand Up @@ -210,7 +210,7 @@
<stackView opaque="NO" contentMode="scaleToFill" distribution="equalSpacing" translatesAutoresizingMaskIntoConstraints="NO" id="dvg-CA-IEL">
<rect key="frame" x="100" y="141" width="400" height="35"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="KD5-DV-lqC">
<button opaque="NO" alpha="0.5" contentMode="scaleToFill" enabled="NO" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="KD5-DV-lqC">
<rect key="frame" x="0.0" y="0.0" width="109" height="35"/>
<color key="backgroundColor" red="0.93333333330000001" green="0.45098039220000002" blue="0.49803921569999998" alpha="1" colorSpace="calibratedRGB"/>
<constraints>
Expand Down Expand Up @@ -322,7 +322,7 @@
<connections>
<outlet property="pasteClipboardButton" destination="RWp-K4-hAi" id="SbT-1b-uRd"/>
<outlet property="scrollView" destination="hrj-AK-bGu" id="fnP-QQ-jhP"/>
<outlet property="startVideo" destination="KD5-DV-lqC" id="ZwM-ki-tN8"/>
<outlet property="startVideoButton" destination="KD5-DV-lqC" id="meN-mo-1Xv"/>
<outlet property="textField" destination="CcW-Oc-KxA" id="FgV-Tu-Cke"/>
</connections>
</viewController>
Expand Down
23 changes: 23 additions & 0 deletions RealmVideo/LinkValidator.swift
@@ -0,0 +1,23 @@
//
// LinkValidator.swift
// RealmVideo
//
// Created by Patrick Balestra on 5/10/16.
// Copyright © 2016 Patrick Balestra. All rights reserved.
//

import Foundation

extension String {

func isValidLink() -> Bool {
let types: NSTextCheckingType = .Link
let detector = try? NSDataDetector(types: types.rawValue)
let stringResults = detector?.firstMatchInString(self, options: [], range: NSMakeRange(0, characters.count))
let pastedResults = detector?.firstMatchInString(self, options: [], range: NSMakeRange(0, characters.count))
if stringResults != nil || pastedResults != nil {
return true
}
return false
}
}
25 changes: 24 additions & 1 deletion RealmVideo/SearchTextField.swift
Expand Up @@ -8,7 +8,15 @@

import UIKit

@IBDesignable class SearchTextField: UITextField {
protocol LinkUpdater: class {
func updateLinkUI(isValid: Bool)
}

@IBDesignable class SearchTextField: UITextField, UITextFieldDelegate {

weak var linkUpdaterDelegate: LinkUpdater?

// MARK: - Initialization

override init(frame: CGRect) {
super.init(frame: frame)
Expand All @@ -25,6 +33,7 @@ import UIKit
layer.borderWidth = 1.0
layer.cornerRadius = 5.0
tintColor = UIColor.whiteColor()
delegate = self

let placeholder = NSAttributedString(string: "realm.io/...", attributes: [NSForegroundColorAttributeName : UIColor(white: 1.0, alpha: 0.5)])
attributedPlaceholder = placeholder
Expand All @@ -42,5 +51,19 @@ import UIKit
text = ""
}

// MARK: - UITextFieldDelegate

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
if let text = textField.text {
linkUpdaterDelegate?.updateLinkUI(text.isValidLink())
}
return true
}

// IBDesignable: Make it look pretty in IB

override func prepareForInterfaceBuilder() {
setUp()
}

}
37 changes: 12 additions & 25 deletions RealmVideo/WelcomeViewController.swift
Expand Up @@ -8,39 +8,23 @@

import UIKit

class WelcomeViewController: UIViewController, UITextFieldDelegate {
class WelcomeViewController: UIViewController, LinkUpdater {

@IBOutlet weak var textField: SearchTextField!
@IBOutlet weak var startVideo: UIButton!
@IBOutlet weak var startVideoButton: UIButton!
@IBOutlet weak var pasteClipboardButton: UIButton!
@IBOutlet weak var scrollView: UIScrollView!

override func viewDidLoad() {
super.viewDidLoad()

textField.delegate = self
textField.becomeFirstResponder()

setValidLink("")
textField.linkUpdaterDelegate = self

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardWillShow), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardWillHide), name: UIKeyboardWillHideNotification, object: nil)
}

func setValidLink(linkString: String) {
let types: NSTextCheckingType = .Link
let detector = try? NSDataDetector(types: types.rawValue)
let stringResults = detector?.firstMatchInString(textField.text!, options: [], range: NSMakeRange(0, textField.text!.characters.count))
let pastedResults = detector?.firstMatchInString(linkString, options: [], range: NSMakeRange(0, linkString.characters.count))

if stringResults != nil || pastedResults != nil {
startVideo.enabled = true
startVideo.alpha = 1.0
} else {
startVideo.enabled = false
startVideo.alpha = 0.5
}
}

// MARK: - Navigation

Expand All @@ -57,15 +41,18 @@ class WelcomeViewController: UIViewController, UITextFieldDelegate {
@IBAction func pasteClipboardButtonPressed(sender: AnyObject) {
if let pasteboardString = UIPasteboard.generalPasteboard().string {
textField.text = pasteboardString
setValidLink(pasteboardString)
updateLinkUI(pasteboardString.isValidLink())
}
}

// MARK: - UITextFieldDelegate

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
setValidLink(string)
return true
func updateLinkUI(isValid: Bool) {
if isValid {
startVideoButton.enabled = true
startVideoButton.alpha = 1.0
} else {
startVideoButton.enabled = false
startVideoButton.alpha = 0.5
}
}

// MARK: - UIKeyboard Notifications
Expand Down

0 comments on commit beebf7e

Please sign in to comment.