@@ -11,6 +11,7 @@ import Ghost
1111
1212enum 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
3437class 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