<commit_before>/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.trackr.ui.archives

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.doOnNextLayout
import androidx.core.view.updatePadding
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import com.example.android.trackr.R
import com.example.android.trackr.data.User
import com.example.android.trackr.databinding.ArchiveFragmentBinding
import com.example.android.trackr.ui.dataBindings
import com.example.android.trackr.ui.detail.TaskDetailFragmentArgs
import com.example.android.trackr.ui.utils.doOnApplyWindowInsets
import com.example.android.trackr.ui.utils.repeatWithViewLifecycle
import com.google.android.material.snackbar.Snackbar
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import org.threeten.bp.Clock
import javax.inject.Inject

@AndroidEntryPoint
class ArchivesFragment : Fragment(R.layout.archive_fragment) {

    private val viewModel: ArchiveViewModel by viewModels()
    private val binding by dataBindings(ArchiveFragmentBinding::bind)

    @Inject
    lateinit var currentUser: User

    @Inject
    lateinit var clock: Clock

    @SuppressLint("ShowToast")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        binding.viewModel = viewModel

        val adapter = ArchiveAdapter(
            currentUser = currentUser,
            clock = clock,
            onItemClick = { task ->
                if (viewModel.selectedCount.value > 0) {
                    viewModel.toggleTaskSelection(task.id)
                } else {
                    navigateToDetail(task.id)
                }
            },
            onItemLongClick = { task -> viewModel.toggleTaskSelection(task.id) },
            onItemStarClicked = { task -> viewModel.toggleTaskStarState(task.id) }
        )
        binding.archivedTasks.adapter = adapter

        repeatWithViewLifecycle {
            launch {
                viewModel.archivedTasks.collect {
                    adapter.submitList(it)
                }
            }
            // Undo unarchiving tasks
            launch {
                viewModel.unarchiveActions.collect { action ->
                    val count = action.taskIds.size
                    Snackbar.make(
                        binding.coordinator,
                        resources.getQuantityString(
                            R.plurals.tasks_unarchived,
                            count,
                            count
                        ),
                        5_000 // 5 seconds
                    )
                        .setAction(R.string.undo) { viewModel.undoUnarchiving(action) }
                        .show()
                }
            }
        }

        binding.bottomBar.setNavigationOnClickListener { viewModel.clearSelection() }
        binding.bottomBar.setOnMenuItemClickListener { item ->
            when (item.itemId) {
                R.id.action_unarchive -> {
                    viewModel.unarchiveSelectedTasks()
                    true
                }
                else -> false
            }
        }

        binding.archivedTasks.doOnApplyWindowInsets { v, insets, padding, _ ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            // BottomAppBar has its own logic to adapt to window insets, but its height isn't
            // updated until measurement, so wait for its next layout.
            binding.bottomBar.doOnNextLayout { bottomBar ->
                v.updatePadding(
                    left = padding.left + systemBars.left,
                    right = padding.right + systemBars.right,
                    bottom = padding.bottom + bottomBar.height
                )
            }
        }
    }

    private fun navigateToDetail(taskId: Long) {
        findNavController().navigate(
            R.id.nav_task_detail,
            TaskDetailFragmentArgs(taskId).toBundle()
        )
    }
}<commit_msg>Cancel selection mode on back press in Archive<commit_after>/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.trackr.ui.archives

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.doOnNextLayout
import androidx.core.view.updatePadding
import androidx.activity.OnBackPressedCallback
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import com.example.android.trackr.R
import com.example.android.trackr.data.User
import com.example.android.trackr.databinding.ArchiveFragmentBinding
import com.example.android.trackr.ui.dataBindings
import com.example.android.trackr.ui.detail.TaskDetailFragmentArgs
import com.example.android.trackr.ui.utils.doOnApplyWindowInsets
import com.example.android.trackr.ui.utils.repeatWithViewLifecycle
import com.google.android.material.snackbar.Snackbar
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import org.threeten.bp.Clock
import javax.inject.Inject

@AndroidEntryPoint
class ArchivesFragment : Fragment(R.layout.archive_fragment) {

    private val viewModel: ArchiveViewModel by viewModels()
    private val binding by dataBindings(ArchiveFragmentBinding::bind)
    private val backPressCallback = BackPressCallback()

    @Inject
    lateinit var currentUser: User

    @Inject
    lateinit var clock: Clock

    @SuppressLint("ShowToast")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        binding.viewModel = viewModel

        val adapter = ArchiveAdapter(
            currentUser = currentUser,
            clock = clock,
            onItemClick = { task ->
                if (viewModel.selectedCount.value > 0) {
                    viewModel.toggleTaskSelection(task.id)
                } else {
                    navigateToDetail(task.id)
                }
            },
            onItemLongClick = { task -> viewModel.toggleTaskSelection(task.id) },
            onItemStarClicked = { task -> viewModel.toggleTaskStarState(task.id) }
        )
        binding.archivedTasks.adapter = adapter

        repeatWithViewLifecycle {
            launch {
                viewModel.archivedTasks.collect {
                    adapter.submitList(it)
                }
            }
            // Undo unarchiving tasks
            launch {
                viewModel.unarchiveActions.collect { action ->
                    val count = action.taskIds.size
                    Snackbar.make(
                        binding.coordinator,
                        resources.getQuantityString(
                            R.plurals.tasks_unarchived,
                            count,
                            count
                        ),
                        5_000 // 5 seconds
                    )
                        .setAction(R.string.undo) { viewModel.undoUnarchiving(action) }
                        .show()
                }
            }
            launch {
                viewModel.selectedCount.collect {
                    backPressCallback.isEnabled = it > 0
                }
            }
        }

        binding.bottomBar.setNavigationOnClickListener { viewModel.clearSelection() }
        binding.bottomBar.setOnMenuItemClickListener { item ->
            when (item.itemId) {
                R.id.action_unarchive -> {
                    viewModel.unarchiveSelectedTasks()
                    true
                }
                else -> false
            }
        }

        binding.archivedTasks.doOnApplyWindowInsets { v, insets, padding, _ ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            // BottomAppBar has its own logic to adapt to window insets, but its height isn't
            // updated until measurement, so wait for its next layout.
            binding.bottomBar.doOnNextLayout { bottomBar ->
                v.updatePadding(
                    left = padding.left + systemBars.left,
                    right = padding.right + systemBars.right,
                    bottom = padding.bottom + bottomBar.height
                )
            }
        }

        requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner, backPressCallback)
    }

    private fun navigateToDetail(taskId: Long) {
        findNavController().navigate(
            R.id.nav_task_detail,
            TaskDetailFragmentArgs(taskId).toBundle()
        )
    }

    inner class BackPressCallback : OnBackPressedCallback(false) {

        override fun handleOnBackPressed() {
            viewModel.clearSelection()
        }
    }
}
{'fraction_non_alphanumeric': 0.07457393360260073, 'fraction_numerical': 0.0028568613929662104, 'mean_word_length': 2.0106761565836297, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2407216', 'n_tokens_mistral': 2624, 'n_tokens_neox': 2499, 'n_words': 578}