Guest User

Untitled

a guest
Feb 12th, 2020
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.96 KB | None | 0 0
  1. private class TopChatsCell: UITableViewCell {
  2.    
  3.     override public func layoutSubviews() {
  4.        super.layoutSubviews()
  5.        if let frameWidth = self.imageView?.frame.width {
  6.            self.imageView?.layer.cornerRadius = frameWidth / 2.0
  7.            self.imageView?.clipsToBounds = true
  8.        }
  9.     }
  10. }
  11.  
  12.  
  13. final class TopChatsController: UIViewController, UITableViewDelegate, UITableViewDataSource
  14. {
  15.    
  16.     private let context: AccountContext
  17.    
  18.     var rootNavigationController: NavigationController?
  19.     var presentationData: PresentationData
  20.     private var presentationDataDisposable: Disposable?
  21.     var dismiss: (() -> Void)?
  22.     var imageLoader: ImageCacheLoader
  23.    
  24.     public var pushControllerImpl: ((ViewController) -> Void)?
  25.    
  26.     var tableView: UITableView = UITableView()
  27.     var topChats: [TopChat] = []
  28.     let cellReuseIdentifier = "TopChatsCell"
  29.    
  30.    
  31.     public init(context: AccountContext) {
  32.         self.context = context
  33.         self.presentationData = self.context.sharedContext.currentPresentationData.with { $0 }
  34.         self.imageLoader = ImageCacheLoader()
  35.        
  36.         super.init(nibName: nil, bundle: nil)
  37.        
  38.         self.presentationDataDisposable = (context.sharedContext.presentationData
  39.             |> deliverOnMainQueue).start(next: { [weak self] presentationData in
  40.                 if let strongSelf = self {
  41.                     let previousTheme = strongSelf.presentationData.theme
  42.                     let previousStrings = strongSelf.presentationData.strings
  43.                    
  44.                     strongSelf.presentationData = presentationData
  45.                    
  46.                     if previousTheme !== presentationData.theme || previousStrings !== presentationData.strings {
  47.                         strongSelf.updateThemeAndStrings()
  48.                     }
  49.                 }
  50.             })
  51.  
  52.         // Load all data at once
  53.         URLSession(configuration: URLSessionConfiguration.default).dataTask(with: URL(string: JSON_URL)!) { data, response, error in
  54.             // ensure there is data returned from this HTTP response
  55.             guard let data = data else {
  56.                 print("No data")
  57.                 return
  58.             }
  59.            
  60.             // Parse JSON into Post array struct using JSONDecoder
  61.             guard let parsedTopChats = try? JSONDecoder().decode([TopChat].self, from: data) else {
  62.                 print("Error: Couldn't decode data into topchats model")
  63.                 return
  64.             }
  65.            
  66.             self.topChats = parsedTopChats
  67.            
  68.             // Make sure to update UI in main thread
  69.             DispatchQueue.main.async {
  70.                 self.tableView.reloadData()
  71.             }
  72.         }.resume()
  73.     }
  74.    
  75.     required init?(coder aDecoder: NSCoder) {
  76.         fatalError("init(coder:) has not been implemented")
  77.     }
  78.    
  79.     override public func viewDidLoad()
  80.     {
  81.         super.viewDidLoad()
  82.        
  83.         tableView.register(TopChatsCell.self, forCellReuseIdentifier: cellReuseIdentifier)
  84.        
  85.        
  86.         self.view.addSubview(self.tableView)
  87.         self.tableView.frame = self.view.bounds
  88.         self.tableView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  89.         self.tableView.dataSource = self
  90.         self.tableView.delegate = self
  91.         self.tableView.backgroundColor = self.presentationData.theme.chatList.pinnedItemBackgroundColor
  92.         self.tableView.separatorColor = self.presentationData.theme.chatList.itemSeparatorColor
  93.     }
  94.    
  95.     override public func viewDidAppear(_ animated: Bool) {
  96.         super.viewDidAppear(animated)
  97.         print("APPEAR TABLEVIEW")
  98.         self.getNavigationController()
  99.     }
  100.    
  101.     public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
  102.     {
  103.         return topChats.count
  104.     }
  105.    
  106.     public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
  107.     {
  108.         let cell: TopChatsCell = tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! TopChatsCell
  109.        
  110.         let topChat = topChats[indexPath.row]
  111.        
  112.         cell.textLabel?.text = topChat.title
  113.         cell.textLabel?.textColor = self.presentationData.theme.chatList.titleColor
  114.         cell.backgroundColor = self.presentationData.theme.chatList.pinnedItemBackgroundColor
  115.         let view = UIView()
  116.         view.backgroundColor = self.presentationData.theme.chatList.itemSelectedBackgroundColor
  117.         cell.selectedBackgroundView = view
  118.        
  119.         imageLoader.obtainImageWithPath(imagePath: AVATAR_URL + topChat.image) { (image) in
  120.             // Before assigning the image, check whether the current cell is visible
  121.             if let updateCell = tableView.cellForRow(at: indexPath) as? TopChatsCell {
  122.                 updateCell.imageView?.image = image
  123.                 updateCell.layoutIfNeeded()
  124.             }
  125.         }
  126.        
  127.         return cell
  128.     }
Advertisement
Add Comment
Please, Sign In to add comment