new display name, url prop support for song item

This commit is contained in:
Gary Wang 2021-12-05 22:48:26 +08:00
parent 482f664f7e
commit 036c31e502
3 changed files with 34 additions and 8 deletions

View File

@ -16,6 +16,7 @@ import net.blumia.pcmdroid.model.parseFromJsonString
import net.blumia.pcmdroid.repository.ServerRepository import net.blumia.pcmdroid.repository.ServerRepository
import net.blumia.pcmdroid.viewmodel.AddServerViewModel import net.blumia.pcmdroid.viewmodel.AddServerViewModel
import okhttp3.FormBody import okhttp3.FormBody
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import okhttp3.Request import okhttp3.Request
@ -63,14 +64,17 @@ class MainViewModel(private val repository: ServerRepository) : ViewModel() {
.post(formBody) .post(formBody)
.build() .build()
var mediaRoot = srv.mediaBaseUrl
if (mediaRoot.startsWith("//")) mediaRoot = request.url.scheme + mediaRoot
try { try {
httpClient.newCall(request).execute().use { response -> httpClient.newCall(request).execute().use { response ->
if (!response.isSuccessful) { if (!response.isSuccessful) {
// TODO: non-200 response will go to here too. // TODO: non-200 response will go to here too.
return@use return@use
} }
val pair = parseFromJsonString(response.body!!.string()) val pair = parseFromJsonString(mediaRoot, "", response.body!!.string())
_drawerFolders.postValue(pair.first) _drawerFolders.postValue(pair.first!!)
// setCurrentServer(srv) // setCurrentServer(srv)
Log.d("vvv", drawerFolders.value.toString()) Log.d("vvv", drawerFolders.value.toString())
} }
@ -103,7 +107,7 @@ class MainViewModel(private val repository: ServerRepository) : ViewModel() {
// TODO: non-200 response will go to here too. // TODO: non-200 response will go to here too.
return@use return@use
} }
val pair = parseFromJsonString(response.body!!.string()) val pair = parseFromJsonString(srv.mediaBaseUrl, folder.folderPath, response.body!!.string())
_folders.postValue(pair.first) _folders.postValue(pair.first)
_songs.postValue(pair.second) _songs.postValue(pair.second)

View File

@ -2,20 +2,24 @@ package net.blumia.pcmdroid.model
import android.net.Uri import android.net.Uri
import android.util.Log import android.util.Log
import okhttp3.HttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import org.json.JSONArray import org.json.JSONArray
import org.json.JSONException import org.json.JSONException
import org.json.JSONObject import org.json.JSONObject
import java.net.URLDecoder import java.net.URLDecoder
import kotlin.io.path.Path
data class Song ( data class Song (
val filePath: String, // with file name val filePath: String, // with file name
val displayName: String = "",
val modifiedTime: Long = 0, val modifiedTime: Long = 0,
val fileSize: Int = 0, val fileSize: Long = 0,
val additionalInfo: Boolean = false, // if sidecar meta-info json file exist. val additionalInfo: Boolean = false, // if sidecar meta-info json file exist.
val url: String, val url: String,
) { ) {
fun displayName(): String { fun displayName(): String {
return Uri.parse(filePath).lastPathSegment ?: "???" return if (displayName.isNotEmpty()) displayName else Uri.parse(filePath).lastPathSegment ?: "???"
} }
} }
@ -27,10 +31,13 @@ data class Folder (
} }
} }
fun parseFromJsonString(jsonString: String): Pair<List<Folder>, List<Song>> { fun parseFromJsonString(mediaRootUrl: String, baseFolder: String, jsonString: String): Pair<List<Folder>, List<Song>> {
val folders = mutableListOf<Folder>() val folders = mutableListOf<Folder>()
val songs = mutableListOf<Song>() val songs = mutableListOf<Song>()
val mediaRoot = if (mediaRootUrl.endsWith('/')) mediaRootUrl else "$mediaRootUrl/"
val filePath = if (baseFolder.endsWith('/') || baseFolder == "") baseFolder else "$baseFolder/"
try { try {
val jsonObj = JSONObject(jsonString) val jsonObj = JSONObject(jsonString)
val result = jsonObj.getJSONObject("result") val result = jsonObj.getJSONObject("result")
@ -44,14 +51,25 @@ fun parseFromJsonString(jsonString: String): Pair<List<Folder>, List<Song>> {
for (i in 0 until musicList.length()) { for (i in 0 until musicList.length()) {
val fileObject = musicList.getJSONObject(i) val fileObject = musicList.getJSONObject(i)
val rawFileName = fileObject.getString("fileName") val rawFileName = fileObject.getString("fileName")
val displayName = if (fileObject.has("displayName")) fileObject.getString("displayName") else ""
val filePathWithName = filePath + rawFileName
var fileUrl = if (fileObject.has("url")) fileObject.getString("url") else (mediaRoot + filePathWithName)
val fileName = URLDecoder.decode(rawFileName, "UTF-8") val fileName = URLDecoder.decode(rawFileName, "UTF-8")
val modifiedTime = fileObject.getLong("modifiedTime") val modifiedTime = fileObject.getLong("modifiedTime")
val fileSize = fileObject.getLong("fileSize") val fileSize = fileObject.getLong("fileSize")
// it's possible that server return a url start without protocol.
if (fileUrl.startsWith("//")) {
fileUrl = mediaRootUrl.toHttpUrlOrNull()?.scheme + fileUrl
}
songs.add( songs.add(
Song( Song(
rawFileName, filePathWithName,
displayName = if (displayName.isNotEmpty()) displayName else fileName,
modifiedTime = modifiedTime, modifiedTime = modifiedTime,
url = rawFileName url = fileUrl,
fileSize = fileSize,
) )
) )
} }

View File

@ -1,5 +1,8 @@
package net.blumia.pcmdroid package net.blumia.pcmdroid
import android.util.Log
import okhttp3.HttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import org.junit.Test import org.junit.Test
import org.junit.Assert.* import org.junit.Assert.*
@ -12,6 +15,7 @@ import org.junit.Assert.*
class ExampleUnitTest { class ExampleUnitTest {
@Test @Test
fun addition_isCorrect() { fun addition_isCorrect() {
println("//d.android.com/tools/testing".toHttpUrlOrNull().toString())
assertEquals(4, 2 + 2) assertEquals(4, 2 + 2)
} }
} }