-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecentArticlesModel.swift
89 lines (75 loc) · 2.96 KB
/
RecentArticlesModel.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//
// RecentArticlesModel.swift
// Gank
//
// Created by 魏星 on 16/7/17.
// Copyright © 2016年 wx. All rights reserved.
//
import Foundation
import RxSwift
import ObjectMapper
class RecentArticlesModel: BaseModel<GankIOApi>{
var articleEntities: [ArticleEntity]
override init() {
articleEntities = []
super.init()
}
func refresh() -> Observable<[ArticleEntity]>{
page = 1
return getArticlesByPage(page)
}
func loadMore() -> Observable<[ArticleEntity]>{
return getArticlesByPage(page + 1)
}
func getArticlesByPage(page: Int) -> Observable<[ArticleEntity]>{
let sourceGirl = requestProvider.request(GankIOApi.ByPageAndKind(kind: "福利", page: page, count: self.offset))
.filterStatusCodes(200...201)
.observeOn(backgroundScheduler)
.map { (response) -> [ArticleEntity] in
if let result = Mapper<BaseEntity<ArticleEntity>>().map(String(data: response.data, encoding: NSUTF8StringEncoding)){
return result.results!
}else{
throw NSError(domain: "parse json error", code: -1, userInfo: ["json": response])
}
}
let sourceVideo = requestProvider.request(GankIOApi.ByPageAndKind(kind: "休息视频", page: page, count: self.offset))
.filterStatusCodes(200...201)
.observeOn(backgroundScheduler)
.map { (response) -> [ArticleEntity] in
if let result = Mapper<BaseEntity<ArticleEntity>>().map(String(data: response.data, encoding: NSUTF8StringEncoding)){
return result.results!
}else{
throw NSError(domain: "parse json error", code: -1, userInfo: ["json": response])
}
}
return Observable.zip(sourceGirl, sourceVideo, resultSelector: { (girls, videos) -> [ArticleEntity] in
for item in girls{
item.desc = item.desc! + " " + self.getFirstVideoDescByPublishTime(videos, publishedAt: item.publishedAt!)
}
return girls
}).doOnNext({ (entities) in
if !entities.isEmpty {
if page == 1{
self.articleEntities = entities
}else{
self.articleEntities += entities
self.page = page
}
}
}).observeOn(MainScheduler.instance)
}
private func getFirstVideoDescByPublishTime(videos: [ArticleEntity],publishedAt: NSDate) -> String{
var videoDesc = ""
for item in videos{
if item.publishedAt == nil{
item.publishedAt = item.createdAt
}
let videoPublishedAt = item.publishedAt
if DateUtils.areDatesSameDay(videoPublishedAt!, dateTwo: publishedAt){
videoDesc = item.desc!
break
}
}
return videoDesc
}
}