Skip to content

Commit

Permalink
fixed: favourite chapters not showing up right away
Browse files Browse the repository at this point in the history
  • Loading branch information
faisalcodes committed Sep 10, 2023
1 parent efb905c commit 992778f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.recyclerview.widget.RecyclerView
import com.peacedesign.android.utils.WindowUtils
import com.quranapp.android.R
import com.quranapp.android.adapters.quranIndex.ADPFavChaptersList
import com.quranapp.android.utils.Logger
import com.quranapp.android.utils.sharedPrefs.SPFavouriteChapters
import com.quranapp.android.views.helper.RecyclerView2
import com.quranapp.android.widgets.PageAlert
Expand Down Expand Up @@ -39,21 +40,24 @@ class FragReaderIndexFavChapters : BaseFragReaderIndex() {
}

private fun updateAdapter(ctx: Context, list: RecyclerView2, adapter: ADPFavChaptersList, items: List<Int>) {
if (items.isEmpty()) {
val hasItems = items.isNotEmpty()
list.visibility = if (hasItems) View.VISIBLE else View.GONE
pageAlert?.visibility = if (hasItems) View.GONE else View.VISIBLE

if (!hasItems) {
noItems(ctx, list)
} else {
pageAlert?.remove()
adapter.chapterNos = items
list.visibility = View.VISIBLE
adapter.notifyDataSetChanged()
}
}

private fun noItems(ctx: Context, list: RecyclerView2) {
if (pageAlert != null) return

pageAlert = PageAlert(ctx).apply {
setMessage(R.string.noItems, R.string.msgNoFavouriteChapters)
show(list.parent as ViewGroup)
list.visibility = View.GONE
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.quranapp.android.utils.sharedPrefs

import android.annotation.SuppressLint
import android.content.Context

object SPFavouriteChapters {
Expand All @@ -13,25 +14,27 @@ object SPFavouriteChapters {
return favChapters?.map { it.toInt() }?.toList() ?: listOf()
}

@SuppressLint("ApplySharedPref")
fun addToFavorites(ctx: Context, chapterNo: Int) {
val favChapters = HashSet(sp(ctx).getStringSet(KEY_FAVOURITE_CHAPTERS, setOf())!!)
favChapters.add(chapterNo.toString())
sp(ctx).edit().apply {
putStringSet(KEY_FAVOURITE_CHAPTERS, favChapters)
apply()
commit()
}
}

fun isAddedToFavorites(ctx: Context, chapterNo: Int): Boolean {
return sp(ctx).getStringSet(KEY_FAVOURITE_CHAPTERS, setOf())!!.contains(chapterNo.toString())
}

@SuppressLint("ApplySharedPref")
fun removeFromFavorites(ctx: Context, chapterNo: Int) {
val favChapters = HashSet(sp(ctx).getStringSet(KEY_FAVOURITE_CHAPTERS, setOf())!!)
favChapters.remove(chapterNo.toString())
sp(ctx).edit().apply {
putStringSet(KEY_FAVOURITE_CHAPTERS, favChapters)
apply()
commit()
}
}
}

0 comments on commit 992778f

Please sign in to comment.