Skip to content

Commit

Permalink
stop refresh entire page on grid change / hide scroll to top button
Browse files Browse the repository at this point in the history
  • Loading branch information
rebelonion committed Dec 27, 2023
1 parent 38c5ae4 commit d177087
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class OfflineMangaAdapter(
private val inflater: LayoutInflater =
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
private var originalItems: List<OfflineMangaModel> = items
private var style = context.getSharedPreferences("Dantotsu", Context.MODE_PRIVATE).getInt("offline_view", 0)

override fun getCount(): Int {
return items.size
Expand All @@ -37,15 +38,10 @@ class OfflineMangaAdapter(
@SuppressLint("SetTextI18n")
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {

val style = context.getSharedPreferences("Dantotsu", Context.MODE_PRIVATE).getInt("offline_view", 0)

var view = convertView

if (view == null && style == 0 ) {
view = inflater.inflate(R.layout.item_media_large, parent, false) // large view
}
else if (view == null && style == 1){
view = inflater.inflate(R.layout.item_media_compact, parent, false) // compact view
val view: View = convertView ?: when(style) {
0 -> inflater.inflate(R.layout.item_media_large, parent, false) // large view
1 -> inflater.inflate(R.layout.item_media_compact, parent, false) // compact view
else -> inflater.inflate(R.layout.item_media_compact, parent, false) // compact view
}

val item = getItem(position) as OfflineMangaModel
Expand All @@ -69,7 +65,7 @@ class OfflineMangaAdapter(
else if (style == 1){
val readchapter = view.findViewById<TextView>(R.id.itemCompactUserProgress) // for compact view
readchapter.text = item.readchapter
totalchapter.text = " | "+item.totalchapter
totalchapter.text = " | " + item.totalchapter
}

// Bind item data to the views
Expand Down Expand Up @@ -104,4 +100,9 @@ class OfflineMangaAdapter(
this.originalItems = items
notifyDataSetChanged()
}

fun notifyNewGrid(){
style = context.getSharedPreferences("Dantotsu", Context.MODE_PRIVATE).getInt("offline_view", 0)
notifyDataSetChanged()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.OvershootInterpolator
import android.widget.AbsListView
import android.widget.AutoCompleteTextView
import android.widget.GridView
import android.widget.ImageView
Expand Down Expand Up @@ -128,7 +129,11 @@ class OfflineMangaFragment : Fragment(), OfflineMangaSearchListener {
style = 0
context?.getSharedPreferences("Dantotsu", Context.MODE_PRIVATE)?.edit()
?.putInt("offline_view", style!!)?.apply()
recreate(requireActivity())
gridView.visibility = View.GONE
gridView = view.findViewById(R.id.gridView)
gridView.adapter = adapter
gridView.visibility = View.VISIBLE
adapter.notifyNewGrid()

}

Expand All @@ -137,7 +142,11 @@ class OfflineMangaFragment : Fragment(), OfflineMangaSearchListener {
style = 1
context?.getSharedPreferences("Dantotsu", Context.MODE_PRIVATE)?.edit()
?.putInt("offline_view", style!!)?.apply()
recreate(requireActivity())
gridView.visibility = View.GONE
gridView = view.findViewById(R.id.gridView1)
gridView.adapter = adapter
gridView.visibility = View.VISIBLE
adapter.notifyNewGrid()
}

gridView = if(style == 0) view.findViewById(R.id.gridView) else view.findViewById(R.id.gridView1)
Expand Down Expand Up @@ -229,9 +238,25 @@ class OfflineMangaFragment : Fragment(), OfflineMangaSearchListener {
}

scrollTop.setOnClickListener {
//TODO: scroll to top
gridView.smoothScrollToPosition(0)
}

// Assuming 'scrollTop' is a view that you want to hide/show
scrollTop.visibility = View.GONE

gridView.setOnScrollListener(object : AbsListView.OnScrollListener {
override fun onScrollStateChanged(view: AbsListView, scrollState: Int) {
// Implement behavior for different scroll states if needed
}

override fun onScroll(view: AbsListView, firstVisibleItem: Int, visibleItemCount: Int, totalItemCount: Int) {
val first = view.getChildAt(0)
val visibility = first != null && first.top < -height
scrollTop.visibility = if (visibility) View.VISIBLE else View.GONE
}
})


}

override fun onResume() {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/fragment_manga_offline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
android:hint="@string/manga"
android:textColorHint="@color/bg_opp"
android:transitionName="@string/search"
android:fontFamily="@font/poppins_bold"
app:boxBackgroundColor="?attr/colorPrimaryContainer"
app:boxCornerRadiusBottomEnd="28dp"
app:boxCornerRadiusBottomStart="28dp"
Expand Down

0 comments on commit d177087

Please sign in to comment.