tmdb api improvements #43
|
@ -10,8 +10,8 @@ android {
|
||||||
applicationId "org.mosad.teapod"
|
applicationId "org.mosad.teapod"
|
||||||
minSdkVersion 23
|
minSdkVersion 23
|
||||||
targetSdkVersion 30
|
targetSdkVersion 30
|
||||||
versionCode 4180 //00.04.100
|
versionCode 4190 //00.04.190
|
||||||
versionName "0.4.2-alpha2"
|
versionName "0.4.2-beta1"
|
||||||
|
|
||||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||||
resValue "string", "build_time", buildTime()
|
resValue "string", "build_time", buildTime()
|
||||||
|
|
|
@ -31,7 +31,7 @@ import androidx.fragment.app.Fragment
|
||||||
import androidx.fragment.app.commit
|
import androidx.fragment.app.commit
|
||||||
import com.afollestad.materialdialogs.MaterialDialog
|
import com.afollestad.materialdialogs.MaterialDialog
|
||||||
import com.afollestad.materialdialogs.callbacks.onDismiss
|
import com.afollestad.materialdialogs.callbacks.onDismiss
|
||||||
import com.google.android.material.bottomnavigation.BottomNavigationView
|
import com.google.android.material.navigation.NavigationBarView
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
import org.mosad.teapod.R
|
import org.mosad.teapod.R
|
||||||
import org.mosad.teapod.databinding.ActivityMainBinding
|
import org.mosad.teapod.databinding.ActivityMainBinding
|
||||||
|
@ -51,7 +51,7 @@ import org.mosad.teapod.util.exitAndRemoveTask
|
||||||
import java.net.SocketTimeoutException
|
import java.net.SocketTimeoutException
|
||||||
import kotlin.system.measureTimeMillis
|
import kotlin.system.measureTimeMillis
|
||||||
|
|
||||||
class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemSelectedListener {
|
class MainActivity : AppCompatActivity(), NavigationBarView.OnItemSelectedListener {
|
||||||
|
|
||||||
private lateinit var binding: ActivityMainBinding
|
private lateinit var binding: ActivityMainBinding
|
||||||
private var activeBaseFragment: Fragment = HomeFragment() // the currently active fragment, home at the start
|
private var activeBaseFragment: Fragment = HomeFragment() // the currently active fragment, home at the start
|
||||||
|
@ -72,7 +72,7 @@ class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemS
|
||||||
theme.applyStyle(getThemeResource(), true)
|
theme.applyStyle(getThemeResource(), true)
|
||||||
|
|
||||||
binding = ActivityMainBinding.inflate(layoutInflater)
|
binding = ActivityMainBinding.inflate(layoutInflater)
|
||||||
binding.navView.setOnNavigationItemSelectedListener(this)
|
binding.navView.setOnItemSelectedListener(this)
|
||||||
setContentView(binding.root)
|
setContentView(binding.root)
|
||||||
|
|
||||||
supportFragmentManager.commit {
|
supportFragmentManager.commit {
|
||||||
|
|
|
@ -4,9 +4,9 @@ import android.util.Log
|
||||||
import com.google.gson.JsonObject
|
import com.google.gson.JsonObject
|
||||||
import com.google.gson.JsonParser
|
import com.google.gson.JsonParser
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
|
import org.mosad.teapod.util.DataTypes.MediaType
|
||||||
import java.net.URL
|
import java.net.URL
|
||||||
import java.net.URLEncoder
|
import java.net.URLEncoder
|
||||||
import org.mosad.teapod.util.DataTypes.MediaType
|
|
||||||
|
|
||||||
class TMDBApiController {
|
class TMDBApiController {
|
||||||
|
|
||||||
|
@ -21,7 +21,11 @@ class TMDBApiController {
|
||||||
private val imageUrl = "https://image.tmdb.org/t/p/w500"
|
private val imageUrl = "https://image.tmdb.org/t/p/w500"
|
||||||
|
|
||||||
suspend fun search(title: String, type: MediaType): TMDBResponse {
|
suspend fun search(title: String, type: MediaType): TMDBResponse {
|
||||||
val searchTerm = title.replace("(Sub)", "").trim()
|
// remove unneeded text from the media title before searching
|
||||||
|
val searchTerm = title.replace("(Sub)", "")
|
||||||
|
.replace(Regex("-?\\s?[0-9]+.\\s?(Staffel|Season)"), "")
|
||||||
|
.replace(Regex("(Staffel|Season)\\s?[0-9]+"), "")
|
||||||
|
.trim()
|
||||||
|
|
||||||
return when (type) {
|
return when (type) {
|
||||||
MediaType.MOVIE -> searchMovie(searchTerm)
|
MediaType.MOVIE -> searchMovie(searchTerm)
|
||||||
|
@ -40,8 +44,12 @@ class TMDBApiController {
|
||||||
val response = JsonParser.parseString(url.readText()).asJsonObject
|
val response = JsonParser.parseString(url.readText()).asJsonObject
|
||||||
// println(response)
|
// println(response)
|
||||||
|
|
||||||
return@withContext if (response.get("total_results").asInt > 0) {
|
val sortedResults = response.get("results").asJsonArray.toList().sortedBy {
|
||||||
response.get("results").asJsonArray.first().asJsonObject.let {
|
getStringNotNull(it.asJsonObject, "name")
|
||||||
|
}
|
||||||
|
|
||||||
|
return@withContext if (sortedResults.isNotEmpty()) {
|
||||||
|
sortedResults.first().asJsonObject.let {
|
||||||
val id = getStringNotNull(it, "id").toInt()
|
val id = getStringNotNull(it, "id").toInt()
|
||||||
val overview = getStringNotNull(it, "overview")
|
val overview = getStringNotNull(it, "overview")
|
||||||
val posterPath = getStringNotNullPrefix(it, "poster_path", imageUrl)
|
val posterPath = getStringNotNullPrefix(it, "poster_path", imageUrl)
|
||||||
|
@ -60,8 +68,12 @@ class TMDBApiController {
|
||||||
val response = JsonParser.parseString(url.readText()).asJsonObject
|
val response = JsonParser.parseString(url.readText()).asJsonObject
|
||||||
// println(response)
|
// println(response)
|
||||||
|
|
||||||
return@withContext if (response.get("total_results").asInt > 0) {
|
val sortedResults = response.get("results").asJsonArray.toList().sortedBy {
|
||||||
response.get("results").asJsonArray.first().asJsonObject.let {
|
getStringNotNull(it.asJsonObject, "name")
|
||||||
|
}
|
||||||
|
|
||||||
|
return@withContext if (sortedResults.isNotEmpty()) {
|
||||||
|
sortedResults.first().asJsonObject.let {
|
||||||
val id = getStringNotNull(it,"id").toInt()
|
val id = getStringNotNull(it,"id").toInt()
|
||||||
val overview = getStringNotNull(it,"overview")
|
val overview = getStringNotNull(it,"overview")
|
||||||
val posterPath = getStringNotNullPrefix(it, "poster_path", imageUrl)
|
val posterPath = getStringNotNullPrefix(it, "poster_path", imageUrl)
|
||||||
|
|
Loading…
Reference in New Issue