This repository was archived by the owner on Sep 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathFeedCollectionViewController.swift
More file actions
66 lines (57 loc) · 1.99 KB
/
FeedCollectionViewController.swift
File metadata and controls
66 lines (57 loc) · 1.99 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
54
55
56
57
58
59
60
61
62
63
64
65
66
//
// FeedCollectionViewController.swift
// Secretly
//
// Created by Luis Ezcurdia on 28/05/21.
// Copyright © 2021 3zcurdia. All rights reserved.
//
import UIKit
import CoreLocation
class FeedCollectionViewController: UIViewController {
let feedService = FeedService()
var posts: [Post]? {
didSet {
self.collectionView.reloadData()
self.refreshControl.endRefreshing()
}
}
let refreshControl = UIRefreshControl()
let postInputView = PostInputViewController()
@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
setupCollectionView()
loadPosts()
}
func setupCollectionView() {
let layout = UICollectionViewFlowLayout()
layout.minimumLineSpacing = 4
layout.minimumInteritemSpacing = 4
layout.sectionInset = UIEdgeInsets(top: 4, left: 8, bottom: 4, right: 8)
postInputView.delegate = self
collectionView.delegate = self
collectionView.dataSource = self
collectionView.prefetchDataSource = self
// collectionView.isPrefetchingEnabled = true
collectionView.collectionViewLayout = layout
let nib = UINib(nibName: String(describing: PostCollectionViewCell.self), bundle: nil)
collectionView.register(nib, forCellWithReuseIdentifier: PostCollectionViewCell.reuseIdentifier)
collectionView.addSubview(refreshControl)
refreshControl.addTarget(self, action: #selector(self.loadPosts), for: UIControl.Event.valueChanged)
}
@objc
func loadPosts() {
feedService.load { [unowned self] posts in self.posts = posts }
}
@IBAction
func onTapAdd(_ sender: Any) {
postInputView.clear()
present(postInputView, animated: true)
}
}
extension FeedCollectionViewController: PostInputViewDelegate {
func didCreatePost(post: Post?) {
guard let upost = post else { return }
self.posts?.insert(upost, at: 0)
}
}