Skip to content

Commit

Permalink
fix: extension dragging
Browse files Browse the repository at this point in the history
  • Loading branch information
rebelonion committed Feb 7, 2024
1 parent d53781e commit 1e7b546
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,19 @@ class InstalledAnimeExtensionsFragment : Fragment(), SearchQueryHandler {
viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder
): Boolean {
extensionsAdapter.onMove(
viewHolder.absoluteAdapterPosition,
target.absoluteAdapterPosition
)
val newList = extensionsAdapter.currentList.toMutableList()
val fromPosition = viewHolder.absoluteAdapterPosition
val toPosition = target.absoluteAdapterPosition
if (fromPosition < toPosition) { //probably need to switch to a recyclerview adapter
for (i in fromPosition until toPosition) {
Collections.swap(newList, i, i + 1)
}
} else {
for (i in fromPosition downTo toPosition + 1) {
Collections.swap(newList, i, i - 1)
}
}
extensionsAdapter.submitList(newList)
return true
}

Expand All @@ -221,6 +230,7 @@ class InstalledAnimeExtensionsFragment : Fragment(), SearchQueryHandler {
viewHolder: RecyclerView.ViewHolder
) {
super.clearView(recyclerView, viewHolder)
extensionsAdapter.updatePref()
viewHolder.itemView.elevation = 0f
viewHolder.itemView.translationZ = 0f
}
Expand All @@ -230,7 +240,6 @@ class InstalledAnimeExtensionsFragment : Fragment(), SearchQueryHandler {

lifecycleScope.launch {
animeExtensionManager.installedExtensionsFlow.collect { extensions ->
logger("asdfg: Extensions updated")
extensionsAdapter.updateData(sortToAnimeSourcesList(extensions))
}
}
Expand Down Expand Up @@ -267,21 +276,16 @@ class InstalledAnimeExtensionsFragment : Fragment(), SearchQueryHandler {
) : ListAdapter<AnimeExtension.Installed, AnimeExtensionsAdapter.ViewHolder>(
DIFF_CALLBACK_INSTALLED
) {
private val data: MutableList<AnimeExtension.Installed> = mutableListOf()

fun updateData(newExtensions: List<AnimeExtension.Installed>) {
submitList(newExtensions)
data.clear()
data.addAll(newExtensions)
}

fun onMove(fromPosition: Int, toPosition: Int) {
Collections.swap(data, fromPosition, toPosition)
val map = data.map { it.name }.toList()
fun updatePref() {
val map = currentList.map { it.name }
PrefManager.setVal(PrefName.AnimeSourcesOrder, map)
AnimeSources.pinnedAnimeSources = map
AnimeSources.performReorderAnimeSources()
notifyItemMoved(fromPosition, toPosition)
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,19 @@ class InstalledMangaExtensionsFragment : Fragment(), SearchQueryHandler {
viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder
): Boolean {
extensionsAdapter.onMove(
viewHolder.absoluteAdapterPosition,
target.absoluteAdapterPosition
)
val newList = extensionsAdapter.currentList.toMutableList()
val fromPosition = viewHolder.absoluteAdapterPosition
val toPosition = target.absoluteAdapterPosition
if (fromPosition < toPosition) { //probably need to switch to a recyclerview adapter
for (i in fromPosition until toPosition) {
Collections.swap(newList, i, i + 1)
}
} else {
for (i in fromPosition downTo toPosition + 1) {
Collections.swap(newList, i, i - 1)
}
}
extensionsAdapter.submitList(newList)
return true
}

Expand All @@ -217,6 +226,7 @@ class InstalledMangaExtensionsFragment : Fragment(), SearchQueryHandler {
viewHolder: RecyclerView.ViewHolder
) {
super.clearView(recyclerView, viewHolder)
extensionsAdapter.updatePref()
viewHolder.itemView.elevation = 0f
viewHolder.itemView.translationZ = 0f
}
Expand Down Expand Up @@ -262,12 +272,8 @@ class InstalledMangaExtensionsFragment : Fragment(), SearchQueryHandler {
DIFF_CALLBACK_INSTALLED
) {

private val data: MutableList<MangaExtension.Installed> = mutableListOf()

fun updateData(newExtensions: List<MangaExtension.Installed>) {
submitList(newExtensions)
data.clear()
data.addAll(newExtensions)
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
Expand All @@ -276,13 +282,11 @@ class InstalledMangaExtensionsFragment : Fragment(), SearchQueryHandler {
return ViewHolder(view)
}

fun onMove(fromPosition: Int, toPosition: Int) {
Collections.swap(data, fromPosition, toPosition)
val map = data.map { it.name }.toList()
fun updatePref() {
val map = currentList.map { it.name }.toList()
PrefManager.setVal(PrefName.MangaSourcesOrder, map)
MangaSources.pinnedMangaSources = map
MangaSources.performReorderMangaSources()
notifyItemMoved(fromPosition, toPosition)
}

@SuppressLint("SetTextI18n", "ClickableViewAccessibility")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,19 @@ class InstalledNovelExtensionsFragment : Fragment(), SearchQueryHandler {
viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder
): Boolean {
extensionsAdapter.onMove(
viewHolder.absoluteAdapterPosition,
target.absoluteAdapterPosition
)
val newList = extensionsAdapter.currentList.toMutableList()
val fromPosition = viewHolder.absoluteAdapterPosition
val toPosition = target.absoluteAdapterPosition
if (fromPosition < toPosition) { //probably need to switch to a recyclerview adapter
for (i in fromPosition until toPosition) {
Collections.swap(newList, i, i + 1)
}
} else {
for (i in fromPosition downTo toPosition + 1) {
Collections.swap(newList, i, i - 1)
}
}
extensionsAdapter.submitList(newList)
return true
}

Expand All @@ -146,6 +155,7 @@ class InstalledNovelExtensionsFragment : Fragment(), SearchQueryHandler {
viewHolder: RecyclerView.ViewHolder
) {
super.clearView(recyclerView, viewHolder)
extensionsAdapter.updatePref()
viewHolder.itemView.elevation = 0f
viewHolder.itemView.translationZ = 0f
}
Expand Down Expand Up @@ -191,21 +201,16 @@ class InstalledNovelExtensionsFragment : Fragment(), SearchQueryHandler {
) : ListAdapter<NovelExtension.Installed, NovelExtensionsAdapter.ViewHolder>(
DIFF_CALLBACK_INSTALLED
) {
private val data: MutableList<NovelExtension.Installed> = mutableListOf()

fun updateData(newExtensions: List<NovelExtension.Installed>) {
submitList(newExtensions)
data.clear()
data.addAll(newExtensions)
}

fun onMove(fromPosition: Int, toPosition: Int) {
Collections.swap(data, fromPosition, toPosition)
val map = data.map { it.name }.toList()
fun updatePref() {
val map = currentList.map { it.name }
PrefManager.setVal(PrefName.NovelSourcesOrder, map)
NovelSources.pinnedNovelSources = map
NovelSources.performReorderNovelSources()
notifyItemMoved(fromPosition, toPosition)
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
Expand Down

0 comments on commit 1e7b546

Please sign in to comment.