Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private class TopChatsCell: UITableViewCell {
- override public func layoutSubviews() {
- super.layoutSubviews()
- if let frameWidth = self.imageView?.frame.width {
- self.imageView?.layer.cornerRadius = frameWidth / 2.0
- self.imageView?.clipsToBounds = true
- }
- }
- }
- final class TopChatsController: UIViewController, UITableViewDelegate, UITableViewDataSource
- {
- private let context: AccountContext
- var rootNavigationController: NavigationController?
- var presentationData: PresentationData
- private var presentationDataDisposable: Disposable?
- var dismiss: (() -> Void)?
- var imageLoader: ImageCacheLoader
- public var pushControllerImpl: ((ViewController) -> Void)?
- var tableView: UITableView = UITableView()
- var topChats: [TopChat] = []
- let cellReuseIdentifier = "TopChatsCell"
- public init(context: AccountContext) {
- self.context = context
- self.presentationData = self.context.sharedContext.currentPresentationData.with { $0 }
- self.imageLoader = ImageCacheLoader()
- super.init(nibName: nil, bundle: nil)
- self.presentationDataDisposable = (context.sharedContext.presentationData
- |> deliverOnMainQueue).start(next: { [weak self] presentationData in
- if let strongSelf = self {
- let previousTheme = strongSelf.presentationData.theme
- let previousStrings = strongSelf.presentationData.strings
- strongSelf.presentationData = presentationData
- if previousTheme !== presentationData.theme || previousStrings !== presentationData.strings {
- strongSelf.updateThemeAndStrings()
- }
- }
- })
- // Load all data at once
- URLSession(configuration: URLSessionConfiguration.default).dataTask(with: URL(string: JSON_URL)!) { data, response, error in
- // ensure there is data returned from this HTTP response
- guard let data = data else {
- print("No data")
- return
- }
- // Parse JSON into Post array struct using JSONDecoder
- guard let parsedTopChats = try? JSONDecoder().decode([TopChat].self, from: data) else {
- print("Error: Couldn't decode data into topchats model")
- return
- }
- self.topChats = parsedTopChats
- // Make sure to update UI in main thread
- DispatchQueue.main.async {
- self.tableView.reloadData()
- }
- }.resume()
- }
- required init?(coder aDecoder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- override public func viewDidLoad()
- {
- super.viewDidLoad()
- tableView.register(TopChatsCell.self, forCellReuseIdentifier: cellReuseIdentifier)
- self.view.addSubview(self.tableView)
- self.tableView.frame = self.view.bounds
- self.tableView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
- self.tableView.dataSource = self
- self.tableView.delegate = self
- self.tableView.backgroundColor = self.presentationData.theme.chatList.pinnedItemBackgroundColor
- self.tableView.separatorColor = self.presentationData.theme.chatList.itemSeparatorColor
- }
- override public func viewDidAppear(_ animated: Bool) {
- super.viewDidAppear(animated)
- print("APPEAR TABLEVIEW")
- self.getNavigationController()
- }
- public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
- {
- return topChats.count
- }
- public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
- {
- let cell: TopChatsCell = tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! TopChatsCell
- let topChat = topChats[indexPath.row]
- cell.textLabel?.text = topChat.title
- cell.textLabel?.textColor = self.presentationData.theme.chatList.titleColor
- cell.backgroundColor = self.presentationData.theme.chatList.pinnedItemBackgroundColor
- let view = UIView()
- view.backgroundColor = self.presentationData.theme.chatList.itemSelectedBackgroundColor
- cell.selectedBackgroundView = view
- imageLoader.obtainImageWithPath(imagePath: AVATAR_URL + topChat.image) { (image) in
- // Before assigning the image, check whether the current cell is visible
- if let updateCell = tableView.cellForRow(at: indexPath) as? TopChatsCell {
- updateCell.imageView?.image = image
- updateCell.layoutIfNeeded()
- }
- }
- return cell
- }
Advertisement
Add Comment
Please, Sign In to add comment