2019-02-12 19:22:37 +01:00
|
|
|
/* Copyright 2019 Conny Duck
|
|
|
|
*
|
|
|
|
* This file is a part of Tusky.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
|
|
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with Tusky; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
|
|
|
|
package com.keylesspalace.tusky
|
|
|
|
|
|
|
|
import android.os.Bundle
|
2019-12-02 16:53:24 +01:00
|
|
|
import android.util.Log
|
2019-02-12 19:22:37 +01:00
|
|
|
import android.view.MenuItem
|
2020-05-15 22:10:29 +02:00
|
|
|
import android.widget.FrameLayout
|
2019-03-24 08:59:55 +01:00
|
|
|
import androidx.appcompat.app.AlertDialog
|
|
|
|
import androidx.appcompat.widget.AppCompatEditText
|
2020-05-15 22:10:29 +02:00
|
|
|
import androidx.core.view.updatePadding
|
2019-02-13 19:20:43 +01:00
|
|
|
import androidx.lifecycle.Lifecycle
|
2019-02-12 19:22:37 +01:00
|
|
|
import androidx.recyclerview.widget.DividerItemDecoration
|
|
|
|
import androidx.recyclerview.widget.ItemTouchHelper
|
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
2020-05-15 22:10:29 +02:00
|
|
|
import at.connyduck.sparkbutton.helpers.Utils
|
2019-02-12 19:22:37 +01:00
|
|
|
import com.keylesspalace.tusky.adapter.ItemInteractionListener
|
2019-12-02 16:53:24 +01:00
|
|
|
import com.keylesspalace.tusky.adapter.ListSelectionAdapter
|
2019-02-12 19:22:37 +01:00
|
|
|
import com.keylesspalace.tusky.adapter.TabAdapter
|
|
|
|
import com.keylesspalace.tusky.appstore.EventHub
|
|
|
|
import com.keylesspalace.tusky.appstore.MainTabsChangedEvent
|
|
|
|
import com.keylesspalace.tusky.di.Injectable
|
2019-12-02 16:53:24 +01:00
|
|
|
import com.keylesspalace.tusky.network.MastodonApi
|
2019-03-24 08:59:55 +01:00
|
|
|
import com.keylesspalace.tusky.util.onTextChanged
|
2019-02-12 19:22:37 +01:00
|
|
|
import com.keylesspalace.tusky.util.visible
|
2019-03-24 08:59:55 +01:00
|
|
|
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider.from
|
2019-09-22 08:19:44 +02:00
|
|
|
import com.uber.autodispose.autoDispose
|
2019-02-13 19:20:43 +01:00
|
|
|
import io.reactivex.Single
|
2019-12-02 16:53:24 +01:00
|
|
|
import io.reactivex.android.schedulers.AndroidSchedulers
|
2019-02-13 19:20:43 +01:00
|
|
|
import io.reactivex.schedulers.Schedulers
|
2019-02-12 19:22:37 +01:00
|
|
|
import kotlinx.android.synthetic.main.activity_tab_preference.*
|
|
|
|
import kotlinx.android.synthetic.main.toolbar_basic.*
|
2019-03-24 08:59:55 +01:00
|
|
|
import java.util.regex.Pattern
|
2019-02-12 19:22:37 +01:00
|
|
|
import javax.inject.Inject
|
|
|
|
|
|
|
|
class TabPreferenceActivity : BaseActivity(), Injectable, ItemInteractionListener {
|
|
|
|
|
2019-12-02 16:53:24 +01:00
|
|
|
@Inject
|
|
|
|
lateinit var mastodonApi: MastodonApi
|
2019-02-12 19:22:37 +01:00
|
|
|
@Inject
|
|
|
|
lateinit var eventHub: EventHub
|
|
|
|
|
|
|
|
private lateinit var currentTabs: MutableList<TabData>
|
|
|
|
private lateinit var currentTabsAdapter: TabAdapter
|
|
|
|
private lateinit var touchHelper: ItemTouchHelper
|
|
|
|
private lateinit var addTabAdapter: TabAdapter
|
|
|
|
|
2019-03-13 09:32:59 +01:00
|
|
|
private var tabsChanged = false
|
|
|
|
|
2019-02-12 19:22:37 +01:00
|
|
|
private val selectedItemElevation by lazy { resources.getDimension(R.dimen.selected_drag_item_elevation) }
|
|
|
|
|
2019-03-24 08:59:55 +01:00
|
|
|
private val hashtagRegex by lazy { Pattern.compile("([\\w_]*[\\p{Alpha}_][\\w_]*)", Pattern.CASE_INSENSITIVE) }
|
|
|
|
|
2019-02-12 19:22:37 +01:00
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
|
|
|
|
setContentView(R.layout.activity_tab_preference)
|
|
|
|
|
|
|
|
setSupportActionBar(toolbar)
|
|
|
|
|
|
|
|
supportActionBar?.apply {
|
|
|
|
setTitle(R.string.title_tab_preferences)
|
|
|
|
setDisplayHomeAsUpEnabled(true)
|
|
|
|
setDisplayShowHomeEnabled(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
currentTabs = (accountManager.activeAccount?.tabPreferences ?: emptyList()).toMutableList()
|
2019-12-19 18:35:55 +01:00
|
|
|
currentTabsAdapter = TabAdapter(currentTabs, false, this, currentTabs.size <= MIN_TAB_COUNT)
|
2019-02-12 19:22:37 +01:00
|
|
|
currentTabsRecyclerView.adapter = currentTabsAdapter
|
|
|
|
currentTabsRecyclerView.layoutManager = LinearLayoutManager(this)
|
|
|
|
currentTabsRecyclerView.addItemDecoration(DividerItemDecoration(this, LinearLayoutManager.VERTICAL))
|
|
|
|
|
|
|
|
addTabAdapter = TabAdapter(listOf(createTabDataFromId(DIRECT)), true, this)
|
|
|
|
addTabRecyclerView.adapter = addTabAdapter
|
|
|
|
addTabRecyclerView.layoutManager = LinearLayoutManager(this)
|
|
|
|
|
2019-03-24 08:59:55 +01:00
|
|
|
touchHelper = ItemTouchHelper(object : ItemTouchHelper.Callback() {
|
2019-02-12 19:22:37 +01:00
|
|
|
override fun getMovementFlags(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder): Int {
|
|
|
|
return makeMovementFlags(ItemTouchHelper.UP or ItemTouchHelper.DOWN, ItemTouchHelper.END)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun isLongPressDragEnabled(): Boolean {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun isItemViewSwipeEnabled(): Boolean {
|
|
|
|
return MIN_TAB_COUNT < currentTabs.size
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean {
|
|
|
|
val temp = currentTabs[viewHolder.adapterPosition]
|
|
|
|
currentTabs[viewHolder.adapterPosition] = currentTabs[target.adapterPosition]
|
|
|
|
currentTabs[target.adapterPosition] = temp
|
|
|
|
|
|
|
|
currentTabsAdapter.notifyItemMoved(viewHolder.adapterPosition, target.adapterPosition)
|
|
|
|
saveTabs()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
|
2019-12-19 18:35:55 +01:00
|
|
|
onTabRemoved(viewHolder.adapterPosition)
|
2019-02-12 19:22:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) {
|
2019-03-24 08:59:55 +01:00
|
|
|
if (actionState == ItemTouchHelper.ACTION_STATE_DRAG) {
|
2019-02-12 19:22:37 +01:00
|
|
|
viewHolder?.itemView?.elevation = selectedItemElevation
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
|
|
|
|
super.clearView(recyclerView, viewHolder)
|
|
|
|
viewHolder.itemView.elevation = 0f
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
touchHelper.attachToRecyclerView(currentTabsRecyclerView)
|
|
|
|
|
|
|
|
|
|
|
|
actionButton.setOnClickListener {
|
|
|
|
actionButton.isExpanded = true
|
|
|
|
}
|
|
|
|
|
|
|
|
scrim.setOnClickListener {
|
|
|
|
actionButton.isExpanded = false
|
|
|
|
}
|
|
|
|
|
2019-02-15 21:35:14 +01:00
|
|
|
maxTabsInfo.text = getString(R.string.max_tab_number_reached, MAX_TAB_COUNT)
|
|
|
|
|
2019-02-12 19:22:37 +01:00
|
|
|
updateAvailableTabs()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onTabAdded(tab: TabData) {
|
2019-03-24 08:59:55 +01:00
|
|
|
|
|
|
|
if (currentTabs.size >= MAX_TAB_COUNT) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
actionButton.isExpanded = false
|
|
|
|
|
|
|
|
if (tab.id == HASHTAG) {
|
2020-05-15 22:10:29 +02:00
|
|
|
showAddHashtagDialog()
|
2019-03-24 08:59:55 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-02 16:53:24 +01:00
|
|
|
if (tab.id == LIST) {
|
|
|
|
showSelectListDialog()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-12 19:22:37 +01:00
|
|
|
currentTabs.add(tab)
|
|
|
|
currentTabsAdapter.notifyItemInserted(currentTabs.size - 1)
|
|
|
|
updateAvailableTabs()
|
|
|
|
saveTabs()
|
|
|
|
}
|
|
|
|
|
2019-12-19 18:35:55 +01:00
|
|
|
override fun onTabRemoved(position: Int) {
|
|
|
|
currentTabs.removeAt(position)
|
|
|
|
currentTabsAdapter.notifyItemRemoved(position)
|
|
|
|
updateAvailableTabs()
|
|
|
|
saveTabs()
|
|
|
|
}
|
|
|
|
|
2019-03-24 08:59:55 +01:00
|
|
|
override fun onActionChipClicked(tab: TabData) {
|
2020-05-15 22:10:29 +02:00
|
|
|
showAddHashtagDialog(tab)
|
2019-03-24 08:59:55 +01:00
|
|
|
}
|
|
|
|
|
2020-05-15 22:10:29 +02:00
|
|
|
override fun onChipClicked(tab: TabData, chipPosition: Int) {
|
|
|
|
val newArguments = tab.arguments.filterIndexed { i, _ -> i != chipPosition }
|
|
|
|
val newTab = tab.copy(arguments = newArguments)
|
|
|
|
val position = currentTabs.indexOf(tab)
|
|
|
|
currentTabs[position] = newTab
|
|
|
|
|
|
|
|
currentTabsAdapter.notifyItemChanged(position)
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun showAddHashtagDialog(tab: TabData? = null) {
|
|
|
|
|
|
|
|
val frameLayout = FrameLayout(this)
|
|
|
|
val padding = Utils.dpToPx(this, 8)
|
|
|
|
frameLayout.updatePadding(left = padding, right = padding)
|
2019-03-24 08:59:55 +01:00
|
|
|
|
|
|
|
val editText = AppCompatEditText(this)
|
|
|
|
editText.setHint(R.string.edit_hashtag_hint)
|
|
|
|
editText.setText("")
|
2020-05-15 22:10:29 +02:00
|
|
|
frameLayout.addView(editText)
|
2019-03-24 08:59:55 +01:00
|
|
|
|
|
|
|
val dialog = AlertDialog.Builder(this)
|
2020-05-15 22:10:29 +02:00
|
|
|
.setTitle(R.string.add_hashtag_title)
|
|
|
|
.setView(frameLayout)
|
2019-03-24 08:59:55 +01:00
|
|
|
.setNegativeButton(android.R.string.cancel, null)
|
|
|
|
.setPositiveButton(R.string.action_save) { _, _ ->
|
|
|
|
val input = editText.text.toString().trim()
|
|
|
|
if (tab == null) {
|
|
|
|
val newTab = createTabDataFromId(HASHTAG, listOf(input))
|
|
|
|
currentTabs.add(newTab)
|
|
|
|
currentTabsAdapter.notifyItemInserted(currentTabs.size - 1)
|
|
|
|
} else {
|
2020-05-15 22:10:29 +02:00
|
|
|
val newTab = tab.copy(arguments = tab.arguments + input)
|
2019-03-24 08:59:55 +01:00
|
|
|
val position = currentTabs.indexOf(tab)
|
|
|
|
currentTabs[position] = newTab
|
|
|
|
|
|
|
|
currentTabsAdapter.notifyItemChanged(position)
|
|
|
|
}
|
|
|
|
|
|
|
|
updateAvailableTabs()
|
|
|
|
saveTabs()
|
|
|
|
}
|
|
|
|
.create()
|
|
|
|
|
|
|
|
editText.onTextChanged { s, _, _, _ ->
|
2019-03-28 21:10:53 +01:00
|
|
|
dialog.getButton(AlertDialog.BUTTON_POSITIVE).isEnabled = validateHashtag(s)
|
2019-03-24 08:59:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
dialog.show()
|
2019-03-28 21:10:53 +01:00
|
|
|
dialog.getButton(AlertDialog.BUTTON_POSITIVE).isEnabled = validateHashtag(editText.text)
|
2019-03-24 08:59:55 +01:00
|
|
|
editText.requestFocus()
|
|
|
|
}
|
|
|
|
|
2019-12-02 16:53:24 +01:00
|
|
|
private fun showSelectListDialog() {
|
|
|
|
val adapter = ListSelectionAdapter(this)
|
|
|
|
mastodonApi.getLists()
|
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
|
|
.autoDispose(from(this, Lifecycle.Event.ON_DESTROY))
|
|
|
|
.subscribe (
|
|
|
|
{ lists ->
|
|
|
|
adapter.addAll(lists)
|
|
|
|
},
|
|
|
|
{ throwable ->
|
|
|
|
Log.e("TabPreferenceActivity", "failed to load lists", throwable)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
AlertDialog.Builder(this)
|
|
|
|
.setTitle(R.string.select_list_title)
|
|
|
|
.setAdapter(adapter) { _, position ->
|
|
|
|
val list = adapter.getItem(position)
|
|
|
|
val newTab = createTabDataFromId(LIST, listOf(list!!.id, list.title))
|
|
|
|
currentTabs.add(newTab)
|
|
|
|
currentTabsAdapter.notifyItemInserted(currentTabs.size - 1)
|
|
|
|
updateAvailableTabs()
|
|
|
|
saveTabs()
|
|
|
|
}
|
|
|
|
.show()
|
|
|
|
}
|
|
|
|
|
2019-03-28 21:10:53 +01:00
|
|
|
private fun validateHashtag(input: CharSequence?): Boolean {
|
|
|
|
val trimmedInput = input?.trim() ?: ""
|
|
|
|
return trimmedInput.isNotEmpty() && hashtagRegex.matcher(trimmedInput).matches()
|
|
|
|
}
|
|
|
|
|
2019-02-12 19:22:37 +01:00
|
|
|
private fun updateAvailableTabs() {
|
|
|
|
val addableTabs: MutableList<TabData> = mutableListOf()
|
|
|
|
|
|
|
|
val homeTab = createTabDataFromId(HOME)
|
2019-03-24 08:59:55 +01:00
|
|
|
if (!currentTabs.contains(homeTab)) {
|
2019-02-12 19:22:37 +01:00
|
|
|
addableTabs.add(homeTab)
|
|
|
|
}
|
|
|
|
val notificationTab = createTabDataFromId(NOTIFICATIONS)
|
2019-03-24 08:59:55 +01:00
|
|
|
if (!currentTabs.contains(notificationTab)) {
|
2019-02-12 19:22:37 +01:00
|
|
|
addableTabs.add(notificationTab)
|
|
|
|
}
|
|
|
|
val localTab = createTabDataFromId(LOCAL)
|
2019-03-24 08:59:55 +01:00
|
|
|
if (!currentTabs.contains(localTab)) {
|
2019-02-12 19:22:37 +01:00
|
|
|
addableTabs.add(localTab)
|
|
|
|
}
|
|
|
|
val federatedTab = createTabDataFromId(FEDERATED)
|
2019-03-24 08:59:55 +01:00
|
|
|
if (!currentTabs.contains(federatedTab)) {
|
2019-02-12 19:22:37 +01:00
|
|
|
addableTabs.add(federatedTab)
|
|
|
|
}
|
|
|
|
val directMessagesTab = createTabDataFromId(DIRECT)
|
2019-03-24 08:59:55 +01:00
|
|
|
if (!currentTabs.contains(directMessagesTab)) {
|
2019-02-12 19:22:37 +01:00
|
|
|
addableTabs.add(directMessagesTab)
|
|
|
|
}
|
|
|
|
|
2019-03-24 08:59:55 +01:00
|
|
|
addableTabs.add(createTabDataFromId(HASHTAG))
|
2019-12-02 16:53:24 +01:00
|
|
|
addableTabs.add(createTabDataFromId(LIST))
|
2019-03-24 08:59:55 +01:00
|
|
|
|
2019-02-12 19:22:37 +01:00
|
|
|
addTabAdapter.updateData(addableTabs)
|
|
|
|
|
|
|
|
maxTabsInfo.visible(addableTabs.size == 0 || currentTabs.size >= MAX_TAB_COUNT)
|
2020-04-06 11:46:38 +02:00
|
|
|
currentTabsAdapter.setRemoveButtonVisible(currentTabs.size > MIN_TAB_COUNT)
|
2019-02-12 19:22:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onStartDelete(viewHolder: RecyclerView.ViewHolder) {
|
|
|
|
touchHelper.startSwipe(viewHolder)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onStartDrag(viewHolder: RecyclerView.ViewHolder) {
|
|
|
|
touchHelper.startDrag(viewHolder)
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun saveTabs() {
|
|
|
|
accountManager.activeAccount?.let {
|
2019-02-13 19:20:43 +01:00
|
|
|
Single.fromCallable {
|
|
|
|
it.tabPreferences = currentTabs
|
|
|
|
accountManager.saveAccount(it)
|
|
|
|
}
|
|
|
|
.subscribeOn(Schedulers.io())
|
2019-09-22 08:19:44 +02:00
|
|
|
.autoDispose(from(this, Lifecycle.Event.ON_DESTROY))
|
2019-02-13 19:20:43 +01:00
|
|
|
.subscribe()
|
|
|
|
|
2019-02-12 19:22:37 +01:00
|
|
|
}
|
2019-03-13 09:32:59 +01:00
|
|
|
tabsChanged = true
|
2019-02-12 19:22:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onBackPressed() {
|
|
|
|
if (actionButton.isExpanded) {
|
|
|
|
actionButton.isExpanded = false
|
|
|
|
} else {
|
|
|
|
super.onBackPressed()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
|
|
|
if (item.itemId == android.R.id.home) {
|
|
|
|
onBackPressed()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onPause() {
|
|
|
|
super.onPause()
|
2019-03-24 08:59:55 +01:00
|
|
|
if (tabsChanged) {
|
2019-03-13 09:32:59 +01:00
|
|
|
eventHub.dispatch(MainTabsChangedEvent(currentTabs))
|
|
|
|
}
|
2019-02-12 19:22:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
private const val MIN_TAB_COUNT = 2
|
|
|
|
private const val MAX_TAB_COUNT = 5
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|