mirror of https://github.com/tasks/tasks
TagPickerActivity and TagRecycleAdapter removed from working set because they've got incompatible with TagPickerViewMode and shall not be used in build but they are because of HILT
parent
957dc66b75
commit
38d2957e84
@ -1,112 +0,0 @@
|
||||
package org.tasks.tags
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.widget.EditText
|
||||
import androidx.activity.viewModels
|
||||
import androidx.compose.ui.state.ToggleableState
|
||||
import androidx.core.widget.addTextChangedListener
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.recyclerview.widget.DefaultItemAnimator
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import kotlinx.coroutines.launch
|
||||
import org.tasks.R
|
||||
import org.tasks.Strings.isNullOrEmpty
|
||||
import org.tasks.billing.Inventory
|
||||
import org.tasks.data.TagData
|
||||
import org.tasks.databinding.ActivityTagPickerBinding
|
||||
import org.tasks.injection.ThemedInjectingAppCompatActivity
|
||||
import org.tasks.themes.ColorProvider
|
||||
import org.tasks.themes.Theme
|
||||
import java.util.*
|
||||
import javax.inject.Inject
|
||||
|
||||
@AndroidEntryPoint
|
||||
class TagPickerActivity : ThemedInjectingAppCompatActivity() {
|
||||
@Inject lateinit var theme: Theme
|
||||
@Inject lateinit var inventory: Inventory
|
||||
@Inject lateinit var colorProvider: ColorProvider
|
||||
|
||||
private val viewModel: TagPickerViewModel by viewModels()
|
||||
private var taskIds: ArrayList<Long>? = null
|
||||
private lateinit var editText: EditText
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
val intent = intent
|
||||
taskIds = intent.getSerializableExtra(EXTRA_TASKS) as ArrayList<Long>?
|
||||
if (savedInstanceState == null) {
|
||||
intent.getParcelableArrayListExtra<TagData>(EXTRA_SELECTED)?.let {
|
||||
viewModel.setSelected(
|
||||
it,
|
||||
intent.getParcelableArrayListExtra(EXTRA_PARTIALLY_SELECTED)
|
||||
)
|
||||
}
|
||||
}
|
||||
val binding = ActivityTagPickerBinding.inflate(layoutInflater)
|
||||
editText = binding.searchInput.apply {
|
||||
addTextChangedListener(
|
||||
onTextChanged = { text, _, _, _ -> onSearch(text) }
|
||||
)
|
||||
}
|
||||
setContentView(binding.root)
|
||||
val toolbar = binding.toolbar
|
||||
toolbar.setNavigationIcon(R.drawable.ic_outline_arrow_back_24px)
|
||||
toolbar.setNavigationOnClickListener { onBackPressed() }
|
||||
val themeColor = theme.themeColor
|
||||
themeColor.applyToNavigationBar(this)
|
||||
val recyclerAdapter = TagRecyclerAdapter(this, viewModel, inventory, colorProvider) { tagData, vh ->
|
||||
onToggle(tagData, vh)
|
||||
}
|
||||
val recyclerView = binding.recyclerView
|
||||
recyclerView.adapter = recyclerAdapter
|
||||
(recyclerView.itemAnimator as DefaultItemAnimator?)!!.supportsChangeAnimations = false
|
||||
recyclerView.layoutManager = LinearLayoutManager(this)
|
||||
viewModel.observe(this) { recyclerAdapter.submitList(it) }
|
||||
editText.setText(viewModel.text)
|
||||
}
|
||||
|
||||
private fun onToggle(tagData: TagData, vh: TagPickerViewHolder) = lifecycleScope.launch {
|
||||
val newTag = tagData.id == null
|
||||
val newState = viewModel.toggle(tagData, vh.isChecked || newTag)
|
||||
vh.updateCheckbox(
|
||||
when (newState) {
|
||||
ToggleableState.On -> CheckBoxTriStates.State.CHECKED
|
||||
ToggleableState.Off -> CheckBoxTriStates.State.UNCHECKED
|
||||
else -> CheckBoxTriStates.State.PARTIALLY_CHECKED
|
||||
}
|
||||
)
|
||||
if (newTag) {
|
||||
clear()
|
||||
}
|
||||
}
|
||||
|
||||
private fun onSearch(text: CharSequence?) {
|
||||
viewModel.search(text?.toString() ?: "")
|
||||
}
|
||||
|
||||
override fun onBackPressed() {
|
||||
if (isNullOrEmpty(viewModel.text)) {
|
||||
val data = Intent()
|
||||
data.putExtra(EXTRA_TASKS, taskIds)
|
||||
data.putParcelableArrayListExtra(EXTRA_PARTIALLY_SELECTED, viewModel.getPartiallySelected())
|
||||
data.putParcelableArrayListExtra(EXTRA_SELECTED, viewModel.getSelected())
|
||||
setResult(Activity.RESULT_OK, data)
|
||||
finish()
|
||||
} else {
|
||||
clear()
|
||||
}
|
||||
}
|
||||
|
||||
private fun clear() {
|
||||
editText.setText("")
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val EXTRA_SELECTED = "extra_tags"
|
||||
const val EXTRA_PARTIALLY_SELECTED = "extra_partial"
|
||||
const val EXTRA_TASKS = "extra_tasks"
|
||||
}
|
||||
}
|
@ -1,64 +0,0 @@
|
||||
package org.tasks.tags
|
||||
|
||||
import android.content.Context
|
||||
import android.view.LayoutInflater
|
||||
import android.view.ViewGroup
|
||||
import androidx.compose.ui.state.ToggleableState
|
||||
import androidx.recyclerview.widget.AsyncListDiffer
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import org.tasks.R
|
||||
import org.tasks.billing.Inventory
|
||||
import org.tasks.data.TagData
|
||||
import org.tasks.databinding.RowTagPickerBinding
|
||||
import org.tasks.themes.ColorProvider
|
||||
import org.tasks.themes.CustomIcons.getIconResId
|
||||
|
||||
internal class TagRecyclerAdapter(
|
||||
private val context: Context,
|
||||
private val viewModel: TagPickerViewModel,
|
||||
private val inventory: Inventory,
|
||||
private val colorProvider: ColorProvider,
|
||||
private val callback: (TagData, TagPickerViewHolder) -> Unit
|
||||
) : RecyclerView.Adapter<TagPickerViewHolder>() {
|
||||
|
||||
private val differ: AsyncListDiffer<TagData> = AsyncListDiffer(this, TagDiffCallback())
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
|
||||
TagPickerViewHolder(
|
||||
context,
|
||||
RowTagPickerBinding.inflate(LayoutInflater.from(context), parent, false),
|
||||
callback
|
||||
)
|
||||
|
||||
override fun onBindViewHolder(holder: TagPickerViewHolder, position: Int) {
|
||||
val tagData = differ.currentList[position]
|
||||
val backCompat: (ToggleableState) -> CheckBoxTriStates.State =
|
||||
{ state: ToggleableState ->
|
||||
when (state) {
|
||||
ToggleableState.On -> CheckBoxTriStates.State.CHECKED
|
||||
ToggleableState.Off -> CheckBoxTriStates.State.UNCHECKED
|
||||
else -> CheckBoxTriStates.State.PARTIALLY_CHECKED
|
||||
}
|
||||
}
|
||||
holder.bind(tagData, getColor(tagData), getIcon(tagData), backCompat(viewModel.getState(tagData)))
|
||||
}
|
||||
|
||||
override fun getItemCount(): Int = differ.currentList.size
|
||||
|
||||
private fun getColor(tagData: TagData): Int {
|
||||
if (tagData.getColor() != 0) {
|
||||
val themeColor = colorProvider.getThemeColor(tagData.getColor()!!, true)
|
||||
if (inventory.purchasedThemes() || themeColor.isFree) {
|
||||
return themeColor.primaryColor
|
||||
}
|
||||
}
|
||||
return context.getColor(R.color.icon_tint_with_alpha)
|
||||
}
|
||||
|
||||
private fun getIcon(tagData: TagData): Int? =
|
||||
tagData.getIcon()?.takeIf { it < 1000 || inventory.hasPro }?.let { getIconResId(it) }
|
||||
|
||||
fun submitList(tagData: List<TagData>?) {
|
||||
differ.submitList(tagData)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue