Guest User

Untitled

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