Skip to content

Commit 71ded9b

Browse files
committed
sample
1 parent ac814b4 commit 71ded9b

File tree

6 files changed

+239
-17
lines changed

6 files changed

+239
-17
lines changed

Sample/Sample.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
5C51E3EC201C240700418EF5 /* PortfolioTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5C51E3EA201C240700418EF5 /* PortfolioTableViewController.swift */; };
11+
5C51E3ED201C240700418EF5 /* PortfolioTableViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 5C51E3EB201C240700418EF5 /* PortfolioTableViewController.xib */; };
1012
5C8C0E7C2018F2AE00C3DC06 /* DisplayViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5C8C0E7A2018F2AE00C3DC06 /* DisplayViewController.swift */; };
1113
5C8C0E7D2018F2AE00C3DC06 /* DisplayViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 5C8C0E7B2018F2AE00C3DC06 /* DisplayViewController.xib */; };
1214
64DE600D2018AFFC00EF60C3 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64DE600C2018AFFC00EF60C3 /* AppDelegate.swift */; };
@@ -21,6 +23,8 @@
2123
/* Begin PBXFileReference section */
2224
0CE951D399A2F9B87AAF5153 /* Pods_Sample.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Sample.framework; sourceTree = BUILT_PRODUCTS_DIR; };
2325
3A81CA63E5D315C9A0151C2A /* Pods-Sample.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Sample.release.xcconfig"; path = "Pods/Target Support Files/Pods-Sample/Pods-Sample.release.xcconfig"; sourceTree = "<group>"; };
26+
5C51E3EA201C240700418EF5 /* PortfolioTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PortfolioTableViewController.swift; sourceTree = "<group>"; };
27+
5C51E3EB201C240700418EF5 /* PortfolioTableViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = PortfolioTableViewController.xib; sourceTree = "<group>"; };
2428
5C8C0E7A2018F2AE00C3DC06 /* DisplayViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DisplayViewController.swift; sourceTree = "<group>"; };
2529
5C8C0E7B2018F2AE00C3DC06 /* DisplayViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = DisplayViewController.xib; sourceTree = "<group>"; };
2630
64DE60092018AFFC00EF60C3 /* Sample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Sample.app; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -79,6 +83,8 @@
7983
64DE601E2018B3A000EF60C3 /* Response.swift */,
8084
64DE600C2018AFFC00EF60C3 /* AppDelegate.swift */,
8185
64DE600E2018AFFC00EF60C3 /* ViewController.swift */,
86+
5C51E3EA201C240700418EF5 /* PortfolioTableViewController.swift */,
87+
5C51E3EB201C240700418EF5 /* PortfolioTableViewController.xib */,
8288
5C8C0E7A2018F2AE00C3DC06 /* DisplayViewController.swift */,
8389
5C8C0E7B2018F2AE00C3DC06 /* DisplayViewController.xib */,
8490
64DE60102018AFFC00EF60C3 /* Main.storyboard */,
@@ -162,6 +168,7 @@
162168
64DE60172018AFFC00EF60C3 /* LaunchScreen.storyboard in Resources */,
163169
64DE60142018AFFC00EF60C3 /* Assets.xcassets in Resources */,
164170
64DE60122018AFFC00EF60C3 /* Main.storyboard in Resources */,
171+
5C51E3ED201C240700418EF5 /* PortfolioTableViewController.xib in Resources */,
165172
5C8C0E7D2018F2AE00C3DC06 /* DisplayViewController.xib in Resources */,
166173
);
167174
runOnlyForDeploymentPostprocessing = 0;
@@ -227,6 +234,7 @@
227234
isa = PBXSourcesBuildPhase;
228235
buildActionMask = 2147483647;
229236
files = (
237+
5C51E3EC201C240700418EF5 /* PortfolioTableViewController.swift in Sources */,
230238
64DE600F2018AFFC00EF60C3 /* ViewController.swift in Sources */,
231239
64DE600D2018AFFC00EF60C3 /* AppDelegate.swift in Sources */,
232240
64DE601F2018B3A000EF60C3 /* Response.swift in Sources */,

Sample/Sample.xcworkspace/xcuserdata/Meniny.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,21 @@
44
version = "2.0">
55
<Breakpoints>
66
<BreakpointProxy
7-
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
7+
BreakpointExtensionID = "Xcode.Breakpoint.SwiftErrorBreakpoint">
8+
<BreakpointContent
9+
shouldBeEnabled = "Yes"
10+
ignoreCount = "0"
11+
continueAfterRunningActions = "No">
12+
</BreakpointContent>
13+
</BreakpointProxy>
14+
<BreakpointProxy
15+
BreakpointExtensionID = "Xcode.Breakpoint.ExceptionBreakpoint">
816
<BreakpointContent
917
shouldBeEnabled = "Yes"
1018
ignoreCount = "0"
1119
continueAfterRunningActions = "No"
12-
filePath = "../Ghost/Core/ArnoldResponse.swift"
13-
timestampString = "538507195.791525"
14-
startingColumnNumber = "9223372036854775807"
15-
endingColumnNumber = "9223372036854775807"
16-
startingLineNumber = "69"
17-
endingLineNumber = "69"
18-
landmarkName = "decode()"
19-
landmarkType = "7">
20+
scope = "0"
21+
stopOnStyle = "0">
2022
</BreakpointContent>
2123
</BreakpointProxy>
2224
</Breakpoints>
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
//
2+
// PortfolioTableViewController.swift
3+
// Sample
4+
//
5+
// Created by Meniny on 2018-01-27.
6+
// Copyright © 2018年 Meniny Lab. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
class PortfolioTableViewController: UITableViewController {
12+
13+
var portfolio: ProtfolioResponse?
14+
15+
override func viewDidLoad() {
16+
super.viewDidLoad()
17+
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "PCell")
18+
}
19+
20+
// MARK: - Table view data source
21+
22+
override func numberOfSections(in tableView: UITableView) -> Int {
23+
// #warning Incomplete implementation, return the number of sections
24+
return 1
25+
}
26+
27+
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
28+
// #warning Incomplete implementation, return the number of rows
29+
return self.portfolio?.portfolio.data.count ?? 0
30+
}
31+
32+
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
33+
let cell = tableView.dequeueReusableCell(withIdentifier: "PCell", for: indexPath)
34+
35+
if let data = self.portfolio?.portfolio.data[indexPath.row] {
36+
cell.textLabel?.text = "\(data.title)\n\(data.detail)\n\(data.url)\n\(data.image)\n\(data.desc ?? "")"
37+
} else {
38+
cell.textLabel?.text = nil
39+
}
40+
cell.textLabel?.numberOfLines = 0
41+
42+
cell.selectionStyle = .none
43+
44+
return cell
45+
}
46+
47+
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
48+
return 200
49+
}
50+
51+
/*
52+
// Override to support conditional editing of the table view.
53+
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
54+
// Return false if you do not want the specified item to be editable.
55+
return true
56+
}
57+
*/
58+
59+
/*
60+
// Override to support editing the table view.
61+
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
62+
if editingStyle == .delete {
63+
// Delete the row from the data source
64+
tableView.deleteRows(at: [indexPath], with: .fade)
65+
} else if editingStyle == .insert {
66+
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
67+
}
68+
}
69+
*/
70+
71+
/*
72+
// Override to support rearranging the table view.
73+
override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
74+
75+
}
76+
*/
77+
78+
/*
79+
// Override to support conditional rearranging of the table view.
80+
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
81+
// Return false if you do not want the item to be re-orderable.
82+
return true
83+
}
84+
*/
85+
86+
/*
87+
// MARK: - Navigation
88+
89+
// In a storyboard-based application, you will often want to do a little preparation before navigation
90+
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
91+
// Get the new view controller using segue.destinationViewController.
92+
// Pass the selected object to the new view controller.
93+
}
94+
*/
95+
96+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="13142" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
3+
<dependencies>
4+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12042"/>
5+
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
6+
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
7+
</dependencies>
8+
<objects>
9+
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="PortfolioTableViewController" customModuleProvider="target">
10+
<connections>
11+
<outlet property="view" destination="i5M-Pr-FkT" id="sfx-zR-JGt"/>
12+
</connections>
13+
</placeholder>
14+
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
15+
<tableView opaque="NO" clipsSubviews="YES" clearsContextBeforeDrawing="NO" contentMode="scaleToFill" bouncesZoom="NO" style="plain" separatorStyle="default" rowHeight="44" sectionHeaderHeight="22" sectionFooterHeight="22" id="i5M-Pr-FkT">
16+
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
17+
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
18+
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
19+
<viewLayoutGuide key="safeArea" id="vLr-E1-eTs"/>
20+
<connections>
21+
<outlet property="dataSource" destination="-1" id="Tng-2m-Rnh"/>
22+
<outlet property="delegate" destination="-1" id="9aC-8N-iBw"/>
23+
</connections>
24+
</tableView>
25+
</objects>
26+
</document>

Sample/Sample/Response.swift

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,41 @@
77

88
import Foundation
99

10-
struct Response: Codable {
10+
public protocol PortfolioItemType: Codable {
11+
var title: String { get set }
12+
var detail: String { get set }
13+
var image: String { get set }
14+
var url: String { get set }
15+
var available: Bool { get set }
16+
var desc: String? { get set }
17+
}
18+
19+
// --------
20+
21+
struct ProtfolioResponse: Codable {
22+
let portfolio: Portfolio
23+
}
24+
25+
struct Portfolio: Codable {
26+
var type: String
27+
var data: [PortfolioItem]
28+
}
29+
30+
struct PortfolioItem: Codable {
31+
var title: String
32+
var detail: String
33+
var image: String
34+
var url: String
35+
var available: Bool
36+
var desc: String?
37+
38+
enum Keys: String, CodingKey {
39+
case desc = "description"
40+
}
41+
}
42+
43+
// ---------
44+
45+
struct AboutResponse: Codable {
1146
let about: [String]
1247
}

Sample/Sample/ViewController.swift

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Ghost
1111

1212
enum SampleType: String {
1313
case async = "Asynchronous"
14+
case async_portfolio = "Asynchronous Portfolio"
1415
case sync = "Synchronous"
1516
case decode = "Decode"
1617
case hunterAsync = "GhostHunter-Asynchronous"
@@ -19,6 +20,8 @@ enum SampleType: String {
1920
switch self {
2021
case .async:
2122
return #selector(ViewController.sample_async)
23+
case .async_portfolio:
24+
return #selector(ViewController.sample_async_portfolio)
2225
case .sync:
2326
return #selector(ViewController.sample_sync)
2427
case .decode:
@@ -28,7 +31,7 @@ enum SampleType: String {
2831
}
2932
}
3033

31-
static let all: [SampleType] = [.async, .sync, .decode, .hunterAsync]
34+
static let all: [SampleType] = [.async, .async_portfolio, .sync, .decode, .hunterAsync]
3235
}
3336

3437
class ViewController: UITableViewController {
@@ -77,7 +80,6 @@ class ViewController: UITableViewController {
7780

7881
@objc
7982
func sample_async() {
80-
8183
let controller = UIAlertController.init(title: "URL", message: "Enter the url", preferredStyle: .alert)
8284
controller.addTextField { (text) in
8385
text.clearButtonMode = .whileEditing
@@ -87,17 +89,61 @@ class ViewController: UITableViewController {
8789
controller.addAction(UIAlertAction.init(title: "Go", style: .default, handler: { (_) in
8890
if let u = controller.textFields?.first?.text {
8991
if !u.isEmpty {
90-
self.async(url: u)
92+
self._async(url: u)
9193
}
9294
}
9395
}))
9496
controller.addAction(UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil))
9597
self.present(controller, animated: true, completion: nil)
9698
}
9799

98-
func async(url u: String) {
100+
@objc
101+
func sample_async_portfolio() {
102+
let controller = UIAlertController.init(title: "URL", message: "Choose an url", preferredStyle: .alert)
103+
104+
let buttons: [String] = [
105+
"portfolio_android",
106+
"portfolio_ios",
107+
"portfolio_mac",
108+
"portfolio_paintings",
109+
"portfolio_repos",
110+
"portfolio_win"
111+
]
112+
for btn in buttons {
113+
controller.addAction(UIAlertAction.init(title: btn, style: .default, handler: { (action) in
114+
guard let u = action.title, !u.isEmpty else {
115+
self.display("Error")
116+
return
117+
}
118+
self._async_portfolio(u)
119+
}))
120+
}
121+
controller.addAction(UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil))
122+
self.present(controller, animated: true, completion: nil)
123+
}
124+
125+
func _async_portfolio(_ jsonName: String) {
126+
let u = "https://meniny.cn/api/v2/\(jsonName).json"
127+
if let r = GhostRequest.init(u) {
128+
ghost.data(r).async { (response, error) in
129+
do {
130+
if let result: ProtfolioResponse = try response?.decode() {
131+
self.display(result)
132+
} else if let error = error {
133+
self.display("Asynchronous: Ghost error: \(error)")
134+
}
135+
} catch {
136+
self.display("Asynchronous: Parse error: \(error.localizedDescription)")
137+
}
138+
}
139+
} else {
140+
self.display("Asynchronous: Error: \(u)")
141+
}
142+
}
143+
144+
// Asynchronous
145+
func _async(url u: String) {
99146
if let r = GhostRequest.init(u) {
100-
// Asynchronous
101147
ghost.data(r).async { (response, error) in
102148
do {
103149
if let object: [AnyHashable: Any] = try response?.object() {
@@ -130,7 +176,7 @@ class ViewController: UITableViewController {
130176
// Decode
131177
ghost.data(request).async { (response, error) in
132178
do {
133-
if let result: Response = try response?.decode() {
179+
if let result: AboutResponse = try response?.decode() {
134180
self.display(result.about.joined(separator: "\n------\n"))
135181
} else if let error = error {
136182
self.display("Decode: Ghost error: \(error)")
@@ -149,7 +195,7 @@ class ViewController: UITableViewController {
149195
print(pregress)
150196
}, completion: { (response, error) in
151197
do {
152-
if let result: Response = try response?.decode() {
198+
if let result: AboutResponse = try response?.decode() {
153199
self.display(result.about.joined(separator: "\n------\n"))
154200
} else if let error = error {
155201
self.display("NightWatch Asynchronous: Ghost error: \(error)")
@@ -162,6 +208,15 @@ class ViewController: UITableViewController {
162208
self.display("NightWatch: Request error: \(error)")
163209
}
164210
}
211+
212+
func display(_ portfolio: ProtfolioResponse, function: String = #function) {
213+
DispatchQueue.main.async {
214+
let next = PortfolioTableViewController.init(nibName: "PortfolioTableViewController", bundle: nil)
215+
next.portfolio = portfolio
216+
next.title = function
217+
self.navigationController?.show(next, sender: self)
218+
}
219+
}
165220

166221
func display(_ text: String, function: String = #function) {
167222
DispatchQueue.main.async {

0 commit comments

Comments
 (0)