From c0ae0466c2edc3c4e7a65c84b941c7bba16ea931 Mon Sep 17 00:00:00 2001 From: davidoskky Date: Fri, 22 Oct 2021 20:06:39 +0200 Subject: [PATCH] Remove the LikeButton library (#363) * Add functions to add and remove favorites from SharedItems * Remove the LikeButton dependency * Use the theme colors for the icon tint --- app/build.gradle | 3 +- .../bou/readerforselfoss/ReaderActivity.kt | 93 ++++------------ .../adapters/ItemCardAdapter.kt | 103 ++++-------------- .../bou/readerforselfoss/utils/SharedItems.kt | 86 +++++++++++++++ .../main/res/color/ic_menu_heart_color.xml | 8 ++ .../main/res/drawable/ic_menu_heart_60dp.xml | 5 + app/src/main/res/layout/card_item.xml | 18 +-- app/src/main/res/menu/reader_menu.xml | 10 +- app/src/main/res/values/colors.xml | 1 + 9 files changed, 161 insertions(+), 166 deletions(-) create mode 100644 app/src/main/res/color/ic_menu_heart_color.xml create mode 100644 app/src/main/res/drawable/ic_menu_heart_60dp.xml diff --git a/app/build.gradle b/app/build.gradle index 7b8fb5f..a70bcd8 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -130,8 +130,7 @@ dependencies { implementation 'com.burgstaller:okhttp-digest:2.5' // Material-ish things - implementation 'com.ashokvarma.android:bottom-navigation-bar:2.2.0' - implementation 'com.github.jd-alexander:LikeButton:0.2.3' + implementation 'com.ashokvarma.android:bottom-navigation-bar:2.1.0' implementation 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1' // glide diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/ReaderActivity.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/ReaderActivity.kt index 9715b1d..3c0419f 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/ReaderActivity.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/ReaderActivity.kt @@ -2,6 +2,9 @@ package apps.amine.bou.readerforselfoss import android.content.Context import android.content.SharedPreferences +import android.graphics.Color +import android.graphics.PorterDuff +import android.graphics.PorterDuffColorFilter import android.graphics.drawable.ColorDrawable import android.os.Build import android.os.Bundle @@ -14,17 +17,12 @@ import androidx.appcompat.app.AppCompatActivity import android.view.Menu import android.view.MenuItem import android.view.ViewGroup -import android.widget.Toast -import androidx.fragment.app.Fragment import androidx.room.Room import apps.amine.bou.readerforselfoss.api.selfoss.Item import apps.amine.bou.readerforselfoss.api.selfoss.SelfossApi -import apps.amine.bou.readerforselfoss.api.selfoss.SuccessResponse -import apps.amine.bou.readerforselfoss.databinding.ActivityImageBinding import apps.amine.bou.readerforselfoss.databinding.ActivityReaderBinding import apps.amine.bou.readerforselfoss.fragments.ArticleFragment import apps.amine.bou.readerforselfoss.persistence.database.AppDatabase -import apps.amine.bou.readerforselfoss.persistence.entities.ActionEntity import apps.amine.bou.readerforselfoss.persistence.migrations.MIGRATION_1_2 import apps.amine.bou.readerforselfoss.persistence.migrations.MIGRATION_2_3 import apps.amine.bou.readerforselfoss.persistence.migrations.MIGRATION_3_4 @@ -33,16 +31,9 @@ import apps.amine.bou.readerforselfoss.themes.Toppings import apps.amine.bou.readerforselfoss.transformers.DepthPageTransformer import apps.amine.bou.readerforselfoss.utils.Config import apps.amine.bou.readerforselfoss.utils.SharedItems -import apps.amine.bou.readerforselfoss.utils.network.isNetworkAccessible -import apps.amine.bou.readerforselfoss.utils.persistence.toEntity -import apps.amine.bou.readerforselfoss.utils.succeeded import apps.amine.bou.readerforselfoss.utils.toggleStar import com.ftinc.scoop.Scoop import me.relex.circleindicator.CircleIndicator -import retrofit2.Call -import retrofit2.Callback -import retrofit2.Response -import kotlin.concurrent.thread class ReaderActivity : AppCompatActivity() { @@ -63,8 +54,11 @@ class ReaderActivity : AppCompatActivity() { val ALIGN_LEFT = 2 private fun showMenuItem(willAddToFavorite: Boolean) { - toolbarMenu.findItem(R.id.save).isVisible = willAddToFavorite - toolbarMenu.findItem(R.id.unsave).isVisible = !willAddToFavorite + if (willAddToFavorite) { + toolbarMenu.findItem(R.id.star).icon.colorFilter = PorterDuffColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP) + } else { + toolbarMenu.findItem(R.id.star).icon.colorFilter = PorterDuffColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP) + } } private fun canFavorite() { @@ -241,62 +235,23 @@ class ReaderActivity : AppCompatActivity() { onBackPressed() return true } - R.id.save -> { - if (this@ReaderActivity.isNetworkAccessible(null)) { - api.starrItem(allItems[binding.pager.currentItem].id) - .enqueue(object : Callback { - override fun onResponse( - call: Call, - response: Response - ) { - afterSave() - } - - override fun onFailure( - call: Call, - t: Throwable - ) { - Toast.makeText( - baseContext, - R.string.cant_mark_favortie, - Toast.LENGTH_SHORT - ).show() - } - }) + R.id.star -> { + if (allItems[binding.pager.currentItem].starred) { + SharedItems.unstarItem( + this@ReaderActivity, + api, + db, + allItems[binding.pager.currentItem] + ) + afterUnsave() } else { - thread { - db.actionsDao().insertAllActions(ActionEntity(allItems[binding.pager.currentItem].id, false, false, true, false)) - afterSave() - } - } - } - R.id.unsave -> { - if (this@ReaderActivity.isNetworkAccessible(null)) { - api.unstarrItem(allItems[binding.pager.currentItem].id) - .enqueue(object : Callback { - override fun onResponse( - call: Call, - response: Response - ) { - afterUnsave() - } - - override fun onFailure( - call: Call, - t: Throwable - ) { - Toast.makeText( - baseContext, - R.string.cant_unmark_favortie, - Toast.LENGTH_SHORT - ).show() - } - }) - } else { - thread { - db.actionsDao().insertAllActions(ActionEntity(allItems[binding.pager.currentItem].id, false, false, false, true)) - afterUnsave() - } + SharedItems.starItem( + this@ReaderActivity, + api, + db, + allItems[binding.pager.currentItem] + ) + afterSave() } } R.id.align_left -> { diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemCardAdapter.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemCardAdapter.kt index 6ab58c9..2397d57 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemCardAdapter.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemCardAdapter.kt @@ -7,23 +7,21 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.ImageView.ScaleType -import android.widget.Toast import androidx.core.content.ContextCompat import apps.amine.bou.readerforselfoss.R import apps.amine.bou.readerforselfoss.api.selfoss.Item import apps.amine.bou.readerforselfoss.api.selfoss.SelfossApi -import apps.amine.bou.readerforselfoss.api.selfoss.SuccessResponse import apps.amine.bou.readerforselfoss.databinding.CardItemBinding import apps.amine.bou.readerforselfoss.persistence.database.AppDatabase -import apps.amine.bou.readerforselfoss.persistence.entities.ActionEntity import apps.amine.bou.readerforselfoss.themes.AppColors import apps.amine.bou.readerforselfoss.utils.Config import apps.amine.bou.readerforselfoss.utils.LinkOnTouchListener +import apps.amine.bou.readerforselfoss.utils.SharedItems import apps.amine.bou.readerforselfoss.utils.buildCustomTabsIntent import apps.amine.bou.readerforselfoss.utils.customtabs.CustomTabActivityHelper import apps.amine.bou.readerforselfoss.utils.glide.bitmapCenterCrop import apps.amine.bou.readerforselfoss.utils.glide.circularBitmapDrawable -import apps.amine.bou.readerforselfoss.utils.network.isNetworkAccessible +import apps.amine.bou.readerforselfoss.utils.network.isNetworkAvailable import apps.amine.bou.readerforselfoss.utils.openInBrowserAsNewTask import apps.amine.bou.readerforselfoss.utils.openItemUrl import apps.amine.bou.readerforselfoss.utils.shareLink @@ -32,12 +30,6 @@ import apps.amine.bou.readerforselfoss.utils.toTextDrawableString import com.amulyakhare.textdrawable.TextDrawable import com.amulyakhare.textdrawable.util.ColorGenerator import com.bumptech.glide.Glide -import com.like.LikeButton -import com.like.OnLikeListener -import retrofit2.Call -import retrofit2.Callback -import retrofit2.Response -import kotlin.concurrent.thread class ItemCardAdapter( override val app: Activity, @@ -67,13 +59,13 @@ class ItemCardAdapter( with(holder) { val itm = items[position] - - binding.favButton.isLiked = itm.starred + binding.favButton.isSelected = itm.starred binding.title.text = itm.getTitleDecoded() binding.title.setTextColor(ContextCompat.getColor( c, appColors.textColor )) + binding.title.setOnTouchListener(LinkOnTouchListener()) binding.title.setLinkTextColor(appColors.colorAccent) @@ -111,8 +103,6 @@ class ItemCardAdapter( } else { c.circularBitmapDrawable(config, itm.getIcon(c), binding.sourceImage) } - - binding.favButton.isLiked = itm.starred } } @@ -129,75 +119,30 @@ class ItemCardAdapter( private fun handleClickListeners() { - binding.favButton.setOnLikeListener(object : OnLikeListener { - override fun liked(likeButton: LikeButton) { - val (id) = items[bindingAdapterPosition] - if (c.isNetworkAccessible(null)) { - api.starrItem(id).enqueue(object : Callback { - override fun onResponse( - call: Call, - response: Response - ) { - } - - override fun onFailure( - call: Call, - t: Throwable - ) { - binding.favButton.isLiked = false - Toast.makeText( - c, - R.string.cant_mark_favortie, - Toast.LENGTH_SHORT - ).show() - } - }) - } else { - thread { - db.actionsDao().insertAllActions(ActionEntity(id, false, false, true, false)) - } - } - } - - override fun unLiked(likeButton: LikeButton) { - val (id) = items[bindingAdapterPosition] - if (c.isNetworkAccessible(null)) { - api.unstarrItem(id).enqueue(object : Callback { - override fun onResponse( - call: Call, - response: Response - ) { - } - - override fun onFailure( - call: Call, - t: Throwable - ) { - binding.favButton.isLiked = true - Toast.makeText( - c, - R.string.cant_unmark_favortie, - Toast.LENGTH_SHORT - ).show() - } - }) - } else { - thread { - db.actionsDao().insertAllActions(ActionEntity(id, false, false, false, true)) - } - } - } - }) - - binding.shareBtn.setOnClickListener { + binding.favButton.setOnClickListener { val item = items[bindingAdapterPosition] - c.shareLink(item.getLinkDecoded(), item.getTitleDecoded()) + if (isNetworkAvailable(c)) { + if (item.starred) { + SharedItems.unstarItem(c, api, db, item) + item.starred = false + binding.favButton.isSelected = false + } else { + SharedItems.starItem(c, api, db, item) + item.starred = true + binding.favButton.isSelected = true + } + } } - binding.browserBtn.setOnClickListener { - c.openInBrowserAsNewTask(items[bindingAdapterPosition]) + binding.shareBtn.setOnClickListener { + val item = items[bindingAdapterPosition] + c.shareLink(item.getLinkDecoded(), item.getTitleDecoded()) + } + + binding.browserBtn.setOnClickListener { + c.openInBrowserAsNewTask(items[bindingAdapterPosition]) + } } - } private fun handleCustomTabActions() { val customTabsIntent = c.buildCustomTabsIntent() diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/SharedItems.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/SharedItems.kt index 924be56..42fd5d6 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/SharedItems.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/SharedItems.kt @@ -291,6 +291,92 @@ object SharedItems { } } + fun starItem(app: Context, api: SelfossApi, db: AppDatabase, item: Item) { + if (items.contains(item) && !item.starred) { + position = items.indexOf(item) + starItemAtPosition(app, api, db) + } + } + + private fun starItemAtPosition(app: Context, api: SelfossApi, db: AppDatabase) { + val i = items[position] + + if (app.isNetworkAccessible(null)) { + api.starrItem(i.id).enqueue(object : Callback { + override fun onResponse( + call: Call, + response: Response + ) { + val tmpItems = items + tmpItems[position].starred = true + items = tmpItems + + resetDBItem(db) + getFocusedItems() + badgeStarred++ + } + + override fun onFailure( + call: Call, + t: Throwable + ) { + Toast.makeText( + app, + app.getString(R.string.cant_mark_favortie), + Toast.LENGTH_SHORT + ).show() + } + }) + } else { + thread { + db.actionsDao().insertAllActions(ActionEntity(i.id, false, false, true, false)) + } + } + } + + fun unstarItem(app: Context, api: SelfossApi, db: AppDatabase, item: Item) { + if (items.contains(item) && item.starred) { + position = items.indexOf(item) + unstarItemAtPosition(app, api, db) + } + } + + private fun unstarItemAtPosition(app: Context, api: SelfossApi, db: AppDatabase) { + val i = items[position] + + if (app.isNetworkAccessible(null)) { + api.unstarrItem(i.id).enqueue(object : Callback { + override fun onResponse( + call: Call, + response: Response + ) { + val tmpItems = items + tmpItems[position].starred = false + items = tmpItems + + resetDBItem(db) + getFocusedItems() + badgeStarred-- + } + + override fun onFailure( + call: Call, + t: Throwable + ) { + Toast.makeText( + app, + app.getString(R.string.cant_unmark_favortie), + Toast.LENGTH_SHORT + ).show() + } + }) + } else { + thread { + db.actionsDao().insertAllActions(ActionEntity(i.id, false, false, false, true)) + } + } + } + private fun resetDBItem(db: AppDatabase) { if (itemsCaching) { val i = items[position] diff --git a/app/src/main/res/color/ic_menu_heart_color.xml b/app/src/main/res/color/ic_menu_heart_color.xml new file mode 100644 index 0000000..dd8fa5f --- /dev/null +++ b/app/src/main/res/color/ic_menu_heart_color.xml @@ -0,0 +1,8 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_menu_heart_60dp.xml b/app/src/main/res/drawable/ic_menu_heart_60dp.xml new file mode 100644 index 0000000..9cee08b --- /dev/null +++ b/app/src/main/res/drawable/ic_menu_heart_60dp.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/layout/card_item.xml b/app/src/main/res/layout/card_item.xml index d8dcccb..aff415b 100644 --- a/app/src/main/res/layout/card_item.xml +++ b/app/src/main/res/layout/card_item.xml @@ -92,20 +92,22 @@ app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/sourceTitleAndDate"> - + android:scaleType="centerCrop" + app:srcCompat="@drawable/ic_menu_heart_60dp" + app:tint="@color/ic_menu_heart_color" /> + app:srcCompat="@drawable/ic_share_black_24dp" + app:tint="?android:attr/textColorPrimary" /> + app:srcCompat="@drawable/ic_open_in_browser_black_24dp" + app:tint="?android:attr/textColorPrimary" /> diff --git a/app/src/main/res/menu/reader_menu.xml b/app/src/main/res/menu/reader_menu.xml index 84d298e..d811be3 100644 --- a/app/src/main/res/menu/reader_menu.xml +++ b/app/src/main/res/menu/reader_menu.xml @@ -15,14 +15,8 @@ android:title="@string/reader_text_align_justify" /> - diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index db5b380..1048b03 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -7,6 +7,7 @@ #FFe91e63 #FFFFFFFF #FF000000 + #FF0000 @color/colorAccentDark @color/colorAccent @color/pink