Skip to content

Commit 035b74d

Browse files
committed
增加帖子列表接口和帖子模型
1 parent 8bf721e commit 035b74d

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed

ruby-china-ios.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
24742D011D50FE2E00D16CA9 /* RBTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24742CEE1D50FE2E00D16CA9 /* RBTextField.swift */; };
2929
24758CEB1D620FFA00D8C743 /* UIImage+Ext.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24758CEA1D620FFA00D8C743 /* UIImage+Ext.swift */; };
3030
249C0A7F1D75C1E600097458 /* String+Localize.swift in Sources */ = {isa = PBXBuildFile; fileRef = 249C0A7E1D75C1E600097458 /* String+Localize.swift */; };
31+
24B228ED1DAA877600DEC44D /* TopicsService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24B228EC1DAA877600DEC44D /* TopicsService.swift */; };
32+
24B228EF1DAA8B1600DEC44D /* Topic.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24B228EE1DAA8B1600DEC44D /* Topic.swift */; };
33+
24B228F31DAA972500DEC44D /* String+Ext.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24B228F21DAA972500DEC44D /* String+Ext.swift */; };
3134
24E5B4001D6B2C82006515E3 /* ThemeNavigationController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24E5B3FF1D6B2C82006515E3 /* ThemeNavigationController.swift */; };
3235
961580A71D477A7600B32BDA /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 961580A61D477A7600B32BDA /* Assets.xcassets */; };
3336
961F70FA1D6AB0440079212C /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 961F70FC1D6AB0440079212C /* Localizable.strings */; };
@@ -64,6 +67,9 @@
6467
249C0A7D1D75BFF700097458 /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/Localizable.strings"; sourceTree = "<group>"; };
6568
249C0A7E1D75C1E600097458 /* String+Localize.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "String+Localize.swift"; sourceTree = "<group>"; };
6669
249C0A811D75D51F00097458 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/Localizable.strings; sourceTree = "<group>"; };
70+
24B228EC1DAA877600DEC44D /* TopicsService.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TopicsService.swift; sourceTree = "<group>"; };
71+
24B228EE1DAA8B1600DEC44D /* Topic.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Topic.swift; sourceTree = "<group>"; };
72+
24B228F21DAA972500DEC44D /* String+Ext.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "String+Ext.swift"; sourceTree = "<group>"; };
6773
24E5B3FF1D6B2C82006515E3 /* ThemeNavigationController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ThemeNavigationController.swift; sourceTree = "<group>"; };
6874
27849C620AD961F60F073D43 /* Pods-ruby-china-ios.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ruby-china-ios.release.xcconfig"; path = "Pods/Target Support Files/Pods-ruby-china-ios/Pods-ruby-china-ios.release.xcconfig"; sourceTree = "<group>"; };
6975
3D8873230E620CCF5BBCEE42 /* Pods_ruby_china_ios.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_ruby_china_ios.framework; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -117,6 +123,7 @@
117123
children = (
118124
24742CE51D50FE2E00D16CA9 /* APIRequest.swift */,
119125
24742CE61D50FE2E00D16CA9 /* DeviceService.swift */,
126+
24B228EC1DAA877600DEC44D /* TopicsService.swift */,
120127
24742CE71D50FE2E00D16CA9 /* OAuth2.swift */,
121128
24742CE81D50FE2E00D16CA9 /* TurbolinksSessionLib.swift */,
122129
);
@@ -127,6 +134,7 @@
127134
isa = PBXGroup;
128135
children = (
129136
24742CEA1D50FE2E00D16CA9 /* User.swift */,
137+
24B228EE1DAA8B1600DEC44D /* Topic.swift */,
130138
);
131139
path = Models;
132140
sourceTree = "<group>";
@@ -147,6 +155,7 @@
147155
children = (
148156
24758CEA1D620FFA00D8C743 /* UIImage+Ext.swift */,
149157
249C0A7E1D75C1E600097458 /* String+Localize.swift */,
158+
24B228F21DAA972500DEC44D /* String+Ext.swift */,
150159
);
151160
path = Extensions;
152161
sourceTree = "<group>";
@@ -353,6 +362,7 @@
353362
files = (
354363
24758CEB1D620FFA00D8C743 /* UIImage+Ext.swift in Sources */,
355364
24742CF71D50FE2E00D16CA9 /* SignInViewController.swift in Sources */,
365+
24B228ED1DAA877600DEC44D /* TopicsService.swift in Sources */,
356366
24742CEF1D50FE2E00D16CA9 /* EditAccountViewController.swift in Sources */,
357367
24742CF81D50FE2E00D16CA9 /* TopicsViewController.swift in Sources */,
358368
24742CF51D50FE2E00D16CA9 /* SideMenuNavigationController.swift in Sources */,
@@ -363,9 +373,11 @@
363373
9670F1901D4235D200128F8A /* AppDelegate.swift in Sources */,
364374
24742CFA1D50FE2E00D16CA9 /* APIRequest.swift in Sources */,
365375
24742CF31D50FE2E00D16CA9 /* NotificationsViewController.swift in Sources */,
376+
24B228EF1DAA8B1600DEC44D /* Topic.swift in Sources */,
366377
24742CFC1D50FE2E00D16CA9 /* OAuth2.swift in Sources */,
367378
249C0A7F1D75C1E600097458 /* String+Localize.swift in Sources */,
368379
24742CFD1D50FE2E00D16CA9 /* TurbolinksSessionLib.swift in Sources */,
380+
24B228F31DAA972500DEC44D /* String+Ext.swift in Sources */,
369381
24742CF41D50FE2E00D16CA9 /* RootViewController.swift in Sources */,
370382
96F9FD6D1D59A814004FD824 /* ProfileViewController.swift in Sources */,
371383
24742CF61D50FE2E00D16CA9 /* SideMenuViewController.swift in Sources */,
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//
2+
// TopicsService.swift
3+
// ruby-china-ios
4+
//
5+
// Created by 柯磊 on 16/10/9.
6+
// Copyright © 2016年 ruby-china. All rights reserved.
7+
//
8+
9+
class TopicsService {
10+
11+
enum ListType: String {
12+
/// 活跃的
13+
case last_actived
14+
/// 最近的
15+
case recent
16+
/// 无回复的
17+
case no_reply
18+
/// 受欢迎的
19+
case popular
20+
/// 精华
21+
case excellent
22+
}
23+
24+
/// 获取帖子列表
25+
///
26+
/// - parameter type: 排序类型
27+
/// - parameter node_id: 节点编号,传大于 0 时返回该节点的帖子
28+
/// - parameter offset: 分页起始位置
29+
/// - parameter limit: 分页大小,范围 1..150
30+
static func list(type: ListType = .last_actived, node_id: Int = 0, offset: Int = 0, limit: Int = 20, callback: (statusCode: Int?, result: [Topic]?) -> ()) {
31+
32+
var parameters = [String: AnyObject]()
33+
parameters["type"] = type.rawValue
34+
parameters["offset"] = offset
35+
parameters["limit"] = limit
36+
if node_id > 0 {
37+
parameters["node_id"] = node_id
38+
}
39+
40+
APIRequest.shared.get("/api/v3/topics.json", parameters: parameters) { (statusCode, result) in
41+
var topics: [Topic]? = nil
42+
43+
guard let _ = result, topicList = result!["topics"].array where topicList.count > 0 else {
44+
callback(statusCode: statusCode, result: topics)
45+
return
46+
}
47+
48+
topics = [Topic]()
49+
for topicJSON in topicList {
50+
topics!.append(Topic(json: topicJSON))
51+
}
52+
53+
callback(statusCode: statusCode, result: topics)
54+
}
55+
}
56+
57+
}

ruby-china-ios/Models/Topic.swift

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//
2+
// Topic.swift
3+
// ruby-china-ios
4+
//
5+
// Created by 柯磊 on 16/10/9.
6+
// Copyright © 2016年 ruby-china. All rights reserved.
7+
//
8+
9+
import Foundation
10+
import SwiftyJSON
11+
12+
struct Topic {
13+
let id: Int
14+
let user: User
15+
let title: String
16+
let createdAt: NSDate
17+
let updatedAt: NSDate
18+
let repliedAt: NSDate?
19+
let repliesCount: Int
20+
let lastReplyUserID: Int?
21+
let lastReplyUserLogin: String?
22+
let likesCount: Int
23+
24+
let nodeName: String
25+
let nodeID: Int
26+
27+
/// 精华帖
28+
let excellent: Bool
29+
let deleted: Bool
30+
31+
init(json: JSON) {
32+
id = json["id"].intValue
33+
user = User(json: json["user"])!
34+
title = json["title"].stringValue
35+
createdAt = json["created_at"].stringValue.dateValueFromISO8601()!
36+
updatedAt = json["updated_at"].stringValue.dateValueFromISO8601()!
37+
repliedAt = json["replied_at"].string?.dateValueFromISO8601()
38+
repliesCount = json["replies_count"].intValue
39+
lastReplyUserID = json["last_reply_user_id"].int
40+
lastReplyUserLogin = json["last_reply_user_login"].string
41+
likesCount = json["likes_count"].intValue
42+
43+
nodeName = json["node_name"].stringValue
44+
nodeID = json["node_id"].intValue
45+
46+
excellent = json["excellent"].boolValue
47+
deleted = json["deleted"].boolValue
48+
}
49+
}

0 commit comments

Comments
 (0)