-
-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: sort subscriptions in groups (#443)
- Loading branch information
1 parent
2180086
commit ae95b61
Showing
6 changed files
with
184 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
app/src/main/java/ani/dantotsu/settings/SubscriptionSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package ani.dantotsu.settings | ||
|
||
import android.app.AlertDialog | ||
import android.content.Context | ||
import android.graphics.drawable.Drawable | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import ani.dantotsu.R | ||
import ani.dantotsu.databinding.ItemExtensionBinding | ||
import ani.dantotsu.notifications.subscription.SubscriptionHelper | ||
import com.xwray.groupie.GroupieAdapter | ||
import com.xwray.groupie.viewbinding.BindableItem | ||
|
||
class SubscriptionSource( | ||
private val parserName: String, | ||
private val subscriptions: MutableList<SubscriptionHelper.Companion.SubscribeMedia>, | ||
private val adapter: GroupieAdapter, | ||
private var parserIcon: Drawable? = null, | ||
private val onGroupRemoved: (SubscriptionSource) -> Unit | ||
) : BindableItem<ItemExtensionBinding>() { | ||
private lateinit var binding: ItemExtensionBinding | ||
private var isExpanded = false | ||
|
||
override fun bind(viewBinding: ItemExtensionBinding, position: Int) { | ||
binding = viewBinding | ||
binding.extensionNameTextView.text = parserName | ||
updateSubscriptionCount() | ||
binding.extensionSubscriptions.visibility = View.VISIBLE | ||
|
||
binding.extensionSubscriptions.setOnClickListener(null) | ||
binding.root.setOnClickListener { | ||
isExpanded = !isExpanded | ||
toggleSubscriptions() | ||
} | ||
binding.subscriptionCount.setOnClickListener { | ||
showRemoveAllSubscriptionsDialog(it.context) | ||
} | ||
binding.extensionIconImageView.visibility = View.VISIBLE | ||
val layoutParams = binding.extensionIconImageView.layoutParams as ViewGroup.MarginLayoutParams | ||
layoutParams.leftMargin = 28 | ||
binding.extensionIconImageView.layoutParams = layoutParams | ||
|
||
parserIcon?.let { | ||
binding.extensionIconImageView.setImageDrawable(it) | ||
} ?: run { | ||
binding.extensionIconImageView.setImageResource(R.drawable.control_background_40dp) | ||
} | ||
|
||
binding.extensionPinImageView.visibility = View.GONE | ||
binding.extensionVersionTextView.visibility = View.GONE | ||
binding.closeTextView.visibility = View.GONE | ||
binding.settingsImageView.visibility = View.GONE | ||
} | ||
|
||
private fun updateSubscriptionCount() { | ||
binding.subscriptionCount.text = subscriptions.size.toString() | ||
binding.subscriptionCount.visibility = if (subscriptions.isEmpty()) View.GONE else View.VISIBLE | ||
} | ||
|
||
private fun showRemoveAllSubscriptionsDialog(context: Context) { | ||
AlertDialog.Builder(context, R.style.MyPopup) | ||
.setTitle(R.string.remove_all_subscriptions) | ||
.setMessage(context.getString(R.string.remove_all_subscriptions_desc, parserName)) | ||
.setPositiveButton(R.string.apply) { _, _ -> | ||
removeAllSubscriptions() | ||
} | ||
.setNegativeButton(R.string.cancel, null) | ||
.show() | ||
} | ||
|
||
private fun removeAllSubscriptions() { | ||
subscriptions.forEach { subscription -> | ||
SubscriptionHelper.deleteSubscription(subscription.id, false) | ||
} | ||
if (isExpanded) { | ||
val startPosition = adapter.getAdapterPosition(this) + 1 | ||
repeat(subscriptions.size) { | ||
adapter.removeGroupAtAdapterPosition(startPosition) | ||
} | ||
} | ||
subscriptions.clear() | ||
onGroupRemoved(this) | ||
} | ||
|
||
private fun removeSubscription(id: Any?) { | ||
subscriptions.removeAll { it.id == id } | ||
updateSubscriptionCount() | ||
if (subscriptions.isEmpty()) { | ||
onGroupRemoved(this) | ||
} else { | ||
adapter.notifyItemChanged(adapter.getAdapterPosition(this)) | ||
} | ||
} | ||
|
||
private fun toggleSubscriptions() { | ||
val startPosition = adapter.getAdapterPosition(this) + 1 | ||
if (isExpanded) { | ||
subscriptions.forEachIndexed { index, subscribeMedia -> | ||
adapter.add(startPosition + index, SubscriptionItem(subscribeMedia.id, subscribeMedia, adapter) { removedId -> | ||
removeSubscription(removedId) | ||
}) | ||
} | ||
} else { | ||
repeat(subscriptions.size) { | ||
adapter.removeGroupAtAdapterPosition(startPosition) | ||
} | ||
} | ||
} | ||
|
||
override fun getLayout(): Int = R.layout.item_extension | ||
|
||
override fun initializeViewBinding(view: View): ItemExtensionBinding = ItemExtensionBinding.bind(view) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters