Skip to content

Commit

Permalink
Merge pull request #354 from Reco1I/refresh-button
Browse files Browse the repository at this point in the history
Fix potential crash when showing BeatmapListing and add "Retry" button
  • Loading branch information
Rian8337 authored May 5, 2024
2 parents 96309a6 + fd9bd95 commit 87a1d13
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 40 deletions.
10 changes: 10 additions & 0 deletions res/drawable/refresh_24px.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="960"
android:viewportHeight="960"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M481.54,780Q355.91,780 268.73,692.83Q181.54,605.66 181.54,480.06Q181.54,354.46 268.73,267.23Q355.91,180 481.54,180Q551.69,180 614.31,211.19Q676.92,242.39 718.46,299.23L718.46,210Q718.46,197.25 727.09,188.63Q735.72,180 748.47,180Q761.23,180 769.84,188.63Q778.46,197.25 778.46,210L778.46,388.46Q778.46,403.83 768.07,414.22Q757.67,424.61 742.31,424.61L563.85,424.61Q551.1,424.61 542.47,415.99Q533.85,407.36 533.85,394.6Q533.85,381.85 542.47,373.23Q551.1,364.62 563.85,364.62L691.85,364.62Q660.23,306.69 604.15,273.35Q548.08,240 481.54,240Q381.54,240 311.54,310Q241.54,380 241.54,480Q241.54,580 311.54,650Q381.54,720 481.54,720Q551.27,720 609.21,683.38Q667.15,646.77 697,585.69Q702.69,574.77 713.73,570.42Q724.77,566.08 736.23,569.92Q748.46,573.77 753.35,585.92Q758.23,598.08 752.54,609Q715.38,686.69 642.84,733.34Q570.3,780 481.54,780Z"/>
</vector>
31 changes: 24 additions & 7 deletions res/layout/beatmap_downloader_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,40 @@
android:id="@+id/logo"
android:layout_width="100dp"
android:layout_height="36dp"
android:layout_marginRight="12dp"
android:src="@drawable/powered_by_osudirect"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"/>
android:layout_marginRight="12dp"
android:src="@drawable/powered_by_osudirect" />

<com.google.android.material.progressindicator.CircularProgressIndicator
android:id="@+id/indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/logo"
android:layout_centerVertical="true"
android:layout_marginRight="12dp"
android:layout_toLeftOf="@id/refresh"
android:indeterminate="true"
app:trackThickness="3dp"
app:trackCornerRadius="2dp"
app:indicatorColor="#FFF"
app:indicatorSize="20dp" />
app:indicatorSize="20dp"
app:trackCornerRadius="2dp"
app:trackThickness="3dp" />

<Button
android:id="@+id/refresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="12dp"
android:layout_toLeftOf="@id/logo"
android:background="?attr/selectableItemBackground"
android:drawableLeft="@drawable/refresh_24px"
android:drawablePadding="8dp"
android:visibility="gone"
android:drawableTint="#FFF"
android:padding="16dp"
android:text="Retry"
android:textAllCaps="false"
android:textColor="#FFF" />

</RelativeLayout>

Expand Down
92 changes: 62 additions & 30 deletions src/com/reco1l/osu/beatmaplisting/BeatmapListing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@ package com.reco1l.osu.beatmaplisting

import android.graphics.BitmapFactory
import android.util.Log
import android.view.*
import android.view.View.*
import android.view.ContextThemeWrapper
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.view.View.GONE
import android.view.View.OnKeyListener
import android.view.View.VISIBLE
import android.view.ViewGroup
import android.view.ViewOutlineProvider
import android.view.inputmethod.EditorInfo
import android.widget.*
import android.widget.Button
import android.widget.EditText
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.TextView.OnEditorActionListener
import androidx.core.text.buildSpannedString
import androidx.core.text.color
Expand All @@ -21,7 +33,12 @@ import com.reco1l.framework.net.IDownloaderObserver
import com.reco1l.framework.net.JsonArrayRequest
import com.reco1l.osu.OsuColors
import com.reco1l.osu.mainThread
import kotlinx.coroutines.*
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlinx.coroutines.SupervisorJob
import ru.nsu.ccfit.zuev.audio.Status
import ru.nsu.ccfit.zuev.osu.Config
import ru.nsu.ccfit.zuev.osu.GlobalManager
Expand All @@ -30,27 +47,21 @@ import ru.nsu.ccfit.zuev.osu.ToastLogger
import ru.nsu.ccfit.zuev.osuplus.R
import java.net.URL
import java.text.SimpleDateFormat
import java.util.*
import java.util.TimeZone


object BeatmapListing : BaseFragment(),
class BeatmapListing : BaseFragment(),
IDownloaderObserver,
OnEditorActionListener,
OnKeyListener {


var mirror = BeatmapMirror.OSU_DIRECT

var isPlayingMusic = false
private set


override val layoutID = R.layout.beatmap_downloader_fragment


private val adapter = BeatmapSetAdapter()

private val searchScope = CoroutineScope(Dispatchers.IO)
private val searchScope = CoroutineScope(Dispatchers.IO + SupervisorJob())

private val scrollListener = object : OnScrollListener() {

Expand All @@ -76,9 +87,20 @@ object BeatmapListing : BaseFragment(),

private lateinit var recyclerView: RecyclerView

private lateinit var retryButton: Button

private lateinit var searchBox: EditText


init {
current?.dismiss()

mainThread {
current = this
}
}


override fun onLoadView() {

val itemWidth = resources.getDimension(R.dimen.beatmap_downloader_item_width)
Expand All @@ -91,14 +113,16 @@ object BeatmapListing : BaseFragment(),
recyclerView.addOnScrollListener(scrollListener)
recyclerView.adapter = adapter

retryButton = findViewById(R.id.refresh)!!
retryButton.setOnClickListener { search(true) }

searchBox = findViewById(R.id.search)!!
searchBox.setOnEditorActionListener(this)
searchBox.setOnKeyListener(this)

indicator = findViewById(R.id.indicator)!!

val close = findViewById<ImageButton>(R.id.close)!!
close.setOnClickListener {
findViewById<ImageButton>(R.id.close)!!.setOnClickListener {
dismiss()
}

Expand All @@ -108,7 +132,10 @@ object BeatmapListing : BaseFragment(),

fun search(keepData: Boolean) {

mainThread { indicator.visibility = VISIBLE }
mainThread {
indicator.visibility = VISIBLE
retryButton.visibility = GONE
}

pendingRequest?.cancel()
pendingRequest = searchScope.launch(CoroutineExceptionHandler { _, throwable ->
Expand All @@ -119,10 +146,7 @@ object BeatmapListing : BaseFragment(),

mainThread {
indicator.visibility = GONE

if (adapter.data.isEmpty()) {
dismiss()
}
retryButton.visibility = VISIBLE
}
}) {

Expand Down Expand Up @@ -164,7 +188,7 @@ object BeatmapListing : BaseFragment(),
}

fun stopPreviews(shouldResumeMusic: Boolean) {
if (!BeatmapListing::recyclerView.isInitialized) {
if (!::recyclerView.isInitialized) {
return
}

Expand Down Expand Up @@ -207,17 +231,25 @@ object BeatmapListing : BaseFragment(),
stopPreviews(true)

pendingRequest?.cancel()
pendingRequest = null

offset = 0
adapter.data.clear()

mainThread {
searchBox.text = null
adapter.notifyDataSetChanged()
super.dismiss()
current = null
}
}


companion object {

var current: BeatmapListing? = null

var mirror = BeatmapMirror.OSU_DIRECT

var isPlayingMusic = false
private set

}

}


Expand Down Expand Up @@ -297,7 +329,7 @@ class BeatmapSetDetails(val beatmapSet: BeatmapSetModel, val holder: BeatmapSetV
previewButton.setOnClickListener {

if (holder.previewStream == null) {
BeatmapListing.stopPreviews(false)
BeatmapListing.current!!.stopPreviews(false)
holder.playPreview(beatmapSet)
} else {
holder.stopPreview(true)
Expand Down Expand Up @@ -496,7 +528,7 @@ class BeatmapSetViewHolder(itemView: View, private val mediaScope: CoroutineScop
previewButton.setOnClickListener {

if (previewStream == null) {
BeatmapListing.stopPreviews(false)
BeatmapListing.current!!.stopPreviews(false)
playPreview(beatmapSet)
} else {
stopPreview(true)
Expand All @@ -518,7 +550,7 @@ class BeatmapSetViewHolder(itemView: View, private val mediaScope: CoroutineScop
fun playPreview(beatmapSet: BeatmapSetModel) {
previewJob = mediaScope.launch {

BeatmapListing.stopPreviews(true)
BeatmapListing.current!!.stopPreviews(true)

try {
previewStream = URLBassStream(BeatmapListing.mirror.previewEndpoint(beatmapSet.beatmaps[0].id)) {
Expand Down
2 changes: 1 addition & 1 deletion src/com/reco1l/osu/ui/BeatmapButton.kt
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class BeatmapButton : Sprite(0f, 0f, getResources().getTexture("menu-button-back
if (libraryManager.library.isEmpty())
{
getGlobal().songService.pause()
BeatmapListing.show()
BeatmapListing().show()
return true
}

Expand Down
2 changes: 1 addition & 1 deletion src/com/reco1l/osu/ui/MainMenu.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class MainMenu(val main: MainScene)
getGlobal().songService.isGaming = false
getGlobal().engine.scene = main.scene

BeatmapListing.show()
BeatmapListing().show()
} else {
main.musicControl(MusicOption.PLAY)

Expand Down
2 changes: 1 addition & 1 deletion src/ru/nsu/ccfit/zuev/osu/MainScene.java
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX

if (pSceneTouchEvent.isActionUp()) {
setColor(1, 1, 1);
BeatmapListing.INSTANCE.show();
new BeatmapListing().show();
return true;
}

Expand Down

0 comments on commit 87a1d13

Please sign in to comment.