forked from amritnew/Sigma
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookMarkViewModel.swift
More file actions
53 lines (40 loc) · 1.33 KB
/
BookMarkViewModel.swift
File metadata and controls
53 lines (40 loc) · 1.33 KB
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
//
// MyTrailsViewModel.swift
// Sigma
//
// Created by Vinicius Mangueira on 08/09/19.
// Copyright © 2019 Vinicius Mangueira. All rights reserved.
//
import Foundation
class BookMarkViewModel: ConfigurableViewModel {
var updateList: UpdateClosure?
private let coreDao = CoreDao<FavoritePost>(with: "SigmaContainer")
private var posts: [BookmarkCellViewModel] = [] {
didSet {
DispatchQueue.main.async {
self.updateList?()
}
}
}
public func numberOfRows() -> Int {
let rows = self.posts.count
if rows == 0 {
self.fetchData()
}
return self.posts.count
}
private func fetchData() {
self.posts = coreDao.fetchAll().map({BookmarkCellViewModel($0)})
}
public func cellViewModel(forIndex index: Int) -> BookmarkCellViewModel {
if index < self.posts.count {
return self.posts[index]
}
return BookmarkCellViewModel(FavoritePost())
}
public func performPostAtFavorite(forIndex index: Int) -> Post {
let postCellVM = cellViewModel(forIndex: index)
let post = Post(title: postCellVM.favoritePost?.title ?? "Empty", markdownText: postCellVM.favoritePost?.markdownText ?? "Empty")
return post
}
}