Merge pull request #758 from Bnyro/master

btn UI improvements and bug fixes
This commit is contained in:
Bnyro 2022-07-11 16:40:38 +02:00 committed by GitHub
commit 15e3c59fb5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 56 additions and 69 deletions

View File

@ -177,11 +177,13 @@ class SearchAdapter(
binding.searchSubButton.setOnClickListener { binding.searchSubButton.setOnClickListener {
if (!isSubscribed) { if (!isSubscribed) {
subscribe(token, channelId) subscribe(token, channelId)
binding.searchSubButton.text = binding.root.context.getString(R.string.unsubscribe) binding.searchSubButton.text =
binding.root.context.getString(R.string.unsubscribe)
isSubscribed = true isSubscribed = true
} else { } else {
unsubscribe(token, channelId) unsubscribe(token, channelId)
binding.searchSubButton.text = binding.root.context.getString(R.string.subscribe) binding.searchSubButton.text =
binding.root.context.getString(R.string.subscribe)
isSubscribed = false isSubscribed = false
} }
} }

View File

@ -17,8 +17,6 @@ import com.squareup.picasso.Picasso
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import retrofit2.HttpException
import java.io.IOException
class SubscriptionChannelAdapter(private val subscriptions: MutableList<Subscription>) : class SubscriptionChannelAdapter(private val subscriptions: MutableList<Subscription>) :
RecyclerView.Adapter<SubscriptionChannelViewHolder>() { RecyclerView.Adapter<SubscriptionChannelViewHolder>() {
@ -68,17 +66,14 @@ class SubscriptionChannelAdapter(private val subscriptions: MutableList<Subscrip
private fun subscribe(context: Context, channelId: String) { private fun subscribe(context: Context, channelId: String) {
fun run() { fun run() {
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
val response = try { try {
val token = PreferenceHelper.getToken(context) val token = PreferenceHelper.getToken(context)
RetrofitInstance.authApi.subscribe( RetrofitInstance.authApi.subscribe(
token, token,
Subscribe(channelId) Subscribe(channelId)
) )
} catch (e: IOException) { } catch (e: Exception) {
println(e) Log.e(TAG, e.toString())
Log.e(TAG, "IOException, you might not have internet connection")
} catch (e: HttpException) {
Log.e(TAG, "HttpException, unexpected response")
} }
subscribed = true subscribed = true
isLoading = false isLoading = false
@ -90,17 +85,14 @@ class SubscriptionChannelAdapter(private val subscriptions: MutableList<Subscrip
private fun unsubscribe(context: Context, channelId: String) { private fun unsubscribe(context: Context, channelId: String) {
fun run() { fun run() {
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
val response = try { try {
val token = PreferenceHelper.getToken(context) val token = PreferenceHelper.getToken(context)
RetrofitInstance.authApi.unsubscribe( RetrofitInstance.authApi.unsubscribe(
token, token,
Subscribe(channelId) Subscribe(channelId)
) )
} catch (e: IOException) { } catch (e: Exception) {
println(e) Log.e(TAG, e.toString())
Log.e(TAG, "IOException, you might not have internet connection")
} catch (e: HttpException) {
Log.e(TAG, "HttpException, unexpected response")
} }
subscribed = false subscribed = false
isLoading = false isLoading = false

View File

@ -16,7 +16,6 @@ import com.github.libretube.obj.Subscribe
import com.github.libretube.preferences.PreferenceHelper import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.util.RetrofitInstance import com.github.libretube.util.RetrofitInstance
import com.github.libretube.util.formatShort import com.github.libretube.util.formatShort
import com.google.android.material.button.MaterialButton
import com.squareup.picasso.Picasso import com.squareup.picasso.Picasso
import retrofit2.HttpException import retrofit2.HttpException
import java.io.IOException import java.io.IOException
@ -58,7 +57,7 @@ class ChannelFragment : Fragment() {
binding.channelRefresh.isRefreshing = true binding.channelRefresh.isRefreshing = true
fetchChannel() fetchChannel()
if (PreferenceHelper.getToken(requireContext()) != "") { if (PreferenceHelper.getToken(requireContext()) != "") {
isSubscribed(binding.channelSubscribe) isSubscribed()
} }
} }
refreshChannel() refreshChannel()
@ -81,7 +80,7 @@ class ChannelFragment : Fragment() {
} }
} }
private fun isSubscribed(button: MaterialButton) { private fun isSubscribed() {
@SuppressLint("ResourceAsColor") @SuppressLint("ResourceAsColor")
fun run() { fun run() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
@ -91,28 +90,26 @@ class ChannelFragment : Fragment() {
channelId!!, channelId!!,
token token
) )
} catch (e: IOException) { } catch (e: Exception) {
println(e) Log.e(TAG, e.toString())
Log.e(TAG, "IOException, you might not have internet connection")
return@launchWhenCreated
} catch (e: HttpException) {
Log.e(TAG, "HttpException, unexpected response")
return@launchWhenCreated return@launchWhenCreated
} }
runOnUiThread { runOnUiThread {
if (response.subscribed == true) { if (response.subscribed == true) {
isSubscribed = true isSubscribed = true
button.text = getString(R.string.unsubscribe) binding.channelSubscribe.text = getString(R.string.unsubscribe)
} }
if (response.subscribed != null) { if (response.subscribed != null) {
button.setOnClickListener { binding.channelSubscribe.apply {
if (isSubscribed) { setOnClickListener {
unsubscribe() text = if (isSubscribed) {
button.text = getString(R.string.subscribe) unsubscribe()
} else { getString(R.string.subscribe)
subscribe() } else {
button.text = getString(R.string.unsubscribe) subscribe()
getString(R.string.unsubscribe)
}
} }
} }
} }
@ -125,19 +122,14 @@ class ChannelFragment : Fragment() {
private fun subscribe() { private fun subscribe() {
fun run() { fun run() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val response = try { try {
val token = PreferenceHelper.getToken(requireContext()) val token = PreferenceHelper.getToken(requireContext())
RetrofitInstance.authApi.subscribe( RetrofitInstance.authApi.subscribe(
token, token,
Subscribe(channelId) Subscribe(channelId)
) )
} catch (e: IOException) { } catch (e: Exception) {
println(e) Log.e(TAG, e.toString())
Log.e(TAG, "IOException, you might not have internet connection")
return@launchWhenCreated
} catch (e: HttpException) {
Log.e(TAG, "HttpException, unexpected response$e")
return@launchWhenCreated
} }
isSubscribed = true isSubscribed = true
} }
@ -148,19 +140,14 @@ class ChannelFragment : Fragment() {
private fun unsubscribe() { private fun unsubscribe() {
fun run() { fun run() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val response = try { try {
val token = PreferenceHelper.getToken(requireContext()) val token = PreferenceHelper.getToken(requireContext())
RetrofitInstance.authApi.unsubscribe( RetrofitInstance.authApi.unsubscribe(
token, token,
Subscribe(channelId) Subscribe(channelId)
) )
} catch (e: IOException) { } catch (e: Exception) {
println(e) Log.e(TAG, e.toString())
Log.e(TAG, "IOException, you might not have internet connection")
return@launchWhenCreated
} catch (e: HttpException) {
Log.e(TAG, "HttpException, unexpected response")
return@launchWhenCreated
} }
isSubscribed = false isSubscribed = false
} }

View File

@ -9,28 +9,30 @@ class DoubleClickListener(
private val callback: Callback private val callback: Callback
) : View.OnClickListener { ) : View.OnClickListener {
private var lastClicked: Long = -1L private var lastClicked: Long = -1L
private var doubleClicked: Boolean = false
override fun onClick(v: View?) { override fun onClick(v: View?) {
lastClicked = when { lastClicked = when {
lastClicked == -1L -> { lastClicked == -1L -> {
doubleClicked = false checkForSingleClick()
System.currentTimeMillis() System.currentTimeMillis()
} }
isDoubleClicked() -> { isDoubleClicked() -> {
doubleClicked = true
callback.doubleClicked() callback.doubleClicked()
-1L -1L
} }
else -> { else -> {
Handler(Looper.getMainLooper()).postDelayed({ checkForSingleClick()
if (!doubleClicked) callback.singleClicked()
}, doubleClickTimeLimitMills)
System.currentTimeMillis() System.currentTimeMillis()
} }
} }
} }
private fun checkForSingleClick() {
Handler(Looper.getMainLooper()).postDelayed({
if (lastClicked != -1L) callback.singleClicked()
}, doubleClickTimeLimitMills)
}
private fun getTimeDiff(from: Long, to: Long): Long { private fun getTimeDiff(from: Long, to: Long): Long {
return to - from return to - from
} }

View File

@ -44,7 +44,7 @@
android:layout_marginTop="2dp" android:layout_marginTop="2dp"
android:text="@string/subscribe" android:text="@string/subscribe"
android:textColor="?attr/colorPrimary" android:textColor="?attr/colorPrimary"
android:visibility="gone"/> android:visibility="gone" />
</LinearLayout> </LinearLayout>
</LinearLayout> </LinearLayout>

View File

@ -33,9 +33,10 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentEnd="true" android:layout_alignParentEnd="true"
android:backgroundTint="?attr/colorOnPrimary" android:layout_centerVertical="true"
android:text="@string/unsubscribe" android:elevation="1dp"
android:textColor="@android:color/white" android:text="@string/subscribe"
android:textSize="11sp" android:textColor="?android:attr/textColorPrimary"
android:textSize="12sp"
app:cornerRadius="20dp" /> app:cornerRadius="20dp" />
</RelativeLayout> </RelativeLayout>

View File

@ -9,6 +9,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="10dp" android:layout_marginTop="10dp"
android:animateLayoutChanges="true"
android:orientation="vertical" android:orientation="vertical"
android:paddingStart="20dp" android:paddingStart="20dp"
android:paddingEnd="20dp" android:paddingEnd="20dp"

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<com.github.libretube.views.CustomSwipeToRefresh xmlns:android="http://schemas.android.com/apk/res/android" <com.github.libretube.views.CustomSwipeToRefresh xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/channel_refresh" android:id="@+id/channel_refresh"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -74,15 +75,16 @@
<com.google.android.material.button.MaterialButton <com.google.android.material.button.MaterialButton
android:id="@+id/channel_subscribe" android:id="@+id/channel_subscribe"
style="@style/Widget.Material3.Button.ElevatedButton"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" android:drawableLeft="@drawable/ic_bell_small"
android:backgroundTint="?attr/colorOnPrimary" android:drawableTint="?android:attr/textColorPrimary"
android:drawableStart="@drawable/ic_bell_small" android:elevation="20dp"
android:drawableTint="@android:color/white"
android:text="@string/subscribe" android:text="@string/subscribe"
android:textColor="?android:attr/textColorPrimary" android:textColor="?android:attr/textColorPrimary"
android:textSize="11sp" /> android:textSize="12sp"
app:cornerRadius="20dp" />
</LinearLayout> </LinearLayout>

View File

@ -279,7 +279,7 @@
android:drawableTint="?android:attr/textColorPrimary" android:drawableTint="?android:attr/textColorPrimary"
android:text="@string/subscribe" android:text="@string/subscribe"
android:textColor="?android:attr/textColorPrimary" android:textColor="?android:attr/textColorPrimary"
android:textSize="12dp" android:textSize="12sp"
app:cornerRadius="11dp" /> app:cornerRadius="11dp" />
</RelativeLayout> </RelativeLayout>
@ -389,7 +389,7 @@
android:id="@+id/rewindFL" android:id="@+id/rewindFL"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_weight=".40"> android:layout_weight=".35">
<ImageButton <ImageButton
android:id="@+id/rewindBTN" android:id="@+id/rewindBTN"
@ -407,14 +407,14 @@
<LinearLayout <LinearLayout
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_weight=".20" /> android:layout_weight=".30" />
<!-- double tap forward btn --> <!-- double tap forward btn -->
<FrameLayout <FrameLayout
android:id="@+id/forwardFL" android:id="@+id/forwardFL"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_weight=".40"> android:layout_weight=".35">
<ImageButton <ImageButton
android:id="@+id/forwardBTN" android:id="@+id/forwardBTN"