<commit_before>package org.stepik.android.view.comment.ui.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.appcompat.content.res.AppCompatResources
import androidx.core.content.ContextCompat
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.SimpleItemAnimator
import kotlinx.android.synthetic.main.activity_comments.*
import kotlinx.android.synthetic.main.empty_comments.*
import kotlinx.android.synthetic.main.empty_comments.view.*
import kotlinx.android.synthetic.main.error_no_connection.*
import kotlinx.android.synthetic.main.view_centered_toolbar.*
import org.stepic.droid.R
import org.stepic.droid.analytic.Analytic
import org.stepic.droid.base.App
import org.stepic.droid.base.FragmentActivityBase
import org.stepic.droid.ui.util.initCenteredToolbar
import org.stepic.droid.ui.util.setOnPaginationListener
import org.stepic.droid.ui.util.snackbar
import org.stepik.android.domain.comment.model.CommentsData
import org.stepik.android.model.Step
import org.stepik.android.model.Submission
import org.stepik.android.model.comments.Comment
import org.stepik.android.model.comments.DiscussionThread
import org.stepik.android.model.comments.Vote
import org.stepik.android.presentation.comment.CommentsPresenter
import org.stepik.android.presentation.comment.CommentsView
import org.stepik.android.presentation.comment.model.CommentItem
import org.stepik.android.view.comment.model.DiscussionOrderItem
import org.stepik.android.view.comment.ui.adapter.decorator.CommentItemDecoration
import org.stepik.android.view.comment.ui.adapter.delegate.CommentDataAdapterDelegate
import org.stepik.android.view.comment.ui.adapter.delegate.CommentLoadMoreRepliesAdapterDelegate
import org.stepik.android.view.comment.ui.adapter.delegate.CommentPlaceholderAdapterDelegate
import org.stepik.android.view.comment.ui.dialog.ComposeCommentDialogFragment
import org.stepik.android.view.comment.ui.dialog.RemoveCommentDialogFragment
import org.stepik.android.view.comment.ui.dialog.SolutionCommentDialogFragment
import org.stepik.android.view.ui.delegate.ViewStateDelegate
import ru.nobird.android.ui.adapters.DefaultDelegateAdapter
import ru.nobird.android.view.base.ui.extension.showIfNotExists
import javax.inject.Inject

class CommentsActivity :
    FragmentActivityBase(),
    CommentsView,
    ComposeCommentDialogFragment.Callback,
    RemoveCommentDialogFragment.Callback {
    companion object {
        private const val EXTRA_DISCUSSION_THREAD = "discussion_thread"
        private const val EXTRA_DISCUSSION_ID = "discussion_id"
        private const val EXTRA_STEP = "step"
        private const val EXTRA_IS_NEED_OPEN_COMPOSE = "is_need_open_compose"

        /**
         * [discussionId] - discussion id from deep link
         */
        fun createIntent(
            context: Context,
            step: Step,
            discussionThread: DiscussionThread,
            discussionId: Long? = null,
            isNeedOpenCompose: Boolean = false
        ): Intent =
            Intent(context, CommentsActivity::class.java)
                .putExtra(EXTRA_STEP, step)
                .putExtra(EXTRA_DISCUSSION_THREAD, discussionThread)
                .putExtra(EXTRA_DISCUSSION_ID, discussionId ?: -1)
                .putExtra(EXTRA_IS_NEED_OPEN_COMPOSE, isNeedOpenCompose)
    }

    @Inject
    internal lateinit var viewModelFactory: ViewModelProvider.Factory

    private lateinit var commentsPresenter: CommentsPresenter

    private lateinit var viewStateDelegate: ViewStateDelegate<CommentsView.State>
    private lateinit var commentsViewStateDelegate: ViewStateDelegate<CommentsView.CommentsState>
    private lateinit var commentsAdapter: DefaultDelegateAdapter<CommentItem>

    private var isMenuOrderGroupVisible: Boolean = false
    private var menuDiscussionOrderItem: DiscussionOrderItem = DiscussionOrderItem.LAST_DISCUSSION

    private val commentPlaceholders = List(10) { CommentItem.Placeholder }

    private val step by lazy { intent.getParcelableExtra<Step>(EXTRA_STEP) }
    private val discussionThread by lazy { intent.getParcelableExtra<DiscussionThread>(EXTRA_DISCUSSION_THREAD) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_comments)

        injectComponent()
        commentsPresenter = ViewModelProviders
            .of(this, viewModelFactory)
            .get(CommentsPresenter::class.java)

        initCenteredToolbar(
            titleRes =
                if (discussionThread.thread == DiscussionThread.THREAD_SOLUTIONS) {
                    R.string.solutions_title
                } else {
                    R.string.comments_title
                },
            showHomeButton = true
        )
        centeredToolbar.overflowIcon =
            AppCompatResources.getDrawable(this, R.drawable.ic_comments_ordering)

        commentsAdapter = DefaultDelegateAdapter()
        commentsAdapter += CommentPlaceholderAdapterDelegate()
        commentsAdapter += CommentDataAdapterDelegate(
            actionListener = object : CommentDataAdapterDelegate.ActionListener {
                override fun onReplyClicked(parentCommentId: Long) {
                    showCommentComposeDialog(step, parent = parentCommentId)
                }

                override fun onVoteClicked(commentDataItem: CommentItem.Data, voteValue: Vote.Value) {
                    commentsPresenter.onChangeVote(commentDataItem, voteValue)
                }

                override fun onEditCommentClicked(commentDataItem: CommentItem.Data) {
                    showCommentComposeDialog(step, commentDataItem.comment.parent, commentDataItem.comment, commentDataItem.solution?.submission)
                }

                override fun onRemoveCommentClicked(commentDataItem: CommentItem.Data) {
                    showRemoveCommentDialog(commentDataItem.id)
                }

                override fun onProfileClicked(commentDataItem: CommentItem.Data) {
                    screenManager.openProfile(this@CommentsActivity, commentDataItem.comment.user ?: return)
                }

                override fun onSolutionClicked(discussionId: Long, solution: CommentItem.Data.Solution) {
                    showSolutionDialog(discussionId, solution)
                }
            }
        )
        commentsAdapter += CommentLoadMoreRepliesAdapterDelegate(commentsPresenter::onLoadMoreReplies)

        with(commentsRecycler) {
            adapter = commentsAdapter
            layoutManager = LinearLayoutManager(context)

            addItemDecoration(CommentItemDecoration(
                separatorColor = ContextCompat.getColor(context, R.color.grey04),
                bigSeparatorBounds =
                    CommentItemDecoration.SeparatorBounds(
                        size = resources.getDimensionPixelSize(R.dimen.comment_item_separator_big),
                        offset = 0
                    ),
                smallSeparatorBounds =
                    CommentItemDecoration.SeparatorBounds(
                        size = resources.getDimensionPixelSize(R.dimen.comment_item_separator_small),
                        offset = resources.getDimensionPixelOffset(R.dimen.comment_item_reply_separator_offset)
                    )
            ))

            setOnPaginationListener(commentsPresenter::onLoadMore)

            (itemAnimator as? SimpleItemAnimator)
                ?.supportsChangeAnimations = false
        }

        viewStateDelegate = ViewStateDelegate()
        viewStateDelegate.addState<CommentsView.State.Idle>()
        viewStateDelegate.addState<CommentsView.State.Loading>(commentsRecycler)<commit_msg>hide fab upon scroll<commit_after>package org.stepik.android.view.comment.ui.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.appcompat.content.res.AppCompatResources
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.SimpleItemAnimator
import kotlinx.android.synthetic.main.activity_comments.*
import kotlinx.android.synthetic.main.empty_comments.*
import kotlinx.android.synthetic.main.empty_comments.view.*
import kotlinx.android.synthetic.main.error_no_connection.*
import kotlinx.android.synthetic.main.view_centered_toolbar.*
import org.stepic.droid.R
import org.stepic.droid.analytic.Analytic
import org.stepic.droid.base.App
import org.stepic.droid.base.FragmentActivityBase
import org.stepic.droid.ui.util.initCenteredToolbar
import org.stepic.droid.ui.util.setOnPaginationListener
import org.stepic.droid.ui.util.snackbar
import org.stepik.android.domain.comment.model.CommentsData
import org.stepik.android.model.Step
import org.stepik.android.model.Submission
import org.stepik.android.model.comments.Comment
import org.stepik.android.model.comments.DiscussionThread
import org.stepik.android.model.comments.Vote
import org.stepik.android.presentation.comment.CommentsPresenter
import org.stepik.android.presentation.comment.CommentsView
import org.stepik.android.presentation.comment.model.CommentItem
import org.stepik.android.view.comment.model.DiscussionOrderItem
import org.stepik.android.view.comment.ui.adapter.decorator.CommentItemDecoration
import org.stepik.android.view.comment.ui.adapter.delegate.CommentDataAdapterDelegate
import org.stepik.android.view.comment.ui.adapter.delegate.CommentLoadMoreRepliesAdapterDelegate
import org.stepik.android.view.comment.ui.adapter.delegate.CommentPlaceholderAdapterDelegate
import org.stepik.android.view.comment.ui.dialog.ComposeCommentDialogFragment
import org.stepik.android.view.comment.ui.dialog.RemoveCommentDialogFragment
import org.stepik.android.view.comment.ui.dialog.SolutionCommentDialogFragment
import org.stepik.android.view.ui.delegate.ViewStateDelegate
import ru.nobird.android.ui.adapters.DefaultDelegateAdapter
import ru.nobird.android.view.base.ui.extension.showIfNotExists
import javax.inject.Inject

class CommentsActivity :
    FragmentActivityBase(),
    CommentsView,
    ComposeCommentDialogFragment.Callback,
    RemoveCommentDialogFragment.Callback {
    companion object {
        private const val EXTRA_DISCUSSION_THREAD = "discussion_thread"
        private const val EXTRA_DISCUSSION_ID = "discussion_id"
        private const val EXTRA_STEP = "step"
        private const val EXTRA_IS_NEED_OPEN_COMPOSE = "is_need_open_compose"

        /**
         * [discussionId] - discussion id from deep link
         */
        fun createIntent(
            context: Context,
            step: Step,
            discussionThread: DiscussionThread,
            discussionId: Long? = null,
            isNeedOpenCompose: Boolean = false
        ): Intent =
            Intent(context, CommentsActivity::class.java)
                .putExtra(EXTRA_STEP, step)
                .putExtra(EXTRA_DISCUSSION_THREAD, discussionThread)
                .putExtra(EXTRA_DISCUSSION_ID, discussionId ?: -1)
                .putExtra(EXTRA_IS_NEED_OPEN_COMPOSE, isNeedOpenCompose)
    }

    @Inject
    internal lateinit var viewModelFactory: ViewModelProvider.Factory

    private lateinit var commentsPresenter: CommentsPresenter

    private lateinit var viewStateDelegate: ViewStateDelegate<CommentsView.State>
    private lateinit var commentsViewStateDelegate: ViewStateDelegate<CommentsView.CommentsState>
    private lateinit var commentsAdapter: DefaultDelegateAdapter<CommentItem>

    private var isMenuOrderGroupVisible: Boolean = false
    private var menuDiscussionOrderItem: DiscussionOrderItem = DiscussionOrderItem.LAST_DISCUSSION

    private val commentPlaceholders = List(10) { CommentItem.Placeholder }

    private val step by lazy { intent.getParcelableExtra<Step>(EXTRA_STEP) }
    private val discussionThread by lazy { intent.getParcelableExtra<DiscussionThread>(EXTRA_DISCUSSION_THREAD) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_comments)

        injectComponent()
        commentsPresenter = ViewModelProviders
            .of(this, viewModelFactory)
            .get(CommentsPresenter::class.java)

        initCenteredToolbar(
            titleRes =
                if (discussionThread.thread == DiscussionThread.THREAD_SOLUTIONS) {
                    R.string.solutions_title
                } else {
                    R.string.comments_title
                },
            showHomeButton = true
        )
        centeredToolbar.overflowIcon =
            AppCompatResources.getDrawable(this, R.drawable.ic_comments_ordering)

        commentsAdapter = DefaultDelegateAdapter()
        commentsAdapter += CommentPlaceholderAdapterDelegate()
        commentsAdapter += CommentDataAdapterDelegate(
            actionListener = object : CommentDataAdapterDelegate.ActionListener {
                override fun onReplyClicked(parentCommentId: Long) {
                    showCommentComposeDialog(step, parent = parentCommentId)
                }

                override fun onVoteClicked(commentDataItem: CommentItem.Data, voteValue: Vote.Value) {
                    commentsPresenter.onChangeVote(commentDataItem, voteValue)
                }

                override fun onEditCommentClicked(commentDataItem: CommentItem.Data) {
                    showCommentComposeDialog(step, commentDataItem.comment.parent, commentDataItem.comment, commentDataItem.solution?.submission)
                }

                override fun onRemoveCommentClicked(commentDataItem: CommentItem.Data) {
                    showRemoveCommentDialog(commentDataItem.id)
                }

                override fun onProfileClicked(commentDataItem: CommentItem.Data) {
                    screenManager.openProfile(this@CommentsActivity, commentDataItem.comment.user ?: return)
                }

                override fun onSolutionClicked(discussionId: Long, solution: CommentItem.Data.Solution) {
                    showSolutionDialog(discussionId, solution)
                }
            }
        )
        commentsAdapter += CommentLoadMoreRepliesAdapterDelegate(commentsPresenter::onLoadMoreReplies)

        with(commentsRecycler) {
            adapter = commentsAdapter
            layoutManager = LinearLayoutManager(context)

            addItemDecoration(CommentItemDecoration(
                separatorColor = ContextCompat.getColor(context, R.color.grey04),
                bigSeparatorBounds =
                    CommentItemDecoration.SeparatorBounds(
                        size = resources.getDimensionPixelSize(R.dimen.comment_item_separator_big),
                        offset = 0
                    ),
                smallSeparatorBounds =
                    CommentItemDecoration.SeparatorBounds(
                        size = resources.getDimensionPixelSize(R.dimen.comment_item_separator_small),
                        offset = resources.getDimensionPixelOffset(R.dimen.comment_item_reply_separator_offset)
                    )
            ))

            setOnPaginationListener(commentsPresenter::onLoadMore)

            addOnScrollListener(object : RecyclerView.OnScrollListener() {
                override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                    super.onScrolled(recyclerView, dx, dy)
                    if (dy > 0 && composeCommentButton.isVisible) {
                        composeCommentButton.hide()
                    } else if (dy < 0 && !composeCommentButton.isVisible) {
                        composeCommentButton.show()
                    }
                }
            })

            (itemAnimator as? SimpleItemAnimator)
                ?.supportsChangeAnimations = false
        }

        viewStateDelegate = ViewStateDelegate()
        viewStateDelegate.addState<CommentsView.State.Idle>()
        viewStateDelegate.addState<CommentsView.State.Loading>(commentsRecycler)
{'fraction_non_alphanumeric': 0.07108170450406702, 'fraction_numerical': 0.000849823965035814, 'mean_word_length': 3.0518937530742747, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, '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': '422400', 'n_tokens_mistral': 4369, 'n_tokens_neox': 4102, 'n_words': 656}