<commit_before>package com.pr0gramm.app.ui.fragments

import android.app.Fragment
import android.content.Intent
import android.content.res.ColorStateList
import android.graphics.PorterDuff
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.github.salomonbrys.kodein.instance
import com.github.salomonbrys.kodein.with
import com.pr0gramm.app.R
import com.pr0gramm.app.UserClasses
import com.pr0gramm.app.api.pr0gramm.LoginCookieHandler
import com.pr0gramm.app.feed.FeedFilter
import com.pr0gramm.app.orm.Bookmark
import com.pr0gramm.app.services.*
import com.pr0gramm.app.services.NavigationProvider.NavigationItem
import com.pr0gramm.app.ui.*
import com.pr0gramm.app.ui.base.BaseFragment
import com.pr0gramm.app.ui.dialogs.LogoutDialogFragment
import com.pr0gramm.app.util.AndroidUtility.getStatusBarHeight
import com.pr0gramm.app.util.CustomTabsHelper
import com.pr0gramm.app.util.onErrorResumeEmpty
import com.pr0gramm.app.util.use
import com.pr0gramm.app.util.visible
import java.util.*
import java.util.concurrent.TimeUnit

/**
 */
class DrawerFragment : BaseFragment("DrawerFragment") {
    private val userService: UserService by instance()
    private val cookieHandler: LoginCookieHandler by instance()
    private val bookmarkService: BookmarkService by instance()

    private val navigationProvider: NavigationProvider by injector.with { activity }.instance()

    private val usernameView: TextView by bindView(R.id.username)
    private val userTypeView: TextView by bindView(R.id.user_type)
    private val benisView: TextView by bindView(R.id.kpi_benis)
    private val benisDeltaView: TextView by bindView(R.id.benis_delta)
    private val benisContainer: View by bindView(R.id.benis_container)
    private val benisGraph: ImageView by bindView(R.id.benis_graph)
    private val actionRules: TextView by bindView(R.id.action_rules)
    private val actionPremium: TextView by bindView(R.id.action_premium)
    private val loginView: TextView by bindView(R.id.action_login)
    private val logoutView: TextView by bindView(R.id.action_logout)
    private val feedbackView: TextView by bindView(R.id.action_contact)
    private val settingsView: TextView by bindView(R.id.action_settings)
    private val inviteView: TextView by bindView(R.id.action_invite)
    private val userImageView: View by bindView(R.id.user_image)

    private val navItemsRecyclerView: RecyclerView by bindView(R.id.drawer_nav_list)

    private val navigationAdapter = NavigationAdapter()
    private val doIfAuthorizedHelper = LoginActivity.helper(this)

    private lateinit var defaultColor: ColorStateList
    private lateinit var markedColor: ColorStateList


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.left_drawer, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        view.context.obtainStyledAttributes(intArrayOf(R.attr.colorAccent, android.R.attr.textColorPrimary)).use { result ->
            markedColor = ColorStateList.valueOf(result.getColor(result.getIndex(0), 0))
            defaultColor = ColorStateList.valueOf(result.getColor(result.getIndex(1), 0))
        }

        // add some space on the top for the translucent status bar
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            val params = userImageView.layoutParams as ViewGroup.MarginLayoutParams
            params.topMargin += getStatusBarHeight(activity)
        }

        // initialize the top navigation items
        navItemsRecyclerView.adapter = navigationAdapter
        navItemsRecyclerView.layoutManager = LinearLayoutManager(activity)
        navItemsRecyclerView.isNestedScrollingEnabled = false

        // add the static items to the navigation
        navigationAdapter.setNavigationItems(navigationProvider.categoryNavigationItems(null, false))

        settingsView.setOnClickListener({
            val intent = Intent(activity, SettingsActivity::class.java)
            startActivity(intent)
        })

        inviteView.setOnClickListener({
            val intent = Intent(activity, InviteActivity::class.java)
            startActivity(intent)
        })

        actionRules.setOnClickListener({
            val intent = Intent(activity, RulesActivity::class.java)
            startActivity(intent)
        })

        actionPremium.setOnClickListener {
            Track.registerLinkClicked()
            val uri = Uri.parse("https://pr0gramm.com/pr0mium/iap")
            CustomTabsHelper(context).openCustomTab(uri)
        }

        loginView.setOnClickListener({
            val intent = Intent(activity, LoginActivity::class.java)
            startActivity(intent)
        })

        logoutView.setOnClickListener({
            LogoutDialogFragment().show(fragmentManager, null)
        })

        feedbackView.setOnClickListener({
            val intent = Intent(activity, ContactActivity::class.java)
            startActivity(intent)
        })

        benisGraph.setOnClickListener {
            val fragment = BenisGraphFragment()
            var transaction = fragmentManager.beginTransaction()
            transaction.replace(R.id.content, fragment)
            transaction.addToBackStack(null)
            transaction.commit()
        }

        // colorize all the secondary icons.
        val views = listOf(loginView, logoutView, feedbackView, settingsView, inviteView, actionRules, actionPremium)
        for (v in views) {
            val secondary = ColorStateList.valueOf(0x80808080.toInt())
            changeCompoundDrawableColor(v, secondary)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        doIfAuthorizedHelper.onActivityResult(requestCode, resultCode)
    }

    private fun onBenisGraphClicked() {
        DialogBuilder.start(activity)
                .cancelable()
                .content(R.string.benis_graph_explanation)
                .positive()
                .show()
    }

    override fun onResume() {
        super.onResume()

        userService.loginState()
                .compose(bindToLifecycleAsync())
                .onErrorResumeEmpty()
                .subscribe({ this.onLoginStateChanged(it) })

        navigationProvider.navigationItems()
                .distinctUntilChanged()
                .compose(bindToLifecycleAsync())
                .retryWhen { err -> err.delay(5, TimeUnit.SECONDS) }
                .subscribe { navigationAdapter.setNavigationItems(it) }
    }

    private fun onLoginStateChanged(state: UserService.LoginState) {
        if (state.authorized) {
            usernameView.text = state.name
            usernameView.setOnClickListener { callback.onUsernameClicked() }
<commit_msg>refactoring fragment-transaction into OnClicked-Function<commit_after>package com.pr0gramm.app.ui.fragments

import android.app.Fragment
import android.content.Intent
import android.content.res.ColorStateList
import android.graphics.PorterDuff
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.github.salomonbrys.kodein.instance
import com.github.salomonbrys.kodein.with
import com.pr0gramm.app.R
import com.pr0gramm.app.UserClasses
import com.pr0gramm.app.api.pr0gramm.LoginCookieHandler
import com.pr0gramm.app.feed.FeedFilter
import com.pr0gramm.app.orm.Bookmark
import com.pr0gramm.app.services.*
import com.pr0gramm.app.services.NavigationProvider.NavigationItem
import com.pr0gramm.app.ui.*
import com.pr0gramm.app.ui.base.BaseFragment
import com.pr0gramm.app.ui.dialogs.LogoutDialogFragment
import com.pr0gramm.app.util.AndroidUtility.getStatusBarHeight
import com.pr0gramm.app.util.CustomTabsHelper
import com.pr0gramm.app.util.onErrorResumeEmpty
import com.pr0gramm.app.util.use
import com.pr0gramm.app.util.visible
import java.util.*
import java.util.concurrent.TimeUnit

/**
 */
class DrawerFragment : BaseFragment("DrawerFragment") {
    private val userService: UserService by instance()
    private val cookieHandler: LoginCookieHandler by instance()
    private val bookmarkService: BookmarkService by instance()

    private val navigationProvider: NavigationProvider by injector.with { activity }.instance()

    private val usernameView: TextView by bindView(R.id.username)
    private val userTypeView: TextView by bindView(R.id.user_type)
    private val benisView: TextView by bindView(R.id.kpi_benis)
    private val benisDeltaView: TextView by bindView(R.id.benis_delta)
    private val benisContainer: View by bindView(R.id.benis_container)
    private val benisGraph: ImageView by bindView(R.id.benis_graph)
    private val actionRules: TextView by bindView(R.id.action_rules)
    private val actionPremium: TextView by bindView(R.id.action_premium)
    private val loginView: TextView by bindView(R.id.action_login)
    private val logoutView: TextView by bindView(R.id.action_logout)
    private val feedbackView: TextView by bindView(R.id.action_contact)
    private val settingsView: TextView by bindView(R.id.action_settings)
    private val inviteView: TextView by bindView(R.id.action_invite)
    private val userImageView: View by bindView(R.id.user_image)

    private val navItemsRecyclerView: RecyclerView by bindView(R.id.drawer_nav_list)

    private val navigationAdapter = NavigationAdapter()
    private val doIfAuthorizedHelper = LoginActivity.helper(this)

    private lateinit var defaultColor: ColorStateList
    private lateinit var markedColor: ColorStateList


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.left_drawer, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        view.context.obtainStyledAttributes(intArrayOf(R.attr.colorAccent, android.R.attr.textColorPrimary)).use { result ->
            markedColor = ColorStateList.valueOf(result.getColor(result.getIndex(0), 0))
            defaultColor = ColorStateList.valueOf(result.getColor(result.getIndex(1), 0))
        }

        // add some space on the top for the translucent status bar
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            val params = userImageView.layoutParams as ViewGroup.MarginLayoutParams
            params.topMargin += getStatusBarHeight(activity)
        }

        // initialize the top navigation items
        navItemsRecyclerView.adapter = navigationAdapter
        navItemsRecyclerView.layoutManager = LinearLayoutManager(activity)
        navItemsRecyclerView.isNestedScrollingEnabled = false

        // add the static items to the navigation
        navigationAdapter.setNavigationItems(navigationProvider.categoryNavigationItems(null, false))

        settingsView.setOnClickListener({
            val intent = Intent(activity, SettingsActivity::class.java)
            startActivity(intent)
        })

        inviteView.setOnClickListener({
            val intent = Intent(activity, InviteActivity::class.java)
            startActivity(intent)
        })

        actionRules.setOnClickListener({
            val intent = Intent(activity, RulesActivity::class.java)
            startActivity(intent)
        })

        actionPremium.setOnClickListener {
            Track.registerLinkClicked()
            val uri = Uri.parse("https://pr0gramm.com/pr0mium/iap")
            CustomTabsHelper(context).openCustomTab(uri)
        }

        loginView.setOnClickListener({
            val intent = Intent(activity, LoginActivity::class.java)
            startActivity(intent)
        })

        logoutView.setOnClickListener({
            LogoutDialogFragment().show(fragmentManager, null)
        })

        feedbackView.setOnClickListener({
            val intent = Intent(activity, ContactActivity::class.java)
            startActivity(intent)
        })

        benisGraph.setOnClickListener {
            this.onBenisGraphClicked()
        }

        // colorize all the secondary icons.
        val views = listOf(loginView, logoutView, feedbackView, settingsView, inviteView, actionRules, actionPremium)
        for (v in views) {
            val secondary = ColorStateList.valueOf(0x80808080.toInt())
            changeCompoundDrawableColor(v, secondary)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        doIfAuthorizedHelper.onActivityResult(requestCode, resultCode)
    }

    private fun onBenisGraphClicked() {
        val fragment = BenisGraphFragment()
        var transaction = fragmentManager.beginTransaction()
        transaction.replace(R.id.content, fragment)
        transaction.addToBackStack(null)
        transaction.commit()
    }

    override fun onResume() {
        super.onResume()

        userService.loginState()
                .compose(bindToLifecycleAsync())
                .onErrorResumeEmpty()
                .subscribe({ this.onLoginStateChanged(it) })

        navigationProvider.navigationItems()
                .distinctUntilChanged()
                .compose(bindToLifecycleAsync())
                .retryWhen { err -> err.delay(5, TimeUnit.SECONDS) }
                .subscribe { navigationAdapter.setNavigationItems(it) }
    }

    private fun onLoginStateChanged(state: UserService.LoginState) {
        if (state.authorized) {
            usernameView.text = state.name
            usernameView.setOnClickListener { callback.onUsernameClicked() }

{'fraction_non_alphanumeric': 0.08337379482555317, 'fraction_numerical': 0.004994104182562253, 'mean_word_length': 3.3349368610944077, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 2, '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': '4906628', 'n_tokens_mistral': 4049, 'n_tokens_neox': 3888, 'n_words': 799}