<commit_before>//
//  MembersListViewController.swift
//  Rocket.Chat
//
//  Created by <NAME> on 9/19/17.
//  Copyright © 2017 Rocket.Chat. All rights reserved.
//

import UIKit

class MembersListViewData {
    var subscription: Subscription?

    let pageSize = 50
    var currentPage = 0

    var showing: Int = 0
    var total: Int = 0

    var title: String {
        return String(format: localized("chat.members.list.title"), total)
    }

    var isShowingAllMembers: Bool {
        return showing >= total
    }

    var canInviteUsers: Bool {
        return subscription?.canInviteUsers() ?? false
    }

    var membersPages: [[User]] = []
    var members: FlattenCollection<[[User]]> {
        return membersPages.joined()
    }

    func member(at index: Int) -> User {
        return members[members.index(members.startIndex, offsetBy: index)]
    }

    private var isLoadingMoreMembers = false
    func loadMoreMembers(completion: (() -> Void)? = nil) {
        if isLoadingMoreMembers { return }

        if let subscription = subscription {
            isLoadingMoreMembers = true

            let request = RoomMembersRequest(roomId: subscription.rid, type: subscription.type)
            let options: APIRequestOptionSet = [.paginated(count: pageSize, offset: currentPage*pageSize)]

            API.current()?.fetch(request, options: options) { [weak self] response in
                guard let strongSelf = self else { return }
                switch response {
                case .resource(let resource):
                    strongSelf.showing += resource.count ?? 0
                    strongSelf.total = resource.total ?? 0
                    if let members = resource.members {
                        strongSelf.membersPages.append(members.compactMap { $0 })
                    }

                    strongSelf.currentPage += 1

                    strongSelf.isLoadingMoreMembers = false
                    completion?()
                case .error:
                    Alert.defaultError.present()
                }
            }
        }
    }
}

class MembersListViewController: BaseViewController {
    @IBOutlet weak var membersTableView: UITableView!
    var loaderCell: LoaderTableViewCell!

    var data = MembersListViewData() {
        didSet {
            UIView.performWithoutAnimation {
                membersTableView?.reloadData()
            }

            title = data.title
        }
    }

    @objc func refreshControlDidPull(_ sender: UIRefreshControl) {
        refreshMembers()
    }

    func refreshMembers() {
        let data = MembersListViewData()
        data.subscription = self.data.subscription
        data.loadMoreMembers { [weak self] in
            self?.data = data

            if self?.membersTableView?.refreshControl?.isRefreshing ?? false {
                self?.membersTableView?.refreshControl?.endRefreshing()
            }
        }
    }

    func loadMoreMembers() {
        data.loadMoreMembers { [weak self] in
            self?.title = self?.data.title

            if self?.membersTableView?.refreshControl?.isRefreshing ?? false {
                self?.membersTableView?.refreshControl?.endRefreshing()
            }

            UIView.performWithoutAnimation {
                self?.membersTableView?.reloadData()
            }
        }
    }
}

// MARK: ViewController
extension MembersListViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        let refreshControl = UIRefreshControl()
        refreshControl.addTarget(self, action: #selector(refreshControlDidPull), for: .valueChanged)

        membersTableView.refreshControl = refreshControl

        registerCells()

        if let cell = membersTableView.dequeueReusableCell(withIdentifier: LoaderTableViewCell.identifier) as? LoaderTableViewCell {
            self.loaderCell = cell
        }

        title = data.title

        if data.canInviteUsers {
            navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(inviteUsersButtonPressed(sender:)))
        }
    }

    @objc func inviteUsersButtonPressed(sender: Any) {
        performSegue(withIdentifier: "toAddUsers", sender: self)
    }

    func registerCells() {
        membersTableView.register(UINib(
            nibName: "MemberCell",
            bundle: Bundle.main
        ), forCellReuseIdentifier: MemberCell.identifier)

        membersTableView.register(UINib(
            nibName: "LoaderTableViewCell",
            bundle: Bundle.main
        ), forCellReuseIdentifier: LoaderTableViewCell.identifier)
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        refreshMembers()

        guard let refreshControl = membersTableView.refreshControl else { return }
        membersTableView.contentOffset = CGPoint(x: 0, y: -refreshControl.frame.size.height)
        membersTableView.refreshControl?.beginRefreshing()
    }
}

// MARK: Prepare for Segue
extension MembersListViewController {
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        super.prepare(for: segue, sender: sender)<commit_msg>Fix members table view offset bug<commit_after>//
//  MembersListViewController.swift
//  Rocket.Chat
//
//  Created by <NAME> on 9/19/17.
//  Copyright © 2017 Rocket.Chat. All rights reserved.
//

import UIKit

class MembersListViewData {
    var subscription: Subscription?

    let pageSize = 50
    var currentPage = 0

    var showing: Int = 0
    var total: Int = 0

    var title: String {
        return String(format: localized("chat.members.list.title"), total)
    }

    var isShowingAllMembers: Bool {
        return showing >= total
    }

    var canInviteUsers: Bool {
        return subscription?.canInviteUsers() ?? false
    }

    var membersPages: [[User]] = []
    var members: FlattenCollection<[[User]]> {
        return membersPages.joined()
    }

    func member(at index: Int) -> User {
        return members[members.index(members.startIndex, offsetBy: index)]
    }

    private var isLoadingMoreMembers = false
    func loadMoreMembers(completion: (() -> Void)? = nil) {
        if isLoadingMoreMembers { return }

        if let subscription = subscription {
            isLoadingMoreMembers = true

            let request = RoomMembersRequest(roomId: subscription.rid, type: subscription.type)
            let options: APIRequestOptionSet = [.paginated(count: pageSize, offset: currentPage*pageSize)]

            API.current()?.fetch(request, options: options) { [weak self] response in
                guard let strongSelf = self else { return }
                switch response {
                case .resource(let resource):
                    strongSelf.showing += resource.count ?? 0
                    strongSelf.total = resource.total ?? 0
                    if let members = resource.members {
                        strongSelf.membersPages.append(members.compactMap { $0 })
                    }

                    strongSelf.currentPage += 1

                    strongSelf.isLoadingMoreMembers = false
                    completion?()
                case .error:
                    Alert.defaultError.present()
                }
            }
        }
    }
}

class MembersListViewController: BaseViewController {
    @IBOutlet weak var membersTableView: UITableView!
    var loaderCell: LoaderTableViewCell!

    var data = MembersListViewData() {
        didSet {
            UIView.performWithoutAnimation {
                membersTableView?.reloadData()
            }

            title = data.title
        }
    }

    @objc func refreshControlDidPull(_ sender: UIRefreshControl) {
        refreshMembers()
    }

    func refreshMembers() {
        let data = MembersListViewData()
        data.subscription = self.data.subscription
        data.loadMoreMembers { [weak self] in
            self?.data = data

            if self?.membersTableView?.refreshControl?.isRefreshing ?? false {
                self?.membersTableView?.refreshControl?.endRefreshing()
            }
        }
    }

    func loadMoreMembers() {
        data.loadMoreMembers { [weak self] in
            self?.title = self?.data.title

            if self?.membersTableView?.refreshControl?.isRefreshing ?? false {
                self?.membersTableView?.refreshControl?.endRefreshing()
            }

            UIView.performWithoutAnimation {
                self?.membersTableView?.reloadData()
            }
        }
    }
}

// MARK: ViewController
extension MembersListViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        let refreshControl = UIRefreshControl()
        refreshControl.addTarget(self, action: #selector(refreshControlDidPull), for: .valueChanged)

        membersTableView.refreshControl = refreshControl

        registerCells()

        if let cell = membersTableView.dequeueReusableCell(withIdentifier: LoaderTableViewCell.identifier) as? LoaderTableViewCell {
            self.loaderCell = cell
        }

        title = data.title

        if data.canInviteUsers {
            navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(inviteUsersButtonPressed(sender:)))
        }
    }

    @objc func inviteUsersButtonPressed(sender: Any) {
        performSegue(withIdentifier: "toAddUsers", sender: self)
    }

    func registerCells() {
        membersTableView.register(UINib(
            nibName: "MemberCell",
            bundle: Bundle.main
        ), forCellReuseIdentifier: MemberCell.identifier)

        membersTableView.register(UINib(
            nibName: "LoaderTableViewCell",
            bundle: Bundle.main
        ), forCellReuseIdentifier: LoaderTableViewCell.identifier)
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        refreshMembers()

        membersTableView.refreshControl?.beginRefreshing()
    }
}

// MARK: Prepare for Segue
extension MembersListViewController {
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        super.prepare(for: segue, sender: sender)
{'fraction_non_alphanumeric': 0.08938711572351674, 'fraction_numerical': 0.0036224789504601526, 'mean_word_length': 2.199185718759787, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4625333', 'n_tokens_mistral': 2749, 'n_tokens_neox': 2569, 'n_words': 583}