<commit_before>import UIKit
import Siesta

class UserViewController: UIViewController, UISearchBarDelegate, ResourceObserver {

    @IBOutlet weak var loginButton: UIButton!
    @IBOutlet weak var userInfoView: UIView!
    @IBOutlet weak var usernameLabel, fullNameLabel: UILabel!
    @IBOutlet weak var avatar: RemoteImageView!
    var statusOverlay = ResourceStatusOverlay()

    var repoListVC: RepositoryListViewController?

    var userResource: Resource? {
        didSet {
            // One call to removeObservers() removes both self and statusOverlay as observers of the old resource,
            // since both observers are owned by self (see below).

            oldValue?.removeObservers(ownedBy: self)
            oldValue?.cancelLoadIfUnobserved(afterDelay: 0.1)

            // Adding ourselves as an observer triggers an immediate call to resourceChanged().

            userResource?.addObserver(self)
                         .addObserver(statusOverlay, owner: self)
                         .loadIfNeeded()
        }
    }

    func resourceChanged(resource: Resource, event: ResourceEvent) {
        // typedContent() infers that we want a User from context: showUser() expects one. Our content tranformer
        // configuation in GithubAPI makes it so that the userResource actually holds a User. It is up to a Siesta
        // client to ensure that the transformer output and the expected content type line up like this.
        //
        // If there were a type mismatch, typedContent() would return nil. (We could also provide a default value with
        // the ifNone: param.)

        showUser(userResource?.typedContent())
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = SiestaTheme.darkColor

        statusOverlay.embedIn(self)
        showActiveRepos()
    }

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

        setNeedsStatusBarAppearanceUpdate()
        updateLoginButton()
    }

    override func preferredStatusBarStyle() -> UIStatusBarStyle {
        return .LightContent;
    }

    override func viewDidLayoutSubviews() {
        statusOverlay.positionToCover(userInfoView)
    }

    func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
        if let searchText = searchBar.text where !searchText.isEmpty {

            // Setting userResource triggers a load and display of the new user data. Note that Siesta’s redunant
            // request elimination and model caching make it reasonable to do this on every keystroke.

            userResource = GithubAPI.user(searchText)
        } else {
            userResource = nil
            showActiveRepos()
        }
    }

    func showUser(user: User?) {
        guard user != nil else {
            showActiveRepos()
            return
        }

        // It's often easiest to make the same code path handle both the “data” and “no data” states.
        // If this UI update were more expensive, we could choose to do it only on ObserverAdded or NewData.

        usernameLabel.text = user?.login
        fullNameLabel.text = user?.name
        avatar.imageURL = user?.avatarURL

        // Setting the repositoriesResource property of the embedded VC triggers load & display of the user’s repos.

        repoListVC?.repositoriesResource =
            userResource?
                .optionalRelative(user?.repositoriesURL)?
                .withParam("sort", "updated")
    }

    func showActiveRepos() {
        usernameLabel.text = "Active Repositories"
        fullNameLabel.text = nil
        avatar.imageURL = nil
        repoListVC?.repositoriesResource = GithubAPI.activeRepositories
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "repos" {
            repoListVC = segue.destinationViewController as? RepositoryListViewController
        }
    }

    @IBAction func logInOrOut() {
        if(GithubAPI.isAuthenticated) {
            GithubAPI.logOut()
            updateLoginButton()
        } else {
            performSegueWithIdentifier("login", sender: loginButton)
        }<commit_msg> Streamlined code paths in UserViewController<commit_after>import UIKit
import Siesta

class UserViewController: UIViewController, UISearchBarDelegate, ResourceObserver {

    @IBOutlet weak var loginButton: UIButton!
    @IBOutlet weak var userInfoView: UIView!
    @IBOutlet weak var usernameLabel, fullNameLabel: UILabel!
    @IBOutlet weak var avatar: RemoteImageView!
    var statusOverlay = ResourceStatusOverlay()

    var repoListVC: RepositoryListViewController?

    var userResource: Resource? {
        didSet {
            // One call to removeObservers() removes both self and statusOverlay as observers of the old resource,
            // since both observers are owned by self (see below).

            oldValue?.removeObservers(ownedBy: self)
            oldValue?.cancelLoadIfUnobserved(afterDelay: 0.1)

            // Adding ourselves as an observer triggers an immediate call to resourceChanged().

            userResource?.addObserver(self)
                         .addObserver(statusOverlay, owner: self)
                         .loadIfNeeded()
        }
    }

    func resourceChanged(resource: Resource, event: ResourceEvent) {
        // typedContent() infers that we want a User from context: showUser() expects one. Our content tranformer
        // configuation in GithubAPI makes it so that the userResource actually holds a User. It is up to a Siesta
        // client to ensure that the transformer output and the expected content type line up like this.
        //
        // If there were a type mismatch, typedContent() would return nil. (We could also provide a default value with
        // the ifNone: param.)

        showUser(userResource?.typedContent())
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = SiestaTheme.darkColor

        statusOverlay.embedIn(self)
        showUser(nil)
    }

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

        setNeedsStatusBarAppearanceUpdate()
        updateLoginButton()
    }

    override func preferredStatusBarStyle() -> UIStatusBarStyle {
        return .LightContent;
    }

    override func viewDidLayoutSubviews() {
        statusOverlay.positionToCover(userInfoView)
    }

    func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
        if let searchText = searchBar.text where !searchText.isEmpty {

            // Setting userResource triggers a load and display of the new user data. Note that Siesta’s redunant
            // request elimination and model caching make it reasonable to do this on every keystroke.

            userResource = GithubAPI.user(searchText)
        } else {
            userResource = nil
            showUser(nil)
        }
    }

    func showUser(user: User?) {
        // It's often easiest to make the same code path handle both the “data” and “no data” states.
        // If this UI update were more expensive, we could choose to do it only on ObserverAdded or NewData.

        fullNameLabel.text = user?.name
        avatar.imageURL = user?.avatarURL

        // Here the “data” and “no data” states diverge enough that it’s worth taking two separate code paths.
        // Note however that declaring these two variables without initializers guarantees that they’ll both be
        // set in either branch before they’re used.

        let title: String?
        let repositoriesResource: Resource?

        if let user = user {
            title = user.login
            repositoriesResource =
                userResource?
                    .optionalRelative(user.repositoriesURL)?
                    .withParam("sort", "updated")
        } else {
            title = user?.login
            repositoriesResource = GithubAPI.activeRepositories
        }

        // Setting the repositoriesResource property of the embedded VC triggers load & display of the user’s repos.

        repoListVC?.repositoriesResource = repositoriesResource
        usernameLabel.text = title
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "repos" {
            repoListVC = segue.destinationViewController as? RepositoryListViewController
        }
    }

    @IBAction func logInOrOut() {
        if(GithubAPI.isAuthenticated) {
            GithubAPI.logOut()
            updateLoginButton()
        } else {
            performSegueWithIdentifier("login", sender: loginButton)
        }
{'fraction_non_alphanumeric': 0.06884266176301575, 'fraction_numerical': 0.0004597172738765659, 'mean_word_length': 2.408538973756365, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '5214144', 'n_tokens_mistral': 2255, 'n_tokens_neox': 2131, 'n_words': 721}