update some libraries & coroutines 1.5.0
* androidx.core 1.3.2 -> 1.5.0 * androidx.appcompat 1.2.0 -> 1.3.0 * gson 2.8.6 -> 2.8.7 * coroutines-android 1.4.3 -> 1.5.0 * don't use GlobalScope, use lifecycleScope and vieModelScope instead. This fixes a few issues when fragments where destroied befor the coroutine finished. * gradle wrapper 7.0 -> 7.9.2
This commit is contained in:
@ -3,16 +3,12 @@ package org.mosad.teapod.util
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import android.util.Log
|
||||
import android.widget.Toast
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.JsonParser
|
||||
import kotlinx.coroutines.*
|
||||
import org.mosad.teapod.R
|
||||
import java.io.File
|
||||
import java.io.FileReader
|
||||
import java.io.FileWriter
|
||||
import java.lang.Exception
|
||||
import java.net.URI
|
||||
|
||||
/**
|
||||
* This controller contains the logic for permanently saved data.
|
||||
@ -45,7 +41,7 @@ object StorageController {
|
||||
fun saveMyList(context: Context): Job {
|
||||
val file = File(context.filesDir, fileNameMyList)
|
||||
|
||||
return GlobalScope.launch(Dispatchers.IO) {
|
||||
return CoroutineScope(Dispatchers.IO).launch {
|
||||
file.writeText(Gson().toJson(myList.distinct()))
|
||||
}
|
||||
}
|
||||
|
@ -24,8 +24,8 @@ class TMDBApiController {
|
||||
val searchTerm = title.replace("(Sub)", "").trim()
|
||||
|
||||
return when (type) {
|
||||
MediaType.MOVIE -> searchMovie(searchTerm).await()
|
||||
MediaType.TVSHOW -> searchTVShow(searchTerm).await()
|
||||
MediaType.MOVIE -> searchMovie(searchTerm)
|
||||
MediaType.TVSHOW -> searchTVShow(searchTerm)
|
||||
else -> {
|
||||
Log.e(javaClass.name, "Wrong Type: $type")
|
||||
TMDBResponse()
|
||||
@ -34,62 +34,56 @@ class TMDBApiController {
|
||||
|
||||
}
|
||||
|
||||
fun searchTVShow(title: String): Deferred<TMDBResponse> {
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
private suspend fun searchTVShow(title: String): TMDBResponse = withContext(Dispatchers.IO) {
|
||||
val url = URL("$searchTVUrl$preparedParameters&query=${URLEncoder.encode(title, "UTF-8")}")
|
||||
val response = JsonParser.parseString(url.readText()).asJsonObject
|
||||
//println(response)
|
||||
|
||||
return GlobalScope.async {
|
||||
val response = JsonParser.parseString(url.readText()).asJsonObject
|
||||
//println(response)
|
||||
return@withContext if (response.get("total_results").asInt > 0) {
|
||||
response.get("results").asJsonArray.first().asJsonObject.let {
|
||||
val id = getStringNotNull(it, "id").toInt()
|
||||
val overview = getStringNotNull(it, "overview")
|
||||
val posterPath = getStringNotNullPrefix(it, "poster_path", imageUrl)
|
||||
val backdropPath = getStringNotNullPrefix(it, "backdrop_path", imageUrl)
|
||||
|
||||
if (response.get("total_results").asInt > 0) {
|
||||
response.get("results").asJsonArray.first().asJsonObject.let {
|
||||
val id = getStringNotNull(it, "id").toInt()
|
||||
val overview = getStringNotNull(it, "overview")
|
||||
val posterPath = getStringNotNullPrefix(it, "poster_path", imageUrl)
|
||||
val backdropPath = getStringNotNullPrefix(it, "backdrop_path", imageUrl)
|
||||
|
||||
TMDBResponse(id, "", overview, posterPath, backdropPath)
|
||||
}
|
||||
} else {
|
||||
TMDBResponse()
|
||||
TMDBResponse(id, "", overview, posterPath, backdropPath)
|
||||
}
|
||||
} else {
|
||||
TMDBResponse()
|
||||
}
|
||||
}
|
||||
|
||||
fun searchMovie(title: String): Deferred<TMDBResponse> {
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
private suspend fun searchMovie(title: String): TMDBResponse = withContext(Dispatchers.IO) {
|
||||
val url = URL("$searchMovieUrl$preparedParameters&query=${URLEncoder.encode(title, "UTF-8")}")
|
||||
val response = JsonParser.parseString(url.readText()).asJsonObject
|
||||
//println(response)
|
||||
|
||||
return GlobalScope.async {
|
||||
val response = JsonParser.parseString(url.readText()).asJsonObject
|
||||
//println(response)
|
||||
return@withContext if (response.get("total_results").asInt > 0) {
|
||||
response.get("results").asJsonArray.first().asJsonObject.let {
|
||||
val id = getStringNotNull(it,"id").toInt()
|
||||
val overview = getStringNotNull(it,"overview")
|
||||
val posterPath = getStringNotNullPrefix(it, "poster_path", imageUrl)
|
||||
val backdropPath = getStringNotNullPrefix(it, "backdrop_path", imageUrl)
|
||||
val runtime = getMovieRuntime(id)
|
||||
|
||||
if (response.get("total_results").asInt > 0) {
|
||||
response.get("results").asJsonArray.first().asJsonObject.let {
|
||||
val id = getStringNotNull(it,"id").toInt()
|
||||
val overview = getStringNotNull(it,"overview")
|
||||
val posterPath = getStringNotNullPrefix(it, "poster_path", imageUrl)
|
||||
val backdropPath = getStringNotNullPrefix(it, "backdrop_path", imageUrl)
|
||||
val runtime = getMovieRuntime(id)
|
||||
|
||||
TMDBResponse(id, "", overview, posterPath, backdropPath, runtime)
|
||||
}
|
||||
} else {
|
||||
TMDBResponse()
|
||||
TMDBResponse(id, "", overview, posterPath, backdropPath, runtime)
|
||||
}
|
||||
} else {
|
||||
TMDBResponse()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* currently only used for runtime, need a rework
|
||||
*/
|
||||
fun getMovieRuntime(id: Int): Int = runBlocking {
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
suspend fun getMovieRuntime(id: Int): Int = withContext(Dispatchers.IO) {
|
||||
val url = URL("$getMovieUrl/$id?api_key=$apiKey&language=$language")
|
||||
|
||||
GlobalScope.async {
|
||||
val response = JsonParser.parseString(url.readText()).asJsonObject
|
||||
|
||||
return@async getStringNotNull(response,"runtime").toInt()
|
||||
}.await()
|
||||
val response = JsonParser.parseString(url.readText()).asJsonObject
|
||||
return@withContext getStringNotNull(response,"runtime").toInt()
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user