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.Dispatchers
import kotlinx.coroutines.launch
import retrofit2.HttpException
import java.io.IOException
class SubscriptionChannelAdapter(private val subscriptions: MutableList<Subscription>) :
RecyclerView.Adapter<SubscriptionChannelViewHolder>() {
@ -68,17 +66,14 @@ class SubscriptionChannelAdapter(private val subscriptions: MutableList<Subscrip
private fun subscribe(context: Context, channelId: String) {
fun run() {
CoroutineScope(Dispatchers.IO).launch {
val response = try {
try {
val token = PreferenceHelper.getToken(context)
RetrofitInstance.authApi.subscribe(
token,
Subscribe(channelId)
)
} catch (e: IOException) {
println(e)
Log.e(TAG, "IOException, you might not have internet connection")
} catch (e: HttpException) {
Log.e(TAG, "HttpException, unexpected response")
} catch (e: Exception) {
Log.e(TAG, e.toString())
}
subscribed = true
isLoading = false
@ -90,17 +85,14 @@ class SubscriptionChannelAdapter(private val subscriptions: MutableList<Subscrip
private fun unsubscribe(context: Context, channelId: String) {
fun run() {
CoroutineScope(Dispatchers.IO).launch {
val response = try {
try {
val token = PreferenceHelper.getToken(context)
RetrofitInstance.authApi.unsubscribe(
token,
Subscribe(channelId)
)
} catch (e: IOException) {
println(e)
Log.e(TAG, "IOException, you might not have internet connection")
} catch (e: HttpException) {
Log.e(TAG, "HttpException, unexpected response")
} catch (e: Exception) {
Log.e(TAG, e.toString())
}
subscribed = 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.util.RetrofitInstance
import com.github.libretube.util.formatShort
import com.google.android.material.button.MaterialButton
import com.squareup.picasso.Picasso
import retrofit2.HttpException
import java.io.IOException
@ -58,7 +57,7 @@ class ChannelFragment : Fragment() {
binding.channelRefresh.isRefreshing = true
fetchChannel()
if (PreferenceHelper.getToken(requireContext()) != "") {
isSubscribed(binding.channelSubscribe)
isSubscribed()
}
}
refreshChannel()
@ -81,7 +80,7 @@ class ChannelFragment : Fragment() {
}
}
private fun isSubscribed(button: MaterialButton) {
private fun isSubscribed() {
@SuppressLint("ResourceAsColor")
fun run() {
lifecycleScope.launchWhenCreated {
@ -91,28 +90,26 @@ class ChannelFragment : Fragment() {
channelId!!,
token
)
} catch (e: IOException) {
println(e)
Log.e(TAG, "IOException, you might not have internet connection")
return@launchWhenCreated
} catch (e: HttpException) {
Log.e(TAG, "HttpException, unexpected response")
} catch (e: Exception) {
Log.e(TAG, e.toString())
return@launchWhenCreated
}
runOnUiThread {
if (response.subscribed == true) {
isSubscribed = true
button.text = getString(R.string.unsubscribe)
binding.channelSubscribe.text = getString(R.string.unsubscribe)
}
if (response.subscribed != null) {
button.setOnClickListener {
if (isSubscribed) {
unsubscribe()
button.text = getString(R.string.subscribe)
} else {
subscribe()
button.text = getString(R.string.unsubscribe)
binding.channelSubscribe.apply {
setOnClickListener {
text = if (isSubscribed) {
unsubscribe()
getString(R.string.subscribe)
} else {
subscribe()
getString(R.string.unsubscribe)
}
}
}
}
@ -125,19 +122,14 @@ class ChannelFragment : Fragment() {
private fun subscribe() {
fun run() {
lifecycleScope.launchWhenCreated {
val response = try {
try {
val token = PreferenceHelper.getToken(requireContext())
RetrofitInstance.authApi.subscribe(
token,
Subscribe(channelId)
)
} catch (e: IOException) {
println(e)
Log.e(TAG, "IOException, you might not have internet connection")
return@launchWhenCreated
} catch (e: HttpException) {
Log.e(TAG, "HttpException, unexpected response$e")
return@launchWhenCreated
} catch (e: Exception) {
Log.e(TAG, e.toString())
}
isSubscribed = true
}
@ -148,19 +140,14 @@ class ChannelFragment : Fragment() {
private fun unsubscribe() {
fun run() {
lifecycleScope.launchWhenCreated {
val response = try {
try {
val token = PreferenceHelper.getToken(requireContext())
RetrofitInstance.authApi.unsubscribe(
token,
Subscribe(channelId)
)
} catch (e: IOException) {
println(e)
Log.e(TAG, "IOException, you might not have internet connection")
return@launchWhenCreated
} catch (e: HttpException) {
Log.e(TAG, "HttpException, unexpected response")
return@launchWhenCreated
} catch (e: Exception) {
Log.e(TAG, e.toString())
}
isSubscribed = false
}

View File

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

View File

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

View File

@ -279,7 +279,7 @@
android:drawableTint="?android:attr/textColorPrimary"
android:text="@string/subscribe"
android:textColor="?android:attr/textColorPrimary"
android:textSize="12dp"
android:textSize="12sp"
app:cornerRadius="11dp" />
</RelativeLayout>