Author: Adam Evyčędo <git@apiote.xyz>
add newer api structs
%!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 531a5f2cb50732a4d8f0e9a197339dd9045eea0c..ba9efd50df8b04de0fb4a5e27527cd044edd1128 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 @@ -112,14 +112,14 @@ null ) } -suspend fun getLine(context: Context, server: Server, feedID: String, line: String): Result { +suspend fun getLine(context: Context, server: Server, feedID: String, lineName: String, lineID: String): Result { return request( server, "lines", - line, - mapOf(), + lineName, + mapOf("lineID" to lineID), context, - arrayOf(1u, 2u), + arrayOf(1u, 2u, 0u), feedID ) } @@ -213,7 +213,7 @@ "${server.apiPath}/${feeds?.ifEmpty { server.feeds.getIDs() } ?: server.feeds.getIDs()}/$resource${ if (item == null) { "" } else { - "/$item" + "/${URLEncoder.encode(item, "utf-8")}" } }${ params.map { diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/api/Interfaces.kt b/app/src/main/java/xyz/apiote/bimba/czwek/api/Interfaces.kt index ca416f8c472519ef800011d579d02f1d735e6bf6..4bfc2f5c505cd365ec5a973bc01a4558856f35c9 100644 --- a/app/src/main/java/xyz/apiote/bimba/czwek/api/Interfaces.kt +++ b/app/src/main/java/xyz/apiote/bimba/czwek/api/Interfaces.kt @@ -7,6 +7,7 @@ interface QueryableV1 interface QueryableV2 interface QueryableV3 +interface QueryableV4 interface LocatableV1 interface LocatableV2 interface LocatableV3 \ No newline at end of file diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/api/Structs.kt b/app/src/main/java/xyz/apiote/bimba/czwek/api/Structs.kt index 426f7aadef379bb756e4d90be674f0d67ab83de8..be4ec222b4c4101a9706ef4790cac08c36e14904 100644 --- a/app/src/main/java/xyz/apiote/bimba/czwek/api/Structs.kt +++ b/app/src/main/java/xyz/apiote/bimba/czwek/api/Structs.kt @@ -494,7 +494,7 @@ val zone: String, val feedID: String, val position: PositionV1, val changeOptions: List<ChangeOptionV1> -) : QueryableV2, Parcelable, LocatableV2, QueryableV3, LocatableV3 { +) : QueryableV2, Parcelable, LocatableV2, QueryableV3, LocatableV3, QueryableV4 { companion object { fun unmarshal(stream: InputStream): StopV2 { val reader = Reader(stream) @@ -628,6 +628,54 @@ for (i in 0UL until directionsNum) { graphs.add(LineGraph.unmarshal(stream)) } return LineV2( + name = name, + colour = colour, + type = LineTypeV3.of(type.toULong().toUInt()), + feedID = feedID, + headsigns = headsigns, + graphs = graphs + ) + } + } +} + +data class LineV3( + val id: String, + val name: String, + val colour: ColourV1, + val type: LineTypeV3, + val feedID: String, + val headsigns: List<List<String>>, + val graphs: List<LineGraph>, +) : QueryableV4 { + override fun toString(): String { + return "$name ($type) [$colour]\n${headsigns.map { "-> ${it.joinToString()}" }}" + } + + companion object { + fun unmarshal(stream: InputStream): LineV3 { + val reader = Reader(stream) + val id = reader.readString() + val name = reader.readString() + val colour = ColourV1.unmarshal(stream) + val type = reader.readUInt() + val feedID = reader.readString() + var directionsNum = reader.readUInt().toULong() + val headsigns = (0UL until directionsNum).map { + val headsignsNum = reader.readUInt().toULong() + val headsignsDir = mutableListOf<String>() + for (j in 0UL until headsignsNum) { + headsignsDir.add(reader.readString()) + } + headsignsDir + } + directionsNum = reader.readUInt().toULong() + val graphs = mutableListOf<LineGraph>() + for (i in 0UL until directionsNum) { + graphs.add(LineGraph.unmarshal(stream)) + } + return LineV3( + id = id, name = name, colour = colour, type = LineTypeV3.of(type.toULong().toUInt()), diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/api/responses/Line.kt b/app/src/main/java/xyz/apiote/bimba/czwek/api/responses/Line.kt index 1a4528959aac80bc5f0887ea98a32120318ce351..f39fe6a3bed1b7ab03f45ed4a6cac4019df41d13 100644 --- a/app/src/main/java/xyz/apiote/bimba/czwek/api/responses/Line.kt +++ b/app/src/main/java/xyz/apiote/bimba/czwek/api/responses/Line.kt @@ -6,7 +6,7 @@ package xyz.apiote.bimba.czwek.api.responses import xyz.apiote.bimba.czwek.api.LineV1 import xyz.apiote.bimba.czwek.api.LineV2 -import xyz.apiote.bimba.czwek.api.responses.UnknownResponseVersion +import xyz.apiote.bimba.czwek.api.LineV3 import xyz.apiote.fruchtfleisch.Reader import java.io.InputStream @@ -25,11 +25,11 @@ } } data class LineResponseDev( - val line: LineV2 + val line: LineV3 ) : LineResponse { companion object { fun unmarshal(stream: InputStream): LineResponseDev { - return LineResponseDev(LineV2.unmarshal(stream)) + return LineResponseDev(LineV3.unmarshal(stream)) } } } @@ -38,8 +38,8 @@ data class LineResponseV2( val line: LineV2 ) : LineResponse { companion object { - fun unmarshal(stream: InputStream): LineResponseDev { - return LineResponseDev(LineV2.unmarshal(stream)) + fun unmarshal(stream: InputStream): LineResponseV2 { + return LineResponseV2(LineV2.unmarshal(stream)) } } } diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/api/responses/Queryables.kt b/app/src/main/java/xyz/apiote/bimba/czwek/api/responses/Queryables.kt index 9e5d0a467f6b052d867a9777d3f7ce0e5a9a8a14..64f526286fd71e04dcfbc891278d066cdc339bb2 100644 --- a/app/src/main/java/xyz/apiote/bimba/czwek/api/responses/Queryables.kt +++ b/app/src/main/java/xyz/apiote/bimba/czwek/api/responses/Queryables.kt @@ -6,13 +6,14 @@ package xyz.apiote.bimba.czwek.api.responses import xyz.apiote.bimba.czwek.api.LineV1 import xyz.apiote.bimba.czwek.api.LineV2 +import xyz.apiote.bimba.czwek.api.LineV3 import xyz.apiote.bimba.czwek.api.QueryableV1 import xyz.apiote.bimba.czwek.api.QueryableV2 import xyz.apiote.bimba.czwek.api.QueryableV3 +import xyz.apiote.bimba.czwek.api.QueryableV4 import xyz.apiote.bimba.czwek.api.StopV1 import xyz.apiote.bimba.czwek.api.StopV2 import xyz.apiote.bimba.czwek.api.UnknownResourceVersionException -import xyz.apiote.bimba.czwek.api.responses.UnknownResponseVersion import xyz.apiote.fruchtfleisch.Reader import java.io.InputStream @@ -31,10 +32,10 @@ } } } -data class QueryablesResponseDev(val queryables: List<QueryableV3>) : QueryablesResponse { +data class QueryablesResponseDev(val queryables: List<QueryableV4>) : QueryablesResponse { companion object { fun unmarshal(stream: InputStream): QueryablesResponseDev { - val queryables = mutableListOf<QueryableV3>() + val queryables = mutableListOf<QueryableV4>() val reader = Reader(stream) val n = reader.readUInt().toULong() for (i in 0UL until n) { @@ -44,7 +45,7 @@ queryables.add(StopV2.unmarshal(stream)) } 1UL -> { - queryables.add(LineV2.unmarshal(stream)) + queryables.add(LineV3.unmarshal(stream)) } else -> { @@ -59,7 +60,7 @@ } data class QueryablesResponseV3(val queryables: List<QueryableV3>) : QueryablesResponse { companion object { - fun unmarshal(stream: InputStream): QueryablesResponseDev { + fun unmarshal(stream: InputStream): QueryablesResponseV3 { val queryables = mutableListOf<QueryableV3>() val reader = Reader(stream) val n = reader.readUInt().toULong() @@ -78,7 +79,7 @@ throw UnknownResourceVersionException("Queryable/$r", 0u) } } } - return QueryablesResponseDev(queryables) + return QueryablesResponseV3(queryables) } } } 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 495bc0f692891d55ab0fb3877f5f4138e34f20d6..71db491df3d7eaaa4ffa9aa2352b61ab497257e1 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 @@ -40,7 +40,8 @@ suspend fun getLine( context: Context, feedID: String, - line: String, + lineName: String, + lineID: String, ): Line? suspend fun queryQueryables( diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/repo/Line.kt b/app/src/main/java/xyz/apiote/bimba/czwek/repo/Line.kt index cdffec7beddc8f0caee7728aa408c9bb8204092b..7a28e2b3e2128782b096ffc2744719151997fe65 100644 --- a/app/src/main/java/xyz/apiote/bimba/czwek/repo/Line.kt +++ b/app/src/main/java/xyz/apiote/bimba/czwek/repo/Line.kt @@ -9,8 +9,10 @@ import android.graphics.drawable.BitmapDrawable import android.graphics.drawable.Drawable import xyz.apiote.bimba.czwek.api.LineV1 import xyz.apiote.bimba.czwek.api.LineV2 +import xyz.apiote.bimba.czwek.api.LineV3 data class Line( + val id: String, val name: String, val colour: Colour, val type: LineType, @@ -21,6 +23,7 @@ ) : Queryable, LineAbstract { constructor(line: LineV1) : this( line.name, + line.name, Colour(line.colour), LineType.of(line.type), line.feedID, @@ -28,6 +31,17 @@ line.headsigns, line.graphs.map{LineGraph(it)} ) constructor(line: LineV2) : this( + line.name, + line.name, + Colour(line.colour), + LineType.of(line.type), + line.feedID, + line.headsigns, + line.graphs.map{LineGraph(it)} + ) + + constructor(line: LineV3) : this( + line.id, line.name, Colour(line.colour), LineType.of(line.type), 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 e653fc195e8d1dd45debbcbb9bccbfbc11bd8832..4674bcdff37fd253355aa83b084806947a9249bb 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 @@ -77,7 +77,8 @@ override suspend fun getLine( context: Context, feedID: String, - line: String, + lineName: String, + lineID: String, ): Line? { 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 8ff66768ee65c317d46dc6fbe4712e66b9d3f14c..c5c8c19ce7fb30d5b55e0f06b7ba572f2df4dedf 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 @@ -9,6 +9,7 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import xyz.apiote.bimba.czwek.api.LineV1 import xyz.apiote.bimba.czwek.api.LineV2 +import xyz.apiote.bimba.czwek.api.LineV3 import xyz.apiote.bimba.czwek.api.PositionV1 import xyz.apiote.bimba.czwek.api.Server import xyz.apiote.bimba.czwek.api.StopV1 @@ -186,9 +187,9 @@ } } override suspend fun getLine( - context: Context, feedID: String, line: String + context: Context, feedID: String, lineName: String, lineID: String ): Line? { - val result = xyz.apiote.bimba.czwek.api.getLine(context, Server.get(context), feedID, line) + val result = xyz.apiote.bimba.czwek.api.getLine(context, Server.get(context), feedID, lineName, lineID) if (result.error != null) { if (result.stream != null) { val response = withContext(Dispatchers.IO) { ErrorResponse.unmarshal(result.stream) } @@ -246,8 +247,8 @@ withContext(Dispatchers.IO) { QueryablesResponse.unmarshal(result.stream!!) }) { is QueryablesResponseDev -> response.queryables.map { when (it) { is StopV2 -> Stop(it) - is LineV2 -> Line(it) - else -> throw UnknownResourceException("queryablesV2", it::class) + is LineV3 -> Line(it) + else -> throw UnknownResourceException("queryablesV4", it::class) } } @@ -270,7 +271,7 @@ is QueryablesResponseV3 -> response.queryables.map { when (it) { is StopV2 -> Stop(it) is LineV2 -> Line(it) - else -> throw UnknownResourceException("queryablesV2", it::class) + else -> throw UnknownResourceException("queryablesV3", it::class) } } diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/search/LineGraphActivity.kt b/app/src/main/java/xyz/apiote/bimba/czwek/search/LineGraphActivity.kt index fb67383a684ee6f127ee291adbbf77391f1317bd..128a2199a685110c6bfd442bf81ed6509cbda12f 100644 --- a/app/src/main/java/xyz/apiote/bimba/czwek/search/LineGraphActivity.kt +++ b/app/src/main/java/xyz/apiote/bimba/czwek/search/LineGraphActivity.kt @@ -29,20 +29,22 @@ binding = ActivityLineGraphBinding.inflate(layoutInflater) setContentView(binding.root) - val lineName = intent.getStringExtra("line")!! + val lineName = intent.getStringExtra("lineName")!! + val lineID = intent.getStringExtra("lineID")!! val feedID = intent.getStringExtra("feedID")!! binding.title.text = lineName - getGraph(lineName, feedID) + getGraph(lineName, lineID, feedID) } private fun getGraph( lineName: String, + lineID: String, feedID: String, ) { MainScope().launch { try { val repository = OnlineRepository() - val line = repository.getLine(this@LineGraphActivity, feedID, lineName) + val line = repository.getLine(this@LineGraphActivity, feedID, lineName, lineID) line?.let { sectionsPagerAdapter = SectionsPagerAdapter(supportFragmentManager, it) val viewPager: ViewPager = binding.viewPager diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/search/Results.kt b/app/src/main/java/xyz/apiote/bimba/czwek/search/Results.kt index ef4757d062459420ae3714d74bce0c0592bcb441..d18cb129731d39ea38fdf1878605754b52088caa 100644 --- a/app/src/main/java/xyz/apiote/bimba/czwek/search/Results.kt +++ b/app/src/main/java/xyz/apiote/bimba/czwek/search/Results.kt @@ -226,7 +226,8 @@ } is Line -> { val intent = Intent(context, LineGraphActivity::class.java).apply { - putExtra("line", it.name) + putExtra("lineName", it.name) + putExtra("lineID", it.id) putExtra("feedID", it.feedID) } context!!.startActivity(intent)