Fix scrobbling ui issues

pull/302/head
Koitharu 3 years ago
parent 8bc8df7625
commit 205a2e10a5
No known key found for this signature in database
GPG Key ID: 8E861F8CE6E7CE27

@ -4,10 +4,12 @@ import android.animation.LayoutTransition
import android.content.Context import android.content.Context
import android.util.AttributeSet import android.util.AttributeSet
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.Menu
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.view.WindowInsets import android.view.WindowInsets
import androidx.annotation.AttrRes import androidx.annotation.AttrRes
import androidx.annotation.MenuRes
import androidx.annotation.StringRes import androidx.annotation.StringRes
import androidx.appcompat.widget.Toolbar import androidx.appcompat.widget.Toolbar
import androidx.coordinatorlayout.widget.CoordinatorLayout import androidx.coordinatorlayout.widget.CoordinatorLayout
@ -15,16 +17,16 @@ import androidx.core.content.withStyledAttributes
import androidx.core.view.* import androidx.core.view.*
import androidx.lifecycle.Lifecycle import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.LifecycleOwner
import com.google.android.material.R as materialR
import com.google.android.material.appbar.AppBarLayout import com.google.android.material.appbar.AppBarLayout
import com.google.android.material.appbar.MaterialToolbar import com.google.android.material.appbar.MaterialToolbar
import com.google.android.material.bottomsheet.BottomSheetBehavior import com.google.android.material.bottomsheet.BottomSheetBehavior
import java.util.*
import org.koitharu.kotatsu.R import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.databinding.LayoutSheetHeaderBinding import org.koitharu.kotatsu.databinding.LayoutSheetHeaderBinding
import org.koitharu.kotatsu.utils.ext.getAnimationDuration import org.koitharu.kotatsu.utils.ext.getAnimationDuration
import org.koitharu.kotatsu.utils.ext.getThemeDrawable import org.koitharu.kotatsu.utils.ext.getThemeDrawable
import org.koitharu.kotatsu.utils.ext.parents import org.koitharu.kotatsu.utils.ext.parents
import java.util.*
import com.google.android.material.R as materialR
private const val THROTTLE_DELAY = 200L private const val THROTTLE_DELAY = 200L
@ -53,6 +55,9 @@ class BottomSheetHeaderBar @JvmOverloads constructor(
val toolbar: MaterialToolbar val toolbar: MaterialToolbar
get() = binding.toolbar get() = binding.toolbar
val menu: Menu
get() = binding.toolbar.menu
var title: CharSequence? var title: CharSequence?
get() = binding.toolbar.title get() = binding.toolbar.title
set(value) { set(value) {
@ -140,6 +145,10 @@ class BottomSheetHeaderBar @JvmOverloads constructor(
binding.toolbar.invalidateMenu() binding.toolbar.invalidateMenu()
} }
fun inflateMenu(@MenuRes resId: Int) {
binding.toolbar.inflateMenu(resId)
}
fun setNavigationOnClickListener(onClickListener: OnClickListener) { fun setNavigationOnClickListener(onClickListener: OnClickListener) {
binding.toolbar.setNavigationOnClickListener(onClickListener) binding.toolbar.setNavigationOnClickListener(onClickListener)
} }
@ -258,6 +267,7 @@ class BottomSheetHeaderBar @JvmOverloads constructor(
} }
lp lp
} }
else -> Toolbar.LayoutParams(params) else -> Toolbar.LayoutParams(params)
} }
} }

@ -2,6 +2,7 @@ package org.koitharu.kotatsu.scrobbling.data
import android.content.Context import android.content.Context
import androidx.core.content.edit import androidx.core.content.edit
import org.jsoup.internal.StringUtil.StringJoiner
import org.koitharu.kotatsu.scrobbling.domain.model.ScrobblerService import org.koitharu.kotatsu.scrobbling.domain.model.ScrobblerService
import org.koitharu.kotatsu.scrobbling.domain.model.ScrobblerUser import org.koitharu.kotatsu.scrobbling.domain.model.ScrobblerUser
@ -39,12 +40,12 @@ class ScrobblerStorage(context: Context, service: ScrobblerService) {
remove(KEY_USER) remove(KEY_USER)
return@edit return@edit
} }
val str = buildString { val str = StringJoiner("\n")
appendLine(value.id) .add(value.id)
appendLine(value.nickname) .add(value.nickname)
appendLine(value.avatar) .add(value.avatar)
appendLine(value.service.name) .add(value.service.name)
} .complete()
putString(KEY_USER, str) putString(KEY_USER, str)
} }

@ -3,7 +3,11 @@ package org.koitharu.kotatsu.scrobbling.ui.selector
import android.app.Dialog import android.app.Dialog
import android.content.DialogInterface import android.content.DialogInterface
import android.os.Bundle import android.os.Bundle
import android.view.* import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView import android.widget.AdapterView
import android.widget.ArrayAdapter import android.widget.ArrayAdapter
import android.widget.Toast import android.widget.Toast
@ -12,7 +16,6 @@ import androidx.core.view.isVisible
import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentManager
import coil.ImageLoader import coil.ImageLoader
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
import org.koitharu.kotatsu.R import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.base.domain.MangaIntent import org.koitharu.kotatsu.base.domain.MangaIntent
import org.koitharu.kotatsu.base.ui.BaseBottomSheet import org.koitharu.kotatsu.base.ui.BaseBottomSheet
@ -28,6 +31,7 @@ import org.koitharu.kotatsu.utils.ext.assistedViewModels
import org.koitharu.kotatsu.utils.ext.getDisplayMessage import org.koitharu.kotatsu.utils.ext.getDisplayMessage
import org.koitharu.kotatsu.utils.ext.requireParcelable import org.koitharu.kotatsu.utils.ext.requireParcelable
import org.koitharu.kotatsu.utils.ext.withArgs import org.koitharu.kotatsu.utils.ext.withArgs
import javax.inject.Inject
@AndroidEntryPoint @AndroidEntryPoint
class ScrobblingSelectorBottomSheet : class ScrobblingSelectorBottomSheet :
@ -120,7 +124,7 @@ class ScrobblingSelectorBottomSheet :
return false return false
} }
viewModel.search(query) viewModel.search(query)
binding.headerBar.toolbar.menu.findItem(R.id.action_search)?.collapseActionView() binding.headerBar.menu.findItem(R.id.action_search)?.collapseActionView()
return true return true
} }
@ -128,7 +132,7 @@ class ScrobblingSelectorBottomSheet :
override fun onKey(dialog: DialogInterface?, keyCode: Int, event: KeyEvent?): Boolean { override fun onKey(dialog: DialogInterface?, keyCode: Int, event: KeyEvent?): Boolean {
if (keyCode == KeyEvent.KEYCODE_BACK) { if (keyCode == KeyEvent.KEYCODE_BACK) {
val menuItem = binding.headerBar.toolbar.menu.findItem(R.id.action_search) ?: return false val menuItem = binding.headerBar.menu.findItem(R.id.action_search) ?: return false
if (menuItem.isActionViewExpanded) { if (menuItem.isActionViewExpanded) {
if (event?.action == KeyEvent.ACTION_UP) { if (event?.action == KeyEvent.ACTION_UP) {
menuItem.collapseActionView() menuItem.collapseActionView()
@ -153,8 +157,8 @@ class ScrobblingSelectorBottomSheet :
} }
private fun initOptionsMenu() { private fun initOptionsMenu() {
binding.headerBar.toolbar.inflateMenu(R.menu.opt_shiki_selector) binding.headerBar.inflateMenu(R.menu.opt_shiki_selector)
val searchMenuItem = binding.headerBar.toolbar.menu.findItem(R.id.action_search) val searchMenuItem = binding.headerBar.menu.findItem(R.id.action_search)
searchMenuItem.setOnActionExpandListener(this) searchMenuItem.setOnActionExpandListener(this)
val searchView = searchMenuItem.actionView as SearchView val searchView = searchMenuItem.actionView as SearchView
searchView.setOnQueryTextListener(this) searchView.setOnQueryTextListener(this)
@ -168,7 +172,11 @@ class ScrobblingSelectorBottomSheet :
binding.spinnerScrobblers.isVisible = false binding.spinnerScrobblers.isVisible = false
return return
} }
val adapter = ArrayAdapter(requireContext(), android.R.layout.simple_spinner_item, entries) val adapter = ArrayAdapter(
requireContext(),
android.R.layout.simple_spinner_item,
entries.map { getString(it.scrobblerService.titleResId) },
)
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
binding.spinnerScrobblers.adapter = adapter binding.spinnerScrobblers.adapter = adapter
viewModel.selectedScrobblerIndex.observe(viewLifecycleOwner) { viewModel.selectedScrobblerIndex.observe(viewLifecycleOwner) {

@ -9,7 +9,9 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.Snackbar
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.koitharu.kotatsu.R import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.base.ui.BasePreferenceFragment import org.koitharu.kotatsu.base.ui.BasePreferenceFragment
import org.koitharu.kotatsu.core.network.cookies.MutableCookieJar import org.koitharu.kotatsu.core.network.cookies.MutableCookieJar
@ -18,11 +20,13 @@ import org.koitharu.kotatsu.core.prefs.AppSettings
import org.koitharu.kotatsu.local.data.CacheDir import org.koitharu.kotatsu.local.data.CacheDir
import org.koitharu.kotatsu.local.data.LocalStorageManager import org.koitharu.kotatsu.local.data.LocalStorageManager
import org.koitharu.kotatsu.scrobbling.anilist.data.AniListRepository import org.koitharu.kotatsu.scrobbling.anilist.data.AniListRepository
import org.koitharu.kotatsu.scrobbling.data.ScrobblerRepository
import org.koitharu.kotatsu.scrobbling.shikimori.data.ShikimoriRepository import org.koitharu.kotatsu.scrobbling.shikimori.data.ShikimoriRepository
import org.koitharu.kotatsu.search.domain.MangaSearchRepository import org.koitharu.kotatsu.search.domain.MangaSearchRepository
import org.koitharu.kotatsu.tracker.domain.TrackingRepository import org.koitharu.kotatsu.tracker.domain.TrackingRepository
import org.koitharu.kotatsu.utils.FileSize import org.koitharu.kotatsu.utils.FileSize
import org.koitharu.kotatsu.utils.ext.getDisplayMessage import org.koitharu.kotatsu.utils.ext.getDisplayMessage
import org.koitharu.kotatsu.utils.ext.printStackTraceDebug
import org.koitharu.kotatsu.utils.ext.viewLifecycleScope import org.koitharu.kotatsu.utils.ext.viewLifecycleScope
import javax.inject.Inject import javax.inject.Inject
@ -78,8 +82,8 @@ class HistorySettingsFragment : BasePreferenceFragment(R.string.history_and_cach
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
bindShikimoriSummary() bindScrobblerSummary(AppSettings.KEY_SHIKIMORI, shikimoriRepository)
bindAniListSummary() bindScrobblerSummary(AppSettings.KEY_ANILIST, aniListRepository)
} }
override fun onPreferenceTreeClick(preference: Preference): Boolean { override fun onPreferenceTreeClick(preference: Preference): Boolean {
@ -120,7 +124,7 @@ class HistorySettingsFragment : BasePreferenceFragment(R.string.history_and_cach
AppSettings.KEY_SHIKIMORI -> { AppSettings.KEY_SHIKIMORI -> {
if (!shikimoriRepository.isAuthorized) { if (!shikimoriRepository.isAuthorized) {
launchShikimoriAuth() launchScrobblerAuth(shikimoriRepository)
true true
} else { } else {
super.onPreferenceTreeClick(preference) super.onPreferenceTreeClick(preference)
@ -129,7 +133,7 @@ class HistorySettingsFragment : BasePreferenceFragment(R.string.history_and_cach
AppSettings.KEY_ANILIST -> { AppSettings.KEY_ANILIST -> {
if (!aniListRepository.isAuthorized) { if (!aniListRepository.isAuthorized) {
launchAniListAuth() launchScrobblerAuth(aniListRepository)
true true
} else { } else {
super.onPreferenceTreeClick(preference) super.onPreferenceTreeClick(preference)
@ -199,36 +203,34 @@ class HistorySettingsFragment : BasePreferenceFragment(R.string.history_and_cach
}.show() }.show()
} }
private fun bindShikimoriSummary() { private fun bindScrobblerSummary(key: String, repository: ScrobblerRepository) {
findPreference<Preference>(AppSettings.KEY_SHIKIMORI)?.summary = if (shikimoriRepository.isAuthorized) { val pref = findPreference<Preference>(key) ?: return
getString(R.string.logged_in_as, shikimoriRepository.cachedUser?.nickname) if (!repository.isAuthorized) {
} else { pref.setSummary(R.string.disabled)
getString(R.string.disabled) return
}
} }
val username = repository.cachedUser?.nickname
private fun bindAniListSummary() { if (username != null) {
findPreference<Preference>(AppSettings.KEY_ANILIST)?.summary = if (aniListRepository.isAuthorized) { pref.summary = getString(R.string.logged_in_as, username)
getString(R.string.logged_in_as, aniListRepository.cachedUser?.nickname)
} else { } else {
getString(R.string.disabled) pref.setSummary(R.string.loading_)
viewLifecycleScope.launch {
pref.summary = withContext(Dispatchers.Default) {
runCatching {
repository.loadUser().nickname
}.getOrElse {
it.printStackTraceDebug()
it.getDisplayMessage(resources)
}
} }
} }
private fun launchShikimoriAuth() {
runCatching {
val intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse(shikimoriRepository.oauthUrl)
startActivity(intent)
}.onFailure {
Snackbar.make(listView, it.getDisplayMessage(resources), Snackbar.LENGTH_LONG).show()
} }
} }
private fun launchAniListAuth() { private fun launchScrobblerAuth(repository: ScrobblerRepository) {
runCatching { runCatching {
val intent = Intent(Intent.ACTION_VIEW) val intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse(aniListRepository.oauthUrl) intent.data = Uri.parse(repository.oauthUrl)
startActivity(intent) startActivity(intent)
}.onFailure { }.onFailure {
Snackbar.make(listView, it.getDisplayMessage(resources), Snackbar.LENGTH_LONG).show() Snackbar.make(listView, it.getDisplayMessage(resources), Snackbar.LENGTH_LONG).show()

@ -8,7 +8,8 @@
<androidx.constraintlayout.widget.ConstraintLayout <androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"> android:layout_height="wrap_content"
android:paddingBottom="16dp">
<com.google.android.material.bottomsheet.BottomSheetDragHandleView <com.google.android.material.bottomsheet.BottomSheetDragHandleView
android:id="@+id/dragHandle" android:id="@+id/dragHandle"
@ -103,7 +104,6 @@
android:layout_marginStart="16dp" android:layout_marginStart="16dp"
android:layout_marginTop="12dp" android:layout_marginTop="12dp"
android:layout_marginEnd="16dp" android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:lineSpacingMultiplier="1.2" android:lineSpacingMultiplier="1.2"
android:textAppearance="?attr/textAppearanceBodyMedium" android:textAppearance="?attr/textAppearanceBodyMedium"
android:textIsSelectable="true" android:textIsSelectable="true"

Loading…
Cancel
Save