Skip to content

Commit

Permalink
add sort and categories floating buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
tsynik committed Apr 19, 2024
1 parent 7dae9ae commit b3234bd
Show file tree
Hide file tree
Showing 20 changed files with 587 additions and 76 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/ru/yourok/torrserve/server/api/Api.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ object Api {
val host = Net.getHostUrl("/torrents")
val req = TorrentReq("list").toString()
val resp = postJson(host, req)
return if (Settings.sortTorrByTitle())
return if (Settings.sortTorrByTitle)
Gson().fromJson(resp, Array<Torrent>::class.java).toList().sortedWith(compareBy { it.title })
else
Gson().fromJson(resp, Array<Torrent>::class.java).toList()
Expand Down
13 changes: 9 additions & 4 deletions app/src/main/java/ru/yourok/torrserve/settings/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ import ru.yourok.torrserve.server.api.Api
import java.io.File

object Settings {
val showFab: Boolean
get() = get("show_fab", true)

val showSortFab: Boolean
get() = get("show_sort_fab", true)

val sortTorrByTitle: Boolean
get() = get("sort_torrents", false)

fun getServerAuth(): String = get("server_auth", "").trim()
fun useLocalAuth(): Boolean = get("local_auth", false)
Expand Down Expand Up @@ -43,9 +51,6 @@ object Settings {
fun setShowBanner(v: Boolean) = set("show_banner", v)

fun showCover(): Boolean = get("show_cover", true)
fun sortTorrByTitle(): Boolean = get("sort_torrents", false)
fun showFab(): Boolean = get("show_fab", true)

fun getTheme(): String = get("theme", "auto")
fun setTheme(v: String) = set("theme", v)

Expand Down Expand Up @@ -88,7 +93,7 @@ object Settings {
suspend fun isShowCat(): Boolean {
return try {
val vi = Api.getMatrixVersionInt()
vi > 131 && get("show_cats", false) // MatriX.132 add Categories
vi > 131 && get("show_cat_fab", true) // MatriX.132 add Categories
} catch (e: Exception) {
false
}
Expand Down
213 changes: 192 additions & 21 deletions app/src/main/java/ru/yourok/torrserve/ui/activities/main/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import kotlinx.coroutines.withContext
import ru.yourok.torrserve.BuildConfig
import ru.yourok.torrserve.R
import ru.yourok.torrserve.app.App
import ru.yourok.torrserve.atv.Utils
import ru.yourok.torrserve.ext.clearStackFragment
import ru.yourok.torrserve.server.api.Api
import ru.yourok.torrserve.server.local.ServerFile
Expand Down Expand Up @@ -139,7 +140,8 @@ class MainActivity : AppCompatActivity() {
themeUtil.onResume(this)
//TorrService.start()
updateStatus()
if (Settings.showFab()) setupFab()
if (Settings.showFab) setupFab()
if (Settings.showSortFab) setupSortFab()
lifecycleScope.launch(Dispatchers.IO) {
if (Settings.isShowCat()) {
withContext(Dispatchers.Main) {
Expand Down Expand Up @@ -186,15 +188,17 @@ class MainActivity : AppCompatActivity() {
override fun onDrawerOpened(drawerView: View) {
super.onDrawerOpened(drawerView)

if (Settings.showFab()) showFab(false)
if (Settings.showFab) showFab(false)
if (Settings.showSortFab) showSortFab(false)
lifecycleScope.launch(Dispatchers.IO) {
if (Settings.isShowCat()) withContext(Dispatchers.Main) { showCatFab(false) }
}
}

override fun onDrawerClosed(drawerView: View) {
super.onDrawerClosed(drawerView)
if (Settings.showFab()) showFab(true)
if (Settings.showFab) showFab(true)
if (Settings.showSortFab && isInTorrents) showSortFab(true)
lifecycleScope.launch(Dispatchers.IO) {
if (Settings.isShowCat() && isInTorrents) withContext(Dispatchers.Main) { showCatFab(true) } else withContext(Dispatchers.Main) { showCatFab(false) }
}
Expand Down Expand Up @@ -265,15 +269,8 @@ class MainActivity : AppCompatActivity() {
}
}

private fun showFab(show: Boolean = true) {
val fab: FloatingActionButton? = findViewById(R.id.fab)
if (show)
fab?.show()
else
fab?.hide()
}

private fun setupFab() { // Fab
if (Utils.isTvBox()) return
val fab: FloatingActionButton? = findViewById(R.id.fab)
fab?.apply {
setImageDrawable(AppCompatResources.getDrawable(this.context, R.mipmap.ic_launcher))
Expand All @@ -294,30 +291,204 @@ class MainActivity : AppCompatActivity() {
}
}

private fun showCatFab(show: Boolean = true) {
val fab: FloatingActionButton? = findViewById(R.id.cat_fab)
private fun showFab(show: Boolean = true) {
val fab: FloatingActionButton? = findViewById(R.id.fab)
if (show)
fab?.show()
else
fab?.hide()
}

private fun setupCatFab() { // categories options menu
val fab: FloatingActionButton? = findViewById(R.id.cat_fab)
val color = ThemeUtil.getColorFromAttr(this, R.attr.colorAccent)
private fun setupSortFab() {
if (Utils.isTvBox()) return

val accentColor = ThemeUtil.getColorFromAttr(this, R.attr.colorAccent)
val actionsColor = ThemeUtil.getColorFromAttr(this, R.attr.colorMainMenu)

val fab: FloatingActionButton? = findViewById(R.id.sort_fab)
fab?.apply {
setImageDrawable(AppCompatResources.getDrawable(this.context, R.drawable.round_view_list_24))
if (Settings.sortTorrByTitle)
setImageDrawable(AppCompatResources.getDrawable(this.context, R.drawable.round_filter_list_24))
else
setImageDrawable(AppCompatResources.getDrawable(this.context, R.drawable.round_sort_by_alpha_24))
customSize = dp2px(32f)
setMaxImageSize(dp2px(24f))
backgroundTintList = ColorStateList.valueOf(
color
)
backgroundTintList = ColorStateList.valueOf(actionsColor)
setColorFilter(accentColor)
setRippleColor(ColorStateList.valueOf(accentColor))
setOnClickListener {
// TODO
if (isInTorrents) {
val f = supportFragmentManager.findFragmentById(R.id.container)
(f as TorrentsFragment?)?.sort()
}
if (Settings.sortTorrByTitle)
setImageDrawable(AppCompatResources.getDrawable(this.context, R.drawable.round_filter_list_24))
else
setImageDrawable(AppCompatResources.getDrawable(this.context, R.drawable.round_sort_by_alpha_24))
}
}
// visibility change
if (isInTorrents)
showSortFab(true)
else
showSortFab(false)
}

private fun showSortFab(show: Boolean = true) {
val fab: FloatingActionButton? = findViewById(R.id.sort_fab)
if (show)
fab?.show()
else
fab?.hide()
}

private fun showCatFab(show: Boolean = true) {
val fab: FloatingActionButton? = findViewById(R.id.cat_fab)
if (show) {
fab?.show()
} else {
hideActions()
isCatsOpen = false
fab?.hide()
}
}

private var isCatsOpen = false
private var movFab: FloatingActionButton? = null
private var movText: TextView? = null
private var tvFab: FloatingActionButton? = null
private var tvText: TextView? = null
private var musFab: FloatingActionButton? = null
private var musText: TextView? = null
private var othFab: FloatingActionButton? = null
private var othText: TextView? = null
private var allFab: FloatingActionButton? = null
private var allText: TextView? = null

private fun setupCatFab() { // categories options menu
if (Utils.isTvBox()) return

val accentColor = ThemeUtil.getColorFromAttr(this, R.attr.colorAccent)
val actionsColor = ThemeUtil.getColorFromAttr(this, R.attr.colorMainMenu)
//val textColor = ThemeUtil.getColorFromAttr(this, R.attr.colorBright)

val catFab: FloatingActionButton? = findViewById(R.id.cat_fab)
movFab = findViewById(R.id.mov_fab)
movText = findViewById<TextView?>(R.id.mov_fab_text)?.apply { setTextColor(accentColor) }
tvFab = findViewById(R.id.tv_fab)
tvText = findViewById<TextView?>(R.id.tv_fab_text)?.apply { setTextColor(accentColor) }
musFab = findViewById(R.id.mus_fab)
musText = findViewById<TextView?>(R.id.mus_fab_text)?.apply { setTextColor(accentColor) }
othFab = findViewById(R.id.oth_fab)
othText = findViewById<TextView?>(R.id.oth_fab_text)?.apply { setTextColor(accentColor) }
allFab = findViewById(R.id.all_fab)
allText = findViewById<TextView?>(R.id.all_fab_text)?.apply { setTextColor(accentColor) }
catFab?.apply {
setImageDrawable(AppCompatResources.getDrawable(this.context, R.drawable.round_view_list_24))
customSize = dp2px(32f)
setMaxImageSize(dp2px(24f))
backgroundTintList = ColorStateList.valueOf(actionsColor)
setColorFilter(accentColor)
setRippleColor(ColorStateList.valueOf(accentColor))
setOnClickListener {
if (!isCatsOpen) {
showActions()
} else {
hideActions()
}
isCatsOpen = !isCatsOpen
}
}
movFab?.apply {
backgroundTintList = ColorStateList.valueOf(actionsColor)
setColorFilter(accentColor)
setRippleColor(ColorStateList.valueOf(accentColor))
setOnClickListener {
filterTorrents("movie")
hideActions()
isCatsOpen = false
}
}
tvFab?.apply {
backgroundTintList = ColorStateList.valueOf(actionsColor)
setColorFilter(accentColor)
setRippleColor(ColorStateList.valueOf(accentColor))
setOnClickListener {
filterTorrents("tv")
hideActions()
isCatsOpen = false
}
}
musFab?.apply {
backgroundTintList = ColorStateList.valueOf(actionsColor)
setColorFilter(accentColor)
setRippleColor(ColorStateList.valueOf(accentColor))
setOnClickListener {
filterTorrents("music")
hideActions()
isCatsOpen = false
}
}
othFab?.apply {
backgroundTintList = ColorStateList.valueOf(actionsColor)
setColorFilter(accentColor)
setRippleColor(ColorStateList.valueOf(accentColor))
setOnClickListener {
filterTorrents("other")
hideActions()
isCatsOpen = false
}
}
allFab?.apply {
backgroundTintList = ColorStateList.valueOf(actionsColor)
setColorFilter(accentColor)
setRippleColor(ColorStateList.valueOf(accentColor))
setOnClickListener {
filterTorrents()
hideActions()
isCatsOpen = false
}
}
// visibility change
if (isInTorrents) {
showCatFab(true)
//hideActions()
} else {
showCatFab(false)
}
}

private fun showActions() {
movFab?.show()
tvFab?.show()
musFab?.show()
othFab?.show()
allFab?.show()
movText?.visibility = View.VISIBLE
tvText?.visibility = View.VISIBLE
musText?.visibility = View.VISIBLE
othText?.visibility = View.VISIBLE
allText?.visibility = View.VISIBLE
}

private fun hideActions() {
movFab?.hide()
tvFab?.hide()
musFab?.hide()
othFab?.hide()
allFab?.hide()
movText?.visibility = View.GONE
tvText?.visibility = View.GONE
musText?.visibility = View.GONE
othText?.visibility = View.GONE
allText?.visibility = View.GONE
}

private fun filterTorrents(category: String = "") {
if (isInTorrents) {
val f = supportFragmentManager.findFragmentById(R.id.container)
lifecycleScope.launch { (f as TorrentsFragment?)?.filter(category) }
}
}

private fun setupNavigator() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ class AddFragment : TSFragment() {

@SuppressLint("NotifyDataSetChanged")
private fun sortResults() {
val list = torrsAdapter.list.toMutableList()
val list = torrsAdapter.list //.toMutableList()
when (sortMode) {
0 -> {
torrsAdapter.set(list.sortedBy { it.Title })
Expand Down Expand Up @@ -303,7 +303,6 @@ class AddFragment : TSFragment() {
}

private fun setupSortFab() { // Sort Fab
//if (Utils.isTV()) return
val fab: FloatingActionButton? = requireActivity().findViewById(R.id.sortFab)
val themedContext = ContextThemeWrapper(App.context, ThemeUtil.selectedTheme)
val selectedColor = getColorFromAttr(themedContext, R.attr.colorPrimary)
Expand Down Expand Up @@ -337,13 +336,11 @@ class AddFragment : TSFragment() {
}

private fun showSortFab() {
//if (Utils.isTV()) return
val fab: FloatingActionButton? = requireActivity().findViewById(R.id.sortFab)
fab?.show()
}

private fun hideSortFab() {
//if (Utils.isTV()) return
val fab: FloatingActionButton? = requireActivity().findViewById(R.id.sortFab)
fab?.hide()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,20 @@ class SettingsFragment : PreferenceFragmentCompat() {
}
}

findPreference<SwitchPreferenceCompat>("show_sort_fab")?.apply {
setOnPreferenceClickListener {
requireActivity().recreate()
true
}
}

findPreference<SwitchPreferenceCompat>("show_cat_fab")?.apply {
setOnPreferenceClickListener {
requireActivity().recreate()
true
}
}

findPreference<ListPreference>("app_theme")?.apply {
val darkMode = if (isDarkMode(this.context)) "NM" else "DM"
summary = "$summary (${darkMode})"
Expand Down Expand Up @@ -228,11 +242,15 @@ class SettingsFragment : PreferenceFragmentCompat() {
true
}
}
// hide FAB pref on TVs (no FAB in landscape)
// hide FAB prefs on TVs (no FAB in landscape)
val fabPref = findPreference<Preference>("show_fab")
if (Utils.isTvBox())
val sortFabPref = findPreference<Preference>("show_sort_fab")
val catFabPref = findPreference<Preference>("show_cat_fab")
if (Utils.isTvBox()) {
fabPref?.let { ps?.removePreference(it) }

sortFabPref?.let { ps?.removePreference(it) }
catFabPref?.let { ps?.removePreference(it) }
}
}

override fun onResume() {
Expand Down
Loading

0 comments on commit b3234bd

Please sign in to comment.