// // PostsLoader.swift // librx // // Created by Yunlong Zheng on 15/8/17. // Copyright (c) 2015年 librx. All rights reserved. // import Foundation class PostsLoader { typealias PostfixLoadCompetion = (posts:[Post], error: String?) ->() typealias ExplorefixLoadCompetion = (sources: [ExploreSource]) ->() enum PostSection: CustomStringConvertible { case Default case RecentTimeLine case HotTimeLine case MyPostTimeLine case UserPosts(userId: Int) case Liked case UserLiked(userId: Int) case BookMark case UserBookMark(userId: Int) case Search(_: String) var description: String { switch(self) { case .Default: return "HotForYou" case .RecentTimeLine: return "Recent" case .HotTimeLine: return "HotTimeline" case .Search(_): return "search" case .MyPostTimeLine: return "MyPost" case .Liked: return "MyLike" case .BookMark: return "Bookmar" case .UserPosts(let userId): return "user\(userId)posts" case .UserLiked(let userId): return "user\(userId)liked" case .UserBookMark(let userId): return "user\(userId)bookmark" } } var cacheKey: String { switch(self) { case .Default: return "defaultpostkey" case .RecentTimeLine: return "recenttimelinekey" case .HotTimeLine: return "hottimelinekey" case .Search(_): return "searchkey" case .MyPostTimeLine: return "myposttimelinekey" case .Liked: return "likedkey" case .BookMark: return "bookmarkkey" case .UserPosts(let userId): return "user\(userId)postskey" case .UserLiked(let userId): return "user\(userId)likedkey" case .UserBookMark(let userId): return "user\(userId)bookmarkkey" } } var tableString: String { switch(self) { case .Default: return "当前没有可显示的内容,请下拉刷新" case .RecentTimeLine: return "人人可用的信息分享平台" case .HotTimeLine: return "这里我们将向你展示最热的内容" case .Search(_): return "当前没有可显示的内容,请下拉刷新" case .MyPostTimeLine: return "分享,也是一种生活态度" case .Liked: return "喜欢,来表明你的个性" case .BookMark: return "收藏,的就是对你最有价值的" case .UserPosts(_): return "还没有分享任何内容" case .UserLiked(_): return "喜欢,来表明你的个性" case .UserBookMark(_): return "收藏,的就是对你最有价值的" } } } private (set) var hasMore: Bool = false private var page: Int = 1 private var isLoading: Bool = false private let keyword: String! private let section: PostSection init(_ section: PostSection = .RecentTimeLine) { self.section = section switch (section){ case let .Search(keyword): self.keyword = keyword default: self.keyword = nil } } func loadExploreSourcesFromUrl(page: Int=1, url: String, completion: ExplorefixLoadCompetion) { if isLoading { return } isLoading = true return exploreSourcesFromUrl(url, completion: completion) } func load(page: Int=1, completion: PostfixLoadCompetion){ if isLoading { return } isLoading = true switch(section) { case .MyPostTimeLine: return postsByMe(page, completion:completion) case .Liked: return postsByMeLiked(page, completion: completion) case .BookMark: return postsByMeBookMark(page, completion:completion) case .UserPosts(let userId): return postsByUser(page, userId: userId, completion: completion) case .UserLiked(let userId): return postsByUserLiked(page, userId: userId, completion: completion) case .UserBookMark(let userId): return postsByUserBookMark(page, userId: userId, completion: completion) default: return postsBySection(page, completion: completion) } } func exploreSourcesFromUrl(url: String, completion: ExplorefixLoadCompetion){ LibrxService.loadExploreSourcesByUrl(url) { sources in completion(sources: sources) } } func postsByUser(page: Int=1, userId: Int, completion: PostfixLoadCompetion) { LibrxService.postsForUser(page, userId: userId, section: section){ [weak self] (posts, error) in if let storongSelf = self { storongSelf.isLoading = false completion(posts: posts, error: error) } } } func postsByMe(page: Int=1, completion: PostfixLoadCompetion) { LibrxService.postsForMyPosts(page, section: section){ [weak self] (posts, error) in if let storongSelf = self { storongSelf.isLoading = false completion(posts: posts, error: error) } } } func postsByMeLiked(page: Int=1,completion: PostfixLoadCompetion) { let userId = LocalStore.accessUserId() LibrxService.postsForUserLiked(page, userId: userId!, section: section){ [weak self] (posts, error) in if let storongSelf = self { storongSelf.isLoading = false completion(posts: posts, error: error) } } } func postsByUserLiked(page: Int=1, userId: Int, completion: PostfixLoadCompetion) { LibrxService.postsForUserLiked(page, userId: userId, section: section){ [weak self] (posts, error) in if let storongSelf = self { storongSelf.isLoading = false completion(posts: posts, error: error) } } } func postsByMeBookMark(page: Int=1, completion: PostfixLoadCompetion) { let userId = LocalStore.accessUserId() LibrxService.postsForUserBookMark(page, userId: userId!, section: section){ [weak self] (posts, error) in if let storongSelf = self { storongSelf.isLoading = false completion(posts: posts, error: error) } } } func postsByUserBookMark(page: Int=1, userId: Int, completion: PostfixLoadCompetion) { LibrxService.postsForUserBookMark(page, userId: userId, section: section){ [weak self] (posts, error) in if let storongSelf = self { storongSelf.isLoading = false completion(posts: posts, error: error) } } } func postsBySection(page: Int=1, completion: PostfixLoadCompetion) { LibrxService.postsBySection(page, keyword: self.keyword, section: section) { [weak self] (posts, error) in if let storongSelf = self { storongSelf.isLoading = false completion(posts: posts, error: error) } } } }