Skip to content

Commit a98f3d5

Browse files
Merge pull request #51 from zats/twitter-handler
Twitter handler
2 parents 92d7ddc + f3dc804 commit a98f3d5

11 files changed

+88
-55
lines changed

trySwift.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
499CCFF21CC2E0F4007A5BBB /* UIViewControllerExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 499CCFF11CC2E0F4007A5BBB /* UIViewControllerExtension.swift */; };
2828
49F7B2811E8475F900F09768 /* SplitViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49F7B2801E8475F900F09768 /* SplitViewController.swift */; };
2929
4D498DB1B98D4B496FDBB7AA /* Pods_try__Extension.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CD2E9D5733A6E1B6E10AFEEF /* Pods_try__Extension.framework */; };
30+
5CDB20792048952A00C3E0D3 /* TwitterFollowDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5CDB20782048952A00C3E0D3 /* TwitterFollowDelegate.swift */; };
3031
7AD1E19C80B78BB08E3DF079 /* Pods_trySwift.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 620220ABCCCDF47FDAF48B67 /* Pods_trySwift.framework */; };
3132
EAFE1C26E49EFABF83487BDC /* Pods_try__Today.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 24718BF11477753FCD47A7A8 /* Pods_try__Today.framework */; };
3233
FA0E2B4F1E63B90400B40814 /* SessionDetailInterfaceController.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA0E2B4E1E63B90400B40814 /* SessionDetailInterfaceController.swift */; };
@@ -193,6 +194,7 @@
193194
499CCFF11CC2E0F4007A5BBB /* UIViewControllerExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIViewControllerExtension.swift; sourceTree = "<group>"; };
194195
49F7B2801E8475F900F09768 /* SplitViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SplitViewController.swift; sourceTree = "<group>"; };
195196
58F81AD508535BD405F98215 /* AVFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AVFoundation.framework; path = System/Library/Frameworks/AVFoundation.framework; sourceTree = SDKROOT; };
197+
5CDB20782048952A00C3E0D3 /* TwitterFollowDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TwitterFollowDelegate.swift; sourceTree = "<group>"; };
196198
5FA07FBC036240AEF8B7C739 /* Pods-trySwift.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-trySwift.release.xcconfig"; path = "Pods/Target Support Files/Pods-trySwift/Pods-trySwift.release.xcconfig"; sourceTree = "<group>"; };
197199
620220ABCCCDF47FDAF48B67 /* Pods_trySwift.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_trySwift.framework; sourceTree = BUILT_PRODUCTS_DIR; };
198200
6C1F9C27BCD8099DCA758F7A /* CoreMedia.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreMedia.framework; path = System/Library/Frameworks/CoreMedia.framework; sourceTree = SDKROOT; };
@@ -381,6 +383,14 @@
381383
name = Frameworks;
382384
sourceTree = "<group>";
383385
};
386+
5CDB20772048951F00C3E0D3 /* Twitter */ = {
387+
isa = PBXGroup;
388+
children = (
389+
5CDB20782048952A00C3E0D3 /* TwitterFollowDelegate.swift */,
390+
);
391+
path = Twitter;
392+
sourceTree = "<group>";
393+
};
384394
F413680C9BA29129B4319377 /* Pods */ = {
385395
isa = PBXGroup;
386396
children = (
@@ -449,6 +459,7 @@
449459
FA39E8CB1C6AF89D0074B6BE /* Utilities */ = {
450460
isa = PBXGroup;
451461
children = (
462+
5CDB20772048951F00C3E0D3 /* Twitter */,
452463
FA39E8ED1C6C034B0074B6BE /* UIColorExtension.swift */,
453464
499CCFF11CC2E0F4007A5BBB /* UIViewControllerExtension.swift */,
454465
FA3A1CB51D68E1950042F8DD /* WatchSessionManager.swift */,
@@ -1123,6 +1134,7 @@
11231134
isa = PBXSourcesBuildPhase;
11241135
buildActionMask = 2147483647;
11251136
files = (
1137+
5CDB20792048952A00C3E0D3 /* TwitterFollowDelegate.swift in Sources */,
11261138
499BD62A1D05910200E74061 /* Twitter.swift in Sources */,
11271139
FA39E90F1C6C81870074B6BE /* SponsorsViewController.swift in Sources */,
11281140
FA36B7071E5DA3970022E6A9 /* DateFormatterExtension.swift in Sources */,

trySwift/OfficeHoursDetailViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ extension OfficeHoursDetailViewController {
4747
return cell
4848
case .speakerInfo:
4949
let cell = tableView.dequeueReusableCell(forIndexPath: indexPath) as SpeakerTableViewCell
50-
cell.configure(withSpeaker: speaker, selectionEnabled: false, accessoryEnabled: false)
50+
cell.configure(withSpeaker: speaker, selectionEnabled: false, accessoryEnabled: false, delegate: self)
5151
return cell
5252
case .bio:
5353
let cell = tableView.dequeueReusableCell(forIndexPath: indexPath) as TextTableViewCell

trySwift/SessionDetailsViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ extension SessionDetailsViewController {
4949
return cell
5050
case .speakerInfo:
5151
let cell = tableView.dequeueReusableCell(forIndexPath: indexPath) as SpeakerTableViewCell
52-
cell.configure(withSpeaker: presentation.speaker!, selectionEnabled: false, accessoryEnabled: false)
52+
cell.configure(withSpeaker: presentation.speaker!, selectionEnabled: false, accessoryEnabled: false, delegate: self)
5353
return cell
5454
case .summary:
5555
let cell = tableView.dequeueReusableCell(forIndexPath: indexPath) as TextTableViewCell

trySwift/SpeakerDetailViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ extension SpeakerDetailViewController {
4343
switch SpeakerDetail(rawValue: indexPath.row)! {
4444
case .header:
4545
let cell = tableView.dequeueReusableCell(forIndexPath: indexPath) as SpeakerTableViewCell
46-
cell.configure(withSpeaker: speaker, selectionEnabled: false, accessoryEnabled: false)
46+
cell.configure(withSpeaker: speaker, selectionEnabled: false, accessoryEnabled: false, delegate: self)
4747
return cell
4848
case .bio:
4949
let cell = tableView.dequeueReusableCell(forIndexPath: indexPath) as TextTableViewCell

trySwift/SpeakerTableViewCell.swift

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,31 @@ class SpeakerTableViewCell: UITableViewCell {
1414

1515
@IBOutlet weak var speakerImageView: UIImageView!
1616
@IBOutlet weak var speakerNameLabel: UILabel!
17-
@IBOutlet weak var speakerTwitterLabel: UILabel!
18-
17+
@IBOutlet weak var speakerTwitterButton: UIButton!
18+
19+
fileprivate var speaker: Speaker?
20+
fileprivate weak var delegate: TwitterFollowDelegate?
21+
1922
override func awakeFromNib() {
2023
super.awakeFromNib()
21-
22-
speakerTwitterLabel.textColor = .trySwiftAccentColor()
24+
speakerTwitterButton.setTitleColor(.trySwiftAccentColor(), for: .normal)
2325
}
2426

25-
func configure(withSpeaker speaker: Speaker, selectionEnabled: Bool = true, accessoryEnabled: Bool = true) {
2627

28+
@IBAction func speakerTwitterButtonDidTap(_ sender: Any) {
29+
guard let speaker = speaker else {
30+
assertionFailure("Speaker is not set")
31+
return
32+
}
33+
self.delegate?.followUser(speaker.twitter)
34+
}
35+
36+
func configure(withSpeaker speaker: Speaker, selectionEnabled: Bool = true, accessoryEnabled: Bool = true, delegate: TwitterFollowDelegate) {
2737
let scale = UIScreen.main.scale
2838
let processor = RoundCornerImageProcessor(cornerRadius: 34, targetSize: CGSize(width: 67, height: 67))
2939
speakerImageView.kf.setImage(with: speaker.imageURL, placeholder: nil, options: [.processor(processor), .scaleFactor(scale)])
3040
speakerNameLabel.text = speaker.localizedName
31-
speakerTwitterLabel.text = "@\(speaker.twitter)"
32-
41+
speakerTwitterButton.setTitle("@\(speaker.twitter)", for: .normal)
3342
if !selectionEnabled {
3443
selectionStyle = .none
3544
}
@@ -40,5 +49,8 @@ class SpeakerTableViewCell: UITableViewCell {
4049

4150
setNeedsUpdateConstraints()
4251
layoutIfNeeded()
52+
53+
self.speaker = speaker
54+
self.delegate = delegate
4355
}
4456
}

trySwift/SpeakerTableViewCell.xib

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="11762" systemVersion="16D32" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="13771" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
33
<device id="retina4_7" orientation="portrait">
44
<adaptation id="fullscreen"/>
55
</device>
66
<dependencies>
7-
<deployment identifier="iOS"/>
8-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11757"/>
7+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13772"/>
98
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
109
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
1110
</dependencies>
@@ -16,46 +15,46 @@
1615
<rect key="frame" x="0.0" y="0.0" width="320" height="82"/>
1716
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
1817
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="KGk-i7-Jjw" id="H2p-sc-9uM">
19-
<rect key="frame" x="0.0" y="0.0" width="287" height="81.5"/>
18+
<rect key="frame" x="0.0" y="0.0" width="286" height="81.5"/>
2019
<autoresizingMask key="autoresizingMask"/>
2120
<subviews>
2221
<imageView userInteractionEnabled="NO" contentMode="scaleAspectFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="GJ0-wN-VIc">
23-
<rect key="frame" x="8" y="8" width="67" height="67"/>
22+
<rect key="frame" x="16" y="11" width="67" height="67"/>
2423
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
2524
<constraints>
2625
<constraint firstAttribute="width" constant="67" id="CSc-Fq-CFF"/>
2726
<constraint firstAttribute="height" constant="67" id="G3p-Xx-UFN"/>
2827
</constraints>
2928
</imageView>
30-
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Speaker Name" textAlignment="natural" lineBreakMode="wordWrap" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="ir9-dv-8TT">
31-
<rect key="frame" x="90" y="21.5" width="112" height="20.5"/>
29+
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" verticalCompressionResistancePriority="749" text="Speaker Name" textAlignment="natural" lineBreakMode="wordWrap" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="ir9-dv-8TT">
30+
<rect key="frame" x="98" y="24.5" width="112" height="15"/>
3231
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
3332
<fontDescription key="fontDescription" type="system" pointSize="17"/>
3433
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
3534
<nil key="highlightedColor"/>
3635
</label>
37-
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="@twitter" textAlignment="natural" lineBreakMode="characterWrap" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="980-ih-bht">
38-
<rect key="frame" x="90" y="44" width="57" height="18"/>
39-
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
40-
<fontDescription key="fontDescription" type="system" weight="light" pointSize="15"/>
41-
<color key="textColor" red="0.33333333333333331" green="0.33333333333333331" blue="0.33333333333333331" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
42-
<nil key="highlightedColor"/>
43-
</label>
36+
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="f5Z-lq-2Xw">
37+
<rect key="frame" x="98" y="35" width="58" height="30"/>
38+
<state key="normal" title="@twitter"/>
39+
<connections>
40+
<action selector="speakerTwitterButtonDidTap:" destination="KGk-i7-Jjw" eventType="touchUpInside" id="TQ8-gr-bc9"/>
41+
</connections>
42+
</button>
4443
</subviews>
4544
<constraints>
4645
<constraint firstItem="ir9-dv-8TT" firstAttribute="leading" secondItem="GJ0-wN-VIc" secondAttribute="trailing" constant="15" id="14W-um-tOD"/>
46+
<constraint firstAttribute="bottom" secondItem="f5Z-lq-2Xw" secondAttribute="bottom" constant="16.5" id="5XV-yg-wdC"/>
4747
<constraint firstItem="GJ0-wN-VIc" firstAttribute="leading" secondItem="H2p-sc-9uM" secondAttribute="leadingMargin" id="6Mk-ta-AhD"/>
48-
<constraint firstItem="980-ih-bht" firstAttribute="top" secondItem="ir9-dv-8TT" secondAttribute="bottom" constant="2" id="9kl-XZ-6MT"/>
4948
<constraint firstItem="ir9-dv-8TT" firstAttribute="top" secondItem="H2p-sc-9uM" secondAttribute="topMargin" constant="13.5" id="Gd8-aC-ye0"/>
5049
<constraint firstItem="GJ0-wN-VIc" firstAttribute="top" secondItem="H2p-sc-9uM" secondAttribute="topMargin" id="Kp2-Vu-vZw"/>
51-
<constraint firstAttribute="bottomMargin" secondItem="980-ih-bht" secondAttribute="bottom" constant="11.5" id="NDV-bw-ILg"/>
52-
<constraint firstItem="980-ih-bht" firstAttribute="leading" secondItem="ir9-dv-8TT" secondAttribute="leading" id="Yzq-oB-Hza"/>
50+
<constraint firstItem="f5Z-lq-2Xw" firstAttribute="leading" secondItem="ir9-dv-8TT" secondAttribute="leading" id="S1y-Og-m7A"/>
51+
<constraint firstItem="f5Z-lq-2Xw" firstAttribute="top" secondItem="ir9-dv-8TT" secondAttribute="bottom" constant="-4.5" id="SbS-Eh-s8L"/>
5352
</constraints>
5453
</tableViewCellContentView>
5554
<connections>
5655
<outlet property="speakerImageView" destination="GJ0-wN-VIc" id="pub-eH-fa9"/>
5756
<outlet property="speakerNameLabel" destination="ir9-dv-8TT" id="ELP-of-NZX"/>
58-
<outlet property="speakerTwitterLabel" destination="980-ih-bht" id="Jxc-OH-eH3"/>
57+
<outlet property="speakerTwitterButton" destination="f5Z-lq-2Xw" id="Euk-CM-I8n"/>
5958
</connections>
6059
<point key="canvasLocation" x="243" y="307"/>
6160
</tableViewCell>

trySwift/SpeakersViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ extension SpeakersViewController {
7272
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
7373
let cell = tableView.dequeueReusableCell(forIndexPath: indexPath) as SpeakerTableViewCell
7474

75-
cell.configure(withSpeaker: speakers[indexPath.row])
75+
cell.configure(withSpeaker: speakers[indexPath.row], delegate: self)
7676

7777
return cell
7878
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// TwitterFollowDelegate.swift
3+
// trySwift
4+
//
5+
// Created by Sash Zats on 3/2/18.
6+
// Copyright © 2018 NatashaTheRobot. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
protocol TwitterFollowDelegate: class {
12+
func followUser(_ username: String)
13+
}
14+
15+
extension UIViewController: TwitterFollowDelegate {
16+
17+
func followUser(_ username: String) {
18+
var applicationOpened = false
19+
let application = UIApplication.shared
20+
for twitterURL in Twitter.urls(forUsername: username) {
21+
if let url = URL(string: twitterURL) , application.canOpenURL(url) && !applicationOpened {
22+
application.open(url, options: [String:Any](), completionHandler: nil)
23+
applicationOpened = true
24+
break
25+
}
26+
}
27+
28+
if !applicationOpened {
29+
if let twitterURL = URL(string: "https://twitter.com/\(username)") {
30+
openSafariViewController(withURL: twitterURL)
31+
}
32+
}
33+
}
34+
}
35+

trySwift/TwitterFollowTableViewCell.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88

99
import UIKit
1010

11-
protocol TwitterFollowDelegate: class {
12-
func followUser(_ username: String)
13-
}
14-
1511
class TwitterFollowTableViewCell: UITableViewCell {
1612

1713
@IBOutlet weak var followButton: UIButton!

trySwift/UIViewControllerExtension.swift

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,6 @@ extension UIViewController: SFSafariViewControllerDelegate {
2323
}
2424
}
2525

26-
extension UIViewController: TwitterFollowDelegate {
27-
28-
func followUser(_ username: String) {
29-
var applicationOpened = false
30-
let application = UIApplication.shared
31-
for twitterURL in Twitter.urls(forUsername: username) {
32-
if let url = URL(string: twitterURL) , application.canOpenURL(url) && !applicationOpened {
33-
application.open(url, options: [String:Any](), completionHandler: nil)
34-
applicationOpened = true
35-
break
36-
}
37-
}
38-
39-
if !applicationOpened {
40-
if let twitterURL = URL(string: "http://twitter.com/\(username)") {
41-
openSafariViewController(withURL: twitterURL)
42-
}
43-
}
44-
}
45-
}
46-
4726
extension UIViewController: MFMailComposeViewControllerDelegate {
4827

4928
func sendMail(withConfiguration configuration: MailConfiguration) {

0 commit comments

Comments
 (0)