improve btns

This commit is contained in:
Bnyro 2022-07-11 16:25:47 +02:00
parent 0650f35474
commit ce060f1df2
5 changed files with 39 additions and 57 deletions

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 {
text = if (isSubscribed) {
unsubscribe() unsubscribe()
button.text = getString(R.string.subscribe) getString(R.string.subscribe)
} else { } else {
subscribe() subscribe()
button.text = getString(R.string.unsubscribe) 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

@ -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

@ -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>