Author: Adam <git@apiote.xyz>
load more departures
%!v(PANIC=String method: strings: negative Repeat count)
diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/api/Api.kt b/app/src/main/java/xyz/apiote/bimba/czwek/api/Api.kt index 00f07b1aab4839fa64de9b894f87b72fe3ee4e55..4a55f6332c6ae928d872e58573a3a3dca71aa9b3 100644 --- a/app/src/main/java/xyz/apiote/bimba/czwek/api/Api.kt +++ b/app/src/main/java/xyz/apiote/bimba/czwek/api/Api.kt @@ -122,11 +122,19 @@ ) } suspend fun getDepartures( - cm: ConnectivityManager, server: Server, feedID: String, stop: String, line: String? = null + cm: ConnectivityManager, + server: Server, + feedID: String, + stop: String, + line: String? = null, + limit: Int? = null ): Result { val params = mutableMapOf("code" to stop) if (line != null) { params["line"] = line + } + if (limit != null) { + params["limit"] = limit.toString(10) } return request( server, @@ -212,4 +220,4 @@ if (host.startsWith("http://") or host.startsWith("https://")) { host } else { "https://$host" - } \ No newline at end of file + } diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/departures/DeparturesActivity.kt b/app/src/main/java/xyz/apiote/bimba/czwek/departures/DeparturesActivity.kt index a706c3b44073120c1213c36679542fba378edcf6..44ad949ddfbc159e7209c351605104ddb1cf45a3 100644 --- a/app/src/main/java/xyz/apiote/bimba/czwek/departures/DeparturesActivity.kt +++ b/app/src/main/java/xyz/apiote/bimba/czwek/departures/DeparturesActivity.kt @@ -17,6 +17,7 @@ import androidx.appcompat.content.res.AppCompatResources import androidx.core.content.res.ResourcesCompat import androidx.core.view.WindowCompat import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView import kotlinx.coroutines.MainScope import kotlinx.coroutines.Runnable import kotlinx.coroutines.launch @@ -39,6 +40,9 @@ private var runnable = Runnable {} private var openBottomSheet: DepartureBottomSheet? = null + private var requestedItemsNumber = 12 + private var allItemsRequested = false + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) _binding = ActivityDeparturesBinding.inflate(layoutInflater) @@ -52,6 +56,21 @@ setExpandedTitleTypeface(tf) } binding.departuresRecycler.layoutManager = LinearLayoutManager(this) + binding.departuresRecycler.addOnScrollListener( + object : RecyclerView.OnScrollListener() { + override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { + super.onScrolled(recyclerView, dx, dy) + val llm = binding.departuresRecycler.layoutManager as LinearLayoutManager + val dataLength = adapter.itemCount + if (llm.findLastCompletelyVisibleItemPosition() == dataLength - 1) { + if (!allItemsRequested) { + requestedItemsNumber += 12 + getDepartures() + } + } + } + } + ) adapter = BimbaDeparturesAdapter(layoutInflater, this, listOf()) { DepartureBottomSheet(it).apply { show(supportFragmentManager, DepartureBottomSheet.TAG) @@ -128,7 +147,14 @@ MainScope().launch { try { val repository = OnlineRepository() val stopDepartures = - repository.getDepartures(cm, getFeedID(), getCode(), getLine(), this@DeparturesActivity) + repository.getDepartures( + cm, + getFeedID(), + getCode(), + getLine(), + this@DeparturesActivity, + requestedItemsNumber + ) updateItems(stopDepartures!!.departures, stopDepartures.stop) openBottomSheet?.departureID()?.let { adapter.get(it) }?.let { openBottomSheet?.update(it) } } catch (e: TrafficResponseException) { @@ -169,6 +195,9 @@ this, R.drawable.error_search ) ) } else { + if (departures.size < requestedItemsNumber) { + allItemsRequested = true + } binding.departuresOverlay.visibility = View.GONE binding.errorImage.visibility = View.GONE binding.errorText.visibility = View.GONE diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/repo/Interfaces.kt b/app/src/main/java/xyz/apiote/bimba/czwek/repo/Interfaces.kt index 129f23f0d2228b0b333c907d06de39bd76d994e6..aa30c413ca65aed5e28f6f53f930acfbba20244e 100644 --- a/app/src/main/java/xyz/apiote/bimba/czwek/repo/Interfaces.kt +++ b/app/src/main/java/xyz/apiote/bimba/czwek/repo/Interfaces.kt @@ -27,7 +27,8 @@ cm: ConnectivityManager, feedID: String, stop: String, line: String?, - context: Context + context: Context, + limit: Int? ): StopDepartures? suspend fun getLocatablesIn( diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/repo/OfflineRepository.kt b/app/src/main/java/xyz/apiote/bimba/czwek/repo/OfflineRepository.kt index 750cf0d4bc662994915fad370e5db855265362c8..9706dd10b196ae2355538c9f7bec053326eff9c4 100644 --- a/app/src/main/java/xyz/apiote/bimba/czwek/repo/OfflineRepository.kt +++ b/app/src/main/java/xyz/apiote/bimba/czwek/repo/OfflineRepository.kt @@ -60,7 +60,8 @@ cm: ConnectivityManager, feedID: String, stop: String, line: String?, - context: Context + context: Context, + limit: Int? ): StopDepartures? { TODO("Not yet implemented") } diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/repo/OnlineRepository.kt b/app/src/main/java/xyz/apiote/bimba/czwek/repo/OnlineRepository.kt index a8ba9ac65b2c6ea37f18ac2cd45c386aeee935c3..6d4493fe97f3795ef5709679f60f0f60122a345d 100644 --- a/app/src/main/java/xyz/apiote/bimba/czwek/repo/OnlineRepository.kt +++ b/app/src/main/java/xyz/apiote/bimba/czwek/repo/OnlineRepository.kt @@ -88,10 +88,11 @@ cm: ConnectivityManager, feedID: String, stop: String, line: String?, - context: Context + context: Context, + limit: Int? ): StopDepartures? { val result = - xyz.apiote.bimba.czwek.api.getDepartures(cm, Server.get(context), feedID, stop, line) + xyz.apiote.bimba.czwek.api.getDepartures(cm, Server.get(context), feedID, stop, line, limit) if (result.error != null) { if (result.stream != null) { val response = withContext(Dispatchers.IO) { ErrorResponse.unmarshal(result.stream) }