Created
June 8, 2020 18:47
-
-
Save DDavis1025/0fdf8e0b3b14eb774e2e3220bb81d8c8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import UIKit | |
class AlbumCell: UICollectionViewCell, UITableViewDelegate, UITableViewDataSource { | |
private var myTableView: UITableView! | |
var homeVC = HomeViewController() | |
var model:PostListViewModel? | |
var child:SpinnerViewController? | |
var cellTitle:UILabel? | |
var cellDesc:UILabel? | |
var usersLoaded:Bool? = false | |
var getUserById:GetUsersById? | |
var array = [String]() | |
var userDictionary = [String: UsersModel]() | |
var users = [UsersModel]() { | |
didSet { | |
print("users should be array \(users)") | |
let group = DispatchGroup() | |
for user in users { | |
group.enter() | |
if let user_id = user.user_id { | |
userDictionary[user_id] = user | |
} | |
group.leave() | |
} | |
group.notify(queue: .main) { | |
self.usersLoaded = true | |
self.myTableView.reloadData() | |
self.refresher?.endRefreshing() | |
} | |
print("user dinctionary \(userDictionary)") | |
} | |
} | |
var posts = [Post]() { | |
didSet { | |
self.child?.willMove(toParent: nil) | |
self.child?.view.removeFromSuperview() | |
self.child?.removeFromParent() | |
myTableView.reloadData() | |
print("posts right now \(posts)") | |
func uniq<S : Sequence, T : Hashable>(source: S) -> [T] where S.Iterator.Element == T { | |
var buffer = [T]() | |
var added = Set<T>() | |
for elem in source { | |
if !added.contains(elem) { | |
buffer.append(elem) | |
added.insert(elem) | |
} | |
} | |
return buffer | |
} | |
var authorId = [String]() | |
for post in self.posts { | |
authorId.append(post.author!) | |
} | |
let uniqueVals = uniq(source: authorId) | |
print("unique vals \(uniqueVals)") | |
for id in uniqueVals { | |
GetUsersById(id: id).getAllPosts { | |
self.users.append(contentsOf: $0) | |
print("got users for this \($0)") | |
} | |
} | |
} | |
} | |
var albumVC:AlbumVC? | |
var imageLoader:DownloadImage? | |
var refresher:UIRefreshControl? | |
var components:URLComponents = { | |
var component = URLComponents() | |
component.scheme = "http" | |
component.host = "localhost" | |
component.port = 8000 | |
return component | |
}() | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
// addSpinner() | |
Webservice().getAllPosts { | |
self.posts = $0 | |
} | |
refresher = UIRefreshControl() | |
refresher?.attributedTitle = NSAttributedString(string: "Pull to refresh") | |
refresher?.addTarget(self, action: #selector(refresh), for: UIControl.Event.valueChanged) | |
addTableView() | |
myTableView.addSubview(refresher!) | |
} | |
@objc func refresh() { | |
Webservice().getAllPosts { | |
self.posts = $0 | |
} | |
} | |
// func addSpinner() { | |
// let child = SpinnerViewController() | |
// addChild(child) | |
// child.view.frame = view.frame | |
// view.addSubview(child.view) | |
// child.didMove(toParent: self) | |
// child.view.backgroundColor = UIColor.white | |
// self.view.bringSubviewToFront(child.view) | |
// } | |
func addTableView() { | |
self.myTableView = UITableView() | |
self.myTableView?.translatesAutoresizingMaskIntoConstraints = false | |
self.myTableView.frame.size.height = self.frame.height | |
// | |
self.myTableView.frame.size.width = self.frame.width | |
self.myTableView.register(FeedCell.self, forCellReuseIdentifier: "MyCell") | |
self.myTableView.dataSource = self | |
self.myTableView.delegate = self | |
myTableView.delaysContentTouches = false | |
self.addSubview(self.myTableView) | |
self.myTableView?.topAnchor.constraint(equalTo: self.topAnchor).isActive = true | |
self.myTableView?.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true | |
self.myTableView?.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true | |
self.myTableView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true | |
myTableView.layoutMargins = UIEdgeInsets.zero | |
myTableView.separatorInset = UIEdgeInsets.zero | |
} | |
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { | |
return 140 | |
} | |
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { | |
tableView.deselectRow(at: indexPath, animated: true) | |
let albumVC = AlbumVC(post: posts[indexPath.row]) | |
homeVC.navigationController?.pushViewController(albumVC, animated: true) | |
} | |
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return posts.count | |
} | |
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell") as! FeedCell | |
let post = posts[indexPath.row] | |
cell.set(post: post) | |
cell.setUser(user: userDictionary[posts[indexPath.row].author!]) | |
return cell | |
} | |
required init?(coder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import UIKit | |
import SwiftUI | |
import Auth0 | |
class HomeViewController: Toolbar, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { | |
let cellId = "cellId" | |
var albumVC:AlbumVC? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
setupMenuBar() | |
setupCollectionView() | |
let logout = UIBarButtonItem(title: "Logout", style: .plain, target: self, action: #selector(logoutTapped)) | |
let profile = UIBarButtonItem(title: "Profile", style: .plain, target: self, action: #selector(addTapped)) | |
let whoToFollow = UIBarButtonItem(title: "toFollow", style: .plain, target: self, action: #selector(toFollowTapped)) | |
navigationItem.leftBarButtonItem = profile | |
navigationItem.rightBarButtonItem = whoToFollow | |
navigationItem.rightBarButtonItem = logout | |
self.navigationController?.isToolbarHidden = false | |
self.navigationController?.isNavigationBarHidden = false | |
} | |
@objc func addTapped() { | |
let profileVC = ProfileViewController() | |
let profileView = ProfileVC() | |
self.navigationController?.pushViewController(profileView, animated: true) | |
} | |
@objc func toFollowTapped() { | |
let toFollowVC = WhoToFollowVC() | |
self.navigationController?.pushViewController(toFollowVC, animated: true) | |
} | |
@objc func logoutTapped() { | |
let authVC = AuthVC() | |
SessionManager.shared.logout { (error) in | |
guard error == nil else { | |
// Handle error | |
print("Error: \(error)") | |
return | |
} | |
} | |
print("Session manager credentials \(SessionManager.shared.credentials)") | |
self.navigationController?.popToRootViewController(animated: true) | |
self.navigationController?.isToolbarHidden = true | |
self.navigationController?.isNavigationBarHidden = true | |
} | |
lazy var collectionView: UICollectionView = { | |
let layout = UICollectionViewFlowLayout() | |
layout.scrollDirection = .horizontal | |
layout.minimumLineSpacing = 0 | |
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout) | |
// cv.backgroundColor = UIColor(red: 244.0/255.0, green: 244.0/255.0, blue: 244.0/255.0, alpha: 1.0) | |
cv.dataSource = self | |
cv.delegate = self | |
return cv | |
}() | |
func setupCollectionView() { | |
view.addSubview(collectionView) | |
view.bringSubviewToFront(collectionView) | |
collectionView.isPagingEnabled = true | |
// collectionView.backgroundColor = UIColor.gray | |
collectionView.translatesAutoresizingMaskIntoConstraints = false | |
collectionView.heightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.heightAnchor).isActive = true | |
collectionView.widthAnchor.constraint(equalTo: view.safeAreaLayoutGuide.widthAnchor).isActive = true | |
collectionView.topAnchor.constraint(equalTo: menuBar.bottomAnchor).isActive = true | |
collectionView.register(AlbumCell.self, forCellWithReuseIdentifier: cellId) | |
// collectionView.contentInset = UIEdgeInsets(top: 50, left: 0, bottom: 0, right: 0) | |
// collectionView.scrollIndicatorInsets = UIEdgeInsets(top: 50, left: 0, bottom: 0, right: 0) | |
} | |
lazy var menuBar:MenuBar = { | |
let mb = MenuBar() | |
mb.homeVC = self | |
return mb | |
}() | |
private func setupMenuBar() { | |
view.addSubview(menuBar) | |
self.menuBar.translatesAutoresizingMaskIntoConstraints = false | |
self.menuBar.heightAnchor.constraint(equalToConstant: 50).isActive = true | |
self.menuBar.widthAnchor.constraint(equalTo: view.safeAreaLayoutGuide.widthAnchor).isActive = true | |
self.menuBar.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true | |
self.menuBar.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true | |
} | |
func scrollViewDidScroll(_ scrollView: UIScrollView) { | |
menuBar.horizontalBarLeftAnchorConstraint?.constant = scrollView.contentOffset.x / 3 | |
} | |
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) { | |
let index = targetContentOffset.pointee.x / view.frame.width | |
let indexPath = NSIndexPath(item: Int(index), section: 0) | |
menuBar.collectionView.selectItem(at: indexPath as IndexPath, animated: true, scrollPosition: []) | |
} | |
func scrollToMenuIndex(menuIndex: Int) { | |
let indexPath = NSIndexPath(item: menuIndex, section: 0) | |
collectionView.scrollToItem(at: indexPath as IndexPath, at: [], animated: true) | |
} | |
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { | |
return 3 | |
} | |
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { | |
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) | |
let colors: [UIColor] = [.yellow, .orange, .red] | |
cell.backgroundColor = colors[indexPath.item] | |
return cell | |
} | |
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { | |
return CGSize(width: view.frame.width, height: view.frame.height) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment