Compare commits
138 Commits
Author | SHA1 | Date | |
---|---|---|---|
d33de371d1 | |||
1ecd25bb06 | |||
fa28eb35ab | |||
d3fe81224b | |||
34c7f9d081 | |||
e835715b9c | |||
001141337d | |||
5cd3d25ebe | |||
215e01c53a | |||
1751963574 | |||
9c3548a866 | |||
ebd96f9849 | |||
85b17d7a76 | |||
f128efea0d | |||
da94003368 | |||
3fdc2aff1b | |||
326da147f1 | |||
f398c82f62 | |||
821f8b5590 | |||
0028cb6dd7 | |||
127bd030b9 | |||
3cadaa5c7a | |||
97966f5ad3 | |||
4c55bb771f | |||
8eb737a831 | |||
522b893dc8 | |||
69e0b6bcca | |||
c34b95795f | |||
9059306e90 | |||
ed0c0a4c61 | |||
03a79346b7 | |||
ad1e3068cd | |||
de1f19c2b7 | |||
12bbc2ef5f | |||
0186cef79e | |||
bc5509cf93 | |||
ef9a0f00d0 | |||
b85d7ae025 | |||
69c9666d2b | |||
7d6c300f7e | |||
1ebc1194e6 | |||
c48328723b | |||
95c8a72c94 | |||
fc04e8e222 | |||
a898a70653 | |||
58aab72097 | |||
35157b78f5 | |||
c6a00ea061 | |||
80a7fc4398 | |||
dd6ca8b90e | |||
e80e81af0f | |||
f852600dc7 | |||
aa49169034 | |||
7abb5cd3e8 | |||
3a71bdd2c7 | |||
629c144c5b | |||
b2196f11da | |||
5b5a74a1de | |||
7a860a7270 | |||
e97ad9a245 | |||
cf435fdb72 | |||
42895a6fba | |||
eaf1cf78e9 | |||
1af82f8370 | |||
d31a19a4f1 | |||
b27666ee69 | |||
e76cbda04d | |||
7fbf639a70 | |||
ff63b3d7a4 | |||
7d32cecd89 | |||
72280f29d8 | |||
cd4cfb7a0c | |||
4a5a6c04ca | |||
554c66e11f | |||
0aece1d8fa | |||
f820d2aac0 | |||
0ea2e5ee97 | |||
a092c5b8be | |||
ab660d0ae7 | |||
be1c001942 | |||
30a5331bbc | |||
0797e9fa3d | |||
75204e522d | |||
2016e03e56 | |||
4505f95309 | |||
e8bf63a666 | |||
a51001ec2e | |||
0b5a8e69fb | |||
61c96f5ce2 | |||
9bf0ae2f63 | |||
f66fca7ebb | |||
df4f43c0a2 | |||
287ef57bdb | |||
aa41884db5 | |||
bec0dc2628 | |||
4fed3ddb91 | |||
e652c001d3 | |||
2f78fbea73 | |||
a1fe08840f | |||
402fb06c9e | |||
188d0d9162 | |||
d5d70e49d2 | |||
f100b4abf3 | |||
f2a798d4f7 | |||
d427691f6e | |||
b4daac0814 | |||
554af530e3 | |||
27e7f2a249 | |||
f97d07c2b8 | |||
ecbbc5db7b | |||
4fd6f9ca7e | |||
63ce910ec5 | |||
7dc41da13c | |||
236ca9a6c9 | |||
a46fd4c6d2 | |||
c4bc3c7ea2 | |||
844ff41dd3 | |||
487c0c3c39 | |||
eafefd9a51 | |||
3935f37267 | |||
39e740cd92 | |||
eeb1c33e43 | |||
8753d4f36f | |||
5ea94b7ded | |||
062013489d | |||
ed9eff433b | |||
c2a5f768b8 | |||
a505315781 | |||
d76538cf28 | |||
309a991007 | |||
0340c83b47 | |||
9dfd2cf70b | |||
26d2da923b | |||
c66c725ee3 | |||
44f99295e9 | |||
d417181b70 | |||
9df5be003b | |||
cf3b1802d5 |
11
README.md
11
README.md
@ -1,14 +1,13 @@
|
||||
# Teapod
|
||||
|
||||
Teapod is a unofficial App for Anime on Demand (AoD). It allows you to watch all your favourite animes from AoD on your android device. To use Teapod you need to have a subscription to AoD.
|
||||
Teapod is a unofficial App for Crunchyroll. It allows you to watch all your favourite animes from Crunchyroll on your android device. To use Teapod you need to have a account at Crunchyroll.
|
||||
|
||||
[<img src="https://fdroid.gitlab.io/artwork/badge/get-it-on.png" height="75">](https://f-droid.org/de/packages/org.mosad.teapod/)
|
||||
|
||||
## Features
|
||||
* Watch all animes from AoD on your Android device
|
||||
* Watch all animes from Crunchyroll on your Android device
|
||||
* Native Player based on ExoPayer
|
||||
* Prefer the OmU version via the app settings
|
||||
* Save your favorite animes to "My List"
|
||||
|
||||
## Screenshots
|
||||
[<img src="https://www.mosad.xyz/images/Teapod/Teapod_Home.webp" width=180>](https://www.mosad.xyz/images/Teapod/Teapod_Home.webp)
|
||||
@ -17,14 +16,14 @@ Teapod is a unofficial App for Anime on Demand (AoD). It allows you to watch all
|
||||
[<img src="https://www.mosad.xyz/images/Teapod/Teapod_Search.webp" width=180>](https://www.mosad.xyz/images/Teapod/Teapod_Search.webp)
|
||||
|
||||
### License
|
||||
Teapod is licensed under the terms and conditions of GPL 3. This Project is not associated with Anime on Demand in any way. But they allow open source apps for their service.
|
||||
Teapod is licensed under the terms and conditions of GPL 3. This Project is not associated with Crunchyroll in any way.
|
||||
|
||||
### Contributing
|
||||
Currentl you need to have an AoD account to contrtibut to Teapod. Contributing without on is kind of impossible.If you want to contribute to Teapod and need an account on this gitea instance, please write me an email.
|
||||
Currently you need to have an Crunchyroll account to contribute to Teapod. Contributing without one is impossible.If you want to contribute to Teapod and need an account on this gitea instance, please write an email.
|
||||
|
||||
### [FAQ](https://git.mosad.xyz/Seil0/teapod/wiki#hilfe)
|
||||
|
||||
#### Why is it called Teapod?
|
||||
Teapod is a Acronym for "The ultimate anime app on demand", hence this project is called Teapod and not Teapot.
|
||||
|
||||
Teapod © 2020-2021 [@Seil0](https://git.mosad.xyz/Seil0)
|
||||
Teapod © 2020-2022 [@Seil0](https://git.mosad.xyz/Seil0)
|
||||
|
@ -1,17 +1,19 @@
|
||||
apply plugin: 'com.android.application'
|
||||
apply plugin: 'kotlin-android'
|
||||
apply plugin: 'kotlin-android-extensions'
|
||||
plugins {
|
||||
id 'com.android.application'
|
||||
id 'kotlin-android'
|
||||
id 'org.jetbrains.kotlin.plugin.serialization' version "$kotlin_version"
|
||||
}
|
||||
|
||||
android {
|
||||
compileSdkVersion 30
|
||||
compileSdkVersion 33
|
||||
buildToolsVersion "30.0.3"
|
||||
|
||||
defaultConfig {
|
||||
applicationId "org.mosad.teapod"
|
||||
minSdkVersion 23
|
||||
targetSdkVersion 30
|
||||
versionCode 4200 //00.04.200
|
||||
versionName "0.4.2"
|
||||
targetSdkVersion 32
|
||||
versionCode 100000 //01.00.000
|
||||
versionName "1.0.0"
|
||||
|
||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||
resValue "string", "build_time", buildTime()
|
||||
@ -29,43 +31,53 @@ android {
|
||||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
}
|
||||
|
||||
compileOptions {
|
||||
sourceCompatibility JavaVersion.VERSION_1_8
|
||||
targetCompatibility JavaVersion.VERSION_1_8
|
||||
}
|
||||
kotlinOptions {
|
||||
jvmTarget = '1.8'
|
||||
kotlin.sourceSets.all {
|
||||
languageSettings.optIn("kotlin.RequiresOptIn")
|
||||
}
|
||||
}
|
||||
namespace 'org.mosad.teapod'
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation fileTree(dir: "libs", include: ["*.jar"])
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.0'
|
||||
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
|
||||
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4'
|
||||
implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.3'
|
||||
|
||||
implementation 'androidx.core:core-ktx:1.6.0'
|
||||
implementation 'androidx.appcompat:appcompat:1.3.0'
|
||||
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
|
||||
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
|
||||
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
|
||||
implementation 'androidx.core:core-ktx:1.9.0'
|
||||
implementation 'androidx.core:core-splashscreen:1.0.0'
|
||||
implementation 'androidx.appcompat:appcompat:1.5.1'
|
||||
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
|
||||
implementation 'androidx.navigation:navigation-fragment-ktx:2.5.2'
|
||||
implementation 'androidx.navigation:navigation-ui-ktx:2.5.2'
|
||||
implementation 'androidx.security:security-crypto:1.1.0-alpha03'
|
||||
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
|
||||
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
|
||||
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1'
|
||||
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1'
|
||||
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1'
|
||||
|
||||
implementation 'com.google.android.material:material:1.4.0'
|
||||
implementation 'com.google.code.gson:gson:2.8.7'
|
||||
implementation 'com.google.android.exoplayer:exoplayer-core:2.14.1'
|
||||
implementation 'com.google.android.exoplayer:exoplayer-hls:2.14.1'
|
||||
implementation 'com.google.android.exoplayer:exoplayer-dash:2.14.1'
|
||||
implementation 'com.google.android.exoplayer:exoplayer-ui:2.14.1'
|
||||
implementation 'com.google.android.exoplayer:extension-mediasession:2.14.1'
|
||||
implementation 'com.google.android.material:material:1.6.1'
|
||||
implementation "com.google.android.exoplayer:exoplayer-core:$exo_version"
|
||||
implementation "com.google.android.exoplayer:exoplayer-hls:$exo_version"
|
||||
implementation "com.google.android.exoplayer:exoplayer-dash:$exo_version"
|
||||
implementation "com.google.android.exoplayer:exoplayer-ui:$exo_version"
|
||||
implementation "com.google.android.exoplayer:extension-mediasession:$exo_version"
|
||||
|
||||
implementation 'org.jsoup:jsoup:1.13.1'
|
||||
implementation 'com.github.bumptech.glide:glide:4.12.0'
|
||||
implementation 'com.facebook.shimmer:shimmer:0.5.0'
|
||||
|
||||
implementation 'com.github.bumptech.glide:glide:4.13.2'
|
||||
implementation 'jp.wasabeef:glide-transformations:4.3.0'
|
||||
implementation 'com.afollestad.material-dialogs:core:3.3.0'
|
||||
implementation 'com.afollestad.material-dialogs:bottomsheets:3.3.0'
|
||||
|
||||
implementation "io.ktor:ktor-client-core:$ktor_version"
|
||||
implementation "io.ktor:ktor-client-android:$ktor_version"
|
||||
implementation "io.ktor:ktor-client-content-negotiation:$ktor_version"
|
||||
implementation "io.ktor:ktor-serialization-kotlinx-json:$ktor_version"
|
||||
|
||||
testImplementation 'junit:junit:4.13.2'
|
||||
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
|
||||
|
32
app/proguard-rules.pro
vendored
32
app/proguard-rules.pro
vendored
@ -22,9 +22,35 @@
|
||||
#-renamesourcefileattribute SourceFile
|
||||
-keep class org.mosad.teapod.util.** { <fields>; }
|
||||
|
||||
#Gson
|
||||
-keepattributes Signature
|
||||
-dontwarn sun.misc.**
|
||||
-keep class org.json.** { *; }
|
||||
|
||||
# kotlinx.serialization
|
||||
# Keep `Companion` object fields of serializable classes.
|
||||
# This avoids serializer lookup through `getDeclaredClasses` as done for named companion objects.
|
||||
-if @kotlinx.serialization.Serializable class **
|
||||
-keepclassmembers class <1> {
|
||||
static <1>$Companion Companion;
|
||||
}
|
||||
|
||||
# Keep `serializer()` on companion objects (both default and named) of serializable classes.
|
||||
-if @kotlinx.serialization.Serializable class ** {
|
||||
static **$* *;
|
||||
}
|
||||
-keepclassmembers class <1>$<3> {
|
||||
kotlinx.serialization.KSerializer serializer(...);
|
||||
}
|
||||
|
||||
# Keep `INSTANCE.serializer()` of serializable objects.
|
||||
-if @kotlinx.serialization.Serializable class ** {
|
||||
public static ** INSTANCE;
|
||||
}
|
||||
-keepclassmembers class <1> {
|
||||
public static <1> INSTANCE;
|
||||
kotlinx.serialization.KSerializer serializer(...);
|
||||
}
|
||||
|
||||
# @Serializable and @Polymorphic are used at runtime for polymorphic serialization.
|
||||
-keepattributes RuntimeVisibleAnnotations,AnnotationDefault
|
||||
|
||||
#misc
|
||||
-dontwarn java.lang.instrument.ClassFileTransformer
|
||||
|
@ -1,7 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
package="org.mosad.teapod">
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
|
||||
@ -13,32 +12,27 @@
|
||||
android:supportsRtl="true"
|
||||
android:theme="@style/AppTheme.Dark">
|
||||
<activity
|
||||
android:name="org.mosad.teapod.ui.activity.SplashActivity"
|
||||
android:label="@string/app_name"
|
||||
android:theme="@style/SplashTheme"
|
||||
android:screenOrientation="portrait">
|
||||
android:exported="true"
|
||||
android:name="org.mosad.teapod.ui.activity.main.MainActivity"
|
||||
android:screenOrientation="portrait"
|
||||
android:theme="@style/Theme.App.Starting">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
<activity
|
||||
android:exported="false"
|
||||
android:name="org.mosad.teapod.ui.activity.onboarding.OnboardingActivity"
|
||||
android:label="@string/app_name"
|
||||
android:screenOrientation="portrait"
|
||||
android:launchMode="singleTop"
|
||||
android:windowSoftInputMode="adjustPan">
|
||||
</activity>
|
||||
<activity
|
||||
android:name="org.mosad.teapod.ui.activity.main.MainActivity"
|
||||
android:label="@string/app_name"
|
||||
android:screenOrientation="portrait">
|
||||
</activity>
|
||||
<activity
|
||||
android:exported="false"
|
||||
android:name="org.mosad.teapod.ui.activity.player.PlayerActivity"
|
||||
android:configChanges="orientation|screenSize|smallestScreenSize|screenLayout|layoutDirection"
|
||||
android:autoRemoveFromRecents="true"
|
||||
android:label="@string/app_name"
|
||||
android:launchMode="singleTask"
|
||||
android:parentActivityName="org.mosad.teapod.ui.activity.main.MainActivity"
|
||||
android:supportsPictureInPicture="true"
|
||||
|
@ -1,473 +0,0 @@
|
||||
/**
|
||||
* Teapod
|
||||
*
|
||||
* Copyright 2020-2021 <seil0@mosad.xyz>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.mosad.teapod.parser
|
||||
|
||||
import android.util.Log
|
||||
import com.google.gson.JsonParser
|
||||
import kotlinx.coroutines.*
|
||||
import org.jsoup.Connection
|
||||
import org.jsoup.Jsoup
|
||||
import org.mosad.teapod.preferences.EncryptedPreferences
|
||||
import org.mosad.teapod.util.*
|
||||
import org.mosad.teapod.util.DataTypes.MediaType
|
||||
import java.io.IOException
|
||||
import java.lang.NumberFormatException
|
||||
import java.util.*
|
||||
import kotlin.random.Random
|
||||
|
||||
object AoDParser {
|
||||
|
||||
private const val baseUrl = "https://www.anime-on-demand.de"
|
||||
private const val loginPath = "/users/sign_in"
|
||||
private const val libraryPath = "/animes"
|
||||
private const val subscriptionPath = "/mypools"
|
||||
|
||||
private const val userAgent = "Mozilla/5.0 (X11; Linux x86_64; rv:84.0) Gecko/20100101 Firefox/84.0"
|
||||
|
||||
private var sessionCookies = mutableMapOf<String, String>()
|
||||
private var csrfToken: String = ""
|
||||
private var loginSuccess = false
|
||||
|
||||
private val mediaList = arrayListOf<Media>() // actual media (data)
|
||||
val itemMediaList = arrayListOf<ItemMedia>() // gui media
|
||||
val highlightsList = arrayListOf<ItemMedia>()
|
||||
val newEpisodesList = arrayListOf<ItemMedia>()
|
||||
val newSimulcastsList = arrayListOf<ItemMedia>()
|
||||
val newTitlesList = arrayListOf<ItemMedia>()
|
||||
val topTenList = arrayListOf<ItemMedia>()
|
||||
|
||||
fun login(): Boolean = runBlocking {
|
||||
|
||||
withContext(Dispatchers.IO) {
|
||||
// get the authenticity token
|
||||
val resAuth = Jsoup.connect(baseUrl + loginPath)
|
||||
.header("User-Agent", userAgent)
|
||||
.execute()
|
||||
|
||||
val authenticityToken = resAuth.parse().select("meta[name=csrf-token]").attr("content")
|
||||
val authCookies = resAuth.cookies()
|
||||
|
||||
//Log.d(javaClass.name, "Received authenticity token: $authenticityToken")
|
||||
//Log.d(javaClass.name, "Received authenticity cookies: $authCookies")
|
||||
|
||||
val data = mapOf(
|
||||
Pair("user[login]", EncryptedPreferences.login),
|
||||
Pair("user[password]", EncryptedPreferences.password),
|
||||
Pair("user[remember_me]", "1"),
|
||||
Pair("commit", "Einloggen"),
|
||||
Pair("authenticity_token", authenticityToken)
|
||||
)
|
||||
|
||||
val resLogin = Jsoup.connect(baseUrl + loginPath)
|
||||
.method(Connection.Method.POST)
|
||||
.timeout(60000) // login can take some time default is 60000 (60 sec)
|
||||
.data(data)
|
||||
.postDataCharset("UTF-8")
|
||||
.cookies(authCookies)
|
||||
.execute()
|
||||
|
||||
//println(resLogin.body())
|
||||
|
||||
sessionCookies = resLogin.cookies()
|
||||
loginSuccess = resLogin.body().contains("Hallo, du bist jetzt angemeldet.")
|
||||
Log.i(javaClass.name, "Status: ${resLogin.statusCode()} (${resLogin.statusMessage()}), login successful: $loginSuccess")
|
||||
|
||||
loginSuccess
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* initially load all media and home screen data
|
||||
*/
|
||||
suspend fun initialLoading() {
|
||||
coroutineScope {
|
||||
launch { loadHome() }
|
||||
launch { listAnimes() }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* get a media by it's ID (int)
|
||||
* @return Media
|
||||
*/
|
||||
suspend fun getMediaById(mediaId: Int): Media {
|
||||
val media = mediaList.first { it.id == mediaId }
|
||||
|
||||
if (media.episodes.isEmpty()) {
|
||||
loadStreams(media).join()
|
||||
}
|
||||
|
||||
return media
|
||||
}
|
||||
|
||||
/**
|
||||
* get subscription info from aod website, remove "Anime-Abo" Prefix and trim
|
||||
*/
|
||||
suspend fun getSubscriptionInfoAsync(): Deferred<String> {
|
||||
return coroutineScope {
|
||||
async(Dispatchers.IO) {
|
||||
val res = Jsoup.connect(baseUrl + subscriptionPath)
|
||||
.cookies(sessionCookies)
|
||||
.get()
|
||||
|
||||
return@async res.select("a:contains(Anime-Abo)").text()
|
||||
.removePrefix("Anime-Abo").trim()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun getSubscriptionUrl(): String {
|
||||
return baseUrl + subscriptionPath
|
||||
}
|
||||
|
||||
suspend fun markAsWatched(mediaId: Int, episodeId: Int) {
|
||||
val episode = getMediaById(mediaId).getEpisodeById(episodeId)
|
||||
episode.watched = true
|
||||
sendCallback(episode.watchedCallback)
|
||||
|
||||
Log.d(javaClass.name, "Marked episode ${episode.id} as watched")
|
||||
}
|
||||
|
||||
// TODO don't use jsoup here
|
||||
private suspend fun sendCallback(callbackPath: String) = coroutineScope {
|
||||
launch(Dispatchers.IO) {
|
||||
val headers = mutableMapOf(
|
||||
Pair("Accept", "application/json, text/javascript, */*; q=0.01"),
|
||||
Pair("Accept-Language", "de,en-US;q=0.7,en;q=0.3"),
|
||||
Pair("Accept-Encoding", "gzip, deflate, br"),
|
||||
Pair("X-CSRF-Token", csrfToken),
|
||||
Pair("X-Requested-With", "XMLHttpRequest"),
|
||||
)
|
||||
|
||||
try {
|
||||
Jsoup.connect(baseUrl + callbackPath)
|
||||
.ignoreContentType(true)
|
||||
.cookies(sessionCookies)
|
||||
.headers(headers)
|
||||
.execute()
|
||||
} catch (ex: IOException) {
|
||||
Log.e(javaClass.name, "Callback for $callbackPath failed.", ex)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* load all media from aod into itemMediaList and mediaList
|
||||
* TODO private suspend fun listAnimes() = withContext(Dispatchers.IO) should also work, maybe a bug in android studio?
|
||||
*/
|
||||
private suspend fun listAnimes() = withContext(Dispatchers.IO) {
|
||||
launch(Dispatchers.IO) {
|
||||
val resAnimes = Jsoup.connect(baseUrl + libraryPath).get()
|
||||
//println(resAnimes)
|
||||
|
||||
itemMediaList.clear()
|
||||
mediaList.clear()
|
||||
resAnimes.select("div.animebox").forEach {
|
||||
val type = if (it.select("p.animebox-link").select("a").text().lowercase(Locale.ROOT) == "zur serie") {
|
||||
MediaType.TVSHOW
|
||||
} else {
|
||||
MediaType.MOVIE
|
||||
}
|
||||
val mediaTitle = it.select("h3.animebox-title").text()
|
||||
val mediaLink = it.select("p.animebox-link").select("a").attr("href")
|
||||
val mediaImage = it.select("p.animebox-image").select("img").attr("src")
|
||||
val mediaShortText = it.select("p.animebox-shorttext").text()
|
||||
val mediaId = mediaLink.substringAfterLast("/").toInt()
|
||||
|
||||
itemMediaList.add(ItemMedia(mediaId, mediaTitle, mediaImage))
|
||||
mediaList.add(Media(mediaId, mediaLink, type).apply {
|
||||
info.title = mediaTitle
|
||||
info.posterUrl = mediaImage
|
||||
info.shortDesc = mediaShortText
|
||||
})
|
||||
}
|
||||
|
||||
Log.i(javaClass.name, "Total library size is: ${mediaList.size}")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* load new episodes, titles and highlights
|
||||
*/
|
||||
private suspend fun loadHome() = withContext(Dispatchers.IO) {
|
||||
launch(Dispatchers.IO) {
|
||||
val resHome = Jsoup.connect(baseUrl).get()
|
||||
|
||||
// get highlights from AoD
|
||||
highlightsList.clear()
|
||||
resHome.select("#aod-highlights").select("div.news-item").forEach {
|
||||
val mediaId = it.select("div.news-item-text").select("a.serienlink")
|
||||
.attr("href").substringAfterLast("/").toIntOrNull()
|
||||
val mediaTitle = it.select("div.news-title").select("h2").text()
|
||||
val mediaImage = it.select("img").attr("src")
|
||||
|
||||
if (mediaId != null) {
|
||||
highlightsList.add(ItemMedia(mediaId, mediaTitle, mediaImage))
|
||||
}
|
||||
}
|
||||
|
||||
// get all new episodes from AoD
|
||||
newEpisodesList.clear()
|
||||
resHome.select("h2:contains(Neue Episoden)").next().select("li").forEach {
|
||||
val mediaId = it.select("a.thumbs").attr("href")
|
||||
.substringAfterLast("/").toIntOrNull()
|
||||
val mediaImage = it.select("a.thumbs > img").attr("src")
|
||||
val mediaTitle = "${it.select("a").text()} - ${it.select("span.neweps").text()}"
|
||||
|
||||
if (mediaId != null) {
|
||||
newEpisodesList.add(ItemMedia(mediaId, mediaTitle, mediaImage))
|
||||
}
|
||||
}
|
||||
|
||||
// get new simulcasts from AoD
|
||||
newSimulcastsList.clear()
|
||||
resHome.select("h2:contains(Neue Simulcasts)").next().select("li").forEach {
|
||||
val mediaId = it.select("a.thumbs").attr("href")
|
||||
.substringAfterLast("/").toIntOrNull()
|
||||
val mediaImage = it.select("a.thumbs > img").attr("src")
|
||||
val mediaTitle = it.select("a").text()
|
||||
|
||||
if (mediaId != null) {
|
||||
newSimulcastsList.add(ItemMedia(mediaId, mediaTitle, mediaImage))
|
||||
}
|
||||
}
|
||||
|
||||
// get new titles from AoD
|
||||
newTitlesList.clear()
|
||||
resHome.select("h2:contains(Neue Anime-Titel)").next().select("li").forEach {
|
||||
val mediaId = it.select("a.thumbs").attr("href")
|
||||
.substringAfterLast("/").toIntOrNull()
|
||||
val mediaImage = it.select("a.thumbs > img").attr("src")
|
||||
val mediaTitle = it.select("a").text()
|
||||
|
||||
if (mediaId != null) {
|
||||
newTitlesList.add(ItemMedia(mediaId, mediaTitle, mediaImage))
|
||||
}
|
||||
}
|
||||
|
||||
// get top ten from AoD
|
||||
topTenList.clear()
|
||||
resHome.select("h2:contains(Anime Top 10)").next().select("li").forEach {
|
||||
val mediaId = it.select("a.thumbs").attr("href")
|
||||
.substringAfterLast("/").toIntOrNull()
|
||||
val mediaImage = it.select("a.thumbs > img").attr("src")
|
||||
val mediaTitle = it.select("a").text()
|
||||
|
||||
if (mediaId != null) {
|
||||
topTenList.add(ItemMedia(mediaId, mediaTitle, mediaImage))
|
||||
}
|
||||
}
|
||||
|
||||
// if highlights is empty, add a random new title
|
||||
if (highlightsList.isEmpty()) {
|
||||
if (newTitlesList.isNotEmpty()) {
|
||||
highlightsList.add(newTitlesList[Random.nextInt(0, newTitlesList.size)])
|
||||
} else {
|
||||
highlightsList.add(ItemMedia(0,"", ""))
|
||||
}
|
||||
}
|
||||
|
||||
Log.i(javaClass.name, "loaded home")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO rework the media loading process, don't modify media object
|
||||
* TODO catch SocketTimeoutException from loading to show a waring dialog
|
||||
* load streams for the media path, movies have one episode
|
||||
* @param media is used as call ba reference
|
||||
*/
|
||||
private suspend fun loadStreams(media: Media) = coroutineScope {
|
||||
launch(Dispatchers.IO) {
|
||||
if (sessionCookies.isEmpty()) login()
|
||||
|
||||
if (!loginSuccess) {
|
||||
Log.w(javaClass.name, "Login, was not successful.")
|
||||
return@launch
|
||||
}
|
||||
|
||||
// get the media page
|
||||
val res = Jsoup.connect(baseUrl + media.link)
|
||||
.cookies(sessionCookies)
|
||||
.get()
|
||||
|
||||
//println(res)
|
||||
|
||||
if (csrfToken.isEmpty()) {
|
||||
csrfToken = res.select("meta[name=csrf-token]").attr("content")
|
||||
//Log.i(javaClass.name, "New csrf token is $csrfToken")
|
||||
}
|
||||
|
||||
val besides = res.select("div.besides").first()
|
||||
val playlists = besides.select("input.streamstarter_html5").map { streamstarter ->
|
||||
parsePlaylistAsync(
|
||||
streamstarter.attr("data-playlist"),
|
||||
streamstarter.attr("data-lang")
|
||||
)
|
||||
}.awaitAll()
|
||||
|
||||
playlists.forEach { aod ->
|
||||
// TODO improve language handling
|
||||
val locale = when (aod.extLanguage) {
|
||||
"ger" -> Locale.GERMAN
|
||||
"jap" -> Locale.JAPANESE
|
||||
else -> Locale.ROOT
|
||||
}
|
||||
|
||||
aod.playlist.forEach { ep ->
|
||||
try {
|
||||
if (media.hasEpisode(ep.mediaid)) {
|
||||
media.getEpisodeById(ep.mediaid).streams.add(
|
||||
Stream(ep.sources.first().file, locale)
|
||||
)
|
||||
} else {
|
||||
media.episodes.add(Episode(
|
||||
id = ep.mediaid,
|
||||
streams = mutableListOf(Stream(ep.sources.first().file, locale)),
|
||||
posterUrl = ep.image,
|
||||
title = ep.title,
|
||||
description = ep.description,
|
||||
number = getNumberFromTitle(ep.title, media.type)
|
||||
))
|
||||
}
|
||||
} catch (ex: Exception) {
|
||||
Log.w(javaClass.name, "Could not parse episode information.", ex)
|
||||
}
|
||||
}
|
||||
}
|
||||
Log.i(javaClass.name, "Loaded playlists successfully")
|
||||
|
||||
// additional info from the media page
|
||||
res.select("table.vertical-table").select("tr").forEach { row ->
|
||||
when (row.select("th").text().lowercase(Locale.ROOT)) {
|
||||
"produktionsjahr" -> media.info.year = row.select("td").text().toInt()
|
||||
"fsk" -> media.info.age = row.select("td").text().toInt()
|
||||
"episodenanzahl" -> {
|
||||
media.info.episodesCount = row.select("td").text()
|
||||
.substringBefore("/")
|
||||
.filter { it.isDigit() }
|
||||
.toInt()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// similar titles from media page
|
||||
media.info.similar = res.select("h2:contains(Ähnliche Animes)").next().select("li").mapNotNull {
|
||||
val mediaId = it.select("a.thumbs").attr("href")
|
||||
.substringAfterLast("/").toIntOrNull()
|
||||
val mediaImage = it.select("a.thumbs > img").attr("src")
|
||||
val mediaTitle = it.select("a").text()
|
||||
|
||||
if (mediaId != null) {
|
||||
ItemMedia(mediaId, mediaTitle, mediaImage)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
// additional information for tv shows the episode title (description) is loaded from the "api"
|
||||
if (media.type == MediaType.TVSHOW) {
|
||||
res.select("div.three-box-container > div.episodebox").forEach { episodebox ->
|
||||
// make sure the episode has a streaming link
|
||||
if (episodebox.select("input.streamstarter_html5").isNotEmpty()) {
|
||||
val episodeId = episodebox.select("div.flip-front").attr("id").substringAfter("-").toInt()
|
||||
val episodeShortDesc = episodebox.select("p.episodebox-shorttext").text()
|
||||
val episodeWatched = episodebox.select("div.episodebox-icons > div").hasClass("status-icon-orange")
|
||||
val episodeWatchedCallback = episodebox.select("input.streamstarter_html5").eachAttr("data-playlist").first()
|
||||
|
||||
media.episodes.firstOrNull { it.id == episodeId }?.apply {
|
||||
shortDesc = episodeShortDesc
|
||||
watched = episodeWatched
|
||||
watchedCallback = episodeWatchedCallback
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Log.i(javaClass.name, "media loaded successfully")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* don't use Gson().fromJson() as we don't have any control over the api and it may change
|
||||
*/
|
||||
private fun parsePlaylistAsync(playlistPath: String, language: String): Deferred<AoDObject> {
|
||||
if (playlistPath == "[]") {
|
||||
return CompletableDeferred(AoDObject(listOf(), language))
|
||||
}
|
||||
|
||||
return CoroutineScope(Dispatchers.IO).async(Dispatchers.IO) {
|
||||
val headers = mutableMapOf(
|
||||
Pair("Accept", "application/json, text/javascript, */*; q=0.01"),
|
||||
Pair("Accept-Language", "de,en-US;q=0.7,en;q=0.3"),
|
||||
Pair("Accept-Encoding", "gzip, deflate, br"),
|
||||
Pair("X-CSRF-Token", csrfToken),
|
||||
Pair("X-Requested-With", "XMLHttpRequest"),
|
||||
)
|
||||
|
||||
//println("loading streaminfo with cstf: $csrfToken")
|
||||
|
||||
val res = Jsoup.connect(baseUrl + playlistPath)
|
||||
.ignoreContentType(true)
|
||||
.cookies(sessionCookies)
|
||||
.headers(headers)
|
||||
.timeout(120000) // loading the playlist can take some time
|
||||
.execute()
|
||||
|
||||
//Gson().fromJson(res.body(), AoDObject::class.java)
|
||||
|
||||
return@async AoDObject(JsonParser.parseString(res.body()).asJsonObject
|
||||
.get("playlist").asJsonArray.map {
|
||||
Playlist(
|
||||
sources = it.asJsonObject.get("sources").asJsonArray.map { source ->
|
||||
Source(source.asJsonObject.get("file").asString)
|
||||
},
|
||||
image = it.asJsonObject.get("image").asString,
|
||||
title = it.asJsonObject.get("title").asString,
|
||||
description = it.asJsonObject.get("description").asString,
|
||||
mediaid = it.asJsonObject.get("mediaid").asInt
|
||||
)
|
||||
},
|
||||
language
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* get the episode number from the title
|
||||
* @param title the episode title, containing a number after "Ep."
|
||||
* @param type the media type, if not TVSHOW, return 0
|
||||
* @return the episode number, on NumberFormatException return 0
|
||||
*/
|
||||
private fun getNumberFromTitle(title: String, type: MediaType): Int {
|
||||
return if (type == MediaType.TVSHOW) {
|
||||
try {
|
||||
title.substringAfter(", Ep. ").toInt()
|
||||
} catch (nex: NumberFormatException) {
|
||||
0
|
||||
}
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,725 @@
|
||||
/**
|
||||
* Teapod
|
||||
*
|
||||
* Copyright 2020-2022 <seil0@mosad.xyz>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.mosad.teapod.parser.crunchyroll
|
||||
|
||||
import android.util.Log
|
||||
import io.ktor.client.*
|
||||
import io.ktor.client.call.*
|
||||
import io.ktor.client.plugins.*
|
||||
import io.ktor.client.plugins.contentnegotiation.*
|
||||
import io.ktor.client.request.*
|
||||
import io.ktor.client.request.forms.*
|
||||
import io.ktor.client.statement.*
|
||||
import io.ktor.http.*
|
||||
import io.ktor.serialization.kotlinx.json.*
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.serialization.SerializationException
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
import kotlinx.serialization.json.buildJsonObject
|
||||
import kotlinx.serialization.json.put
|
||||
import org.mosad.teapod.preferences.EncryptedPreferences
|
||||
import org.mosad.teapod.preferences.Preferences
|
||||
|
||||
object Crunchyroll {
|
||||
private val TAG = javaClass.name
|
||||
|
||||
private val client = HttpClient {
|
||||
install(ContentNegotiation) {
|
||||
json(Json {
|
||||
ignoreUnknownKeys = true
|
||||
})
|
||||
}
|
||||
}
|
||||
private const val baseUrl = "https://beta-api.crunchyroll.com"
|
||||
private const val basicApiTokenUrl = "https://gitlab.com/-/snippets/2274956/raw/main/snippetfile1.txt"
|
||||
private var basicApiToken: String = ""
|
||||
|
||||
private lateinit var token: Token
|
||||
private var tokenValidUntil: Long = 0
|
||||
@OptIn(DelicateCoroutinesApi::class)
|
||||
private val tokenRefreshContext = newSingleThreadContext("TokenRefreshContext")
|
||||
|
||||
private var accountID = ""
|
||||
private var externalID = ""
|
||||
|
||||
private var policy = ""
|
||||
private var signature = ""
|
||||
private var keyPairID = ""
|
||||
|
||||
private val browsingCache = hashMapOf<String, BrowseResult>()
|
||||
|
||||
/**
|
||||
* Load the pai token, see:
|
||||
* https://git.mosad.xyz/mosad/NonePublicIssues/issues/1
|
||||
*
|
||||
* TODO handle empty file
|
||||
*/
|
||||
fun initBasicApiToken() = runBlocking {
|
||||
withContext(Dispatchers.IO) {
|
||||
basicApiToken = client.get { url(basicApiTokenUrl) }.bodyAsText()
|
||||
Log.i(TAG, "basic auth token: $basicApiToken")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Login to the crunchyroll API.
|
||||
*
|
||||
* @param username The Username/Email of the user to log in
|
||||
* @param password The Accounts Password
|
||||
*
|
||||
* @return Boolean: True if login was successful, else false
|
||||
*/
|
||||
fun login(username: String, password: String): Boolean = runBlocking {
|
||||
val tokenEndpoint = "/auth/v1/token"
|
||||
val formData = Parameters.build {
|
||||
append("username", username)
|
||||
append("password", password)
|
||||
append("grant_type", "password")
|
||||
append("scope", "offline_access")
|
||||
}
|
||||
|
||||
var success = false// is false
|
||||
withContext(Dispatchers.IO) {
|
||||
Log.i(TAG, "getting token ...")
|
||||
|
||||
val status = try {
|
||||
val response: HttpResponse = client.submitForm("$baseUrl$tokenEndpoint", formParameters = formData) {
|
||||
header("Authorization", "Basic $basicApiToken")
|
||||
}
|
||||
token = response.body()
|
||||
tokenValidUntil = System.currentTimeMillis() + (token.expiresIn * 1000)
|
||||
response.status
|
||||
} catch (ex: ClientRequestException) {
|
||||
val status = ex.response.status
|
||||
if (status == HttpStatusCode.Unauthorized) {
|
||||
Log.e(TAG, "Could not complete login: " +
|
||||
"${status.value} ${status.description}. " +
|
||||
"Probably wrong username or password")
|
||||
}
|
||||
|
||||
status
|
||||
}
|
||||
Log.i(TAG, "Login complete with code $status")
|
||||
success = (status == HttpStatusCode.OK)
|
||||
}
|
||||
|
||||
return@runBlocking success
|
||||
}
|
||||
|
||||
private fun refreshToken() {
|
||||
login(EncryptedPreferences.login, EncryptedPreferences.password)
|
||||
}
|
||||
|
||||
/**
|
||||
* Requests: get, post, delete
|
||||
*/
|
||||
|
||||
private suspend inline fun <reified T> request(
|
||||
url: String,
|
||||
httpMethod: HttpMethod,
|
||||
params: List<Pair<String, Any?>> = listOf(),
|
||||
bodyObject: Any = Any()
|
||||
): T = coroutineScope {
|
||||
withContext(tokenRefreshContext) {
|
||||
if (System.currentTimeMillis() > tokenValidUntil) refreshToken()
|
||||
}
|
||||
|
||||
return@coroutineScope (Dispatchers.IO) {
|
||||
val response: T = client.request(url) {
|
||||
method = httpMethod
|
||||
header("Authorization", "${token.tokenType} ${token.accessToken}")
|
||||
params.forEach {
|
||||
parameter(it.first, it.second)
|
||||
}
|
||||
|
||||
// for json set body and content type
|
||||
if (bodyObject is JsonObject) {
|
||||
setBody(bodyObject)
|
||||
contentType(ContentType.Application.Json)
|
||||
}
|
||||
}.body()
|
||||
|
||||
response
|
||||
}
|
||||
}
|
||||
|
||||
private suspend inline fun <reified T> requestGet(
|
||||
endpoint: String,
|
||||
params: List<Pair<String, Any?>> = listOf(),
|
||||
url: String = ""
|
||||
): T {
|
||||
val path = url.ifEmpty { "$baseUrl$endpoint" }
|
||||
|
||||
return request(path, HttpMethod.Get, params)
|
||||
}
|
||||
|
||||
private suspend fun requestPost(
|
||||
endpoint: String,
|
||||
params: List<Pair<String, Any?>> = listOf(),
|
||||
bodyObject: JsonObject
|
||||
) {
|
||||
val path = "$baseUrl$endpoint"
|
||||
|
||||
val response: HttpResponse = request(path, HttpMethod.Post, params, bodyObject)
|
||||
Log.i(TAG, "Response: $response")
|
||||
}
|
||||
|
||||
private suspend fun requestPatch(
|
||||
endpoint: String,
|
||||
params: List<Pair<String, Any?>> = listOf(),
|
||||
bodyObject: JsonObject
|
||||
) {
|
||||
val path = "$baseUrl$endpoint"
|
||||
|
||||
val response: HttpResponse = request(path, HttpMethod.Patch, params, bodyObject)
|
||||
Log.i(TAG, "Response: $response")
|
||||
}
|
||||
|
||||
private suspend fun requestDelete(
|
||||
endpoint: String,
|
||||
params: List<Pair<String, Any?>> = listOf(),
|
||||
url: String = ""
|
||||
) = coroutineScope {
|
||||
val path = url.ifEmpty { "$baseUrl$endpoint" }
|
||||
|
||||
val response: HttpResponse = request(path, HttpMethod.Delete, params)
|
||||
Log.i(TAG, "Response: $response")
|
||||
}
|
||||
|
||||
/**
|
||||
* Basic functions: index, account
|
||||
* Needed for other functions to work properly!
|
||||
*/
|
||||
|
||||
/**
|
||||
* Retrieve the identifiers necessary for streaming. If the identifiers are
|
||||
* retrieved, set the corresponding global var. The identifiers are valid for 24h.
|
||||
*/
|
||||
suspend fun index() {
|
||||
val indexEndpoint = "/index/v2"
|
||||
|
||||
val index: Index = requestGet(indexEndpoint)
|
||||
policy = index.cms.policy
|
||||
signature = index.cms.signature
|
||||
keyPairID = index.cms.keyPairId
|
||||
|
||||
Log.i(TAG, "Policy : $policy")
|
||||
Log.i(TAG, "Signature : $signature")
|
||||
Log.i(TAG, "Key Pair ID : $keyPairID")
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the account id and set the corresponding global var.
|
||||
* The account id is needed for other calls.
|
||||
*
|
||||
* This must be execute on every start for teapod to work properly!
|
||||
*/
|
||||
suspend fun account() {
|
||||
val indexEndpoint = "/accounts/v1/me"
|
||||
|
||||
val account: Account = try {
|
||||
requestGet(indexEndpoint)
|
||||
} catch (ex: SerializationException) {
|
||||
Log.e(TAG, "SerializationException in account(). This is bad!", ex)
|
||||
NoneAccount
|
||||
}
|
||||
|
||||
accountID = account.accountId
|
||||
externalID = account.externalId
|
||||
}
|
||||
|
||||
/**
|
||||
* General element/media functions: browse, search, objects, season_list
|
||||
*/
|
||||
|
||||
/**
|
||||
* Browse the media available on crunchyroll.
|
||||
*
|
||||
* @param sortBy
|
||||
* @param n Number of items to return, defaults to 10
|
||||
*
|
||||
* @return A **[BrowseResult]** object is returned.
|
||||
*/
|
||||
suspend fun browse(
|
||||
categories: List<Categories> = emptyList(),
|
||||
sortBy: SortBy = SortBy.ALPHABETICAL,
|
||||
seasonTag: String = "",
|
||||
start: Int = 0,
|
||||
n: Int = 10
|
||||
): BrowseResult {
|
||||
val browseEndpoint = "/content/v1/browse"
|
||||
val parameters = mutableListOf(
|
||||
"locale" to Preferences.preferredLocale.toLanguageTag(),
|
||||
"sort_by" to sortBy.str,
|
||||
"start" to start,
|
||||
"n" to n
|
||||
)
|
||||
|
||||
// if a season tag is present add it to the parameters
|
||||
if (seasonTag.isNotEmpty()) {
|
||||
parameters.add("season_tag" to seasonTag)
|
||||
}
|
||||
|
||||
// if a season tag is present add it to the parameters
|
||||
if (categories.isNotEmpty()) {
|
||||
parameters.add("categories" to categories.joinToString(",") { it.str })
|
||||
}
|
||||
|
||||
// fetch result if not already cached
|
||||
if (browsingCache.contains(parameters.toString())) {
|
||||
Log.d(TAG, "browse result cached: $parameters")
|
||||
} else {
|
||||
Log.d(TAG, "browse result not cached, fetching: $parameters")
|
||||
val browseResult: BrowseResult = try {
|
||||
requestGet(browseEndpoint, parameters)
|
||||
}catch (ex: SerializationException) {
|
||||
Log.e(TAG, "SerializationException in browse().", ex)
|
||||
NoneBrowseResult
|
||||
}
|
||||
|
||||
// if the cache has more than 100 entries clear it, so it doesn't become a memory problem
|
||||
// Note: this value is totally guessed and should be replaced by a properly researched value
|
||||
if (browsingCache.size > 100) {
|
||||
browsingCache.clear()
|
||||
}
|
||||
|
||||
// add results to cache
|
||||
browsingCache[parameters.toString()] = browseResult
|
||||
}
|
||||
|
||||
return browsingCache[parameters.toString()] ?: NoneBrowseResult
|
||||
}
|
||||
|
||||
/**
|
||||
* Search fo a query term.
|
||||
* Note: currently this function only supports series/tv shows.
|
||||
*
|
||||
* @param query The query term as String
|
||||
* @param n The maximum number of results to return, default = 10
|
||||
* @return A **[SearchResult]** object
|
||||
*/
|
||||
suspend fun search(query: String, n: Int = 10): SearchResult {
|
||||
val searchEndpoint = "/content/v1/search"
|
||||
val parameters = listOf(
|
||||
"locale" to Preferences.preferredLocale.toLanguageTag(),
|
||||
"q" to query,
|
||||
"n" to n,
|
||||
"type" to "series"
|
||||
)
|
||||
|
||||
// TODO episodes have thumbnails as image, and not poster_tall/poster_tall,
|
||||
// to work around this, for now only tv shows are supported
|
||||
|
||||
return try {
|
||||
requestGet(searchEndpoint, parameters)
|
||||
} catch (ex: SerializationException) {
|
||||
Log.e(TAG, "SerializationException in search(), with query = \"$query\".", ex)
|
||||
NoneSearchResult
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a collection of series objects.
|
||||
* Note: episode objects are currently not supported
|
||||
*
|
||||
* @param objects The object IDs as list of Strings
|
||||
* @return A **[Collection]** of Panels
|
||||
*/
|
||||
suspend fun objects(objects: List<String>): Collection<Item> {
|
||||
val episodesEndpoint = "/cms/v2/DE/M3/crunchyroll/objects/${objects.joinToString(",")}"
|
||||
val parameters = listOf(
|
||||
"locale" to Preferences.preferredLocale.toLanguageTag(),
|
||||
"Signature" to signature,
|
||||
"Policy" to policy,
|
||||
"Key-Pair-Id" to keyPairID
|
||||
)
|
||||
|
||||
return try {
|
||||
requestGet(episodesEndpoint, parameters)
|
||||
} catch (ex: SerializationException) {
|
||||
Log.e(TAG, "SerializationException in objects().", ex)
|
||||
NoneCollection
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* List all available seasons as **[SeasonListItem]**.
|
||||
*/
|
||||
@Suppress("unused")
|
||||
suspend fun seasonList(): DiscSeasonList {
|
||||
val seasonListEndpoint = "/content/v1/season_list"
|
||||
val parameters = listOf("locale" to Preferences.preferredLocale.toLanguageTag())
|
||||
|
||||
return try {
|
||||
requestGet(seasonListEndpoint, parameters)
|
||||
} catch (ex: SerializationException) {
|
||||
Log.e(TAG, "SerializationException in seasonList().", ex)
|
||||
NoneDiscSeasonList
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Main media functions: series, season, episodes, playback
|
||||
*/
|
||||
|
||||
/**
|
||||
* series id == crunchyroll id?
|
||||
*/
|
||||
suspend fun series(seriesId: String): Series {
|
||||
val seriesEndpoint = "/cms/v2/${token.country}/M3/crunchyroll/series/$seriesId"
|
||||
val parameters = listOf(
|
||||
"locale" to Preferences.preferredLocale.toLanguageTag(),
|
||||
"Signature" to signature,
|
||||
"Policy" to policy,
|
||||
"Key-Pair-Id" to keyPairID
|
||||
)
|
||||
|
||||
return try {
|
||||
requestGet(seriesEndpoint, parameters)
|
||||
} catch (ex: SerializationException) {
|
||||
Log.e(TAG, "SerializationException in series().", ex)
|
||||
NoneSeries
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the next episode for a series.
|
||||
*
|
||||
* @param seriesId The series id for which to call up next
|
||||
* @return A **[UpNextSeriesItem]** with a Panel representing the up next episode
|
||||
*/
|
||||
suspend fun upNextSeries(seriesId: String): UpNextSeriesItem {
|
||||
val upNextSeriesEndpoint = "/content/v1/up_next_series"
|
||||
val parameters = listOf(
|
||||
"series_id" to seriesId,
|
||||
"locale" to Preferences.preferredLocale.toLanguageTag()
|
||||
)
|
||||
|
||||
return try {
|
||||
requestGet(upNextSeriesEndpoint, parameters)
|
||||
} catch (ex: SerializationException) {
|
||||
Log.e(TAG, "SerializationException in upNextSeries().", ex)
|
||||
NoneUpNextSeriesItem
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all available seasons for a series.
|
||||
*
|
||||
* @param seriesId The series id for which to get the seasons
|
||||
* @return A **[Seasons]** object with a list of **[Season]**
|
||||
*/
|
||||
suspend fun seasons(seriesId: String): Seasons {
|
||||
val seasonsEndpoint = "/cms/v2/${token.country}/M3/crunchyroll/seasons"
|
||||
val parameters = listOf(
|
||||
"series_id" to seriesId,
|
||||
"locale" to Preferences.preferredLocale.toLanguageTag(),
|
||||
"Signature" to signature,
|
||||
"Policy" to policy,
|
||||
"Key-Pair-Id" to keyPairID
|
||||
)
|
||||
|
||||
return try {
|
||||
requestGet(seasonsEndpoint, parameters)
|
||||
} catch (ex: SerializationException) {
|
||||
Log.e(TAG, "SerializationException in seasons().", ex)
|
||||
NoneSeasons
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all available episodes for a season.
|
||||
*
|
||||
* @param seasonId The season id for which to get the episodes
|
||||
* @return A **[Episodes]** object with a list of **[Episode]**
|
||||
*/
|
||||
suspend fun episodes(seasonId: String): Episodes {
|
||||
val episodesEndpoint = "/cms/v2/${token.country}/M3/crunchyroll/episodes"
|
||||
val parameters = listOf(
|
||||
"season_id" to seasonId,
|
||||
"locale" to Preferences.preferredLocale.toLanguageTag(),
|
||||
"Signature" to signature,
|
||||
"Policy" to policy,
|
||||
"Key-Pair-Id" to keyPairID
|
||||
)
|
||||
|
||||
return try {
|
||||
requestGet(episodesEndpoint, parameters)
|
||||
} catch (ex: SerializationException) {
|
||||
Log.e(TAG, "SerializationException in episodes().", ex)
|
||||
NoneEpisodes
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all available subtitles and streams of a episode.
|
||||
*
|
||||
* @param url The playback url of a episode
|
||||
* @return A **[Playback]** object
|
||||
*/
|
||||
suspend fun playback(url: String): Playback {
|
||||
return try {
|
||||
requestGet("", url = url)
|
||||
} catch (ex: SerializationException) {
|
||||
Log.e(TAG, "SerializationException in playback(), with url = $url.", ex)
|
||||
NonePlayback
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Additional media functions: watchlist (series), playhead, similar to
|
||||
*/
|
||||
|
||||
/**
|
||||
* Check if a media is in the user's watchlist.
|
||||
*
|
||||
* @param seriesId The crunchyroll series id of the media to check
|
||||
* @return **[Boolean]**: ture if it was found, else false
|
||||
*/
|
||||
suspend fun isWatchlist(seriesId: String): Boolean {
|
||||
val watchlistSeriesEndpoint = "/content/v1/watchlist/$accountID/$seriesId"
|
||||
val parameters = listOf("locale" to Preferences.preferredLocale.toLanguageTag())
|
||||
|
||||
return try {
|
||||
(requestGet(watchlistSeriesEndpoint, parameters) as JsonObject)
|
||||
.containsKey(seriesId)
|
||||
} catch (ex: SerializationException) {
|
||||
Log.e(TAG, "SerializationException in isWatchlist() with seriesId = $seriesId", ex)
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a media to the user's watchlist.
|
||||
*
|
||||
* @param seriesId The crunchyroll series id of the media to check
|
||||
*/
|
||||
suspend fun postWatchlist(seriesId: String) {
|
||||
val watchlistPostEndpoint = "/content/v1/watchlist/$accountID"
|
||||
val parameters = listOf("locale" to Preferences.preferredLocale.toLanguageTag())
|
||||
|
||||
val json = buildJsonObject {
|
||||
put("content_id", seriesId)
|
||||
}
|
||||
|
||||
requestPost(watchlistPostEndpoint, parameters, json)
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a media from the user's watchlist.
|
||||
*
|
||||
* @param seriesId The crunchyroll series id of the media to check
|
||||
*/
|
||||
suspend fun deleteWatchlist(seriesId: String) {
|
||||
val watchlistDeleteEndpoint = "/content/v1/watchlist/$accountID/$seriesId"
|
||||
val parameters = listOf("locale" to Preferences.preferredLocale.toLanguageTag())
|
||||
|
||||
requestDelete(watchlistDeleteEndpoint, parameters)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get playhead information for all episodes in episodeIDs.
|
||||
* The Information returned contains the playhead position, watched state
|
||||
* and last modified date.
|
||||
*
|
||||
* @param episodeIDs A **[List]** of episodes IDs as strings.
|
||||
* @return A **[Map]**<String, **[PlayheadObject]**> containing playback info.
|
||||
*/
|
||||
suspend fun playheads(episodeIDs: List<String>): PlayheadsMap {
|
||||
val playheadsEndpoint = "/content/v1/playheads/$accountID/${episodeIDs.joinToString(",")}"
|
||||
val parameters = listOf("locale" to Preferences.preferredLocale.toLanguageTag())
|
||||
|
||||
return try {
|
||||
requestGet(playheadsEndpoint, parameters)
|
||||
} catch (ex: SerializationException) {
|
||||
Log.e(TAG, "SerializationException in playheads().", ex)
|
||||
emptyMap()
|
||||
} catch (ex: Throwable) {
|
||||
Log.e(TAG, "Exception in playheads().", ex.cause)
|
||||
emptyMap()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Post the playhead to crunchy (playhead position,watched state)
|
||||
*
|
||||
* @param episodeId A episode ID as strings.
|
||||
* @param playhead The episodes playhead in seconds.
|
||||
*/
|
||||
suspend fun postPlayheads(episodeId: String, playhead: Int) {
|
||||
val playheadsEndpoint = "/content/v1/playheads/$accountID"
|
||||
val parameters = listOf("locale" to Preferences.preferredLocale.toLanguageTag())
|
||||
|
||||
val json = buildJsonObject {
|
||||
put("content_id", episodeId)
|
||||
put("playhead", playhead)
|
||||
}
|
||||
|
||||
try {
|
||||
requestPost(playheadsEndpoint, parameters, json)
|
||||
} catch (ex: Throwable) {
|
||||
Log.e(TAG, "Exception in postPlayheads()", ex.cause)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get similar media for a show/movie.
|
||||
*
|
||||
* @param seriesId The crunchyroll series id of the media
|
||||
* @param n The maximum number of results to return, default = 10
|
||||
* @return A **[SimilarToResult]** object
|
||||
*/
|
||||
suspend fun similarTo(seriesId: String, n: Int = 10): SimilarToResult {
|
||||
val similarToEndpoint = "/content/v1/$accountID/similar_to"
|
||||
val parameters = listOf(
|
||||
"guid" to seriesId,
|
||||
"locale" to Preferences.preferredLocale.toLanguageTag(),
|
||||
"n" to n
|
||||
)
|
||||
|
||||
return try {
|
||||
requestGet(similarToEndpoint, parameters)
|
||||
} catch (ex: SerializationException) {
|
||||
Log.e(TAG, "SerializationException in similarTo().", ex)
|
||||
NoneSimilarToResult
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Listing functions: watchlist (list), up_next_account
|
||||
*/
|
||||
|
||||
/**
|
||||
* List items present in the watchlist.
|
||||
*
|
||||
* @param n Number of items to return, defaults to 20.
|
||||
* @return A **[Watchlist]** containing up to n **[Item]**.
|
||||
*/
|
||||
suspend fun watchlist(n: Int = 20): Watchlist {
|
||||
val watchlistEndpoint = "/content/v1/$accountID/watchlist"
|
||||
val parameters = listOf(
|
||||
"locale" to Preferences.preferredLocale.toLanguageTag(),
|
||||
"n" to n
|
||||
)
|
||||
|
||||
val list: ContinueWatchingList = try {
|
||||
requestGet(watchlistEndpoint, parameters)
|
||||
} catch (ex: SerializationException) {
|
||||
Log.e(TAG, "SerializationException in watchlist().", ex)
|
||||
NoneContinueWatchingList
|
||||
}
|
||||
|
||||
val objects = list.items.map{ it.panel.episodeMetadata.seriesId }
|
||||
return objects(objects)
|
||||
}
|
||||
|
||||
/**
|
||||
* List the next up episodes for the logged in account.
|
||||
*
|
||||
* @param n Number of items to return, defaults to 20.
|
||||
* @return A **[ContinueWatchingList]** containing up to n **[ContinueWatchingItem]**.
|
||||
*/
|
||||
suspend fun upNextAccount(n: Int = 20): ContinueWatchingList {
|
||||
val watchlistEndpoint = "/content/v1/$accountID/up_next_account"
|
||||
val parameters = listOf(
|
||||
"locale" to Preferences.preferredLocale.toLanguageTag(),
|
||||
"n" to n
|
||||
)
|
||||
|
||||
return try {
|
||||
requestGet(watchlistEndpoint, parameters)
|
||||
} catch (ex: SerializationException) {
|
||||
Log.e(TAG, "SerializationException in upNextAccount().", ex)
|
||||
NoneContinueWatchingList
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun recommendations(n: Int = 20, start: Int = 0): RecommendationsList {
|
||||
val recommendationsEndpoint = "/content/v1/$accountID/recommendations"
|
||||
val parameters = listOf(
|
||||
"locale" to Preferences.preferredLocale.toLanguageTag(),
|
||||
"n" to n,
|
||||
"start" to start,
|
||||
"variant_id" to 0
|
||||
)
|
||||
|
||||
return try {
|
||||
requestGet(recommendationsEndpoint, parameters)
|
||||
} catch (ex: SerializationException) {
|
||||
Log.e(TAG, "SerializationException in recommendations().", ex)
|
||||
NoneRecommendationsList
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Account/Profile functions
|
||||
*/
|
||||
|
||||
/**
|
||||
* Get profile information for the currently logged in account.
|
||||
*
|
||||
* @return A **[Profile]** object
|
||||
*/
|
||||
suspend fun profile(): Profile {
|
||||
val profileEndpoint = "/accounts/v1/me/profile"
|
||||
|
||||
return try {
|
||||
requestGet(profileEndpoint)
|
||||
} catch (ex: SerializationException) {
|
||||
Log.e(TAG, "SerializationException in profile().", ex)
|
||||
NoneProfile
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Post the preferred content subtitle language.
|
||||
*
|
||||
* @param languageTag the preferred language as language tag
|
||||
*/
|
||||
suspend fun postPrefSubLanguage(languageTag: String) {
|
||||
val profileEndpoint = "/accounts/v1/me/profile"
|
||||
val json = buildJsonObject {
|
||||
put("preferred_content_subtitle_language", languageTag)
|
||||
}
|
||||
|
||||
requestPatch(profileEndpoint, bodyObject = json)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get additional profile (benefits) information for the currently logged in account.
|
||||
*
|
||||
* * @return A **[Profile]** object
|
||||
*/
|
||||
suspend fun benefits(): Benefits {
|
||||
val profileEndpoint = "/subs/v1/subscriptions/$externalID/benefits"
|
||||
|
||||
return try {
|
||||
requestGet(profileEndpoint)
|
||||
} catch (ex: SerializationException) {
|
||||
Log.e(TAG, "SerializationException in benefits().", ex)
|
||||
NoneBenefits
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,429 @@
|
||||
/**
|
||||
* Teapod
|
||||
*
|
||||
* Copyright 2020-2022 <seil0@mosad.xyz>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.mosad.teapod.parser.crunchyroll
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
import java.util.*
|
||||
|
||||
val supportedLocals = listOf(
|
||||
Locale.forLanguageTag("ar-SA"),
|
||||
Locale.forLanguageTag("de-DE"),
|
||||
Locale.forLanguageTag("en-US"),
|
||||
Locale.forLanguageTag("es-419"),
|
||||
Locale.forLanguageTag("es-ES"),
|
||||
Locale.forLanguageTag("fr-FR"),
|
||||
Locale.forLanguageTag("it-IT"),
|
||||
Locale.forLanguageTag("pt-BR"),
|
||||
Locale.forLanguageTag("pt-PT"),
|
||||
Locale.forLanguageTag("ru-RU"),
|
||||
Locale.ROOT
|
||||
)
|
||||
|
||||
/**
|
||||
* data classes for browse
|
||||
* TODO make class names more clear/possibly overlapping for now
|
||||
*/
|
||||
enum class SortBy(val str: String) {
|
||||
ALPHABETICAL("alphabetical"),
|
||||
NEWLY_ADDED("newly_added"),
|
||||
POPULARITY("popularity")
|
||||
}
|
||||
|
||||
@Suppress("unused")
|
||||
enum class Categories(val str: String) {
|
||||
ACTION("action"),
|
||||
ADVENTURE("adventure"),
|
||||
COMEDY("comedy"),
|
||||
DRAMA("drama"),
|
||||
FANTASY("fantasy"),
|
||||
MUSIC("music"),
|
||||
ROMANCE("romance"),
|
||||
SCI_FI("sci-fi"),
|
||||
SEINEN("seinen"),
|
||||
SHOJO("shojo"),
|
||||
SHONEN("shonen"),
|
||||
SLICE_OF_LIFE("slice+of+life"),
|
||||
SPORTS("sports"),
|
||||
SUPERNATURAL("supernatural"),
|
||||
THRILLER("thriller")
|
||||
}
|
||||
|
||||
/**
|
||||
* token, index, account. This must pe present for the app to work!
|
||||
*/
|
||||
@Serializable
|
||||
data class Token(
|
||||
@SerialName("access_token") val accessToken: String,
|
||||
@SerialName("refresh_token") val refreshToken: String,
|
||||
@SerialName("expires_in") val expiresIn: Int,
|
||||
@SerialName("token_type") val tokenType: String,
|
||||
@SerialName("scope") val scope: String,
|
||||
@SerialName("country") val country: String,
|
||||
@SerialName("account_id") val accountId: String,
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class Index(
|
||||
@SerialName("cms") val cms: CMS,
|
||||
@SerialName("service_available") val serviceAvailable: Boolean,
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class CMS(
|
||||
@SerialName("bucket") val bucket: String,
|
||||
@SerialName("policy") val policy: String,
|
||||
@SerialName("signature") val signature: String,
|
||||
@SerialName("key_pair_id") val keyPairId: String,
|
||||
@SerialName("expires") val expires: String,
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class Account(
|
||||
@SerialName("account_id") val accountId: String,
|
||||
@SerialName("external_id") val externalId: String,
|
||||
@SerialName("email_verified") val emailVerified: Boolean,
|
||||
@SerialName("created") val created: String,
|
||||
)
|
||||
val NoneAccount = Account("", "", false, "")
|
||||
|
||||
/**
|
||||
* search, browse, DiscSeasonList, Watchlist, ContinueWatchingList data types all use Collection
|
||||
*/
|
||||
|
||||
@Serializable
|
||||
data class Collection<T>(
|
||||
@SerialName("total") val total: Int,
|
||||
@SerialName("items") val items: List<T>
|
||||
)
|
||||
|
||||
typealias SearchResult = Collection<SearchCollection>
|
||||
typealias SearchCollection = Collection<Item>
|
||||
typealias BrowseResult = Collection<Item>
|
||||
typealias SimilarToResult = Collection<Item>
|
||||
typealias DiscSeasonList = Collection<SeasonListItem>
|
||||
typealias Watchlist = Collection<Item>
|
||||
typealias ContinueWatchingList = Collection<ContinueWatchingItem>
|
||||
typealias RecommendationsList = Collection<Item>
|
||||
typealias Benefits = Collection<Benefit>
|
||||
|
||||
@Serializable
|
||||
data class UpNextSeriesItem(
|
||||
@SerialName("playhead") val playhead: Int,
|
||||
@SerialName("fully_watched") val fullyWatched: Boolean,
|
||||
@SerialName("never_watched") val neverWatched: Boolean,
|
||||
@SerialName("panel") val panel: EpisodePanel,
|
||||
)
|
||||
|
||||
/**
|
||||
* panel data classes
|
||||
*/
|
||||
|
||||
// the data class Item is used in browse, search, watchlist and similar to
|
||||
// TODO rename to MediaPanel
|
||||
@Serializable
|
||||
data class Item(
|
||||
val id: String,
|
||||
val title: String,
|
||||
val type: String,
|
||||
val channel_id: String,
|
||||
val description: String,
|
||||
val images: Images
|
||||
// TODO series_metadata etc.
|
||||
// TODO add slug_title if present in search, browse, similar to
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class Images(val poster_tall: List<List<Poster>>, val poster_wide: List<List<Poster>>)
|
||||
// crunchyroll why?
|
||||
|
||||
@Serializable
|
||||
data class Poster(val height: Int, val width: Int, val source: String, val type: String)
|
||||
|
||||
/**
|
||||
* season list data classes
|
||||
*/
|
||||
@Serializable
|
||||
data class SeasonListItem(
|
||||
@SerialName("id") val id: String,
|
||||
@SerialName("localization") val localization: SeasonListLocalization
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class SeasonListLocalization(
|
||||
@SerialName("title") val title: String,
|
||||
@SerialName("description") val description: String,
|
||||
)
|
||||
|
||||
/**
|
||||
* continue_watching_item data classes
|
||||
*/
|
||||
@Serializable
|
||||
data class ContinueWatchingItem(
|
||||
@SerialName("panel") val panel: EpisodePanel,
|
||||
@SerialName("new") val new: Boolean,
|
||||
@SerialName("new_content") val newContent: Boolean,
|
||||
// not present in up_next_account -> continue_watching_item
|
||||
// @SerialName("is_favorite") val isFavorite: Boolean,
|
||||
// @SerialName("never_watched") val neverWatched: Boolean,
|
||||
// @SerialName("completion_status") val completionStatus: Boolean,
|
||||
@SerialName("playhead") val playhead: Int,
|
||||
// not present in watchlist -> continue_watching_item
|
||||
@SerialName("fully_watched") val fullyWatched: Boolean = false,
|
||||
)
|
||||
|
||||
// EpisodePanel is used in ContinueWatchingItem and UpNextSeriesItem
|
||||
@Serializable
|
||||
data class EpisodePanel(
|
||||
@SerialName("id") val id: String,
|
||||
@SerialName("title") val title: String,
|
||||
@SerialName("type") val type: String,
|
||||
@SerialName("channel_id") val channelId: String,
|
||||
@SerialName("description") val description: String,
|
||||
@SerialName("episode_metadata") val episodeMetadata: EpisodeMetadata,
|
||||
@SerialName("images") val images: Thumbnail,
|
||||
@SerialName("playback") val playback: String,
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class EpisodeMetadata(
|
||||
@SerialName("duration_ms") val durationMs: Int,
|
||||
@SerialName("episode_number") val episodeNumber: Int? = null, // default/nullable value since optional
|
||||
@SerialName("season_id") val seasonId: String,
|
||||
@SerialName("season_number") val seasonNumber: Int,
|
||||
@SerialName("season_title") val seasonTitle: String,
|
||||
@SerialName("series_id") val seriesId: String,
|
||||
@SerialName("series_title") val seriesTitle: String,
|
||||
)
|
||||
|
||||
val NoneItem = Item("", "", "", "", "", Images(emptyList(), emptyList()))
|
||||
val NoneEpisodeMetadata = EpisodeMetadata(0, 0, "", 0, "", "", "")
|
||||
val NoneEpisodePanel = EpisodePanel("", "", "", "", "", NoneEpisodeMetadata, Thumbnail(listOf()), "")
|
||||
|
||||
val NoneCollection = Collection<Item>(0, emptyList())
|
||||
val NoneSearchResult = SearchResult(0, emptyList())
|
||||
val NoneBrowseResult = BrowseResult(0, emptyList())
|
||||
val NoneSimilarToResult = SimilarToResult(0, emptyList())
|
||||
val NoneDiscSeasonList = DiscSeasonList(0, emptyList())
|
||||
val NoneContinueWatchingList = ContinueWatchingList(0, emptyList())
|
||||
val NoneRecommendationsList = RecommendationsList(0, emptyList())
|
||||
val NoneBenefits = Benefits(0, emptyList())
|
||||
|
||||
val NoneUpNextSeriesItem = UpNextSeriesItem(
|
||||
playhead = 0,
|
||||
fullyWatched = false,
|
||||
neverWatched = false,
|
||||
panel = NoneEpisodePanel
|
||||
)
|
||||
|
||||
/**
|
||||
* series data class
|
||||
*/
|
||||
@Serializable
|
||||
data class Series(
|
||||
@SerialName("id") val id: String,
|
||||
@SerialName("title") val title: String,
|
||||
@SerialName("description") val description: String,
|
||||
@SerialName("images") val images: Images,
|
||||
@SerialName("maturity_ratings") val maturityRatings: List<String>
|
||||
)
|
||||
val NoneSeries = Series("", "", "", Images(emptyList(), emptyList()), emptyList())
|
||||
|
||||
/**
|
||||
* Seasons data classes
|
||||
*/
|
||||
@Serializable
|
||||
data class Seasons(
|
||||
@SerialName("total") val total: Int,
|
||||
@SerialName("items") val items: List<Season>
|
||||
) {
|
||||
fun getPreferredSeason(local: Locale): Season {
|
||||
return items.firstOrNull { season ->
|
||||
// try to get the the first seasons which matches the preferred local
|
||||
season.slugTitle.endsWith("${local.getDisplayLanguage(Locale.ENGLISH)}-dub", true)
|
||||
} ?: items.firstOrNull { season ->
|
||||
// if there is no season with the preferred local, try to find a subbed season
|
||||
season.isSubbed
|
||||
} ?: items.first() // if no preferred language and no sub, use the first season
|
||||
}
|
||||
}
|
||||
|
||||
@Serializable
|
||||
data class Season(
|
||||
@SerialName("id") val id: String,
|
||||
@SerialName("title") val title: String,
|
||||
@SerialName("slug_title") val slugTitle: String,
|
||||
@SerialName("series_id") val seriesId: String,
|
||||
@SerialName("season_number") val seasonNumber: Int,
|
||||
@SerialName("is_subbed") val isSubbed: Boolean,
|
||||
@SerialName("is_dubbed") val isDubbed: Boolean,
|
||||
)
|
||||
|
||||
val NoneSeasons = Seasons(0, emptyList())
|
||||
val NoneSeason = Season("", "", "", "", 0, isSubbed = false, isDubbed = false)
|
||||
|
||||
|
||||
/**
|
||||
* Episodes data classes
|
||||
*/
|
||||
@Serializable
|
||||
data class Episodes(
|
||||
@SerialName("total") val total: Int,
|
||||
@SerialName("items") val items: List<Episode>
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class Episode(
|
||||
@SerialName("id") val id: String,
|
||||
@SerialName("title") val title: String,
|
||||
@SerialName("series_id") val seriesId: String,
|
||||
@SerialName("season_title") val seasonTitle: String,
|
||||
@SerialName("season_id") val seasonId: String,
|
||||
@SerialName("season_number") val seasonNumber: Int,
|
||||
@SerialName("episode") val episode: String,
|
||||
@SerialName("episode_number") val episodeNumber: Int? = null,
|
||||
@SerialName("description") val description: String,
|
||||
@SerialName("next_episode_id") val nextEpisodeId: String? = null, // default/nullable value since optional
|
||||
@SerialName("next_episode_title") val nextEpisodeTitle: String? = null, // default/nullable value since optional
|
||||
@SerialName("is_subbed") val isSubbed: Boolean,
|
||||
@SerialName("is_dubbed") val isDubbed: Boolean,
|
||||
@SerialName("images") val images: Thumbnail,
|
||||
@SerialName("duration_ms") val durationMs: Int,
|
||||
@SerialName("playback") val playback: String,
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class Thumbnail(
|
||||
@SerialName("thumbnail") val thumbnail: List<List<Poster>>
|
||||
)
|
||||
|
||||
val NoneEpisodes = Episodes(0, listOf())
|
||||
val NoneEpisode = Episode(
|
||||
id = "",
|
||||
title = "",
|
||||
seriesId = "",
|
||||
seasonId = "",
|
||||
seasonTitle = "",
|
||||
seasonNumber = 0,
|
||||
episode = "",
|
||||
episodeNumber = 0,
|
||||
description = "",
|
||||
nextEpisodeId = "",
|
||||
nextEpisodeTitle = "",
|
||||
isSubbed = false,
|
||||
isDubbed = false,
|
||||
images = Thumbnail(listOf()),
|
||||
durationMs = 0,
|
||||
playback = ""
|
||||
)
|
||||
|
||||
typealias PlayheadsMap = Map<String, PlayheadObject>
|
||||
|
||||
@Serializable
|
||||
data class PlayheadObject(
|
||||
@SerialName("playhead") val playhead: Int,
|
||||
@SerialName("content_id") val contentId: String,
|
||||
@SerialName("fully_watched") val fullyWatched: Boolean,
|
||||
@SerialName("last_modified") val lastModified: String,
|
||||
)
|
||||
|
||||
/**
|
||||
* playback/stream data classes
|
||||
*/
|
||||
@Serializable
|
||||
data class Playback(
|
||||
@SerialName("audio_locale") val audioLocale: String,
|
||||
@SerialName("subtitles") val subtitles: Map<String, Subtitle>,
|
||||
@SerialName("streams") val streams: Streams,
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class Subtitle(
|
||||
@SerialName("locale") val locale: String,
|
||||
@SerialName("url") val url: String,
|
||||
@SerialName("format") val format: String,
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class Streams(
|
||||
@SerialName("adaptive_dash") val adaptive_dash: Map<String, Stream>,
|
||||
@SerialName("adaptive_hls") val adaptive_hls: Map<String, Stream>,
|
||||
@SerialName("download_hls") val download_hls: Map<String, Stream>,
|
||||
@SerialName("drm_adaptive_dash") val drm_adaptive_dash: Map<String, Stream>,
|
||||
@SerialName("drm_adaptive_hls") val drm_adaptive_hls: Map<String, Stream>,
|
||||
@SerialName("drm_download_hls") val drm_download_hls: Map<String, Stream>,
|
||||
@SerialName("trailer_dash") val trailer_dash: Map<String, Stream>,
|
||||
@SerialName("trailer_hls") val trailer_hls: Map<String, Stream>,
|
||||
@SerialName("vo_adaptive_dash") val vo_adaptive_dash: Map<String, Stream>,
|
||||
@SerialName("vo_adaptive_hls") val vo_adaptive_hls: Map<String, Stream>,
|
||||
@SerialName("vo_drm_adaptive_dash") val vo_drm_adaptive_dash: Map<String, Stream>,
|
||||
@SerialName("vo_drm_adaptive_hls") val vo_drm_adaptive_hls: Map<String, Stream>,
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class Stream(
|
||||
@SerialName("hardsub_locale") val hardsubLocale: String = "", // default/nullable value since might be optional
|
||||
@SerialName("url") val url: String = "", // default/nullable value since optional
|
||||
@SerialName("vcodec") val vcodec: String = "", // default/nullable value since optional
|
||||
)
|
||||
|
||||
val NonePlayback = Playback(
|
||||
"",
|
||||
mapOf(),
|
||||
Streams(
|
||||
mapOf(), mapOf(), mapOf(), mapOf(), mapOf(), mapOf(),
|
||||
mapOf(), mapOf(), mapOf(), mapOf(), mapOf(), mapOf(),
|
||||
)
|
||||
)
|
||||
|
||||
/**
|
||||
* profile data class
|
||||
*/
|
||||
@Serializable
|
||||
data class Profile(
|
||||
@SerialName("avatar") val avatar: String,
|
||||
@SerialName("email") val email: String,
|
||||
@SerialName("maturity_rating") val maturityRating: String,
|
||||
@SerialName("preferred_content_subtitle_language") val preferredContentSubtitleLanguage: String,
|
||||
@SerialName("username") val username: String,
|
||||
)
|
||||
val NoneProfile = Profile(
|
||||
avatar = "",
|
||||
email = "",
|
||||
maturityRating = "",
|
||||
preferredContentSubtitleLanguage = "",
|
||||
username = ""
|
||||
)
|
||||
|
||||
/**
|
||||
* benefit data class
|
||||
*/
|
||||
@Serializable
|
||||
data class Benefit(
|
||||
@SerialName("benefit") val benefit: String,
|
||||
@SerialName("source") val source: String,
|
||||
)
|
||||
val NoneBenefit = Benefit(
|
||||
benefit = "",
|
||||
source = ""
|
||||
)
|
@ -4,10 +4,13 @@ import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import org.mosad.teapod.R
|
||||
import org.mosad.teapod.util.DataTypes
|
||||
import java.util.*
|
||||
|
||||
object Preferences {
|
||||
|
||||
var preferSecondary = false
|
||||
var preferredLocale: Locale = Locale.forLanguageTag("en-US") // TODO this should be saved (potential offline usage) but fetched on start
|
||||
internal set
|
||||
var preferSubbed = false
|
||||
internal set
|
||||
var autoplay = true
|
||||
internal set
|
||||
@ -16,6 +19,10 @@ object Preferences {
|
||||
var theme = DataTypes.Theme.DARK
|
||||
internal set
|
||||
|
||||
// dev settings
|
||||
var updatePlayhead = true
|
||||
internal set
|
||||
|
||||
private fun getSharedPref(context: Context): SharedPreferences {
|
||||
return context.getSharedPreferences(
|
||||
context.getString(R.string.preference_file_key),
|
||||
@ -23,13 +30,22 @@ object Preferences {
|
||||
)
|
||||
}
|
||||
|
||||
fun savePreferSecondary(context: Context, preferSecondary: Boolean) {
|
||||
fun savePreferredLocal(context: Context, preferredLocale: Locale) {
|
||||
with(getSharedPref(context).edit()) {
|
||||
putBoolean(context.getString(R.string.save_key_prefer_secondary), preferSecondary)
|
||||
putString(context.getString(R.string.save_key_preferred_local), preferredLocale.toLanguageTag())
|
||||
apply()
|
||||
}
|
||||
|
||||
this.preferSecondary = preferSecondary
|
||||
this.preferredLocale = preferredLocale
|
||||
}
|
||||
|
||||
fun savePreferSecondary(context: Context, preferSubbed: Boolean) {
|
||||
with(getSharedPref(context).edit()) {
|
||||
putBoolean(context.getString(R.string.save_key_prefer_secondary), preferSubbed)
|
||||
apply()
|
||||
}
|
||||
|
||||
this.preferSubbed = preferSubbed
|
||||
}
|
||||
|
||||
fun saveAutoplay(context: Context, autoplay: Boolean) {
|
||||
@ -59,13 +75,27 @@ object Preferences {
|
||||
this.theme = theme
|
||||
}
|
||||
|
||||
fun saveUpdatePlayhead(context: Context, updatePlayhead: Boolean) {
|
||||
with(getSharedPref(context).edit()) {
|
||||
putBoolean(context.getString(R.string.save_key_update_playhead), updatePlayhead)
|
||||
apply()
|
||||
}
|
||||
|
||||
this.updatePlayhead = updatePlayhead
|
||||
}
|
||||
|
||||
/**
|
||||
* initially load the stored values
|
||||
*/
|
||||
fun load(context: Context) {
|
||||
val sharedPref = getSharedPref(context)
|
||||
|
||||
preferSecondary = sharedPref.getBoolean(
|
||||
preferredLocale = Locale.forLanguageTag(
|
||||
sharedPref.getString(
|
||||
context.getString(R.string.save_key_preferred_local), "en-US"
|
||||
) ?: "en-US"
|
||||
)
|
||||
preferSubbed = sharedPref.getBoolean(
|
||||
context.getString(R.string.save_key_prefer_secondary), false
|
||||
)
|
||||
autoplay = sharedPref.getBoolean(
|
||||
@ -79,6 +109,11 @@ object Preferences {
|
||||
context.getString(R.string.save_key_theme), DataTypes.Theme.DARK.toString()
|
||||
) ?: DataTypes.Theme.DARK.toString()
|
||||
)
|
||||
|
||||
// dev settings
|
||||
updatePlayhead = sharedPref.getBoolean(
|
||||
context.getString(R.string.save_key_update_playhead), true
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
|
@ -1,18 +0,0 @@
|
||||
package org.mosad.teapod.ui.activity
|
||||
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import org.mosad.teapod.ui.activity.main.MainActivity
|
||||
|
||||
|
||||
class SplashActivity : AppCompatActivity() {
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
val intent = Intent(this, MainActivity::class.java)
|
||||
startActivity(intent)
|
||||
finish()
|
||||
}
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
/**
|
||||
* Teapod
|
||||
*
|
||||
* Copyright 2020-2021 <seil0@mosad.xyz>
|
||||
* Copyright 2020-2022 <seil0@mosad.xyz>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@ -26,16 +26,16 @@ import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.view.MenuItem
|
||||
import androidx.activity.addCallback
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.commit
|
||||
import com.afollestad.materialdialogs.MaterialDialog
|
||||
import com.afollestad.materialdialogs.callbacks.onDismiss
|
||||
import com.google.android.material.navigation.NavigationBarView
|
||||
import kotlinx.coroutines.*
|
||||
import org.mosad.teapod.R
|
||||
import org.mosad.teapod.databinding.ActivityMainBinding
|
||||
import org.mosad.teapod.parser.AoDParser
|
||||
import org.mosad.teapod.parser.crunchyroll.Crunchyroll
|
||||
import org.mosad.teapod.preferences.EncryptedPreferences
|
||||
import org.mosad.teapod.preferences.Preferences
|
||||
import org.mosad.teapod.ui.activity.main.fragments.AccountFragment
|
||||
@ -44,20 +44,18 @@ import org.mosad.teapod.ui.activity.main.fragments.LibraryFragment
|
||||
import org.mosad.teapod.ui.activity.main.fragments.SearchFragment
|
||||
import org.mosad.teapod.ui.activity.onboarding.OnboardingActivity
|
||||
import org.mosad.teapod.ui.activity.player.PlayerActivity
|
||||
import org.mosad.teapod.ui.components.LoginDialog
|
||||
import org.mosad.teapod.util.DataTypes
|
||||
import org.mosad.teapod.util.StorageController
|
||||
import org.mosad.teapod.util.exitAndRemoveTask
|
||||
import java.net.SocketTimeoutException
|
||||
import org.mosad.teapod.util.metadb.MetaDBController
|
||||
import java.util.*
|
||||
import kotlin.system.measureTimeMillis
|
||||
|
||||
class MainActivity : AppCompatActivity(), NavigationBarView.OnItemSelectedListener {
|
||||
private val classTag = javaClass.name
|
||||
|
||||
private lateinit var binding: ActivityMainBinding
|
||||
private var activeBaseFragment: Fragment = HomeFragment() // the currently active fragment, home at the start
|
||||
|
||||
companion object {
|
||||
var wasInitialized = false
|
||||
lateinit var instance: MainActivity
|
||||
}
|
||||
|
||||
@ -66,9 +64,12 @@ class MainActivity : AppCompatActivity(), NavigationBarView.OnItemSelectedListen
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
// Handle the splash screen transition.
|
||||
installSplashScreen()
|
||||
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
if (!wasInitialized) { load() }
|
||||
load() // start the initial loading
|
||||
theme.applyStyle(getThemeResource(), true)
|
||||
|
||||
binding = ActivityMainBinding.inflate(layoutInflater)
|
||||
@ -78,16 +79,14 @@ class MainActivity : AppCompatActivity(), NavigationBarView.OnItemSelectedListen
|
||||
supportFragmentManager.commit {
|
||||
replace(R.id.nav_host_fragment, activeBaseFragment, activeBaseFragment.javaClass.simpleName)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onBackPressed() {
|
||||
if (supportFragmentManager.backStackEntryCount > 0) {
|
||||
supportFragmentManager.popBackStack()
|
||||
} else {
|
||||
if (activeBaseFragment !is HomeFragment) {
|
||||
binding.navView.selectedItemId = R.id.navigation_home
|
||||
onBackPressedDispatcher.addCallback {
|
||||
if (supportFragmentManager.backStackEntryCount > 0) {
|
||||
supportFragmentManager.popBackStack()
|
||||
} else {
|
||||
super.onBackPressed()
|
||||
if (activeBaseFragment !is HomeFragment) {
|
||||
binding.navView.selectedItemId = R.id.navigation_home
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -137,53 +136,50 @@ class MainActivity : AppCompatActivity(), NavigationBarView.OnItemSelectedListen
|
||||
*/
|
||||
private fun load() {
|
||||
val time = measureTimeMillis {
|
||||
val loadingJob = CoroutineScope(Dispatchers.IO + CoroutineName("InitialLoadingScope"))
|
||||
.async { AoDParser.initialLoading() } // start the initial loading
|
||||
|
||||
// load all saved stuff here
|
||||
Preferences.load(this)
|
||||
EncryptedPreferences.readCredentials(this)
|
||||
StorageController.load(this)
|
||||
|
||||
// show onboarding
|
||||
if (EncryptedPreferences.password.isEmpty()) {
|
||||
// load meta db at the start, it doesn't depend on any third party
|
||||
val metaJob = initMetaDB()
|
||||
|
||||
// always initialize the api token
|
||||
Crunchyroll.initBasicApiToken()
|
||||
|
||||
// show onboarding if no password is set, or login fails
|
||||
if (EncryptedPreferences.password.isEmpty() || !Crunchyroll.login(
|
||||
EncryptedPreferences.login,
|
||||
EncryptedPreferences.password
|
||||
)
|
||||
) {
|
||||
showOnboarding()
|
||||
} else {
|
||||
try {
|
||||
if (!AoDParser.login()) {
|
||||
showLoginDialog()
|
||||
}
|
||||
} catch (ex: SocketTimeoutException) {
|
||||
Log.w(javaClass.name, "Timeout during login!")
|
||||
|
||||
// show waring dialog before finishing
|
||||
MaterialDialog(this).show {
|
||||
title(R.string.dialog_timeout_head)
|
||||
message(R.string.dialog_timeout_desc)
|
||||
onDismiss { exitAndRemoveTask() }
|
||||
}
|
||||
runBlocking {
|
||||
initCrunchyroll().joinAll()
|
||||
metaJob.join() // meta loading should be done here
|
||||
}
|
||||
}
|
||||
|
||||
runBlocking { loadingJob.await() } // wait for initial loading to finish
|
||||
}
|
||||
Log.i(javaClass.name, "loading and login in $time ms")
|
||||
|
||||
wasInitialized = true
|
||||
Log.i(classTag, "loading in $time ms")
|
||||
}
|
||||
|
||||
private fun showLoginDialog() {
|
||||
LoginDialog(this, false).positiveButton {
|
||||
EncryptedPreferences.saveCredentials(login, password, context)
|
||||
private fun initCrunchyroll(): List<Job> {
|
||||
val scope = CoroutineScope(Dispatchers.IO + CoroutineName("InitialCrunchyLoading"))
|
||||
return listOf(
|
||||
scope.launch { Crunchyroll.index() },
|
||||
scope.launch { Crunchyroll.account() },
|
||||
scope.launch {
|
||||
// update the local preferred content language, since it may have changed
|
||||
val locale = Locale.forLanguageTag(Crunchyroll.profile().preferredContentSubtitleLanguage)
|
||||
Preferences.savePreferredLocal(this@MainActivity, locale)
|
||||
|
||||
if (!AoDParser.login()) {
|
||||
showLoginDialog()
|
||||
Log.w(javaClass.name, "Login failed, please try again.")
|
||||
}
|
||||
}.negativeButton {
|
||||
Log.i(javaClass.name, "Login canceled, exiting.")
|
||||
finish()
|
||||
}.show()
|
||||
)
|
||||
}
|
||||
|
||||
private fun initMetaDB(): Job {
|
||||
val scope = CoroutineScope(Dispatchers.IO + CoroutineName("InitialMetaDBLoading"))
|
||||
return scope.launch { MetaDBController.list() }
|
||||
}
|
||||
|
||||
/**
|
||||
@ -197,9 +193,9 @@ class MainActivity : AppCompatActivity(), NavigationBarView.OnItemSelectedListen
|
||||
/**
|
||||
* start the player as new activity
|
||||
*/
|
||||
fun startPlayer(mediaId: Int, episodeId: Int) {
|
||||
fun startPlayer(seasonId: String, episodeId: String) {
|
||||
val intent = Intent(this, PlayerActivity::class.java).apply {
|
||||
putExtra(getString(R.string.intent_media_id), mediaId)
|
||||
putExtra(getString(R.string.intent_season_id), seasonId)
|
||||
putExtra(getString(R.string.intent_episode_id), episodeId)
|
||||
}
|
||||
startActivity(intent)
|
||||
|
@ -9,7 +9,7 @@ import android.view.ViewGroup
|
||||
import android.widget.Toast
|
||||
import androidx.annotation.RawRes
|
||||
import androidx.fragment.app.Fragment
|
||||
import com.afollestad.materialdialogs.MaterialDialog
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||
import org.mosad.teapod.BuildConfig
|
||||
import org.mosad.teapod.R
|
||||
import org.mosad.teapod.databinding.FragmentAboutBinding
|
||||
@ -68,9 +68,9 @@ class AboutFragment : Fragment() {
|
||||
}
|
||||
|
||||
binding.linearLicense.setOnClickListener {
|
||||
MaterialDialog(requireContext())
|
||||
.title(text = License.GPL3.long)
|
||||
.message(text = parseLicense(R.raw.gpl_3_full))
|
||||
MaterialAlertDialogBuilder(requireContext())
|
||||
.setTitle(License.GPL3.long)
|
||||
.setMessage(parseLicense(R.raw.gpl_3_full))
|
||||
.show()
|
||||
}
|
||||
}
|
||||
@ -107,16 +107,14 @@ class AboutFragment : Fragment() {
|
||||
"https://github.com/material-components/material-components-android", License.APACHE2),
|
||||
ThirdPartyComponent("ExoPlayer", "2014 - 2020", "The Android Open Source Project",
|
||||
"https://github.com/google/ExoPlayer", License.APACHE2),
|
||||
ThirdPartyComponent("Gson", "2008", "Google Inc.",
|
||||
"https://github.com/google/gson", License.APACHE2),
|
||||
ThirdPartyComponent("Material design icons", "2020", "Google Inc.",
|
||||
"https://github.com/google/material-design-icons", License.APACHE2),
|
||||
ThirdPartyComponent("Material Dialogs", "", "Aidan Follestad",
|
||||
"https://github.com/afollestad/material-dialogs", License.APACHE2),
|
||||
ThirdPartyComponent("Jsoup", "2009 - 2020", "Jonathan Hedley",
|
||||
"https://jsoup.org/", License.MIT),
|
||||
ThirdPartyComponent("kotlinx.coroutines", "2016 - 2019", "JetBrains",
|
||||
ThirdPartyComponent("Ktor", "2014-2021", "JetBrains s.r.o and contributors",
|
||||
"https://ktor.io/", License.APACHE2),
|
||||
ThirdPartyComponent("kotlinx.coroutines", "2016-2021", "JetBrains s.r.o",
|
||||
"https://github.com/Kotlin/kotlinx.coroutines", License.APACHE2),
|
||||
ThirdPartyComponent(" kotlinx.serialization", "2017-2021", "JetBrains s.r.o",
|
||||
"https://github.com/Kotlin/kotlinx.serialization", License.APACHE2),
|
||||
ThirdPartyComponent("Glide", "2014", "Google Inc.",
|
||||
"https://github.com/bumptech/glide", License.BSD2),
|
||||
ThirdPartyComponent("Glide Transformations", "2020", "Wasabeef",
|
||||
@ -132,9 +130,9 @@ class AboutFragment : Fragment() {
|
||||
License.MIT -> parseLicense(R.raw.mit_full)
|
||||
}
|
||||
|
||||
MaterialDialog(requireContext())
|
||||
.title(text = license.long)
|
||||
.message(text = licenseText)
|
||||
MaterialAlertDialogBuilder(requireContext())
|
||||
.setTitle(license.long)
|
||||
.setMessage(licenseText)
|
||||
.show()
|
||||
}
|
||||
|
||||
@ -152,4 +150,4 @@ class AboutFragment : Fragment() {
|
||||
return sb.toString()
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,57 +1,41 @@
|
||||
package org.mosad.teapod.ui.activity.main.fragments
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.Toast
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.core.view.isVisible
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import com.afollestad.materialdialogs.MaterialDialog
|
||||
import com.afollestad.materialdialogs.list.listItemsSingleChoice
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||
import kotlinx.coroutines.Deferred
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.mosad.teapod.BuildConfig
|
||||
import org.mosad.teapod.R
|
||||
import org.mosad.teapod.databinding.FragmentAccountBinding
|
||||
import org.mosad.teapod.parser.AoDParser
|
||||
import org.mosad.teapod.parser.crunchyroll.Benefits
|
||||
import org.mosad.teapod.parser.crunchyroll.Crunchyroll
|
||||
import org.mosad.teapod.parser.crunchyroll.Profile
|
||||
import org.mosad.teapod.parser.crunchyroll.supportedLocals
|
||||
import org.mosad.teapod.preferences.EncryptedPreferences
|
||||
import org.mosad.teapod.preferences.Preferences
|
||||
import org.mosad.teapod.ui.activity.main.MainActivity
|
||||
import org.mosad.teapod.ui.components.LoginDialog
|
||||
import org.mosad.teapod.ui.components.LoginModalBottomSheet
|
||||
import org.mosad.teapod.util.DataTypes.Theme
|
||||
import org.mosad.teapod.util.StorageController
|
||||
import org.mosad.teapod.util.showFragment
|
||||
import org.mosad.teapod.util.toDisplayString
|
||||
import java.util.*
|
||||
|
||||
class AccountFragment : Fragment() {
|
||||
|
||||
private lateinit var binding: FragmentAccountBinding
|
||||
|
||||
private val getUriExport = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
|
||||
if (result.resultCode == Activity.RESULT_OK) {
|
||||
result.data?.data?.also { uri ->
|
||||
StorageController.exportMyList(requireContext(), uri)
|
||||
}
|
||||
}
|
||||
private var profile: Deferred<Profile> = lifecycleScope.async {
|
||||
Crunchyroll.profile()
|
||||
}
|
||||
|
||||
private val getUriImport = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
|
||||
if (result.resultCode == Activity.RESULT_OK) {
|
||||
result.data?.data?.also { uri ->
|
||||
val success = StorageController.importMyList(requireContext(), uri)
|
||||
if (success == 0) {
|
||||
Toast.makeText(
|
||||
context, getString(R.string.import_data_success),
|
||||
Toast.LENGTH_SHORT
|
||||
).show()
|
||||
}
|
||||
}
|
||||
}
|
||||
private var benefits: Deferred<Benefits> = lifecycleScope.async {
|
||||
Crunchyroll.benefits()
|
||||
}
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
|
||||
@ -62,45 +46,50 @@ class AccountFragment : Fragment() {
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
|
||||
// load subscription (async) info before anything else
|
||||
binding.textAccountSubscription.text = getString(R.string.account_subscription, getString(R.string.loading))
|
||||
binding.textAccountLogin.text = EncryptedPreferences.login
|
||||
|
||||
// load account status and tier (async) info before anything else
|
||||
lifecycleScope.launch {
|
||||
binding.textAccountSubscription.text = getString(
|
||||
R.string.account_subscription,
|
||||
AoDParser.getSubscriptionInfoAsync().await()
|
||||
)
|
||||
benefits.await().apply {
|
||||
this.items.firstOrNull { it.benefit == "cr_premium" }?.let {
|
||||
binding.textAccountSubscription.text = getString(R.string.account_premium)
|
||||
}
|
||||
|
||||
this.items.firstOrNull { it.benefit == "cr_fan_pack" }?.let {
|
||||
binding.textAccountSubscriptionDesc.text =
|
||||
getString(R.string.account_tier, getString(R.string.account_tier_mega_fan))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
binding.textAccountLogin.text = EncryptedPreferences.login
|
||||
binding.textInfoAboutDesc.text = getString(R.string.info_about_desc, BuildConfig.VERSION_NAME, getString(R.string.build_time))
|
||||
// add preferred subtitles
|
||||
lifecycleScope.launch {
|
||||
binding.textSettingsContentLanguageDesc.text = Locale.forLanguageTag(
|
||||
profile.await().preferredContentSubtitleLanguage
|
||||
).displayLanguage
|
||||
}
|
||||
binding.switchSecondary.isChecked = Preferences.preferSubbed
|
||||
binding.switchAutoplay.isChecked = Preferences.autoplay
|
||||
binding.textThemeSelected.text = when (Preferences.theme) {
|
||||
Theme.DARK -> getString(R.string.theme_dark)
|
||||
else -> getString(R.string.theme_light)
|
||||
}
|
||||
|
||||
binding.switchSecondary.isChecked = Preferences.preferSecondary
|
||||
binding.switchAutoplay.isChecked = Preferences.autoplay
|
||||
|
||||
binding.linearDevSettings.isVisible = Preferences.devSettings
|
||||
binding.switchUpdatePlayhead.isChecked = Preferences.updatePlayhead
|
||||
|
||||
binding.textInfoAboutDesc.text = getString(R.string.info_about_desc, BuildConfig.VERSION_NAME, getString(R.string.build_time))
|
||||
|
||||
initActions()
|
||||
}
|
||||
|
||||
private fun initActions() {
|
||||
binding.linearAccountLogin.setOnClickListener {
|
||||
showLoginDialog(true)
|
||||
showLoginDialog()
|
||||
}
|
||||
|
||||
binding.linearAccountSubscription.setOnClickListener {
|
||||
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(AoDParser.getSubscriptionUrl())))
|
||||
}
|
||||
|
||||
binding.linearTheme.setOnClickListener {
|
||||
showThemeDialog()
|
||||
}
|
||||
|
||||
binding.linearInfo.setOnClickListener {
|
||||
activity?.showFragment(AboutFragment())
|
||||
binding.linearSettingsContentLanguage.setOnClickListener {
|
||||
showContentLanguageSelection()
|
||||
}
|
||||
|
||||
binding.switchSecondary.setOnClickListener {
|
||||
@ -111,56 +100,105 @@ class AccountFragment : Fragment() {
|
||||
Preferences.saveAutoplay(requireContext(), binding.switchAutoplay.isChecked)
|
||||
}
|
||||
|
||||
binding.linearTheme.setOnClickListener {
|
||||
showThemeDialog()
|
||||
}
|
||||
|
||||
binding.linearInfo.setOnClickListener {
|
||||
activity?.showFragment(AboutFragment())
|
||||
}
|
||||
|
||||
binding.switchUpdatePlayhead.setOnClickListener {
|
||||
Preferences.saveUpdatePlayhead(requireContext(), binding.switchUpdatePlayhead.isChecked)
|
||||
}
|
||||
|
||||
binding.linearExportData.setOnClickListener {
|
||||
val i = Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
|
||||
addCategory(Intent.CATEGORY_OPENABLE)
|
||||
type = "text/json"
|
||||
putExtra(Intent.EXTRA_TITLE, "my-list.json")
|
||||
}
|
||||
getUriExport.launch(i)
|
||||
// unused
|
||||
}
|
||||
|
||||
binding.linearImportData.setOnClickListener {
|
||||
val i = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
|
||||
addCategory(Intent.CATEGORY_OPENABLE)
|
||||
type = "*/*"
|
||||
}
|
||||
getUriImport.launch(i)
|
||||
// unused
|
||||
}
|
||||
}
|
||||
|
||||
private fun showLoginDialog(firstTry: Boolean) {
|
||||
LoginDialog(requireContext(), firstTry).positiveButton {
|
||||
EncryptedPreferences.saveCredentials(login, password, context)
|
||||
|
||||
if (!AoDParser.login()) {
|
||||
showLoginDialog(false)
|
||||
Log.w(javaClass.name, "Login failed, please try again.")
|
||||
}
|
||||
}.show {
|
||||
private fun showLoginDialog() {
|
||||
val loginModal = LoginModalBottomSheet().apply {
|
||||
login = EncryptedPreferences.login
|
||||
password = ""
|
||||
positiveAction = {
|
||||
EncryptedPreferences.saveCredentials(login, password, requireContext())
|
||||
|
||||
// TODO only dismiss if login was successful
|
||||
this.dismiss()
|
||||
}
|
||||
negativeAction = {
|
||||
this.dismiss()
|
||||
}
|
||||
}
|
||||
activity?.let { loginModal.show(it.supportFragmentManager, LoginModalBottomSheet.TAG) }
|
||||
}
|
||||
|
||||
private fun showContentLanguageSelection() {
|
||||
// we should be able to use the index of supportedLocals for language selection, items is GUI only
|
||||
val items = supportedLocals.map {
|
||||
it.toDisplayString(getString(R.string.settings_content_language_none))
|
||||
}.toTypedArray()
|
||||
|
||||
var initialSelection: Int
|
||||
// profile should be completed here, therefore blocking
|
||||
runBlocking {
|
||||
initialSelection = supportedLocals.indexOf(Locale.forLanguageTag(
|
||||
profile.await().preferredContentSubtitleLanguage))
|
||||
if (initialSelection < 0) initialSelection = supportedLocals.lastIndex
|
||||
}
|
||||
|
||||
MaterialAlertDialogBuilder(requireContext())
|
||||
.setTitle(R.string.settings_content_language)
|
||||
.setSingleChoiceItems(items, initialSelection){ dialog, which ->
|
||||
updatePrefContentLanguage(supportedLocals[which])
|
||||
dialog.dismiss()
|
||||
}
|
||||
.show()
|
||||
}
|
||||
|
||||
@kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
private fun updatePrefContentLanguage(preferredLocale: Locale) {
|
||||
lifecycleScope.launch {
|
||||
Crunchyroll.postPrefSubLanguage(preferredLocale.toLanguageTag())
|
||||
|
||||
}.invokeOnCompletion {
|
||||
// update the local preferred content language
|
||||
Preferences.savePreferredLocal(requireContext(), preferredLocale)
|
||||
|
||||
// update profile since the language selection might have changed
|
||||
profile = lifecycleScope.async { Crunchyroll.profile() }
|
||||
profile.invokeOnCompletion {
|
||||
// update language once loading profile is completed
|
||||
binding.textSettingsContentLanguageDesc.text = Locale.forLanguageTag(
|
||||
profile.getCompleted().preferredContentSubtitleLanguage
|
||||
).displayLanguage
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun showThemeDialog() {
|
||||
val themes = listOf(
|
||||
val items = arrayOf(
|
||||
resources.getString(R.string.theme_light),
|
||||
resources.getString(R.string.theme_dark)
|
||||
)
|
||||
|
||||
MaterialDialog(requireContext()).show {
|
||||
title(R.string.theme)
|
||||
listItemsSingleChoice(items = themes, initialSelection = Preferences.theme.ordinal) { _, index, _ ->
|
||||
when(index) {
|
||||
0 -> Preferences.saveTheme(context, Theme.LIGHT)
|
||||
1 -> Preferences.saveTheme(context, Theme.DARK)
|
||||
else -> Preferences.saveTheme(context, Theme.DARK)
|
||||
MaterialAlertDialogBuilder(requireContext())
|
||||
.setTitle(R.string.settings_content_language)
|
||||
.setSingleChoiceItems(items, Preferences.theme.ordinal){ _, which ->
|
||||
when(which) {
|
||||
0 -> Preferences.saveTheme(requireContext(), Theme.LIGHT)
|
||||
1 -> Preferences.saveTheme(requireContext(), Theme.DARK)
|
||||
else -> Preferences.saveTheme(requireContext(), Theme.DARK)
|
||||
}
|
||||
|
||||
(activity as MainActivity).restart()
|
||||
}
|
||||
}
|
||||
.show()
|
||||
}
|
||||
|
||||
}
|
@ -1,3 +1,25 @@
|
||||
/**
|
||||
* Teapod
|
||||
*
|
||||
* Copyright 2020-2022 <seil0@mosad.xyz>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.mosad.teapod.ui.activity.main.fragments
|
||||
|
||||
import android.os.Bundle
|
||||
@ -5,31 +27,32 @@ import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.core.view.children
|
||||
import androidx.core.view.isVisible
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.viewModels
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.lifecycle.repeatOnLifecycle
|
||||
import com.bumptech.glide.Glide
|
||||
import com.facebook.shimmer.ShimmerFrameLayout
|
||||
import kotlinx.coroutines.launch
|
||||
import org.mosad.teapod.R
|
||||
import org.mosad.teapod.databinding.FragmentHomeBinding
|
||||
import org.mosad.teapod.parser.AoDParser
|
||||
import org.mosad.teapod.ui.activity.main.MainActivity
|
||||
import org.mosad.teapod.util.ItemMedia
|
||||
import org.mosad.teapod.util.StorageController
|
||||
import org.mosad.teapod.util.adapter.MediaItemAdapter
|
||||
import org.mosad.teapod.ui.activity.main.viewmodel.HomeViewModel
|
||||
import org.mosad.teapod.util.adapter.MediaEpisodeListAdapter
|
||||
import org.mosad.teapod.util.adapter.MediaItemListAdapter
|
||||
import org.mosad.teapod.util.decoration.MediaItemDecoration
|
||||
import org.mosad.teapod.util.setDrawableTop
|
||||
import org.mosad.teapod.util.showFragment
|
||||
import org.mosad.teapod.util.startPlayer
|
||||
import org.mosad.teapod.util.toItemMediaList
|
||||
|
||||
class HomeFragment : Fragment() {
|
||||
|
||||
private val classTag = javaClass.name
|
||||
private val model: HomeViewModel by viewModels()
|
||||
private lateinit var binding: FragmentHomeBinding
|
||||
private lateinit var adapterMyList: MediaItemAdapter
|
||||
private lateinit var adapterNewEpisodes: MediaItemAdapter
|
||||
private lateinit var adapterNewSimulcasts: MediaItemAdapter
|
||||
private lateinit var adapterNewTitles: MediaItemAdapter
|
||||
private lateinit var adapterTopTen: MediaItemAdapter
|
||||
|
||||
private lateinit var highlightMedia: ItemMedia
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
|
||||
binding = FragmentHomeBinding.inflate(inflater, container, false)
|
||||
@ -39,128 +62,151 @@ class HomeFragment : Fragment() {
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
|
||||
lifecycleScope.launch {
|
||||
context?.let {
|
||||
initHighlight()
|
||||
initRecyclerViews()
|
||||
initActions()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun initHighlight() {
|
||||
if (AoDParser.highlightsList.isNotEmpty()) {
|
||||
highlightMedia = AoDParser.highlightsList[0]
|
||||
|
||||
binding.textHighlightTitle.text = highlightMedia.title
|
||||
Glide.with(requireContext()).load(highlightMedia.posterUrl)
|
||||
.into(binding.imageHighlight)
|
||||
|
||||
if (StorageController.myList.contains(highlightMedia.id)) {
|
||||
binding.textHighlightMyList.setDrawableTop(R.drawable.ic_baseline_check_24)
|
||||
} else {
|
||||
binding.textHighlightMyList.setDrawableTop(R.drawable.ic_baseline_add_24)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun initRecyclerViews() {
|
||||
binding.recyclerMyList.addItemDecoration(MediaItemDecoration(9))
|
||||
binding.recyclerNewEpisodes.addItemDecoration(MediaItemDecoration(9))
|
||||
binding.recyclerNewSimulcasts.addItemDecoration(MediaItemDecoration(9))
|
||||
binding.recyclerUpNext.addItemDecoration(MediaItemDecoration(9))
|
||||
binding.recyclerWatchlist.addItemDecoration(MediaItemDecoration(9))
|
||||
binding.recyclerRecommendations.addItemDecoration(MediaItemDecoration(9))
|
||||
binding.recyclerNewTitles.addItemDecoration(MediaItemDecoration(9))
|
||||
binding.recyclerTopTen.addItemDecoration(MediaItemDecoration(9))
|
||||
|
||||
// my list
|
||||
adapterMyList = MediaItemAdapter(mapMyListToItemMedia())
|
||||
binding.recyclerMyList.adapter = adapterMyList
|
||||
|
||||
// new episodes
|
||||
adapterNewEpisodes = MediaItemAdapter(AoDParser.newEpisodesList)
|
||||
binding.recyclerNewEpisodes.adapter = adapterNewEpisodes
|
||||
|
||||
// new simulcasts
|
||||
adapterNewSimulcasts = MediaItemAdapter(AoDParser.newSimulcastsList)
|
||||
binding.recyclerNewSimulcasts.adapter = adapterNewSimulcasts
|
||||
|
||||
// new titles
|
||||
adapterNewTitles = MediaItemAdapter(AoDParser.newTitlesList)
|
||||
binding.recyclerNewTitles.adapter = adapterNewTitles
|
||||
|
||||
// top ten
|
||||
adapterTopTen = MediaItemAdapter(AoDParser.topTenList)
|
||||
binding.recyclerTopTen.adapter = adapterTopTen
|
||||
}
|
||||
|
||||
private fun initActions() {
|
||||
binding.buttonPlayHighlight.setOnClickListener {
|
||||
// TODO get next episode
|
||||
lifecycleScope.launch {
|
||||
val media = AoDParser.getMediaById(highlightMedia.id)
|
||||
|
||||
Log.d(javaClass.name, "Starting Player with mediaId: ${media.id}")
|
||||
(activity as MainActivity).startPlayer(media.id, media.episodes.first().id)
|
||||
binding.recyclerUpNext.adapter = MediaEpisodeListAdapter(
|
||||
MediaEpisodeListAdapter.OnClickListener {
|
||||
activity?.startPlayer(it.panel.episodeMetadata.seasonId, it.panel.id)
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
binding.recyclerWatchlist.adapter = MediaItemListAdapter(
|
||||
MediaItemListAdapter.OnClickListener {
|
||||
activity?.showFragment(MediaFragment(it.id))
|
||||
}
|
||||
)
|
||||
|
||||
binding.recyclerRecommendations.adapter = MediaItemListAdapter(
|
||||
MediaItemListAdapter.OnClickListener {
|
||||
activity?.showFragment(MediaFragment(it.id))
|
||||
}
|
||||
)
|
||||
|
||||
binding.recyclerNewTitles.adapter = MediaItemListAdapter(
|
||||
MediaItemListAdapter.OnClickListener {
|
||||
activity?.showFragment(MediaFragment(it.id))
|
||||
}
|
||||
)
|
||||
|
||||
binding.recyclerTopTen.adapter = MediaItemListAdapter(
|
||||
MediaItemListAdapter.OnClickListener {
|
||||
activity?.showFragment(MediaFragment(it.id))
|
||||
}
|
||||
)
|
||||
|
||||
binding.textHighlightMyList.setOnClickListener {
|
||||
if (StorageController.myList.contains(highlightMedia.id)) {
|
||||
StorageController.myList.remove(highlightMedia.id)
|
||||
binding.textHighlightMyList.setDrawableTop(R.drawable.ic_baseline_add_24)
|
||||
} else {
|
||||
StorageController.myList.add(highlightMedia.id)
|
||||
binding.textHighlightMyList.setDrawableTop(R.drawable.ic_baseline_check_24)
|
||||
}
|
||||
StorageController.saveMyList(requireContext())
|
||||
model.toggleHighlightWatchlist()
|
||||
|
||||
updateMyListMedia() // update my list, since it has changed
|
||||
// disable the watchlist button until the result has been loaded
|
||||
binding.textHighlightMyList.isClickable = false
|
||||
// TODO since this might take a few seconds show a loading animation for the watchlist button
|
||||
}
|
||||
|
||||
binding.textHighlightInfo.setOnClickListener {
|
||||
activity?.showFragment(MediaFragment(highlightMedia.id))
|
||||
}
|
||||
|
||||
adapterMyList.onItemClick = { mediaId, _ ->
|
||||
activity?.showFragment(MediaFragment(mediaId))
|
||||
}
|
||||
|
||||
adapterNewEpisodes.onItemClick = { mediaId, _ ->
|
||||
activity?.showFragment(MediaFragment(mediaId))
|
||||
}
|
||||
|
||||
adapterNewSimulcasts.onItemClick = { mediaId, _ ->
|
||||
activity?.showFragment(MediaFragment(mediaId))
|
||||
}
|
||||
|
||||
adapterNewTitles.onItemClick = { mediaId, _ ->
|
||||
activity?.showFragment(MediaFragment(mediaId))
|
||||
}
|
||||
|
||||
adapterTopTen.onItemClick = { mediaId, _ ->
|
||||
activity?.showFragment(MediaFragment(mediaId))
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* update my media list
|
||||
* TODO
|
||||
* * auto call when StorageController.myList is changed
|
||||
* * only update actual change and not all data (performance)
|
||||
*/
|
||||
fun updateMyListMedia() {
|
||||
adapterMyList.updateMediaList(mapMyListToItemMedia())
|
||||
adapterMyList.notifyDataSetChanged()
|
||||
}
|
||||
|
||||
private fun mapMyListToItemMedia(): List<ItemMedia> {
|
||||
return StorageController.myList.mapNotNull { elementId ->
|
||||
AoDParser.itemMediaList.firstOrNull { it.id == elementId }.also {
|
||||
// it the my list entry wasn't found in itemMediaList Log it
|
||||
if (it == null) {
|
||||
Log.w(javaClass.name, "The element with the id $elementId was not found.")
|
||||
viewLifecycleOwner.lifecycleScope.launch {
|
||||
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
|
||||
model.onUiState(viewLifecycleOwner.lifecycleScope) { uiState ->
|
||||
when (uiState) {
|
||||
is HomeViewModel.UiState.Normal -> bindUiStateNormal(uiState)
|
||||
is HomeViewModel.UiState.Loading -> bindUiStateLoading()
|
||||
is HomeViewModel.UiState.Error -> bindUiStateError(uiState)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
private fun bindUiStateNormal(uiState: HomeViewModel.UiState.Normal) {
|
||||
val adapterUpNext = binding.recyclerUpNext.adapter as MediaEpisodeListAdapter
|
||||
adapterUpNext.submitList(uiState.upNextItems.filter { !it.fullyWatched })
|
||||
|
||||
val adapterWatchlist = binding.recyclerWatchlist.adapter as MediaItemListAdapter
|
||||
adapterWatchlist.submitList(uiState.watchlistItems.toItemMediaList())
|
||||
|
||||
val adapterRecommendations = binding.recyclerRecommendations.adapter as MediaItemListAdapter
|
||||
adapterRecommendations.submitList(uiState.recommendationsItems.toItemMediaList())
|
||||
|
||||
val adapterNewTitles = binding.recyclerNewTitles.adapter as MediaItemListAdapter
|
||||
adapterNewTitles.submitList(uiState.recentlyAddedItems.toItemMediaList())
|
||||
|
||||
val adapterTopTen = binding.recyclerTopTen.adapter as MediaItemListAdapter
|
||||
adapterTopTen.submitList(uiState.topTenItems.toItemMediaList())
|
||||
|
||||
// highlight item
|
||||
binding.textHighlightTitle.text = uiState.highlightItem.title
|
||||
Glide.with(requireContext()).load(uiState.highlightItem.images.poster_wide[0][3].source)
|
||||
.into(binding.imageHighlight)
|
||||
|
||||
val iconHighlightWatchlist = if (uiState.highlightIsWatchlist) {
|
||||
R.drawable.ic_baseline_check_24
|
||||
} else {
|
||||
R.drawable.ic_baseline_add_24
|
||||
}
|
||||
binding.textHighlightMyList.setDrawableTop(iconHighlightWatchlist)
|
||||
binding.textHighlightMyList.isClickable = true
|
||||
|
||||
binding.textHighlightInfo.setOnClickListener {
|
||||
activity?.showFragment(MediaFragment(uiState.highlightItem.id))
|
||||
}
|
||||
|
||||
binding.buttonPlayHighlight.setOnClickListener {
|
||||
val panel = uiState.highlightItemUpNext.panel
|
||||
activity?.startPlayer(panel.episodeMetadata.seasonId, panel.id)
|
||||
}
|
||||
|
||||
// disable the shimmer effect
|
||||
disableShimmer()
|
||||
|
||||
// make highlights layout visible again
|
||||
binding.linearHighlight.isVisible = true
|
||||
}
|
||||
|
||||
private fun bindUiStateLoading() {
|
||||
// hide highlights layout
|
||||
binding.linearHighlight.isVisible = false
|
||||
println(binding.root.childCount)
|
||||
binding.root.children.filter { it is ShimmerFrameLayout }.forEach {
|
||||
it as ShimmerFrameLayout
|
||||
it.startShimmer()
|
||||
}
|
||||
}
|
||||
|
||||
private fun bindUiStateError(uiState: HomeViewModel.UiState.Error) {
|
||||
// currently not used
|
||||
Log.e(classTag, "A error occurred while loading a UiState: ${uiState.message}")
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable the shimmer effect for all shimmer layouts and hide them.
|
||||
*/
|
||||
private fun disableShimmer() {
|
||||
binding.shimmerLayoutHighlight.apply {
|
||||
stopShimmer()
|
||||
isVisible = false
|
||||
}
|
||||
binding.shimmerLayoutUpNext.apply {
|
||||
stopShimmer()
|
||||
isVisible = false
|
||||
}
|
||||
binding.shimmerLayoutWatchlist.apply {
|
||||
stopShimmer()
|
||||
isVisible = false
|
||||
}
|
||||
binding.shimmerLayoutRecommendations.apply {
|
||||
stopShimmer()
|
||||
isVisible = false
|
||||
}
|
||||
binding.shimmerLayoutNewTitles.apply {
|
||||
stopShimmer()
|
||||
isVisible = false
|
||||
}
|
||||
binding.shimmerLayoutTopTen.apply {
|
||||
stopShimmer()
|
||||
isVisible = false
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -6,9 +6,12 @@ import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.recyclerview.widget.GridLayoutManager
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import kotlinx.coroutines.launch
|
||||
import org.mosad.teapod.databinding.FragmentLibraryBinding
|
||||
import org.mosad.teapod.parser.AoDParser
|
||||
import org.mosad.teapod.parser.crunchyroll.Crunchyroll
|
||||
import org.mosad.teapod.util.ItemMedia
|
||||
import org.mosad.teapod.util.adapter.MediaItemAdapter
|
||||
import org.mosad.teapod.util.decoration.MediaItemDecoration
|
||||
import org.mosad.teapod.util.showFragment
|
||||
@ -18,6 +21,10 @@ class LibraryFragment : Fragment() {
|
||||
private lateinit var binding: FragmentLibraryBinding
|
||||
private lateinit var adapter: MediaItemAdapter
|
||||
|
||||
private val itemList = arrayListOf<ItemMedia>()
|
||||
private val pageSize = 30
|
||||
private var nextItemIndex = 0
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
|
||||
binding = FragmentLibraryBinding.inflate(inflater, container, false)
|
||||
return binding.root
|
||||
@ -30,15 +37,53 @@ class LibraryFragment : Fragment() {
|
||||
lifecycleScope.launch {
|
||||
// create and set the adapter, needs context
|
||||
context?.let {
|
||||
adapter = MediaItemAdapter(AoDParser.itemMediaList)
|
||||
adapter.onItemClick = { mediaId, _ ->
|
||||
activity?.showFragment(MediaFragment(mediaId))
|
||||
val initialResults = Crunchyroll.browse(n = pageSize)
|
||||
itemList.addAll(initialResults.items.map { item ->
|
||||
ItemMedia(item.id, item.title, item.images.poster_wide[0][0].source)
|
||||
})
|
||||
nextItemIndex += pageSize
|
||||
|
||||
adapter = MediaItemAdapter(itemList)
|
||||
adapter.onItemClick = { mediaIdStr, _ ->
|
||||
activity?.showFragment(MediaFragment(mediaIdStr))
|
||||
}
|
||||
|
||||
binding.recyclerMediaLibrary.adapter = adapter
|
||||
binding.recyclerMediaLibrary.addItemDecoration(MediaItemDecoration(9))
|
||||
// TODO replace with pagination3
|
||||
// https://medium.com/swlh/paging3-recyclerview-pagination-made-easy-333c7dfa8797
|
||||
binding.recyclerMediaLibrary.addOnScrollListener(PaginationScrollListener())
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
inner class PaginationScrollListener: RecyclerView.OnScrollListener() {
|
||||
private var isLoading = false
|
||||
|
||||
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
|
||||
super.onScrolled(recyclerView, dx, dy)
|
||||
val layoutManager = recyclerView.layoutManager as GridLayoutManager?
|
||||
|
||||
if (!isLoading) layoutManager?.let {
|
||||
// itemList.size - 5 to start loading a bit earlier than the actual end
|
||||
if (layoutManager.findLastCompletelyVisibleItemPosition() >= (itemList.size - 5)) {
|
||||
// load new browse results async
|
||||
isLoading = true
|
||||
lifecycleScope.launch {
|
||||
val firstNewItemIndex = itemList.lastIndex + 1
|
||||
val results = Crunchyroll.browse(start = nextItemIndex, n = pageSize)
|
||||
itemList.addAll(results.items.map { item ->
|
||||
ItemMedia(item.id, item.title, item.images.poster_wide[0][0].source)
|
||||
})
|
||||
nextItemIndex += pageSize
|
||||
|
||||
adapter.notifyItemRangeInserted(firstNewItemIndex, pageSize)
|
||||
isLoading = false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -8,8 +8,7 @@ import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.FragmentActivity
|
||||
import androidx.fragment.app.activityViewModels
|
||||
import androidx.fragment.app.viewModels
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.viewpager2.adapter.FragmentStateAdapter
|
||||
import com.bumptech.glide.Glide
|
||||
@ -20,25 +19,29 @@ import jp.wasabeef.glide.transformations.BlurTransformation
|
||||
import kotlinx.coroutines.launch
|
||||
import org.mosad.teapod.R
|
||||
import org.mosad.teapod.databinding.FragmentMediaBinding
|
||||
import org.mosad.teapod.parser.crunchyroll.NoneUpNextSeriesItem
|
||||
import org.mosad.teapod.ui.activity.main.MainActivity
|
||||
import org.mosad.teapod.ui.activity.main.viewmodel.MediaFragmentViewModel
|
||||
import org.mosad.teapod.util.DataTypes.MediaType
|
||||
import org.mosad.teapod.util.Episode
|
||||
import org.mosad.teapod.util.StorageController
|
||||
import org.mosad.teapod.util.tmdb.TMDBApiController
|
||||
import org.mosad.teapod.util.tmdb.TMDBMovie
|
||||
import org.mosad.teapod.util.tmdb.TMDBTVShow
|
||||
|
||||
/**
|
||||
* The media detail fragment.
|
||||
* Note: the fragment is created only once, when selecting a similar title etc.
|
||||
* therefore fragments may be not empty and model may be the old one
|
||||
*/
|
||||
class MediaFragment(private val mediaId: Int) : Fragment() {
|
||||
class MediaFragment(private val mediaIdStr: String) : Fragment() {
|
||||
|
||||
private lateinit var binding: FragmentMediaBinding
|
||||
private lateinit var pagerAdapter: FragmentStateAdapter
|
||||
|
||||
private val fragments = arrayListOf<Fragment>()
|
||||
private val model: MediaFragmentViewModel by viewModels()
|
||||
|
||||
private val fragments = arrayListOf<Fragment>()
|
||||
private var watchlistJobRunning = false
|
||||
private var runOnResume = false
|
||||
|
||||
private val model: MediaFragmentViewModel by activityViewModels()
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
|
||||
binding = FragmentMediaBinding.inflate(inflater, container, false)
|
||||
@ -50,21 +53,21 @@ class MediaFragment(private val mediaId: Int) : Fragment() {
|
||||
binding.frameLoading.visibility = View.VISIBLE
|
||||
|
||||
// tab layout and pager
|
||||
pagerAdapter = ScreenSlidePagerAdapter(requireActivity())
|
||||
pagerAdapter = ScreenSlidePagerAdapter(this)
|
||||
// fix material components issue #1878, if more tabs are added increase
|
||||
binding.pagerEpisodesSimilar.offscreenPageLimit = 2
|
||||
binding.pagerEpisodesSimilar.adapter = pagerAdapter
|
||||
// TODO is position 0 always episodes? (and 1 always similar titles)
|
||||
TabLayoutMediator(binding.tabEpisodesSimilar, binding.pagerEpisodesSimilar) { tab, position ->
|
||||
tab.text = if (model.media.type == MediaType.TVSHOW && position == 0) {
|
||||
getString(R.string.episodes)
|
||||
} else {
|
||||
getString(R.string.similar_titles)
|
||||
tab.text = when(position) {
|
||||
0 -> getString(R.string.episodes)
|
||||
1 -> getString(R.string.similar_titles)
|
||||
else -> ""
|
||||
}
|
||||
}.attach()
|
||||
|
||||
|
||||
lifecycleScope.launch {
|
||||
model.load(mediaId) // load the streams and tmdb for the selected media
|
||||
model.loadCrunchy(mediaIdStr)
|
||||
|
||||
updateGUI()
|
||||
initActions()
|
||||
@ -74,9 +77,27 @@ class MediaFragment(private val mediaId: Int) : Fragment() {
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
|
||||
// update the next ep text if there is one, since it may have changed
|
||||
if (model.nextEpisode.title.isNotEmpty()) {
|
||||
binding.textTitle.text = model.nextEpisode.title
|
||||
if (runOnResume) {
|
||||
/**
|
||||
* FIXME
|
||||
* this is currently also run on back press when multiple MediaFragments have
|
||||
* been open and closed via similar tab
|
||||
*/
|
||||
|
||||
lifecycleScope.launch {
|
||||
model.updateOnResume()
|
||||
|
||||
if (model.upNextSeries != NoneUpNextSeriesItem) {
|
||||
binding.textTitle.text = model.upNextSeries.panel.title
|
||||
}
|
||||
|
||||
// needs to be called after model.updateOnResume()
|
||||
if (fragments.elementAtOrNull(0) is MediaFragmentEpisodes) {
|
||||
(fragments[0] as MediaFragmentEpisodes).updateWatchedState()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
runOnResume = true
|
||||
}
|
||||
}
|
||||
|
||||
@ -85,70 +106,83 @@ class MediaFragment(private val mediaId: Int) : Fragment() {
|
||||
*/
|
||||
private fun updateGUI() = with(model) {
|
||||
// generic gui
|
||||
val backdropUrl = if (tmdb.backdropUrl.isNotEmpty()) tmdb.backdropUrl else media.info.posterUrl
|
||||
val posterUrl = if (tmdb.posterUrl.isNotEmpty()) tmdb.posterUrl else media.info.posterUrl
|
||||
val backdropUrl = tmdbResult.backdropPath?.let { TMDBApiController.imageUrl + it }
|
||||
?: seriesCrunchy.images.poster_wide[0][2].source
|
||||
val posterUrl = tmdbResult.posterPath?.let { TMDBApiController.imageUrl + it }
|
||||
?: seriesCrunchy.images.poster_tall[0][2].source
|
||||
|
||||
// load poster and backdrop
|
||||
Glide.with(requireContext()).load(posterUrl)
|
||||
.into(binding.imagePoster)
|
||||
Glide.with(requireContext()).load(backdropUrl)
|
||||
.apply(RequestOptions.placeholderOf(ColorDrawable(Color.DKGRAY)))
|
||||
.apply(RequestOptions.bitmapTransform(BlurTransformation(20, 3)))
|
||||
.into(binding.imageBackdrop)
|
||||
|
||||
Glide.with(requireContext()).load(posterUrl)
|
||||
.into(binding.imagePoster)
|
||||
binding.textYear.text = when(tmdbResult) {
|
||||
is TMDBTVShow -> (tmdbResult as TMDBTVShow).firstAirDate.substring(0, 4)
|
||||
is TMDBMovie -> (tmdbResult as TMDBMovie).releaseDate.substring(0, 4)
|
||||
else -> ""
|
||||
}
|
||||
binding.textAge.text = seriesCrunchy.maturityRatings.firstOrNull()
|
||||
|
||||
binding.textTitle.text = media.info.title
|
||||
binding.textYear.text = media.info.year.toString()
|
||||
binding.textAge.text = media.info.age.toString()
|
||||
binding.textOverview.text = media.info.shortDesc
|
||||
if (StorageController.myList.contains(media.id)) {
|
||||
Glide.with(requireContext()).load(R.drawable.ic_baseline_check_24).into(binding.imageMyListAction)
|
||||
} else {
|
||||
Glide.with(requireContext()).load(R.drawable.ic_baseline_add_24).into(binding.imageMyListAction)
|
||||
binding.textTitle.text = if (upNextSeries != NoneUpNextSeriesItem) {
|
||||
upNextSeries.panel.title
|
||||
} else seriesCrunchy.title
|
||||
binding.textOverview.text = seriesCrunchy.description
|
||||
|
||||
// set "watchlist" indicator
|
||||
val watchlistIcon = if (isWatchlist) R.drawable.ic_baseline_check_24 else R.drawable.ic_baseline_add_24
|
||||
Glide.with(requireContext()).load(watchlistIcon).into(binding.imageMyListAction)
|
||||
|
||||
/**
|
||||
* clear fragments, since it lives in onCreate scope,
|
||||
* don't do this in onPause/onStop -> FragmentManager transaction
|
||||
* (will be called on similar -> new MediaFragment -> onBackPressed)
|
||||
*/
|
||||
val fragmentsSize = fragments.size
|
||||
fragments.clear()
|
||||
pagerAdapter.notifyItemRangeRemoved(0, fragmentsSize)
|
||||
|
||||
MediaFragmentEpisodes().also {
|
||||
fragments.add(it)
|
||||
pagerAdapter.notifyItemInserted(fragments.indexOf(it))
|
||||
}
|
||||
|
||||
// clear fragments, since it lives in onCreate scope (don't do this in onPause/onStop -> FragmentManager transaction)
|
||||
fragments.clear()
|
||||
pagerAdapter.notifyDataSetChanged()
|
||||
|
||||
// specific gui
|
||||
if (media.type == MediaType.TVSHOW) {
|
||||
// get next episode
|
||||
nextEpisode = if (media.episodes.firstOrNull{ !it.watched } != null) {
|
||||
media.episodes.first{ !it.watched }
|
||||
} else {
|
||||
media.episodes.first()
|
||||
}
|
||||
|
||||
// title is the next episodes title
|
||||
binding.textTitle.text = nextEpisode.title
|
||||
|
||||
// episodes count
|
||||
binding.textEpisodesOrRuntime.text = resources.getQuantityString(
|
||||
R.plurals.text_episodes_count,
|
||||
media.info.episodesCount,
|
||||
media.info.episodesCount
|
||||
)
|
||||
|
||||
// episodes
|
||||
fragments.add(MediaFragmentEpisodes())
|
||||
pagerAdapter.notifyDataSetChanged()
|
||||
} else if (media.type == MediaType.MOVIE) {
|
||||
|
||||
if (tmdb.runtime > 0) {
|
||||
// specific gui (via tmdb)
|
||||
when (tmdbResult) {
|
||||
is TMDBTVShow -> {
|
||||
// episodes count
|
||||
binding.textEpisodesOrRuntime.text = resources.getQuantityString(
|
||||
R.plurals.text_runtime,
|
||||
tmdb.runtime,
|
||||
tmdb.runtime
|
||||
R.plurals.text_episodes_count,
|
||||
episodesCrunchy.total,
|
||||
episodesCrunchy.total
|
||||
)
|
||||
} else {
|
||||
}
|
||||
is TMDBMovie -> {
|
||||
val tmdbMovie = (tmdbResult as TMDBMovie?)
|
||||
|
||||
if (tmdbMovie?.runtime != null) {
|
||||
binding.textEpisodesOrRuntime.text = resources.getQuantityString(
|
||||
R.plurals.text_runtime,
|
||||
tmdbMovie.runtime,
|
||||
tmdbMovie.runtime
|
||||
)
|
||||
} else {
|
||||
binding.textEpisodesOrRuntime.visibility = View.GONE
|
||||
}
|
||||
}
|
||||
else -> {
|
||||
binding.textEpisodesOrRuntime.visibility = View.GONE
|
||||
}
|
||||
}
|
||||
|
||||
// if has similar titles
|
||||
if (media.info.similar.isNotEmpty()) {
|
||||
fragments.add(MediaFragmentSimilar())
|
||||
pagerAdapter.notifyDataSetChanged()
|
||||
if (model.similarTo.total > 0) {
|
||||
MediaFragmentSimilar().also {
|
||||
fragments.add(it)
|
||||
pagerAdapter.notifyItemInserted(fragments.indexOf(it))
|
||||
}
|
||||
}
|
||||
|
||||
// disable scrolling on appbar, if no tabs where added
|
||||
@ -162,27 +196,24 @@ class MediaFragment(private val mediaId: Int) : Fragment() {
|
||||
|
||||
private fun initActions() = with(model) {
|
||||
binding.buttonPlay.setOnClickListener {
|
||||
when (media.type) {
|
||||
MediaType.MOVIE -> playEpisode(media.episodes.first())
|
||||
MediaType.TVSHOW -> playEpisode(nextEpisode)
|
||||
else -> Log.e(javaClass.name, "Wrong Type: $media.type")
|
||||
if (upNextSeries != NoneUpNextSeriesItem) {
|
||||
playEpisode(upNextSeries.panel.episodeMetadata.seasonId, upNextSeries.panel.id)
|
||||
}
|
||||
}
|
||||
|
||||
// add or remove media from myList
|
||||
binding.linearMyListAction.setOnClickListener {
|
||||
if (StorageController.myList.contains(media.id)) {
|
||||
StorageController.myList.remove(media.id)
|
||||
Glide.with(requireContext()).load(R.drawable.ic_baseline_add_24).into(binding.imageMyListAction)
|
||||
} else {
|
||||
StorageController.myList.add(media.id)
|
||||
Glide.with(requireContext()).load(R.drawable.ic_baseline_check_24).into(binding.imageMyListAction)
|
||||
}
|
||||
StorageController.saveMyList(requireContext())
|
||||
// don't allow parallel execution
|
||||
if (!watchlistJobRunning) {
|
||||
watchlistJobRunning = true
|
||||
lifecycleScope.launch {
|
||||
setWatchlist()
|
||||
|
||||
// notify home fragment on change
|
||||
parentFragmentManager.findFragmentByTag("HomeFragment")?.let {
|
||||
(it as HomeFragment).updateMyListMedia()
|
||||
// update "watchlist" indicator
|
||||
val watchlistIcon = if (isWatchlist) R.drawable.ic_baseline_check_24 else R.drawable.ic_baseline_add_24
|
||||
Glide.with(requireContext()).load(watchlistIcon).into(binding.imageMyListAction)
|
||||
watchlistJobRunning = false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -191,17 +222,17 @@ class MediaFragment(private val mediaId: Int) : Fragment() {
|
||||
* play the current episode
|
||||
* TODO this is also used in MediaFragmentEpisode, we should only have on implementation
|
||||
*/
|
||||
private fun playEpisode(ep: Episode) {
|
||||
(activity as MainActivity).startPlayer(model.media.id, ep.id)
|
||||
Log.d(javaClass.name, "Started Player with episodeId: ${ep.id}")
|
||||
private fun playEpisode(seasonId: String, episodeId: String) {
|
||||
(activity as MainActivity).startPlayer(seasonId, episodeId)
|
||||
Log.d(javaClass.name, "Started Player with episodeId: $episodeId")
|
||||
|
||||
model.updateNextEpisode(ep) // set the correct next episode
|
||||
//model.updateNextEpisode(episodeId) // set the correct next episode
|
||||
}
|
||||
|
||||
/**
|
||||
* A simple pager adapter
|
||||
*/
|
||||
private inner class ScreenSlidePagerAdapter(fa: FragmentActivity) : FragmentStateAdapter(fa) {
|
||||
private inner class ScreenSlidePagerAdapter(fragment: Fragment) : FragmentStateAdapter(fragment) {
|
||||
override fun getItemCount(): Int = fragments.size
|
||||
|
||||
override fun createFragment(position: Int): Fragment = fragments[position]
|
||||
|
@ -1,16 +1,20 @@
|
||||
package org.mosad.teapod.ui.activity.main.fragments
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.appcompat.widget.PopupMenu
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.activityViewModels
|
||||
import androidx.fragment.app.viewModels
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import kotlinx.coroutines.launch
|
||||
import org.mosad.teapod.R
|
||||
import org.mosad.teapod.databinding.FragmentMediaEpisodesBinding
|
||||
import org.mosad.teapod.ui.activity.main.MainActivity
|
||||
import org.mosad.teapod.ui.activity.main.viewmodel.MediaFragmentViewModel
|
||||
import org.mosad.teapod.databinding.FragmentMediaEpisodesBinding
|
||||
import org.mosad.teapod.util.Episode
|
||||
import org.mosad.teapod.util.adapter.EpisodeItemAdapter
|
||||
|
||||
class MediaFragmentEpisodes : Fragment() {
|
||||
@ -18,7 +22,7 @@ class MediaFragmentEpisodes : Fragment() {
|
||||
private lateinit var binding: FragmentMediaEpisodesBinding
|
||||
private lateinit var adapterRecEpisodes: EpisodeItemAdapter
|
||||
|
||||
private val model: MediaFragmentViewModel by activityViewModels()
|
||||
private val model: MediaFragmentViewModel by viewModels({requireParentFragment()})
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
|
||||
binding = FragmentMediaEpisodesBinding.inflate(inflater, container, false)
|
||||
@ -28,34 +32,85 @@ class MediaFragmentEpisodes : Fragment() {
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
|
||||
adapterRecEpisodes = EpisodeItemAdapter(model.media.episodes)
|
||||
adapterRecEpisodes = EpisodeItemAdapter(
|
||||
model.currentEpisodesCrunchy,
|
||||
model.tmdbTVSeason.episodes,
|
||||
model.currentPlayheads,
|
||||
EpisodeItemAdapter.OnClickListener { episode ->
|
||||
playEpisode(episode.seasonId, episode.id)
|
||||
},
|
||||
EpisodeItemAdapter.ViewType.MEDIA_FRAGMENT
|
||||
)
|
||||
binding.recyclerEpisodes.adapter = adapterRecEpisodes
|
||||
|
||||
// set onItemClick only in adapter is initialized
|
||||
if (this::adapterRecEpisodes.isInitialized) {
|
||||
adapterRecEpisodes.onImageClick = { _, position ->
|
||||
playEpisode(model.media.episodes[position])
|
||||
// don't show season selection if only one season is present
|
||||
if (model.seasonsCrunchy.total < 2) {
|
||||
binding.buttonSeasonSelection.visibility = View.GONE
|
||||
} else {
|
||||
binding.buttonSeasonSelection.text = getString(
|
||||
R.string.season_number_title,
|
||||
model.currentSeasonCrunchy.seasonNumber,
|
||||
model.currentSeasonCrunchy.title
|
||||
)
|
||||
binding.buttonSeasonSelection.setOnClickListener { v ->
|
||||
showSeasonSelection(v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
|
||||
// if adapterRecEpisodes is initialized, update the watched state for the episodes
|
||||
@SuppressLint("NotifyDataSetChanged")
|
||||
fun updateWatchedState() {
|
||||
// model.currentPlayheads is a val mutable map -> notify dataset changed
|
||||
if (this::adapterRecEpisodes.isInitialized) {
|
||||
model.media.episodes.forEachIndexed { index, episode ->
|
||||
adapterRecEpisodes.updateWatchedState(episode.watched, index)
|
||||
}
|
||||
adapterRecEpisodes.notifyDataSetChanged()
|
||||
}
|
||||
}
|
||||
|
||||
private fun playEpisode(ep: Episode) {
|
||||
(activity as MainActivity).startPlayer(model.media.id, ep.id)
|
||||
Log.d(javaClass.name, "Started Player with episodeId: ${ep.id}")
|
||||
private fun showSeasonSelection(v: View) {
|
||||
// TODO replace with Exposed dropdown menu: https://material.io/components/menus/android#exposed-dropdown-menus
|
||||
val popup = PopupMenu(requireContext(), v)
|
||||
model.seasonsCrunchy.items.forEach { season ->
|
||||
popup.menu.add(getString(
|
||||
R.string.season_number_title,
|
||||
season.seasonNumber,
|
||||
season.title
|
||||
)
|
||||
).also {
|
||||
it.setOnMenuItemClickListener {
|
||||
onSeasonSelected(season.id)
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
model.updateNextEpisode(ep) // set the correct next episode
|
||||
popup.show()
|
||||
}
|
||||
|
||||
/**
|
||||
* Call model to load a new season.
|
||||
* Once loaded update buttonSeasonSelection text and adapterRecEpisodes.
|
||||
*
|
||||
* Suppress waring since invalid.
|
||||
*/
|
||||
@SuppressLint("NotifyDataSetChanged")
|
||||
private fun onSeasonSelected(seasonId: String) {
|
||||
// load the new season
|
||||
lifecycleScope.launch {
|
||||
model.setCurrentSeason(seasonId)
|
||||
binding.buttonSeasonSelection.text = getString(
|
||||
R.string.season_number_title,
|
||||
model.currentSeasonCrunchy.seasonNumber,
|
||||
model.currentSeasonCrunchy.title
|
||||
)
|
||||
adapterRecEpisodes.notifyDataSetChanged()
|
||||
}
|
||||
}
|
||||
|
||||
private fun playEpisode(seasonId: String, episodeId: String) {
|
||||
(activity as MainActivity).startPlayer(seasonId, episodeId)
|
||||
Log.d(javaClass.name, "Started Player with episodeId: $episodeId")
|
||||
|
||||
//model.updateNextEpisode(episodeId) // set the correct next episode
|
||||
}
|
||||
|
||||
}
|
@ -1,3 +1,25 @@
|
||||
/**
|
||||
* Teapod
|
||||
*
|
||||
* Copyright 2020-2022 <seil0@mosad.xyz>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.mosad.teapod.ui.activity.main.fragments
|
||||
|
||||
import android.os.Bundle
|
||||
@ -5,19 +27,18 @@ import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.activityViewModels
|
||||
import androidx.fragment.app.viewModels
|
||||
import org.mosad.teapod.databinding.FragmentMediaSimilarBinding
|
||||
import org.mosad.teapod.ui.activity.main.viewmodel.MediaFragmentViewModel
|
||||
import org.mosad.teapod.util.adapter.MediaItemAdapter
|
||||
import org.mosad.teapod.util.adapter.MediaItemListAdapter
|
||||
import org.mosad.teapod.util.decoration.MediaItemDecoration
|
||||
import org.mosad.teapod.util.showFragment
|
||||
import org.mosad.teapod.util.toItemMediaList
|
||||
|
||||
class MediaFragmentSimilar : Fragment() {
|
||||
|
||||
private val model: MediaFragmentViewModel by viewModels({requireParentFragment()})
|
||||
private lateinit var binding: FragmentMediaSimilarBinding
|
||||
private val model: MediaFragmentViewModel by activityViewModels()
|
||||
|
||||
private lateinit var adapterSimilar: MediaItemAdapter
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
|
||||
binding = FragmentMediaSimilarBinding.inflate(inflater, container, false)
|
||||
@ -27,15 +48,14 @@ class MediaFragmentSimilar : Fragment() {
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
|
||||
adapterSimilar = MediaItemAdapter(model.media.info.similar)
|
||||
binding.recyclerMediaSimilar.adapter = adapterSimilar
|
||||
binding.recyclerMediaSimilar.addItemDecoration(MediaItemDecoration(9))
|
||||
|
||||
// set onItemClick only in adapter is initialized
|
||||
if (this::adapterSimilar.isInitialized) {
|
||||
adapterSimilar.onItemClick = { mediaId, _ ->
|
||||
activity?.showFragment(MediaFragment(mediaId))
|
||||
binding.recyclerMediaSimilar.adapter = MediaItemListAdapter(
|
||||
MediaItemListAdapter.OnClickListener {
|
||||
activity?.showFragment(MediaFragment(it.id))
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
val adapterSimilar = binding.recyclerMediaSimilar.adapter as MediaItemListAdapter
|
||||
adapterSimilar.submitList(model.similarTo.toItemMediaList())
|
||||
}
|
||||
}
|
@ -7,17 +7,24 @@ import android.view.ViewGroup
|
||||
import android.widget.SearchView
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.launch
|
||||
import org.mosad.teapod.databinding.FragmentSearchBinding
|
||||
import org.mosad.teapod.parser.AoDParser
|
||||
import org.mosad.teapod.util.decoration.MediaItemDecoration
|
||||
import org.mosad.teapod.parser.crunchyroll.Crunchyroll
|
||||
import org.mosad.teapod.util.ItemMedia
|
||||
import org.mosad.teapod.util.adapter.MediaItemAdapter
|
||||
import org.mosad.teapod.util.decoration.MediaItemDecoration
|
||||
import org.mosad.teapod.util.showFragment
|
||||
|
||||
class SearchFragment : Fragment() {
|
||||
|
||||
private lateinit var binding: FragmentSearchBinding
|
||||
private var adapter : MediaItemAdapter? = null
|
||||
private lateinit var adapter: MediaItemAdapter
|
||||
|
||||
private val itemList = arrayListOf<ItemMedia>()
|
||||
private var searchJob: Job? = null
|
||||
private var oldSearchQuery = ""
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
|
||||
binding = FragmentSearchBinding.inflate(inflater, container, false)
|
||||
@ -30,10 +37,10 @@ class SearchFragment : Fragment() {
|
||||
lifecycleScope.launch {
|
||||
// create and set the adapter, needs context
|
||||
context?.let {
|
||||
adapter = MediaItemAdapter(AoDParser.itemMediaList)
|
||||
adapter!!.onItemClick = { mediaId, _ ->
|
||||
adapter = MediaItemAdapter(itemList)
|
||||
adapter.onItemClick = { mediaIdStr, _ ->
|
||||
binding.searchText.clearFocus()
|
||||
activity?.showFragment(MediaFragment(mediaId))
|
||||
activity?.showFragment(MediaFragment(mediaIdStr))
|
||||
}
|
||||
|
||||
binding.recyclerMediaSearch.adapter = adapter
|
||||
@ -47,16 +54,65 @@ class SearchFragment : Fragment() {
|
||||
private fun initActions() {
|
||||
binding.searchText.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
|
||||
override fun onQueryTextSubmit(query: String?): Boolean {
|
||||
adapter?.filter?.filter(query)
|
||||
adapter?.notifyDataSetChanged()
|
||||
query?.let { search(it) }
|
||||
return false
|
||||
}
|
||||
|
||||
override fun onQueryTextChange(newText: String?): Boolean {
|
||||
adapter?.filter?.filter(newText)
|
||||
adapter?.notifyDataSetChanged()
|
||||
newText?.let { search(it) }
|
||||
return false
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private fun search(query: String) {
|
||||
// if the query hasn't changed since the last successful search, return
|
||||
if (query == oldSearchQuery) return
|
||||
|
||||
// cancel search job if one is already running
|
||||
if (searchJob?.isActive == true) searchJob?.cancel()
|
||||
|
||||
searchJob = lifecycleScope.async {
|
||||
// TODO maybe wait a few ms (500ms?) before searching, if the user inputs any other chars
|
||||
val results = Crunchyroll.search(query, 50)
|
||||
|
||||
itemList.clear() // TODO needs clean up
|
||||
|
||||
// TODO add top results first heading
|
||||
itemList.addAll(results.items[0].items.map { item ->
|
||||
ItemMedia(item.id, item.title, item.images.poster_wide[0][0].source)
|
||||
})
|
||||
|
||||
// TODO currently only tv shows are supported, hence only the first items array
|
||||
// should be always present
|
||||
|
||||
// // TODO add tv shows heading
|
||||
// if (results.items.size >= 2) {
|
||||
// itemList.addAll(results.items[1].items.map { item ->
|
||||
// ItemMedia(item.id, item.title, item.images.poster_wide[0][0].source)
|
||||
// })
|
||||
// }
|
||||
//
|
||||
// // TODO add movies heading
|
||||
// if (results.items.size >= 3) {
|
||||
// itemList.addAll(results.items[2].items.map { item ->
|
||||
// ItemMedia(item.id, item.title, item.images.poster_wide[0][0].source)
|
||||
// })
|
||||
// }
|
||||
//
|
||||
// // TODO add episodes heading
|
||||
// if (results.items.size >= 4) {
|
||||
// itemList.addAll(results.items[3].items.map { item ->
|
||||
// ItemMedia(item.id, item.title, item.images.poster_wide[0][0].source)
|
||||
// })
|
||||
// }
|
||||
|
||||
adapter.notifyDataSetChanged()
|
||||
//adapter.notifyItemRangeInserted(0, itemList.size)
|
||||
|
||||
// after successfully searching the query term, add it as old query, to make sure we
|
||||
// don't search again if the query hasn't changed
|
||||
oldSearchQuery = query
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,126 @@
|
||||
/**
|
||||
* Teapod
|
||||
*
|
||||
* Copyright 2020-2022 <seil0@mosad.xyz>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.mosad.teapod.ui.activity.main.viewmodel
|
||||
|
||||
import androidx.lifecycle.LifecycleCoroutineScope
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.flow.*
|
||||
import kotlinx.coroutines.launch
|
||||
import org.mosad.teapod.parser.crunchyroll.*
|
||||
import kotlin.random.Random
|
||||
|
||||
class HomeViewModel : ViewModel() {
|
||||
|
||||
private val uiState = MutableStateFlow<UiState>(UiState.Loading)
|
||||
|
||||
sealed class UiState {
|
||||
object Loading : UiState()
|
||||
data class Normal(
|
||||
val upNextItems: List<ContinueWatchingItem>,
|
||||
val watchlistItems: List<Item>,
|
||||
val recommendationsItems: List<Item>,
|
||||
val recentlyAddedItems: List<Item>,
|
||||
val topTenItems: List<Item>,
|
||||
val highlightItem: Item,
|
||||
val highlightItemUpNext: UpNextSeriesItem,
|
||||
val highlightIsWatchlist:Boolean
|
||||
) : UiState()
|
||||
data class Error(val message: String?) : UiState()
|
||||
}
|
||||
|
||||
init {
|
||||
load()
|
||||
}
|
||||
|
||||
fun onUiState(scope: LifecycleCoroutineScope, collector: (UiState) -> Unit) {
|
||||
scope.launch { uiState.collect { collector(it) } }
|
||||
}
|
||||
|
||||
fun load() {
|
||||
viewModelScope.launch {
|
||||
uiState.emit(UiState.Loading)
|
||||
try {
|
||||
// run the loading in parallel to speed up the process
|
||||
val upNextJob = viewModelScope.async { Crunchyroll.upNextAccount().items }
|
||||
val watchlistJob = viewModelScope.async { Crunchyroll.watchlist(50).items }
|
||||
val recommendationsJob = viewModelScope.async {
|
||||
Crunchyroll.recommendations(20).items
|
||||
}
|
||||
val recentlyAddedJob = viewModelScope.async {
|
||||
Crunchyroll.browse(sortBy = SortBy.NEWLY_ADDED, n = 50).items
|
||||
}
|
||||
val topTenJob = viewModelScope.async {
|
||||
Crunchyroll.browse(sortBy = SortBy.POPULARITY, n = 10).items
|
||||
}
|
||||
|
||||
val recentlyAddedItems = recentlyAddedJob.await()
|
||||
// FIXME crashes on newTitles.items.size == 0
|
||||
val highlightItem = recentlyAddedItems[Random.nextInt(recentlyAddedItems.size)]
|
||||
val highlightItemUpNextJob = viewModelScope.async {
|
||||
Crunchyroll.upNextSeries(highlightItem.id)
|
||||
}
|
||||
val highlightItemIsWatchlistJob = viewModelScope.async {
|
||||
Crunchyroll.isWatchlist(highlightItem.id)
|
||||
}
|
||||
|
||||
uiState.emit(UiState.Normal(
|
||||
upNextJob.await(), watchlistJob.await(), recommendationsJob.await(),
|
||||
recentlyAddedJob.await(), topTenJob.await(), highlightItem,
|
||||
highlightItemUpNextJob.await(), highlightItemIsWatchlistJob.await()
|
||||
))
|
||||
} catch (e: Exception) {
|
||||
uiState.emit(UiState.Error(e.message))
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggle the watchlist state of the highlight media.
|
||||
*/
|
||||
fun toggleHighlightWatchlist() {
|
||||
viewModelScope.launch {
|
||||
uiState.update { currentUiState ->
|
||||
if (currentUiState is UiState.Normal) {
|
||||
if (currentUiState.highlightIsWatchlist) {
|
||||
Crunchyroll.deleteWatchlist(currentUiState.highlightItem.id)
|
||||
} else {
|
||||
Crunchyroll.postWatchlist(currentUiState.highlightItem.id)
|
||||
}
|
||||
|
||||
// update the watchlist after a item has been added/removed
|
||||
val watchlistItems = Crunchyroll.watchlist(50).items
|
||||
|
||||
currentUiState.copy(
|
||||
watchlistItems = watchlistItems,
|
||||
highlightIsWatchlist = !currentUiState.highlightIsWatchlist)
|
||||
} else {
|
||||
currentUiState
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -2,47 +2,161 @@ package org.mosad.teapod.ui.activity.main.viewmodel
|
||||
|
||||
import android.app.Application
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import org.mosad.teapod.parser.AoDParser
|
||||
import org.mosad.teapod.util.*
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import kotlinx.coroutines.joinAll
|
||||
import kotlinx.coroutines.launch
|
||||
import org.mosad.teapod.parser.crunchyroll.*
|
||||
import org.mosad.teapod.preferences.Preferences
|
||||
import org.mosad.teapod.util.DataTypes.MediaType
|
||||
import org.mosad.teapod.util.tmdb.*
|
||||
|
||||
/**
|
||||
* handle media, next ep and tmdb
|
||||
* TODO this lives in activity, is this correct?
|
||||
*/
|
||||
class MediaFragmentViewModel(application: Application) : AndroidViewModel(application) {
|
||||
|
||||
var media = Media(-1, "", MediaType.OTHER)
|
||||
var seriesCrunchy = NoneSeries // movies are also series
|
||||
internal set
|
||||
var nextEpisode = Episode()
|
||||
var seasonsCrunchy = NoneSeasons
|
||||
internal set
|
||||
var tmdb = TMDBResponse()
|
||||
var currentSeasonCrunchy = NoneSeason
|
||||
internal set
|
||||
var episodesCrunchy = NoneEpisodes
|
||||
internal set
|
||||
val currentEpisodesCrunchy = arrayListOf<Episode>() // used for EpisodeItemAdapter (easier updates)
|
||||
|
||||
// additional media info
|
||||
val currentPlayheads: MutableMap<String, PlayheadObject> = mutableMapOf()
|
||||
var isWatchlist = false
|
||||
internal set
|
||||
var upNextSeries = NoneUpNextSeriesItem
|
||||
internal set
|
||||
var similarTo = NoneSimilarToResult
|
||||
internal set
|
||||
|
||||
// TMDB stuff
|
||||
var mediaType = MediaType.OTHER
|
||||
internal set
|
||||
var tmdbResult: TMDBResult = NoneTMDB // TODO rename
|
||||
internal set
|
||||
var tmdbTVSeason: TMDBTVSeason = NoneTMDBTVSeason
|
||||
internal set
|
||||
|
||||
/**
|
||||
* set media, tmdb and nextEpisode
|
||||
* @param crunchyId the crunchyroll series id
|
||||
*/
|
||||
suspend fun load(mediaId: Int) {
|
||||
media = AoDParser.getMediaById(mediaId)
|
||||
tmdb = TMDBApiController().search(media.info.title, media.type)
|
||||
|
||||
if (media.type == MediaType.TVSHOW) {
|
||||
nextEpisode = if (media.episodes.firstOrNull{ !it.watched } != null) {
|
||||
media.episodes.first{ !it.watched }
|
||||
} else {
|
||||
media.episodes.first()
|
||||
suspend fun loadCrunchy(crunchyId: String) {
|
||||
// load series and seasons info in parallel
|
||||
listOf(
|
||||
viewModelScope.launch { seriesCrunchy = Crunchyroll.series(crunchyId) },
|
||||
viewModelScope.launch { seasonsCrunchy = Crunchyroll.seasons(crunchyId) },
|
||||
viewModelScope.launch { isWatchlist = Crunchyroll.isWatchlist(crunchyId) },
|
||||
viewModelScope.launch { upNextSeries = Crunchyroll.upNextSeries(crunchyId) },
|
||||
viewModelScope.launch { similarTo = Crunchyroll.similarTo(crunchyId) }
|
||||
).joinAll()
|
||||
|
||||
// load the preferred season (preferred language, language per season, not per stream)
|
||||
currentSeasonCrunchy = seasonsCrunchy.getPreferredSeason(Preferences.preferredLocale)
|
||||
|
||||
// Note: if we need to query metaDB, do it now
|
||||
|
||||
// load episodes and metaDB in parallel (tmdb needs mediaType, which is set via episodes)
|
||||
viewModelScope.launch { episodesCrunchy = Crunchyroll.episodes(currentSeasonCrunchy.id) }.join()
|
||||
currentEpisodesCrunchy.clear()
|
||||
currentEpisodesCrunchy.addAll(episodesCrunchy.items)
|
||||
|
||||
// set media type
|
||||
mediaType = episodesCrunchy.items.firstOrNull()?.let {
|
||||
if (it.episodeNumber != null) MediaType.TVSHOW else MediaType.MOVIE
|
||||
} ?: MediaType.OTHER
|
||||
|
||||
// load playheads and tmdb in parallel
|
||||
listOf(
|
||||
viewModelScope.launch {
|
||||
// get playheads (including fully watched state)
|
||||
val episodeIDs = episodesCrunchy.items.map { it.id }
|
||||
currentPlayheads.clear()
|
||||
currentPlayheads.putAll(Crunchyroll.playheads(episodeIDs))
|
||||
},
|
||||
viewModelScope.launch { loadTmdbInfo() } // use tmdb search to get media info
|
||||
).joinAll()
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the tmdb info for the selected media.
|
||||
* The TMDB search return a media type, use this to get the details (movie/tv show and season)
|
||||
*/
|
||||
private suspend fun loadTmdbInfo() {
|
||||
val tmdbApiController = TMDBApiController()
|
||||
|
||||
val tmdbSearchResult = when(mediaType) {
|
||||
MediaType.MOVIE -> tmdbApiController.searchMovie(seriesCrunchy.title)
|
||||
MediaType.TVSHOW -> tmdbApiController.searchTVShow(seriesCrunchy.title)
|
||||
else -> NoneTMDBSearch
|
||||
}
|
||||
// println(tmdbSearchResult)
|
||||
|
||||
tmdbResult = if (tmdbSearchResult.results.isNotEmpty()) {
|
||||
when (val result = tmdbSearchResult.results.first()) {
|
||||
is TMDBSearchResultMovie -> tmdbApiController.getMovieDetails(result.id)
|
||||
is TMDBSearchResultTVShow -> tmdbApiController.getTVShowDetails(result.id)
|
||||
else -> NoneTMDB
|
||||
}
|
||||
} else NoneTMDB
|
||||
// println(tmdbResult)
|
||||
|
||||
// currently not used
|
||||
// tmdbTVSeason = if (tmdbResult is TMDBTVShow) {
|
||||
// tmdbApiController.getTVSeasonDetails(tmdbResult.id, 0)
|
||||
// } else NoneTMDBTVSeason
|
||||
}
|
||||
|
||||
/**
|
||||
* Set currentSeasonCrunchy based on the season id. Also set the new seasons episodes.
|
||||
*
|
||||
* @param seasonId the id of the season to set
|
||||
*/
|
||||
suspend fun setCurrentSeason(seasonId: String) {
|
||||
// return if the id hasn't changed (performance)
|
||||
if (currentSeasonCrunchy.id == seasonId) return
|
||||
|
||||
// set currentSeasonCrunchy to the new season with id == seasonId, if the id isn't found,
|
||||
// don't change the current season (this should/can never happen)
|
||||
currentSeasonCrunchy = seasonsCrunchy.items.firstOrNull {
|
||||
it.id == seasonId
|
||||
} ?: currentSeasonCrunchy
|
||||
|
||||
episodesCrunchy = Crunchyroll.episodes(currentSeasonCrunchy.id)
|
||||
currentEpisodesCrunchy.clear()
|
||||
currentEpisodesCrunchy.addAll(episodesCrunchy.items)
|
||||
|
||||
// update playheads playheads (including fully watched state)
|
||||
val episodeIDs = episodesCrunchy.items.map { it.id }
|
||||
currentPlayheads.clear()
|
||||
currentPlayheads.putAll(Crunchyroll.playheads(episodeIDs))
|
||||
}
|
||||
|
||||
suspend fun setWatchlist() {
|
||||
isWatchlist = if (isWatchlist) {
|
||||
Crunchyroll.deleteWatchlist(seriesCrunchy.id)
|
||||
false
|
||||
} else {
|
||||
Crunchyroll.postWatchlist(seriesCrunchy.id)
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* get the next episode based on episode number (the true next episode)
|
||||
* if no matching is found, use first episode
|
||||
*/
|
||||
fun updateNextEpisode(currentEp: Episode) {
|
||||
if (media.type == MediaType.MOVIE) return // return if movie
|
||||
|
||||
nextEpisode = media.episodes.firstOrNull{ it.number > currentEp.number }
|
||||
?: media.episodes.first()
|
||||
suspend fun updateOnResume() {
|
||||
joinAll(
|
||||
viewModelScope.launch {
|
||||
val episodeIDs = episodesCrunchy.items.map { it.id }
|
||||
currentPlayheads.clear()
|
||||
currentPlayheads.putAll(Crunchyroll.playheads(episodeIDs))
|
||||
},
|
||||
viewModelScope.launch { upNextSeries = Crunchyroll.upNextSeries(seriesCrunchy.id) }
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -4,18 +4,18 @@ import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.view.inputmethod.EditorInfo
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import kotlinx.coroutines.*
|
||||
import org.mosad.teapod.R
|
||||
import org.mosad.teapod.databinding.FragmentOnLoginBinding
|
||||
import org.mosad.teapod.parser.AoDParser
|
||||
import org.mosad.teapod.parser.crunchyroll.Crunchyroll
|
||||
import org.mosad.teapod.preferences.EncryptedPreferences
|
||||
|
||||
class OnLoginFragment: Fragment() {
|
||||
|
||||
private lateinit var binding: FragmentOnLoginBinding
|
||||
private var loginJob: Job? = null
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
|
||||
binding = FragmentOnLoginBinding.inflate(inflater, container, false)
|
||||
@ -29,24 +29,40 @@ class OnLoginFragment: Fragment() {
|
||||
|
||||
private fun initActions() {
|
||||
binding.buttonLogin.setOnClickListener {
|
||||
// get login credentials from gui
|
||||
val email = binding.editTextLogin.text.toString()
|
||||
val password = binding.editTextPassword.text.toString()
|
||||
onLogin()
|
||||
}
|
||||
|
||||
EncryptedPreferences.saveCredentials(email, password, requireContext()) // save the credentials
|
||||
binding.editTextPassword.setOnEditorActionListener { _, actionId, _ ->
|
||||
return@setOnEditorActionListener when (actionId) {
|
||||
EditorInfo.IME_ACTION_DONE -> {
|
||||
onLogin()
|
||||
false // false will hide the keyboards
|
||||
}
|
||||
else -> false
|
||||
}
|
||||
}
|
||||
|
||||
binding.buttonLogin.isClickable = false
|
||||
loginJob = lifecycleScope.launch {
|
||||
if (AoDParser.login()) {
|
||||
// if login was successful, switch to main
|
||||
if (activity is OnboardingActivity) {
|
||||
(activity as OnboardingActivity).launchMainActivity()
|
||||
}
|
||||
} else {
|
||||
withContext(Dispatchers.Main) {
|
||||
binding.textLoginDesc.text = getString(R.string.on_login_failed)
|
||||
binding.buttonLogin.isClickable = true
|
||||
}
|
||||
}
|
||||
|
||||
private fun onLogin() {
|
||||
// get login credentials from gui
|
||||
val email = binding.editTextLogin.text.toString()
|
||||
val password = binding.editTextPassword.text.toString()
|
||||
|
||||
binding.buttonLogin.isClickable = false
|
||||
// FIXME, this seems to run blocking
|
||||
lifecycleScope.launch {
|
||||
// try login credentials
|
||||
val login = Crunchyroll.login(email, password)
|
||||
|
||||
if (login) {
|
||||
// save the credentials and show the main activity
|
||||
EncryptedPreferences.saveCredentials(email, password, requireContext())
|
||||
if (activity is OnboardingActivity) (activity as OnboardingActivity).launchMainActivity()
|
||||
} else {
|
||||
withContext(Dispatchers.Main) {
|
||||
binding.textLoginDesc.text = getString(R.string.on_login_failed)
|
||||
binding.buttonLogin.isClickable = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,20 +3,21 @@ package org.mosad.teapod.ui.activity.onboarding
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import androidx.activity.addCallback
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.FragmentActivity
|
||||
import androidx.viewpager2.adapter.FragmentStateAdapter
|
||||
import com.google.android.material.tabs.TabLayoutMediator
|
||||
import org.mosad.teapod.ui.activity.main.MainActivity
|
||||
import org.mosad.teapod.databinding.ActivityOnboardingBinding
|
||||
import org.mosad.teapod.ui.activity.main.MainActivity
|
||||
|
||||
class OnboardingActivity : AppCompatActivity() {
|
||||
|
||||
private lateinit var binding: ActivityOnboardingBinding
|
||||
private lateinit var pagerAdapter: FragmentStateAdapter
|
||||
|
||||
private val fragments = arrayOf(OnLoginFragment())
|
||||
private val fragments = arrayOf(OnWelcomeFragment(), OnLoginFragment())
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
@ -35,13 +36,11 @@ class OnboardingActivity : AppCompatActivity() {
|
||||
if (fragments.size <= 1) {
|
||||
binding.tabLayout.visibility = View.GONE
|
||||
}
|
||||
}
|
||||
|
||||
override fun onBackPressed() {
|
||||
if (binding.viewPager.currentItem == 0) {
|
||||
super.onBackPressed()
|
||||
} else {
|
||||
binding.viewPager.currentItem = binding.viewPager.currentItem - 1
|
||||
onBackPressedDispatcher.addCallback {
|
||||
if (binding.viewPager.currentItem != 0) {
|
||||
binding.viewPager.currentItem = binding.viewPager.currentItem - 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,3 +1,25 @@
|
||||
/**
|
||||
* Teapod
|
||||
*
|
||||
* Copyright 2020-2022 <seil0@mosad.xyz>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.mosad.teapod.ui.activity.player
|
||||
|
||||
import android.animation.Animator
|
||||
@ -25,14 +47,14 @@ import com.google.android.exoplayer2.ExoPlayer
|
||||
import com.google.android.exoplayer2.Player
|
||||
import com.google.android.exoplayer2.ui.StyledPlayerControlView
|
||||
import com.google.android.exoplayer2.util.Util
|
||||
import kotlinx.android.synthetic.main.activity_player.*
|
||||
import kotlinx.android.synthetic.main.player_controls.*
|
||||
import kotlinx.coroutines.launch
|
||||
import org.mosad.teapod.R
|
||||
import org.mosad.teapod.databinding.ActivityPlayerBinding
|
||||
import org.mosad.teapod.databinding.PlayerControlsBinding
|
||||
import org.mosad.teapod.parser.crunchyroll.NoneEpisode
|
||||
import org.mosad.teapod.preferences.Preferences
|
||||
import org.mosad.teapod.ui.components.EpisodesListPlayer
|
||||
import org.mosad.teapod.ui.components.LanguageSettingsPlayer
|
||||
import org.mosad.teapod.util.DataTypes
|
||||
import org.mosad.teapod.ui.activity.player.fragment.EpisodeListDialogFragment
|
||||
import org.mosad.teapod.ui.activity.player.fragment.LanguageSettingsDialogFragment
|
||||
import org.mosad.teapod.util.hideBars
|
||||
import org.mosad.teapod.util.isInPiPMode
|
||||
import org.mosad.teapod.util.navToLauncherTask
|
||||
@ -43,10 +65,12 @@ import kotlin.concurrent.scheduleAtFixedRate
|
||||
class PlayerActivity : AppCompatActivity() {
|
||||
|
||||
private val model: PlayerViewModel by viewModels()
|
||||
private lateinit var playerBinding: ActivityPlayerBinding
|
||||
private lateinit var controlsBinding: PlayerControlsBinding
|
||||
|
||||
private lateinit var controller: StyledPlayerControlView
|
||||
private lateinit var gestureDetector: GestureDetectorCompat
|
||||
private lateinit var timerUpdates: TimerTask
|
||||
private lateinit var controlsUpdates: TimerTask
|
||||
|
||||
private var wasInPiP = false
|
||||
private var remainingTime: Long = 0
|
||||
@ -60,14 +84,17 @@ class PlayerActivity : AppCompatActivity() {
|
||||
setContentView(R.layout.activity_player)
|
||||
hideBars() // Initial hide the bars
|
||||
|
||||
model.loadMedia(
|
||||
intent.getIntExtra(getString(R.string.intent_media_id), 0),
|
||||
intent.getIntExtra(getString(R.string.intent_episode_id), 0)
|
||||
playerBinding = ActivityPlayerBinding.bind(findViewById(R.id.player_root))
|
||||
controlsBinding = PlayerControlsBinding.bind(findViewById(R.id.player_controls_root))
|
||||
|
||||
model.loadMediaAsync(
|
||||
intent.getStringExtra(getString(R.string.intent_season_id)) ?: "",
|
||||
intent.getStringExtra(getString(R.string.intent_episode_id)) ?: ""
|
||||
)
|
||||
model.currentEpisodeChangedListener.add { onMediaChanged() }
|
||||
gestureDetector = GestureDetectorCompat(this, PlayerGestureListener())
|
||||
|
||||
controller = video_view.findViewById(R.id.exo_controller)
|
||||
controller = playerBinding.videoView.findViewById(R.id.exo_controller)
|
||||
controller.isAnimationEnabled = false // disable controls (time-bar) animation
|
||||
|
||||
initExoPlayer() // call in onCreate, exoplayer lives in view model
|
||||
@ -84,7 +111,7 @@ class PlayerActivity : AppCompatActivity() {
|
||||
super.onStart()
|
||||
if (Util.SDK_INT > 23) {
|
||||
initPlayer()
|
||||
video_view?.onResume()
|
||||
playerBinding.videoView.onResume()
|
||||
}
|
||||
}
|
||||
|
||||
@ -94,7 +121,7 @@ class PlayerActivity : AppCompatActivity() {
|
||||
|
||||
if (Util.SDK_INT <= 23) {
|
||||
initPlayer()
|
||||
video_view?.onResume()
|
||||
playerBinding.videoView.onResume()
|
||||
}
|
||||
}
|
||||
|
||||
@ -121,13 +148,13 @@ class PlayerActivity : AppCompatActivity() {
|
||||
override fun onNewIntent(intent: Intent?) {
|
||||
super.onNewIntent(intent)
|
||||
|
||||
// when the intent changed, lead the new media and play it
|
||||
// when the intent changed, load the new media and play it
|
||||
intent?.let {
|
||||
model.loadMedia(
|
||||
it.getIntExtra(getString(R.string.intent_media_id), 0),
|
||||
it.getIntExtra(getString(R.string.intent_episode_id), 0)
|
||||
model.loadMediaAsync(
|
||||
it.getStringExtra(getString(R.string.intent_season_id)) ?: "",
|
||||
it.getStringExtra(getString(R.string.intent_episode_id)) ?: ""
|
||||
)
|
||||
model.playEpisode(model.currentEpisode, replace = true)
|
||||
model.playCurrentMedia()
|
||||
}
|
||||
}
|
||||
|
||||
@ -146,7 +173,7 @@ class PlayerActivity : AppCompatActivity() {
|
||||
} else {
|
||||
val width = model.player.videoFormat?.width ?: 0
|
||||
val height = model.player.videoFormat?.height ?: 0
|
||||
val contentFrame: View = video_view.findViewById(R.id.exo_content_frame)
|
||||
val contentFrame: View = playerBinding.videoView.findViewById(R.id.exo_content_frame)
|
||||
val contentRect = with(contentFrame) {
|
||||
val (x, y) = intArrayOf(0, 0).also(::getLocationInWindow)
|
||||
Rect(x, y, x + width, y + height)
|
||||
@ -165,20 +192,19 @@ class PlayerActivity : AppCompatActivity() {
|
||||
|
||||
override fun onPictureInPictureModeChanged(
|
||||
isInPictureInPictureMode: Boolean,
|
||||
newConfig: Configuration?
|
||||
newConfig: Configuration
|
||||
) {
|
||||
super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig)
|
||||
}
|
||||
|
||||
// Hide the full-screen UI (controls, etc.) while in picture-in-picture mode.
|
||||
video_view.useController = !isInPictureInPictureMode
|
||||
playerBinding.videoView.useController = !isInPictureInPictureMode
|
||||
|
||||
// TODO also hide language settings/episodes list
|
||||
}
|
||||
|
||||
private fun initPlayer() {
|
||||
if (model.media.id < 0) {
|
||||
Log.e(javaClass.name, "No media was set.")
|
||||
this.finish()
|
||||
}
|
||||
|
||||
initVideoView()
|
||||
initTimeUpdates()
|
||||
|
||||
@ -197,88 +223,115 @@ class PlayerActivity : AppCompatActivity() {
|
||||
override fun onPlaybackStateChanged(state: Int) {
|
||||
super.onPlaybackStateChanged(state)
|
||||
|
||||
loading.visibility = when (state) {
|
||||
playerBinding.loading.visibility = when (state) {
|
||||
ExoPlayer.STATE_READY -> View.GONE
|
||||
ExoPlayer.STATE_BUFFERING -> View.VISIBLE
|
||||
else -> View.GONE
|
||||
}
|
||||
|
||||
exo_play_pause.visibility = when (loading.visibility) {
|
||||
View.GONE -> View.VISIBLE
|
||||
View.VISIBLE -> View.INVISIBLE
|
||||
else -> View.VISIBLE
|
||||
// don't use isVisible to hide exoPlayPause, as it will set the visibility to GONE
|
||||
controlsBinding.exoPlayPause.visibility = when(playerBinding.loading.isVisible) {
|
||||
true -> View.INVISIBLE
|
||||
false -> View.VISIBLE
|
||||
}
|
||||
|
||||
if (state == ExoPlayer.STATE_ENDED && model.nextEpisode != null && Preferences.autoplay) {
|
||||
if (state == ExoPlayer.STATE_ENDED && hasNextEpisode() && Preferences.autoplay) {
|
||||
playNextEpisode()
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
// revert back to the old behaviour (blocking init) in case there are any issues with async init
|
||||
// start playing the current episode, after all needed player components have been initialized
|
||||
model.playEpisode(model.currentEpisode, true)
|
||||
//model.playCurrentMedia(model.currentPlayhead)
|
||||
}
|
||||
|
||||
@SuppressLint("ClickableViewAccessibility")
|
||||
private fun initVideoView() {
|
||||
video_view.player = model.player
|
||||
playerBinding.videoView.player = model.player
|
||||
|
||||
// when the player controls get hidden, hide the bars too
|
||||
video_view.setControllerVisibilityListener {
|
||||
playerBinding.videoView.setControllerVisibilityListener {
|
||||
when (it) {
|
||||
View.GONE -> hideBars()
|
||||
View.GONE -> {
|
||||
hideBars()
|
||||
// TODO also hide the skip op button
|
||||
}
|
||||
View.VISIBLE -> updateControls()
|
||||
}
|
||||
}
|
||||
|
||||
video_view.setOnTouchListener { _, event ->
|
||||
playerBinding.videoView.setOnTouchListener { _, event ->
|
||||
gestureDetector.onTouchEvent(event)
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
private fun initActions() {
|
||||
exo_close_player.setOnClickListener {
|
||||
controlsBinding.exoClosePlayer.setOnClickListener {
|
||||
this.finish()
|
||||
}
|
||||
rwd_10.setOnButtonClickListener { rewind() }
|
||||
ffwd_10.setOnButtonClickListener { fastForward() }
|
||||
button_next_ep.setOnClickListener { playNextEpisode() }
|
||||
button_language.setOnClickListener { showLanguageSettings() }
|
||||
button_episodes.setOnClickListener { showEpisodesList() }
|
||||
button_next_ep_c.setOnClickListener { playNextEpisode() }
|
||||
controlsBinding.rwd10.setOnButtonClickListener { rewind() }
|
||||
controlsBinding.ffwd10.setOnButtonClickListener { fastForward() }
|
||||
playerBinding.buttonNextEp.setOnClickListener { playNextEpisode() }
|
||||
playerBinding.buttonSkipOp.setOnClickListener { skipOpening() }
|
||||
controlsBinding.buttonLanguage.setOnClickListener { showLanguageSettings() }
|
||||
controlsBinding.buttonEpisodes.setOnClickListener { showEpisodesList() }
|
||||
controlsBinding.buttonNextEpC.setOnClickListener { playNextEpisode() }
|
||||
}
|
||||
|
||||
private fun initGUI() {
|
||||
if (model.media.type == DataTypes.MediaType.MOVIE) {
|
||||
button_episodes.visibility = View.GONE
|
||||
}
|
||||
// TODO reimplement for cr
|
||||
// if (model.media.type == DataTypes.MediaType.MOVIE) {
|
||||
// button_episodes.visibility = View.GONE
|
||||
// }
|
||||
}
|
||||
|
||||
private fun initTimeUpdates() {
|
||||
if (this::timerUpdates.isInitialized) {
|
||||
timerUpdates.cancel()
|
||||
if (this::controlsUpdates.isInitialized) {
|
||||
controlsUpdates.cancel()
|
||||
}
|
||||
|
||||
timerUpdates = Timer().scheduleAtFixedRate(0, 500) {
|
||||
controlsUpdates = Timer().scheduleAtFixedRate(0, 500) {
|
||||
lifecycleScope.launch {
|
||||
val btnNextEpIsVisible = button_next_ep.isVisible
|
||||
val currentPosition = model.player.currentPosition
|
||||
val btnNextEpIsVisible = playerBinding.buttonNextEp.isVisible
|
||||
val controlsVisible = controller.isVisible
|
||||
|
||||
// make sure remaining time is > 0
|
||||
if (model.player.duration > 0) {
|
||||
remainingTime = model.player.duration - model.player.currentPosition
|
||||
remainingTime = model.player.duration - currentPosition
|
||||
remainingTime = if (remainingTime < 0) 0 else remainingTime
|
||||
} else {
|
||||
remainingTime = 0
|
||||
}
|
||||
|
||||
if (remainingTime in 1..20000) {
|
||||
// if the next ep button is not visible, make it visible. Don't show in pip mode
|
||||
if (!btnNextEpIsVisible && model.nextEpisode != null && Preferences.autoplay && !isInPiPMode()) {
|
||||
// TODO add metaDB ending_start support
|
||||
// if remaining time > 1 and < 20 sec, a next ep is set, autoplay is enabled
|
||||
// and not in pip: show next ep button
|
||||
if (remainingTime in 1000..20000) {
|
||||
if (!btnNextEpIsVisible && hasNextEpisode() && Preferences.autoplay && !isInPiPMode()) {
|
||||
showButtonNextEp()
|
||||
}
|
||||
} else if (btnNextEpIsVisible) {
|
||||
hideButtonNextEp()
|
||||
}
|
||||
|
||||
// if meta data is present and opening_start & opening_duration are valid, show skip opening
|
||||
model.currentEpisodeMeta?.let {
|
||||
if (it.openingDuration > 0 &&
|
||||
currentPosition in it.openingStart..(it.openingStart + 10000) &&
|
||||
!playerBinding.buttonSkipOp.isVisible
|
||||
) {
|
||||
showButtonSkipOp()
|
||||
} else if (playerBinding.buttonSkipOp.isVisible &&
|
||||
currentPosition !in it.openingStart..(it.openingStart + 10000)
|
||||
) {
|
||||
// the button should only be visible, if currentEpisodeMeta != null
|
||||
hideButtonSkipOp()
|
||||
}
|
||||
}
|
||||
|
||||
// if controls are visible, update them
|
||||
if (controlsVisible) {
|
||||
updateControls()
|
||||
@ -288,9 +341,9 @@ class PlayerActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
private fun onPauseOnStop() {
|
||||
video_view?.onPause()
|
||||
playerBinding.videoView.onPause()
|
||||
model.player.pause()
|
||||
timerUpdates.cancel()
|
||||
controlsUpdates.cancel()
|
||||
}
|
||||
|
||||
/**
|
||||
@ -303,7 +356,7 @@ class PlayerActivity : AppCompatActivity() {
|
||||
val seconds = TimeUnit.MILLISECONDS.toSeconds(remainingTime) % 60
|
||||
|
||||
// if remaining time is below 60 minutes, don't show hours
|
||||
exo_remaining.text = if (TimeUnit.MILLISECONDS.toMinutes(remainingTime) < 60) {
|
||||
controlsBinding.exoRemaining.text = if (TimeUnit.MILLISECONDS.toMinutes(remainingTime) < 60) {
|
||||
getString(R.string.time_min_sec, minutes, seconds)
|
||||
} else {
|
||||
getString(R.string.time_hour_min_sec, hours, minutes, seconds)
|
||||
@ -311,24 +364,29 @@ class PlayerActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
/**
|
||||
* update title text and next ep button visibility, set ignoreNextStateEnded
|
||||
* This methode is called, if the current episode has changed.
|
||||
* Update title text and next ep button visibility.
|
||||
* If the currentEpisode changed to NoneEpisode, exit the activity.
|
||||
*/
|
||||
private fun onMediaChanged() {
|
||||
exo_text_title.text = model.getMediaTitle()
|
||||
|
||||
// hide the next ep button, if there is none
|
||||
button_next_ep_c.visibility = if (model.nextEpisode == null) {
|
||||
View.GONE
|
||||
} else {
|
||||
View.VISIBLE
|
||||
if (model.currentEpisode == NoneEpisode) {
|
||||
Log.e(javaClass.name, "No media was set.")
|
||||
this.finish()
|
||||
}
|
||||
|
||||
// hide the episodes button, if the media type changed
|
||||
button_episodes.visibility = if (model.media.type == DataTypes.MediaType.MOVIE) {
|
||||
View.GONE
|
||||
} else {
|
||||
View.VISIBLE
|
||||
}
|
||||
controlsBinding.exoTextTitle.text = model.getMediaTitle()
|
||||
|
||||
// hide the next episode button, if there is none
|
||||
controlsBinding.buttonNextEpC.isVisible = hasNextEpisode()
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the current episode has a next episode.
|
||||
*
|
||||
* @return Boolean: true if there is a next episode, else false.
|
||||
*/
|
||||
private fun hasNextEpisode(): Boolean {
|
||||
return (model.currentEpisode.nextEpisodeId != null && !model.currentEpisodeIsLastEpisode())
|
||||
}
|
||||
|
||||
/**
|
||||
@ -339,41 +397,57 @@ class PlayerActivity : AppCompatActivity() {
|
||||
model.seekToOffset(rwdTime)
|
||||
|
||||
// hide/show needed components
|
||||
exo_double_tap_indicator.visibility = View.VISIBLE
|
||||
ffwd_10_indicator.visibility = View.INVISIBLE
|
||||
rwd_10.visibility = View.INVISIBLE
|
||||
playerBinding.exoDoubleTapIndicator.visibility = View.VISIBLE
|
||||
playerBinding.ffwd10Indicator.visibility = View.INVISIBLE
|
||||
controlsBinding.rwd10.visibility = View.INVISIBLE
|
||||
|
||||
rwd_10_indicator.onAnimationEndCallback = {
|
||||
exo_double_tap_indicator.visibility = View.GONE
|
||||
ffwd_10_indicator.visibility = View.VISIBLE
|
||||
rwd_10.visibility = View.VISIBLE
|
||||
playerBinding.rwd10Indicator.onAnimationEndCallback = {
|
||||
playerBinding.exoDoubleTapIndicator.visibility = View.GONE
|
||||
playerBinding.ffwd10Indicator.visibility = View.VISIBLE
|
||||
controlsBinding.rwd10.visibility = View.VISIBLE
|
||||
}
|
||||
|
||||
// run animation
|
||||
rwd_10_indicator.runOnClickAnimation()
|
||||
playerBinding.rwd10Indicator.runOnClickAnimation()
|
||||
}
|
||||
|
||||
private fun fastForward() {
|
||||
model.seekToOffset(fwdTime)
|
||||
|
||||
// hide/show needed components
|
||||
exo_double_tap_indicator.visibility = View.VISIBLE
|
||||
rwd_10_indicator.visibility = View.INVISIBLE
|
||||
ffwd_10.visibility = View.INVISIBLE
|
||||
playerBinding.exoDoubleTapIndicator.visibility = View.VISIBLE
|
||||
playerBinding.rwd10Indicator.visibility = View.INVISIBLE
|
||||
controlsBinding.ffwd10.visibility = View.INVISIBLE
|
||||
|
||||
ffwd_10_indicator.onAnimationEndCallback = {
|
||||
exo_double_tap_indicator.visibility = View.GONE
|
||||
rwd_10_indicator.visibility = View.VISIBLE
|
||||
ffwd_10.visibility = View.VISIBLE
|
||||
playerBinding.ffwd10Indicator.onAnimationEndCallback = {
|
||||
playerBinding.exoDoubleTapIndicator.visibility = View.GONE
|
||||
playerBinding.rwd10Indicator.visibility = View.VISIBLE
|
||||
controlsBinding.ffwd10.visibility = View.VISIBLE
|
||||
}
|
||||
|
||||
// run animation
|
||||
ffwd_10_indicator.runOnClickAnimation()
|
||||
playerBinding.ffwd10Indicator.runOnClickAnimation()
|
||||
}
|
||||
|
||||
private fun playNextEpisode() {
|
||||
model.playNextEpisode()
|
||||
// disable the next episode buttons, so a user can't double click it
|
||||
playerBinding.buttonNextEp.isClickable = false
|
||||
controlsBinding.buttonNextEpC.isClickable = false
|
||||
|
||||
hideButtonNextEp()
|
||||
model.playNextEpisode()
|
||||
|
||||
// enable the next episode buttons when playNextEpisode() has returned
|
||||
playerBinding.buttonNextEp.isClickable = true
|
||||
controlsBinding.buttonNextEpC.isClickable = true
|
||||
}
|
||||
|
||||
private fun skipOpening() {
|
||||
// calculate the seek time
|
||||
model.currentEpisodeMeta?.let {
|
||||
val seekTime = (it.openingStart + it.openingDuration) - model.player.currentPosition
|
||||
model.seekToOffset(seekTime)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -381,10 +455,10 @@ class PlayerActivity : AppCompatActivity() {
|
||||
* TODO improve the show animation
|
||||
*/
|
||||
private fun showButtonNextEp() {
|
||||
button_next_ep.visibility = View.VISIBLE
|
||||
button_next_ep.alpha = 0.0f
|
||||
playerBinding.buttonNextEp.isVisible = true
|
||||
playerBinding.buttonNextEp.alpha = 0.0f
|
||||
|
||||
button_next_ep.animate()
|
||||
playerBinding.buttonNextEp.animate()
|
||||
.alpha(1.0f)
|
||||
.setListener(null)
|
||||
}
|
||||
@ -394,31 +468,45 @@ class PlayerActivity : AppCompatActivity() {
|
||||
* TODO improve the hide animation
|
||||
*/
|
||||
private fun hideButtonNextEp() {
|
||||
button_next_ep.animate()
|
||||
playerBinding.buttonNextEp.animate()
|
||||
.alpha(0.0f)
|
||||
.setListener(object : AnimatorListenerAdapter() {
|
||||
override fun onAnimationEnd(animation: Animator?) {
|
||||
override fun onAnimationEnd(animation: Animator) {
|
||||
super.onAnimationEnd(animation)
|
||||
button_next_ep.visibility = View.GONE
|
||||
playerBinding.buttonNextEp.isVisible = false
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private fun showButtonSkipOp() {
|
||||
playerBinding.buttonSkipOp.isVisible = true
|
||||
playerBinding.buttonSkipOp.alpha = 0.0f
|
||||
|
||||
playerBinding.buttonSkipOp.animate()
|
||||
.alpha(1.0f)
|
||||
.setListener(null)
|
||||
}
|
||||
|
||||
private fun hideButtonSkipOp() {
|
||||
playerBinding.buttonSkipOp.animate()
|
||||
.alpha(0.0f)
|
||||
.setListener(object : AnimatorListenerAdapter() {
|
||||
override fun onAnimationEnd(animation: Animator) {
|
||||
super.onAnimationEnd(animation)
|
||||
playerBinding.buttonSkipOp.isVisible = false
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
private fun showEpisodesList() {
|
||||
val episodesList = EpisodesListPlayer(this, model = model).apply {
|
||||
onViewRemovedAction = { model.player.play() }
|
||||
}
|
||||
player_layout.addView(episodesList)
|
||||
pauseAndHideControls()
|
||||
EpisodeListDialogFragment().show(supportFragmentManager, EpisodeListDialogFragment.TAG)
|
||||
}
|
||||
|
||||
private fun showLanguageSettings() {
|
||||
val languageSettings = LanguageSettingsPlayer(this, model = model).apply {
|
||||
onViewRemovedAction = { model.player.play() }
|
||||
}
|
||||
player_layout.addView(languageSettings)
|
||||
pauseAndHideControls()
|
||||
LanguageSettingsDialogFragment().show(supportFragmentManager, LanguageSettingsDialogFragment.TAG)
|
||||
}
|
||||
|
||||
/**
|
||||
@ -435,9 +523,9 @@ class PlayerActivity : AppCompatActivity() {
|
||||
/**
|
||||
* on single tap hide or show the controls
|
||||
*/
|
||||
override fun onSingleTapConfirmed(e: MotionEvent?): Boolean {
|
||||
override fun onSingleTapConfirmed(e: MotionEvent): Boolean {
|
||||
if (!isInPiPMode()) {
|
||||
if (controller.isVisible) controller.hide() else controller.show()
|
||||
if (controller.isVisible) controller.hide() else controller.show()
|
||||
}
|
||||
|
||||
return true
|
||||
@ -446,9 +534,9 @@ class PlayerActivity : AppCompatActivity() {
|
||||
/**
|
||||
* on double tap rewind or forward
|
||||
*/
|
||||
override fun onDoubleTap(e: MotionEvent?): Boolean {
|
||||
val eventPosX = e?.x?.toInt() ?: 0
|
||||
val viewCenterX = video_view.measuredWidth / 2
|
||||
override fun onDoubleTap(e: MotionEvent): Boolean {
|
||||
val eventPosX = e.x.toInt()
|
||||
val viewCenterX = playerBinding.videoView.measuredWidth / 2
|
||||
|
||||
// if the event position is on the left side rewind, if it's on the right forward
|
||||
if (eventPosX < viewCenterX) rewind() else fastForward()
|
||||
@ -459,14 +547,14 @@ class PlayerActivity : AppCompatActivity() {
|
||||
/**
|
||||
* not used
|
||||
*/
|
||||
override fun onDoubleTapEvent(e: MotionEvent?): Boolean {
|
||||
override fun onDoubleTapEvent(e: MotionEvent): Boolean {
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* on long press toggle pause/play
|
||||
*/
|
||||
override fun onLongPress(e: MotionEvent?) {
|
||||
override fun onLongPress(e: MotionEvent) {
|
||||
model.togglePausePlay()
|
||||
}
|
||||
|
||||
|
@ -1,3 +1,25 @@
|
||||
/**
|
||||
* Teapod
|
||||
*
|
||||
* Copyright 2020-2022 <seil0@mosad.xyz>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.mosad.teapod.ui.activity.player
|
||||
|
||||
import android.app.Application
|
||||
@ -6,24 +28,20 @@ import android.support.v4.media.session.MediaSessionCompat
|
||||
import android.util.Log
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.google.android.exoplayer2.C
|
||||
import com.google.android.exoplayer2.ExoPlayer
|
||||
import com.google.android.exoplayer2.MediaItem
|
||||
import com.google.android.exoplayer2.SimpleExoPlayer
|
||||
import com.google.android.exoplayer2.Player
|
||||
import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector
|
||||
import com.google.android.exoplayer2.source.MediaSource
|
||||
import com.google.android.exoplayer2.source.hls.HlsMediaSource
|
||||
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory
|
||||
import com.google.android.exoplayer2.util.Util
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.coroutines.*
|
||||
import org.mosad.teapod.R
|
||||
import org.mosad.teapod.parser.AoDParser
|
||||
import org.mosad.teapod.parser.crunchyroll.*
|
||||
import org.mosad.teapod.preferences.Preferences
|
||||
import org.mosad.teapod.util.DataTypes
|
||||
import org.mosad.teapod.util.Episode
|
||||
import org.mosad.teapod.util.Media
|
||||
import org.mosad.teapod.util.metadb.EpisodeMeta
|
||||
import org.mosad.teapod.util.metadb.Meta
|
||||
import org.mosad.teapod.util.metadb.MetaDBController
|
||||
import org.mosad.teapod.util.metadb.TVShowMeta
|
||||
import java.util.*
|
||||
import kotlin.collections.ArrayList
|
||||
import kotlin.concurrent.scheduleAtFixedRate
|
||||
|
||||
/**
|
||||
* PlayerViewModel handles all stuff related to media/episodes.
|
||||
@ -31,25 +49,60 @@ import kotlin.collections.ArrayList
|
||||
* the next episode will be update and the callback is handled.
|
||||
*/
|
||||
class PlayerViewModel(application: Application) : AndroidViewModel(application) {
|
||||
private val classTag = javaClass.name
|
||||
|
||||
val player = SimpleExoPlayer.Builder(application).build()
|
||||
private val dataSourceFactory = DefaultDataSourceFactory(application, Util.getUserAgent(application, "Teapod"))
|
||||
val player = ExoPlayer.Builder(application).build()
|
||||
private val mediaSession = MediaSessionCompat(application, "TEAPOD_PLAYER_SESSION")
|
||||
private val playheadAutoUpdate: TimerTask
|
||||
|
||||
val currentEpisodeChangedListener = ArrayList<() -> Unit>()
|
||||
private val preferredLanguage = if (Preferences.preferSecondary) Locale.JAPANESE else Locale.GERMAN
|
||||
private var currentPlayhead: Long = 0
|
||||
|
||||
var media: Media = Media(-1, "", DataTypes.MediaType.OTHER)
|
||||
// tmdb/meta data
|
||||
var mediaMeta: Meta? = null
|
||||
internal set
|
||||
var currentEpisode = Episode()
|
||||
var currentEpisodeMeta: EpisodeMeta? = null
|
||||
internal set
|
||||
var nextEpisode: Episode? = null
|
||||
var currentPlayheads: PlayheadsMap = mutableMapOf()
|
||||
internal set
|
||||
var currentLanguage: Locale = Locale.ROOT
|
||||
// var tmdbTVSeason: TMDBTVSeason? =null
|
||||
// internal set
|
||||
|
||||
// crunchyroll episodes/playback
|
||||
var episodes = NoneEpisodes
|
||||
internal set
|
||||
var currentEpisode = NoneEpisode
|
||||
internal set
|
||||
var currentPlayback = NonePlayback
|
||||
|
||||
// current playback settings
|
||||
var currentLanguage: Locale = Preferences.preferredLocale
|
||||
internal set
|
||||
|
||||
init {
|
||||
initMediaSession()
|
||||
|
||||
player.addListener(object : Player.Listener {
|
||||
override fun onPlaybackStateChanged(state: Int) {
|
||||
super.onPlaybackStateChanged(state)
|
||||
|
||||
if (state == ExoPlayer.STATE_ENDED) updatePlayhead()
|
||||
}
|
||||
|
||||
override fun onIsPlayingChanged(isPlaying: Boolean) {
|
||||
super.onIsPlayingChanged(isPlaying)
|
||||
|
||||
if (!isPlaying) updatePlayhead()
|
||||
}
|
||||
})
|
||||
|
||||
playheadAutoUpdate = Timer().scheduleAtFixedRate(0, 30000) {
|
||||
viewModelScope.launch {
|
||||
if (player.isPlaying){
|
||||
updatePlayhead()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCleared() {
|
||||
@ -58,7 +111,7 @@ class PlayerViewModel(application: Application) : AndroidViewModel(application)
|
||||
mediaSession.release()
|
||||
player.release()
|
||||
|
||||
Log.d(javaClass.name, "Released player")
|
||||
Log.d(classTag, "Released player")
|
||||
}
|
||||
|
||||
/**
|
||||
@ -72,24 +125,25 @@ class PlayerViewModel(application: Application) : AndroidViewModel(application)
|
||||
mediaSession.isActive = true
|
||||
}
|
||||
|
||||
fun loadMedia(mediaId: Int, episodeId: Int) {
|
||||
runBlocking {
|
||||
media = AoDParser.getMediaById(mediaId)
|
||||
}
|
||||
fun loadMediaAsync(seasonId: String, episodeId: String) = viewModelScope.launch {
|
||||
episodes = Crunchyroll.episodes(seasonId)
|
||||
|
||||
currentEpisode = media.getEpisodeById(episodeId)
|
||||
nextEpisode = selectNextEpisode()
|
||||
currentLanguage = currentEpisode.getPreferredStream(preferredLanguage).language
|
||||
listOf(
|
||||
viewModelScope.launch { mediaMeta = loadMediaMeta(episodes.items.first().seriesId) },
|
||||
viewModelScope.launch {
|
||||
val episodeIDs = episodes.items.map { it.id }
|
||||
currentPlayheads = Crunchyroll.playheads(episodeIDs)
|
||||
}
|
||||
).joinAll()
|
||||
Log.d(classTag, "meta: $mediaMeta")
|
||||
|
||||
setCurrentEpisode(episodeId)
|
||||
playCurrentMedia(currentPlayhead)
|
||||
}
|
||||
|
||||
fun setLanguage(language: Locale) {
|
||||
currentLanguage = language
|
||||
|
||||
val seekTime = player.currentPosition
|
||||
val mediaSource = HlsMediaSource.Factory(dataSourceFactory).createMediaSource(
|
||||
MediaItem.fromUri(Uri.parse(currentEpisode.getPreferredStream(language).url))
|
||||
)
|
||||
playMedia(mediaSource, true, seekTime)
|
||||
playCurrentMedia(player.currentPosition)
|
||||
}
|
||||
|
||||
// player actions
|
||||
@ -103,56 +157,104 @@ class PlayerViewModel(application: Application) : AndroidViewModel(application)
|
||||
}
|
||||
|
||||
/**
|
||||
* play the next episode, if nextEpisode is not null
|
||||
* play the next episode, if nextEpisodeId is not null
|
||||
*/
|
||||
fun playNextEpisode() = nextEpisode?.let { it ->
|
||||
playEpisode(it, replace = true)
|
||||
fun playNextEpisode() = currentEpisode.nextEpisodeId?.let { nextEpisodeId ->
|
||||
updatePlayhead() // update playhead before switching to new episode
|
||||
setCurrentEpisode(nextEpisodeId, startPlayback = true)
|
||||
}
|
||||
|
||||
/**
|
||||
* set currentEpisode to the param episode and start playing it
|
||||
* update nextEpisode to reflect the change
|
||||
*
|
||||
* updateWatchedState for the next (now current) episode
|
||||
* Set currentEpisodeCr to the episode of the given ID
|
||||
* @param episodeId The ID of the episode you want to set currentEpisodeCr to
|
||||
*/
|
||||
fun playEpisode(episode: Episode, replace: Boolean = false, seekPosition: Long = 0) {
|
||||
val preferredStream = episode.getPreferredStream(currentLanguage)
|
||||
currentLanguage = preferredStream.language // update current language, since it may have changed
|
||||
currentEpisode = episode
|
||||
nextEpisode = selectNextEpisode()
|
||||
currentEpisodeChangedListener.forEach { it() } // update player gui (title)
|
||||
fun setCurrentEpisode(episodeId: String, startPlayback: Boolean = false) {
|
||||
currentEpisode = episodes.items.find { episode ->
|
||||
episode.id == episodeId
|
||||
} ?: NoneEpisode
|
||||
|
||||
val mediaSource = HlsMediaSource.Factory(dataSourceFactory).createMediaSource(
|
||||
MediaItem.fromUri(Uri.parse(preferredStream.url))
|
||||
)
|
||||
playMedia(mediaSource, replace, seekPosition)
|
||||
// TODO improve handling of none present seasons/episodes
|
||||
// update current episode meta
|
||||
currentEpisodeMeta = if (mediaMeta is TVShowMeta && currentEpisode.episodeNumber != null) {
|
||||
(mediaMeta as TVShowMeta)
|
||||
.seasons.getOrNull(currentEpisode.seasonNumber - 1)
|
||||
?.episodes?.getOrNull(currentEpisode.episodeNumber!! - 1)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
|
||||
// if episodes has not been watched, mark as watched
|
||||
if (!episode.watched) {
|
||||
viewModelScope.launch {
|
||||
AoDParser.markAsWatched(media.id, episode.id)
|
||||
// update player gui (title, next ep button) after currentEpisode has changed
|
||||
currentEpisodeChangedListener.forEach { it() }
|
||||
|
||||
// needs to be blocking, currentPlayback must be present when calling playCurrentMedia()
|
||||
runBlocking {
|
||||
joinAll(
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
currentPlayback = Crunchyroll.playback(currentEpisode.playback)
|
||||
},
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
Crunchyroll.playheads(listOf(currentEpisode.id))[currentEpisode.id]?.let {
|
||||
// if the episode was fully watched, start at the beginning
|
||||
currentPlayhead = if (it.fullyWatched) {
|
||||
0
|
||||
} else {
|
||||
(it.playhead.times(1000)).toLong()
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
Log.d(classTag, "playback: ${currentEpisode.playback}")
|
||||
|
||||
if (startPlayback) {
|
||||
playCurrentMedia()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Play the current media from currentPlaybackCr.
|
||||
*
|
||||
* @param seekPosition The seek position for the episode (default = 0).
|
||||
*/
|
||||
fun playCurrentMedia(seekPosition: Long = 0) {
|
||||
// get preferred stream url, set current language if it differs from the preferred one
|
||||
val preferredLocale = currentLanguage
|
||||
val fallbackLocal = Locale.US
|
||||
val url = when {
|
||||
currentPlayback.streams.adaptive_hls.containsKey(preferredLocale.toLanguageTag()) -> {
|
||||
currentPlayback.streams.adaptive_hls[preferredLocale.toLanguageTag()]?.url
|
||||
}
|
||||
currentPlayback.streams.adaptive_hls.containsKey(fallbackLocal.toLanguageTag()) -> {
|
||||
currentLanguage = fallbackLocal
|
||||
currentPlayback.streams.adaptive_hls[fallbackLocal.toLanguageTag()]?.url
|
||||
}
|
||||
else -> {
|
||||
// if no language tag is present use the first entry
|
||||
currentLanguage = Locale.ROOT
|
||||
currentPlayback.streams.adaptive_hls.entries.first().value.url
|
||||
}
|
||||
}
|
||||
Log.i(classTag, "stream url: $url")
|
||||
|
||||
// create the media item
|
||||
val mediaItem = MediaItem.fromUri(Uri.parse(url))
|
||||
player.setMediaItem(mediaItem)
|
||||
player.prepare()
|
||||
|
||||
if (seekPosition > 0) player.seekTo(seekPosition)
|
||||
player.playWhenReady = true
|
||||
}
|
||||
|
||||
/**
|
||||
* change the players media source and start playback
|
||||
* Returns the current episode title (with episode number, if it's a tv show)
|
||||
*/
|
||||
fun playMedia(source: MediaSource, replace: Boolean = false, seekPosition: Long = 0) {
|
||||
if (replace || player.contentDuration == C.TIME_UNSET) {
|
||||
player.setMediaSource(source)
|
||||
player.prepare()
|
||||
if (seekPosition > 0) player.seekTo(seekPosition)
|
||||
player.playWhenReady = true
|
||||
}
|
||||
}
|
||||
|
||||
fun getMediaTitle(): String {
|
||||
return if (media.type == DataTypes.MediaType.TVSHOW) {
|
||||
// currentEpisode.episodeNumber defines the media type (tv show = none null, movie = null)
|
||||
return if (currentEpisode.episodeNumber != null) {
|
||||
getApplication<Application>().getString(
|
||||
R.string.component_episode_title,
|
||||
currentEpisode.number,
|
||||
currentEpisode.description
|
||||
currentEpisode.episode,
|
||||
currentEpisode.title
|
||||
)
|
||||
} else {
|
||||
currentEpisode.title
|
||||
@ -160,16 +262,34 @@ class PlayerViewModel(application: Application) : AndroidViewModel(application)
|
||||
}
|
||||
|
||||
/**
|
||||
* Based on the current episodeId, get the next episode. If there is no next
|
||||
* episode, return null
|
||||
* Check if the current episode is the last in the episodes list.
|
||||
*
|
||||
* @return Boolean: true if it is the last, else false.
|
||||
*/
|
||||
private fun selectNextEpisode(): Episode? {
|
||||
val nextEpIndex = media.episodes.indexOfFirst { it.id == currentEpisode.id } + 1
|
||||
return if (nextEpIndex < media.episodes.size) {
|
||||
media.episodes[nextEpIndex]
|
||||
} else {
|
||||
null
|
||||
fun currentEpisodeIsLastEpisode(): Boolean {
|
||||
return episodes.items.lastOrNull()?.id == currentEpisode.id
|
||||
}
|
||||
|
||||
private suspend fun loadMediaMeta(crSeriesId: String): Meta? {
|
||||
return MetaDBController.getTVShowMetadata(crSeriesId)
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the playhead of the current episode, if currentPosition > 1000ms.
|
||||
*/
|
||||
private fun updatePlayhead() {
|
||||
val playhead = (player.currentPosition / 1000)
|
||||
|
||||
if (playhead > 0 && Preferences.updatePlayhead) {
|
||||
// don't use viewModelScope here. This task may needs to finish, when ViewModel will be cleared
|
||||
CoroutineScope(Dispatchers.IO).launch { Crunchyroll.postPlayheads(currentEpisode.id, playhead.toInt()) }
|
||||
Log.i(javaClass.name, "Set playhead for episode ${currentEpisode.id} to $playhead sec.")
|
||||
}
|
||||
|
||||
viewModelScope.launch {
|
||||
val episodeIDs = episodes.items.map { it.id }
|
||||
currentPlayheads = Crunchyroll.playheads(episodeIDs)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,68 @@
|
||||
package org.mosad.teapod.ui.activity.player.fragment
|
||||
|
||||
import android.content.DialogInterface
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.fragment.app.DialogFragment
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import org.mosad.teapod.R
|
||||
import org.mosad.teapod.databinding.PlayerEpisodesListBinding
|
||||
import org.mosad.teapod.ui.activity.player.PlayerViewModel
|
||||
import org.mosad.teapod.util.adapter.EpisodeItemAdapter
|
||||
import org.mosad.teapod.util.hideBars
|
||||
|
||||
class EpisodeListDialogFragment : DialogFragment() {
|
||||
|
||||
private lateinit var model: PlayerViewModel
|
||||
private lateinit var binding: PlayerEpisodesListBinding
|
||||
|
||||
companion object {
|
||||
const val TAG = "LanguageSettingsDialogFragment"
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setStyle(STYLE_NO_TITLE, R.style.FullScreenDialogStyle)
|
||||
model = ViewModelProvider(requireActivity())[PlayerViewModel::class.java]
|
||||
}
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
|
||||
binding = PlayerEpisodesListBinding.inflate(inflater, container, false)
|
||||
return binding.root
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
|
||||
binding.buttonCloseEpisodesList.setOnClickListener {
|
||||
dismiss()
|
||||
}
|
||||
|
||||
val adapterRecEpisodes = EpisodeItemAdapter(
|
||||
model.episodes.items,
|
||||
null,
|
||||
model.currentPlayheads.toMap(),
|
||||
EpisodeItemAdapter.OnClickListener { episode ->
|
||||
dismiss()
|
||||
model.setCurrentEpisode(episode.id, startPlayback = true)
|
||||
},
|
||||
EpisodeItemAdapter.ViewType.PLAYER
|
||||
)
|
||||
|
||||
// get the position/index of the currently playing episode
|
||||
adapterRecEpisodes.currentSelected = model.episodes.items.indexOfFirst { it.id == model.currentEpisode.id }
|
||||
|
||||
binding.recyclerEpisodesPlayer.adapter = adapterRecEpisodes
|
||||
binding.recyclerEpisodesPlayer.scrollToPosition(adapterRecEpisodes.currentSelected)
|
||||
|
||||
// initially hide the status and navigation bar
|
||||
hideBars(requireDialog().window, binding.root)
|
||||
}
|
||||
|
||||
override fun onDismiss(dialog: DialogInterface) {
|
||||
super.onDismiss(dialog)
|
||||
model.player.play()
|
||||
}
|
||||
}
|
@ -1,66 +1,89 @@
|
||||
package org.mosad.teapod.ui.components
|
||||
package org.mosad.teapod.ui.activity.player.fragment
|
||||
|
||||
import android.content.Context
|
||||
import android.content.DialogInterface
|
||||
import android.graphics.Color
|
||||
import android.graphics.Typeface
|
||||
import android.util.AttributeSet
|
||||
import android.os.Bundle
|
||||
import android.util.TypedValue
|
||||
import android.view.Gravity
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.LinearLayout
|
||||
import android.widget.TextView
|
||||
import androidx.core.view.children
|
||||
import androidx.fragment.app.DialogFragment
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import org.mosad.teapod.R
|
||||
import org.mosad.teapod.databinding.PlayerLanguageSettingsBinding
|
||||
import org.mosad.teapod.ui.activity.player.PlayerViewModel
|
||||
import org.mosad.teapod.util.hideBars
|
||||
import java.util.*
|
||||
|
||||
class LanguageSettingsPlayer @JvmOverloads constructor(
|
||||
context: Context,
|
||||
attrs: AttributeSet? = null,
|
||||
defStyleAttr: Int = 0,
|
||||
model: PlayerViewModel? = null
|
||||
) : LinearLayout(context, attrs, defStyleAttr) {
|
||||
class LanguageSettingsDialogFragment : DialogFragment() {
|
||||
|
||||
private val binding = PlayerLanguageSettingsBinding.inflate(LayoutInflater.from(context), this, true)
|
||||
var onViewRemovedAction: (() -> Unit)? = null // TODO find a better solution for this
|
||||
private lateinit var model: PlayerViewModel
|
||||
private lateinit var binding: PlayerLanguageSettingsBinding
|
||||
|
||||
private var currentLanguage = model?.currentLanguage ?: Locale.ROOT
|
||||
private var selectedLocale = Locale.ROOT
|
||||
|
||||
init {
|
||||
model?.let {
|
||||
model.currentEpisode.streams.forEach { stream ->
|
||||
addLanguage(stream.language.displayName, stream.language == currentLanguage) {
|
||||
currentLanguage = stream.language
|
||||
updateSelectedLanguage(it as TextView)
|
||||
}
|
||||
companion object {
|
||||
const val TAG = "LanguageSettingsDialogFragment"
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setStyle(STYLE_NO_TITLE, R.style.FullScreenDialogStyle)
|
||||
model = ViewModelProvider(requireActivity())[PlayerViewModel::class.java]
|
||||
selectedLocale = model.currentLanguage
|
||||
}
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
|
||||
binding = PlayerLanguageSettingsBinding.inflate(inflater, container, false)
|
||||
return binding.root
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
|
||||
model.currentPlayback.streams.adaptive_hls.keys.forEach { languageTag ->
|
||||
val locale = Locale.forLanguageTag(languageTag)
|
||||
addLanguage(locale, locale == model.currentLanguage) { v ->
|
||||
selectedLocale = locale
|
||||
updateSelectedLanguage(v as TextView)
|
||||
}
|
||||
}
|
||||
|
||||
binding.buttonCloseLanguageSettings.setOnClickListener { close() }
|
||||
binding.buttonCancel.setOnClickListener { close() }
|
||||
binding.buttonCloseLanguageSettings.setOnClickListener { dismiss() }
|
||||
binding.buttonCancel.setOnClickListener { dismiss() }
|
||||
binding.buttonSelect.setOnClickListener {
|
||||
model?.setLanguage(currentLanguage)
|
||||
close()
|
||||
model.setLanguage(selectedLocale)
|
||||
dismiss()
|
||||
}
|
||||
|
||||
// initially hide the status and navigation bar
|
||||
hideBars(requireDialog().window, binding.root)
|
||||
}
|
||||
|
||||
private fun addLanguage(str: String, isSelected: Boolean, onClick: OnClickListener) {
|
||||
override fun onDismiss(dialog: DialogInterface) {
|
||||
super.onDismiss(dialog)
|
||||
model.player.play()
|
||||
}
|
||||
|
||||
private fun addLanguage(locale: Locale, isSelected: Boolean, onClick: View.OnClickListener) {
|
||||
val text = TextView(context).apply {
|
||||
height = 96
|
||||
gravity = Gravity.CENTER_VERTICAL
|
||||
text = str
|
||||
text = if (locale == Locale.ROOT) context.getString(R.string.no_subtitles) else locale.displayLanguage
|
||||
setTextSize(TypedValue.COMPLEX_UNIT_SP, 16f)
|
||||
|
||||
if (isSelected) {
|
||||
setTextColor(context.resources.getColor(R.color.exo_white, context.theme))
|
||||
setTextColor(context.resources.getColor(R.color.textPrimaryDark, context.theme))
|
||||
setTypeface(null, Typeface.BOLD)
|
||||
setCompoundDrawablesRelativeWithIntrinsicBounds(R.drawable.ic_baseline_check_24, 0, 0, 0)
|
||||
compoundDrawablesRelative.getOrNull(0)?.setTint(Color.WHITE)
|
||||
compoundDrawablePadding = 12
|
||||
} else {
|
||||
setTextColor(context.resources.getColor(R.color.textPrimaryDark, context.theme))
|
||||
setTextColor(context.resources.getColor(R.color.textSecondaryDark, context.theme))
|
||||
setPadding(75, 0, 0, 0)
|
||||
}
|
||||
|
||||
@ -81,12 +104,11 @@ class LanguageSettingsPlayer @JvmOverloads constructor(
|
||||
setPadding(75, 0, 0, 0)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// set selected to selected style
|
||||
selected.apply {
|
||||
setTextColor(context.resources.getColor(R.color.exo_white, context.theme))
|
||||
setTextColor(context.resources.getColor(R.color.player_white, context.theme))
|
||||
setTypeface(null, Typeface.BOLD)
|
||||
setCompoundDrawablesRelativeWithIntrinsicBounds(R.drawable.ic_baseline_check_24, 0, 0, 0)
|
||||
setPadding(0, 0, 0, 0)
|
||||
@ -94,10 +116,4 @@ class LanguageSettingsPlayer @JvmOverloads constructor(
|
||||
compoundDrawablePadding = 12
|
||||
}
|
||||
}
|
||||
|
||||
private fun close() {
|
||||
(this.parent as ViewGroup).removeView(this)
|
||||
onViewRemovedAction?.invoke()
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -1,44 +0,0 @@
|
||||
package org.mosad.teapod.ui.components
|
||||
|
||||
import android.content.Context
|
||||
import android.util.AttributeSet
|
||||
import android.view.LayoutInflater
|
||||
import android.view.ViewGroup
|
||||
import android.widget.LinearLayout
|
||||
import org.mosad.teapod.databinding.PlayerEpisodesListBinding
|
||||
import org.mosad.teapod.ui.activity.player.PlayerViewModel
|
||||
import org.mosad.teapod.util.adapter.PlayerEpisodeItemAdapter
|
||||
|
||||
class EpisodesListPlayer @JvmOverloads constructor(
|
||||
context: Context,
|
||||
attrs: AttributeSet? = null,
|
||||
defStyleAttr: Int = 0,
|
||||
model: PlayerViewModel? = null
|
||||
) : LinearLayout(context, attrs, defStyleAttr) {
|
||||
|
||||
private val binding = PlayerEpisodesListBinding.inflate(LayoutInflater.from(context), this, true)
|
||||
private lateinit var adapterRecEpisodes: PlayerEpisodeItemAdapter
|
||||
|
||||
var onViewRemovedAction: (() -> Unit)? = null // TODO find a better solution for this
|
||||
|
||||
init {
|
||||
binding.buttonCloseEpisodesList.setOnClickListener {
|
||||
(this.parent as ViewGroup).removeView(this)
|
||||
onViewRemovedAction?.invoke()
|
||||
}
|
||||
|
||||
model?.let {
|
||||
adapterRecEpisodes = PlayerEpisodeItemAdapter(model.media.episodes)
|
||||
|
||||
adapterRecEpisodes.onImageClick = { _, position ->
|
||||
(this.parent as ViewGroup).removeView(this)
|
||||
model.playEpisode(model.media.episodes[position], replace = true)
|
||||
}
|
||||
adapterRecEpisodes.currentSelected = model.currentEpisode.number - 1
|
||||
|
||||
binding.recyclerEpisodesPlayer.adapter = adapterRecEpisodes
|
||||
binding.recyclerEpisodesPlayer.scrollToPosition(model.currentEpisode.number - 1) // number != index
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -5,13 +5,15 @@ import android.animation.AnimatorListenerAdapter
|
||||
import android.animation.ObjectAnimator
|
||||
import android.content.Context
|
||||
import android.util.AttributeSet
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.widget.FrameLayout
|
||||
import kotlinx.android.synthetic.main.button_fast_forward.view.*
|
||||
import org.mosad.teapod.R
|
||||
import org.mosad.teapod.databinding.ButtonFastForwardBinding
|
||||
|
||||
class FastForwardButton(context: Context, attrs: AttributeSet?): FrameLayout(context, attrs) {
|
||||
|
||||
private val binding = ButtonFastForwardBinding.inflate(LayoutInflater.from(context))
|
||||
private val animationDuration: Long = 800
|
||||
private val buttonAnimation: ObjectAnimator
|
||||
private val labelAnimation: ObjectAnimator
|
||||
@ -19,30 +21,30 @@ class FastForwardButton(context: Context, attrs: AttributeSet?): FrameLayout(con
|
||||
var onAnimationEndCallback: (() -> Unit)? = null
|
||||
|
||||
init {
|
||||
inflate(context, R.layout.button_fast_forward, this)
|
||||
addView(binding.root)
|
||||
|
||||
buttonAnimation = ObjectAnimator.ofFloat(imageButton, View.ROTATION, 0f, 50f).apply {
|
||||
buttonAnimation = ObjectAnimator.ofFloat(binding.imageButton, View.ROTATION, 0f, 50f).apply {
|
||||
duration = animationDuration / 4
|
||||
repeatCount = 1
|
||||
repeatMode = ObjectAnimator.REVERSE
|
||||
addListener(object : AnimatorListenerAdapter() {
|
||||
override fun onAnimationStart(animation: Animator?) {
|
||||
imageButton.isEnabled = false // disable button
|
||||
imageButton.setBackgroundResource(R.drawable.ic_baseline_forward_24)
|
||||
override fun onAnimationStart(animation: Animator) {
|
||||
binding.imageButton.isEnabled = false // disable button
|
||||
binding.imageButton.setBackgroundResource(R.drawable.ic_baseline_forward_24)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
labelAnimation = ObjectAnimator.ofFloat(textView, View.TRANSLATION_X, 35f).apply {
|
||||
labelAnimation = ObjectAnimator.ofFloat(binding.textView, View.TRANSLATION_X, 35f).apply {
|
||||
duration = animationDuration
|
||||
addListener(object : AnimatorListenerAdapter() {
|
||||
// the label animation takes longer then the button animation, reset stuff in here
|
||||
override fun onAnimationEnd(animation: Animator?) {
|
||||
imageButton.isEnabled = true // enable button
|
||||
imageButton.setBackgroundResource(R.drawable.ic_baseline_forward_10_24)
|
||||
override fun onAnimationEnd(animation: Animator) {
|
||||
binding.imageButton.isEnabled = true // enable button
|
||||
binding.imageButton.setBackgroundResource(R.drawable.ic_baseline_forward_10_24)
|
||||
|
||||
textView.visibility = View.GONE
|
||||
textView.animate().translationX(0f)
|
||||
binding.textView.visibility = View.GONE
|
||||
binding.textView.animate().translationX(0f)
|
||||
|
||||
onAnimationEndCallback?.invoke()
|
||||
}
|
||||
@ -51,7 +53,7 @@ class FastForwardButton(context: Context, attrs: AttributeSet?): FrameLayout(con
|
||||
}
|
||||
|
||||
fun setOnButtonClickListener(func: FastForwardButton.() -> Unit) {
|
||||
imageButton.setOnClickListener {
|
||||
binding.imageButton.setOnClickListener {
|
||||
func()
|
||||
}
|
||||
}
|
||||
@ -61,7 +63,7 @@ class FastForwardButton(context: Context, attrs: AttributeSet?): FrameLayout(con
|
||||
buttonAnimation.start()
|
||||
|
||||
// run lbl animation
|
||||
textView.visibility = View.VISIBLE
|
||||
binding.textView.visibility = View.VISIBLE
|
||||
labelAnimation.start()
|
||||
}
|
||||
|
||||
|
@ -1,93 +0,0 @@
|
||||
/**
|
||||
* ProjectLaogai
|
||||
*
|
||||
* Copyright 2019-2020 <seil0@mosad.xyz>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.mosad.teapod.ui.components
|
||||
|
||||
import android.content.Context
|
||||
import android.widget.EditText
|
||||
import com.afollestad.materialdialogs.MaterialDialog
|
||||
import com.afollestad.materialdialogs.bottomsheets.BottomSheet
|
||||
import com.afollestad.materialdialogs.bottomsheets.setPeekHeight
|
||||
import com.afollestad.materialdialogs.customview.customView
|
||||
import com.afollestad.materialdialogs.customview.getCustomView
|
||||
import org.mosad.teapod.R
|
||||
|
||||
class LoginDialog(val context: Context, firstTry: Boolean) {
|
||||
|
||||
private val dialog = MaterialDialog(context, BottomSheet())
|
||||
|
||||
private val editTextLogin: EditText
|
||||
private val editTextPassword: EditText
|
||||
|
||||
var login = ""
|
||||
var password = ""
|
||||
|
||||
init {
|
||||
dialog.title(R.string.login)
|
||||
.message(if (firstTry) R.string.login_desc else R.string.login_failed_desc)
|
||||
.customView(R.layout.dialog_login)
|
||||
.positiveButton(R.string.save)
|
||||
.negativeButton(R.string.cancel)
|
||||
.setPeekHeight(900)
|
||||
|
||||
editTextLogin = dialog.getCustomView().findViewById(R.id.edit_text_login)
|
||||
editTextPassword = dialog.getCustomView().findViewById(R.id.edit_text_password)
|
||||
|
||||
// fix not working accent color
|
||||
//dialog.getActionButton(WhichButton.POSITIVE).updateTextColor(Preferences.colorAccent)
|
||||
//dialog.getActionButton(WhichButton.NEGATIVE).updateTextColor(Preferences.colorAccent)
|
||||
}
|
||||
|
||||
fun positiveButton(func: LoginDialog.() -> Unit): LoginDialog = apply {
|
||||
dialog.positiveButton {
|
||||
login = editTextLogin.text.toString()
|
||||
password = editTextPassword.text.toString()
|
||||
|
||||
func()
|
||||
}
|
||||
}
|
||||
|
||||
fun negativeButton(func: LoginDialog.() -> Unit): LoginDialog = apply {
|
||||
dialog.negativeButton {
|
||||
func()
|
||||
}
|
||||
}
|
||||
|
||||
fun show() {
|
||||
dialog.show()
|
||||
}
|
||||
|
||||
fun show(func: LoginDialog.() -> Unit): LoginDialog = apply {
|
||||
func()
|
||||
|
||||
editTextLogin.setText(login)
|
||||
editTextPassword.setText(password)
|
||||
|
||||
show()
|
||||
}
|
||||
|
||||
@Suppress("unused")
|
||||
fun dismiss() {
|
||||
dialog.dismiss()
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
package org.mosad.teapod.ui.components
|
||||
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
|
||||
import org.mosad.teapod.databinding.ModalBottomSheetLoginBinding
|
||||
|
||||
/**
|
||||
* A bottom sheet with login credential input fields.
|
||||
*
|
||||
* To initialize login or password values, use apply.
|
||||
*/
|
||||
class LoginModalBottomSheet : BottomSheetDialogFragment() {
|
||||
|
||||
private lateinit var binding: ModalBottomSheetLoginBinding
|
||||
|
||||
var login = ""
|
||||
var password = ""
|
||||
|
||||
lateinit var positiveAction: LoginModalBottomSheet.() -> Unit
|
||||
lateinit var negativeAction: LoginModalBottomSheet.() -> Unit
|
||||
|
||||
companion object {
|
||||
const val TAG = "LoginModalBottomSheet"
|
||||
}
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
): View {
|
||||
binding = ModalBottomSheetLoginBinding.inflate(inflater, container, false)
|
||||
return binding.root
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
|
||||
binding.editTextLogin.setText(login)
|
||||
binding.editTextPassword.setText(password)
|
||||
|
||||
binding.positiveButton.setOnClickListener {
|
||||
login = binding.editTextLogin.text.toString()
|
||||
password = binding.editTextPassword.text.toString()
|
||||
|
||||
positiveAction.invoke(this)
|
||||
}
|
||||
binding.negativeButton.setOnClickListener {
|
||||
negativeAction.invoke(this)
|
||||
}
|
||||
}
|
||||
}
|
@ -5,13 +5,15 @@ import android.animation.AnimatorListenerAdapter
|
||||
import android.animation.ObjectAnimator
|
||||
import android.content.Context
|
||||
import android.util.AttributeSet
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.widget.FrameLayout
|
||||
import kotlinx.android.synthetic.main.button_rewind.view.*
|
||||
import org.mosad.teapod.R
|
||||
import org.mosad.teapod.databinding.ButtonRewindBinding
|
||||
|
||||
class RewindButton(context: Context, attrs: AttributeSet): FrameLayout(context, attrs) {
|
||||
|
||||
private val binding = ButtonRewindBinding.inflate(LayoutInflater.from(context))
|
||||
private val animationDuration: Long = 800
|
||||
private val buttonAnimation: ObjectAnimator
|
||||
private val labelAnimation: ObjectAnimator
|
||||
@ -19,29 +21,29 @@ class RewindButton(context: Context, attrs: AttributeSet): FrameLayout(context,
|
||||
var onAnimationEndCallback: (() -> Unit)? = null
|
||||
|
||||
init {
|
||||
inflate(context, R.layout.button_rewind, this)
|
||||
addView(binding.root)
|
||||
|
||||
buttonAnimation = ObjectAnimator.ofFloat(imageButton, View.ROTATION, 0f, -50f).apply {
|
||||
buttonAnimation = ObjectAnimator.ofFloat(binding.imageButton, View.ROTATION, 0f, -50f).apply {
|
||||
duration = animationDuration / 4
|
||||
repeatCount = 1
|
||||
repeatMode = ObjectAnimator.REVERSE
|
||||
addListener(object : AnimatorListenerAdapter() {
|
||||
override fun onAnimationStart(animation: Animator?) {
|
||||
imageButton.isEnabled = false // disable button
|
||||
imageButton.setBackgroundResource(R.drawable.ic_baseline_rewind_24)
|
||||
override fun onAnimationStart(animation: Animator) {
|
||||
binding.imageButton.isEnabled = false // disable button
|
||||
binding.imageButton.setBackgroundResource(R.drawable.ic_baseline_rewind_24)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
labelAnimation = ObjectAnimator.ofFloat(textView, View.TRANSLATION_X, -35f).apply {
|
||||
labelAnimation = ObjectAnimator.ofFloat(binding.textView, View.TRANSLATION_X, -35f).apply {
|
||||
duration = animationDuration
|
||||
addListener(object : AnimatorListenerAdapter() {
|
||||
override fun onAnimationEnd(animation: Animator?) {
|
||||
imageButton.isEnabled = true // enable button
|
||||
imageButton.setBackgroundResource(R.drawable.ic_baseline_rewind_10_24)
|
||||
override fun onAnimationEnd(animation: Animator) {
|
||||
binding.imageButton.isEnabled = true // enable button
|
||||
binding.imageButton.setBackgroundResource(R.drawable.ic_baseline_rewind_10_24)
|
||||
|
||||
textView.visibility = View.GONE
|
||||
textView.animate().translationX(0f)
|
||||
binding.textView.visibility = View.GONE
|
||||
binding.textView.animate().translationX(0f)
|
||||
|
||||
onAnimationEndCallback?.invoke()
|
||||
}
|
||||
@ -50,7 +52,7 @@ class RewindButton(context: Context, attrs: AttributeSet): FrameLayout(context,
|
||||
}
|
||||
|
||||
fun setOnButtonClickListener(func: RewindButton.() -> Unit) {
|
||||
imageButton.setOnClickListener {
|
||||
binding.imageButton.setOnClickListener {
|
||||
func()
|
||||
}
|
||||
}
|
||||
@ -60,7 +62,7 @@ class RewindButton(context: Context, attrs: AttributeSet): FrameLayout(context,
|
||||
buttonAnimation.start()
|
||||
|
||||
// run lbl animation
|
||||
textView.visibility = View.VISIBLE
|
||||
binding.textView.visibility = View.VISIBLE
|
||||
labelAnimation.start()
|
||||
}
|
||||
|
||||
|
@ -5,13 +5,11 @@ import android.app.ActivityManager
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Build
|
||||
import android.view.View
|
||||
import android.view.WindowInsets
|
||||
import android.view.WindowInsetsController
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.FragmentActivity
|
||||
import androidx.fragment.app.commit
|
||||
import org.mosad.teapod.R
|
||||
import org.mosad.teapod.ui.activity.player.PlayerActivity
|
||||
import kotlin.system.exitProcess
|
||||
|
||||
/**
|
||||
@ -27,27 +25,25 @@ fun FragmentActivity.showFragment(fragment: Fragment) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Start the player as new activity.
|
||||
*
|
||||
* @param seasonId The ID of the season the episode to be played is in
|
||||
* @param episodeId The ID of the episode to play
|
||||
*/
|
||||
fun Activity.startPlayer(seasonId: String, episodeId: String) {
|
||||
val intent = Intent(this, PlayerActivity::class.java).apply {
|
||||
putExtra(getString(R.string.intent_season_id), seasonId)
|
||||
putExtra(getString(R.string.intent_episode_id), episodeId)
|
||||
}
|
||||
startActivity(intent)
|
||||
}
|
||||
|
||||
/**
|
||||
* hide the status and navigation bar
|
||||
*/
|
||||
fun Activity.hideBars() {
|
||||
window.apply {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
||||
setDecorFitsSystemWindows(false)
|
||||
insetsController?.apply {
|
||||
hide(WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars())
|
||||
systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_BARS_BY_SWIPE
|
||||
}
|
||||
} else {
|
||||
@Suppress("deprecation")
|
||||
decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE
|
||||
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
|
||||
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
|
||||
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
|
||||
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
|
||||
or View.SYSTEM_UI_FLAG_FULLSCREEN)
|
||||
}
|
||||
}
|
||||
hideBars(window, window.decorView.rootView)
|
||||
}
|
||||
|
||||
fun Activity.isInPiPMode(): Boolean {
|
||||
|
@ -1,13 +1,12 @@
|
||||
package org.mosad.teapod.util
|
||||
|
||||
import java.util.*
|
||||
import kotlin.collections.ArrayList
|
||||
import java.util.Locale
|
||||
|
||||
class DataTypes {
|
||||
enum class MediaType {
|
||||
OTHER,
|
||||
MOVIE,
|
||||
TVSHOW
|
||||
enum class MediaType(val str: String) {
|
||||
OTHER("other"),
|
||||
MOVIE("movie"), // TODO
|
||||
TVSHOW("series")
|
||||
}
|
||||
|
||||
enum class Theme(val str: String) {
|
||||
@ -36,61 +35,47 @@ data class ThirdPartyComponent(
|
||||
* it is uses in the ItemMediaAdapter (RecyclerView)
|
||||
*/
|
||||
data class ItemMedia(
|
||||
val id: Int,
|
||||
val id: String,
|
||||
val title: String,
|
||||
val posterUrl: String
|
||||
val posterUrl: String,
|
||||
)
|
||||
|
||||
/**
|
||||
* TODO the episodes workflow could use a clean up/rework
|
||||
*/
|
||||
data class Media(
|
||||
val id: Int,
|
||||
val link: String,
|
||||
// TODO replace playlist: List<AoDEpisode> with a map?
|
||||
data class AoDMedia(
|
||||
val aodId: Int,
|
||||
val type: DataTypes.MediaType,
|
||||
val info: Info = Info(),
|
||||
val episodes: ArrayList<Episode> = arrayListOf()
|
||||
val title: String,
|
||||
val shortText: String,
|
||||
val posterURL: String,
|
||||
var year: Int,
|
||||
var age: Int,
|
||||
val similar: List<ItemMedia>,
|
||||
val playlist: List<AoDEpisode>,
|
||||
) {
|
||||
fun hasEpisode(id: Int) = episodes.any { it.id == id }
|
||||
fun getEpisodeById(id: Int) = episodes.first { it.id == id }
|
||||
fun getEpisodeById(mediaId: Int) = playlist.firstOrNull { it.mediaId == mediaId }
|
||||
?: AoDEpisodeNone
|
||||
}
|
||||
|
||||
/**
|
||||
* uses var, since the values are written in different steps
|
||||
*/
|
||||
data class Info(
|
||||
var title: String = "",
|
||||
var posterUrl: String = "",
|
||||
var shortDesc: String = "",
|
||||
var description: String = "",
|
||||
var year: Int = 0,
|
||||
var age: Int = 0,
|
||||
var episodesCount: Int = 0,
|
||||
var similar: List<ItemMedia> = listOf()
|
||||
)
|
||||
data class AoDEpisode(
|
||||
val mediaId: Int,
|
||||
val title: String,
|
||||
val description: String,
|
||||
val shortDesc: String,
|
||||
val imageURL: String,
|
||||
val numberStr: String,
|
||||
val index: Int,
|
||||
var watched: Boolean,
|
||||
val watchedCallback: String,
|
||||
val streams: MutableList<Stream>,
|
||||
){
|
||||
fun hasDub() = streams.any { it.language == Locale.GERMAN }
|
||||
|
||||
/**
|
||||
* number = episode number (0..n)
|
||||
*/
|
||||
data class Episode(
|
||||
val id: Int = -1,
|
||||
val streams: MutableList<Stream> = mutableListOf(),
|
||||
val title: String = "",
|
||||
val posterUrl: String = "",
|
||||
val description: String = "",
|
||||
var shortDesc: String = "",
|
||||
val number: Int = 0,
|
||||
var watched: Boolean = false,
|
||||
var watchedCallback: String = ""
|
||||
) {
|
||||
/**
|
||||
* get the preferred stream
|
||||
* @return the preferred stream, if not present use the first stream
|
||||
*/
|
||||
fun getPreferredStream(language: Locale) =
|
||||
streams.firstOrNull { it.language == language } ?: streams.first()
|
||||
|
||||
fun hasDub() = streams.any { it.language == Locale.GERMAN }
|
||||
fun getPreferredStream(language: Locale) = streams.firstOrNull { it.language == language }
|
||||
?: Stream("", Locale.ROOT)
|
||||
}
|
||||
|
||||
data class Stream(
|
||||
@ -98,24 +83,45 @@ data class Stream(
|
||||
val language : Locale
|
||||
)
|
||||
|
||||
/**
|
||||
* this class is used for tmdb responses
|
||||
*/
|
||||
data class TMDBResponse(
|
||||
val id: Int = 0,
|
||||
val title: String = "",
|
||||
val overview: String = "",
|
||||
val posterUrl: String = "",
|
||||
val backdropUrl: String = "",
|
||||
val runtime: Int = 0
|
||||
// TODO will be watched info (state and callback) -> remove description and number
|
||||
data class AoDEpisodeInfo(
|
||||
val aodMediaId: Int,
|
||||
val shortDesc: String,
|
||||
var watched: Boolean,
|
||||
val watchedCallback: String,
|
||||
)
|
||||
|
||||
val AoDMediaNone = AoDMedia(
|
||||
-1,
|
||||
DataTypes.MediaType.OTHER,
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
-1,
|
||||
-1,
|
||||
listOf(),
|
||||
listOf()
|
||||
)
|
||||
|
||||
val AoDEpisodeNone = AoDEpisode(
|
||||
-1,
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
-1,
|
||||
true,
|
||||
"",
|
||||
mutableListOf()
|
||||
)
|
||||
|
||||
/**
|
||||
* this class is used to represent the aod json API?
|
||||
*/
|
||||
data class AoDObject(
|
||||
val playlist: List<Playlist>,
|
||||
val extLanguage: String
|
||||
data class AoDPlaylist(
|
||||
val list: List<Playlist>,
|
||||
val language: Locale
|
||||
)
|
||||
|
||||
data class Playlist(
|
||||
|
@ -1,89 +0,0 @@
|
||||
package org.mosad.teapod.util
|
||||
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import android.util.Log
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.JsonParser
|
||||
import kotlinx.coroutines.*
|
||||
import java.io.File
|
||||
import java.io.FileReader
|
||||
import java.io.FileWriter
|
||||
|
||||
/**
|
||||
* This controller contains the logic for permanently saved data.
|
||||
* On load, it loads the saved files into the variables
|
||||
*/
|
||||
object StorageController {
|
||||
|
||||
private const val fileNameMyList = "my_list.json"
|
||||
|
||||
val myList = ArrayList<Int>() // a list of saved mediaIds
|
||||
|
||||
fun load(context: Context) {
|
||||
loadMyList(context)
|
||||
}
|
||||
|
||||
fun loadMyList(context: Context) {
|
||||
val file = File(context.filesDir, fileNameMyList)
|
||||
|
||||
if (!file.exists()) runBlocking { saveMyList(context).join() }
|
||||
|
||||
try {
|
||||
myList.clear()
|
||||
myList.addAll(JsonParser.parseString(file.readText()).asJsonArray.map { it.asInt }.distinct())
|
||||
} catch (ex: Exception) {
|
||||
myList.clear()
|
||||
Log.e(javaClass.name, "Parsing of My-List failed.")
|
||||
}
|
||||
}
|
||||
|
||||
fun saveMyList(context: Context): Job {
|
||||
val file = File(context.filesDir, fileNameMyList)
|
||||
|
||||
return CoroutineScope(Dispatchers.IO).launch {
|
||||
file.writeText(Gson().toJson(myList.distinct()))
|
||||
}
|
||||
}
|
||||
|
||||
fun exportMyList(context: Context, uri: Uri) {
|
||||
try {
|
||||
context.contentResolver.openFileDescriptor(uri, "w")?.use {
|
||||
FileWriter(it.fileDescriptor).use { writer ->
|
||||
writer.write(Gson().toJson(myList.distinct()))
|
||||
}
|
||||
}
|
||||
} catch (ex: Exception) {
|
||||
Log.e(javaClass.name, "Exporting my list failed.", ex)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* import my list from a (previously exported) json file
|
||||
* @param context the current context
|
||||
* @param uri the uri of the selected file
|
||||
* @return 0 if import was successfull, else 1
|
||||
*/
|
||||
fun importMyList(context: Context, uri: Uri): Int {
|
||||
try {
|
||||
val text = context.contentResolver.openFileDescriptor(uri, "r")?.use {
|
||||
FileReader(it.fileDescriptor).use { reader ->
|
||||
reader.readText()
|
||||
}
|
||||
}
|
||||
|
||||
myList.clear()
|
||||
myList.addAll(JsonParser.parseString(text).asJsonArray.map { it.asInt }.distinct())
|
||||
|
||||
// after the list has been imported also save it
|
||||
saveMyList(context)
|
||||
} catch (ex: Exception) {
|
||||
myList.clear()
|
||||
Log.e(javaClass.name, "Importing my list failed.", ex)
|
||||
return 1
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
}
|
@ -1,121 +0,0 @@
|
||||
package org.mosad.teapod.util
|
||||
|
||||
import android.util.Log
|
||||
import com.google.gson.JsonObject
|
||||
import com.google.gson.JsonParser
|
||||
import kotlinx.coroutines.*
|
||||
import org.mosad.teapod.util.DataTypes.MediaType
|
||||
import java.net.URL
|
||||
import java.net.URLEncoder
|
||||
|
||||
class TMDBApiController {
|
||||
|
||||
private val apiUrl = "https://api.themoviedb.org/3"
|
||||
private val searchMovieUrl = "$apiUrl/search/movie"
|
||||
private val searchTVUrl = "$apiUrl/search/tv"
|
||||
private val getMovieUrl = "$apiUrl/movie"
|
||||
private val apiKey = "de959cf9c07a08b5ca7cb51cda9a40c2"
|
||||
private val language = "de"
|
||||
private val preparedParameters = "?api_key=$apiKey&language=$language"
|
||||
|
||||
private val imageUrl = "https://image.tmdb.org/t/p/w500"
|
||||
|
||||
suspend fun search(title: String, type: MediaType): TMDBResponse {
|
||||
// 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) {
|
||||
MediaType.MOVIE -> searchMovie(searchTerm)
|
||||
MediaType.TVSHOW -> searchTVShow(searchTerm)
|
||||
else -> {
|
||||
Log.e(javaClass.name, "Wrong Type: $type")
|
||||
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)
|
||||
|
||||
val sortedResults = response.get("results").asJsonArray.toList().sortedBy {
|
||||
getStringNotNull(it.asJsonObject, "name")
|
||||
}
|
||||
|
||||
return@withContext if (sortedResults.isNotEmpty()) {
|
||||
sortedResults.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()
|
||||
}
|
||||
}
|
||||
|
||||
@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)
|
||||
|
||||
val sortedResults = response.get("results").asJsonArray.toList().sortedBy {
|
||||
getStringNotNull(it.asJsonObject, "title")
|
||||
}
|
||||
|
||||
return@withContext if (sortedResults.isNotEmpty()) {
|
||||
sortedResults.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()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* currently only used for runtime, need a rework
|
||||
*/
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
suspend fun getMovieRuntime(id: Int): Int = withContext(Dispatchers.IO) {
|
||||
val url = URL("$getMovieUrl/$id?api_key=$apiKey&language=$language")
|
||||
|
||||
val response = JsonParser.parseString(url.readText()).asJsonObject
|
||||
return@withContext getStringNotNull(response,"runtime").toInt()
|
||||
}
|
||||
|
||||
/**
|
||||
* return memberName as string if it's not JsonNull,
|
||||
* else return an empty string
|
||||
*/
|
||||
private fun getStringNotNull(jsonObject: JsonObject, memberName: String): String {
|
||||
return getStringNotNullPrefix(jsonObject, memberName, "")
|
||||
}
|
||||
|
||||
/**
|
||||
* return memberName as string with a prefix if it's not JsonNull,
|
||||
* else return an empty string
|
||||
*/
|
||||
private fun getStringNotNullPrefix(jsonObject: JsonObject, memberName: String, prefix: String): String {
|
||||
return if (!jsonObject.get(memberName).isJsonNull) {
|
||||
prefix + jsonObject.get(memberName).asString
|
||||
} else {
|
||||
""
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,7 +1,67 @@
|
||||
package org.mosad.teapod.util
|
||||
|
||||
import android.view.View
|
||||
import android.view.Window
|
||||
import android.widget.TextView
|
||||
import androidx.core.view.WindowCompat
|
||||
import androidx.core.view.WindowInsetsCompat
|
||||
import androidx.core.view.WindowInsetsControllerCompat
|
||||
import org.mosad.teapod.parser.crunchyroll.Collection
|
||||
import org.mosad.teapod.parser.crunchyroll.ContinueWatchingItem
|
||||
import org.mosad.teapod.parser.crunchyroll.Item
|
||||
import java.util.*
|
||||
|
||||
fun TextView.setDrawableTop(drawable: Int) {
|
||||
this.setCompoundDrawablesWithIntrinsicBounds(0, drawable, 0, 0)
|
||||
}
|
||||
|
||||
fun <T> concatenate(vararg lists: List<T>): List<T> {
|
||||
return listOf(*lists).flatten()
|
||||
}
|
||||
|
||||
// TODO move to correct location
|
||||
fun Collection<Item>.toItemMediaList(): List<ItemMedia> {
|
||||
return this.items.map {
|
||||
ItemMedia(it.id, it.title, it.images.poster_wide[0][0].source)
|
||||
}
|
||||
}
|
||||
|
||||
@JvmName("toItemMediaListItem")
|
||||
fun List<Item>.toItemMediaList(): List<ItemMedia> {
|
||||
return this.map {
|
||||
ItemMedia(it.id, it.title, it.images.poster_wide[0][0].source)
|
||||
}
|
||||
}
|
||||
|
||||
@JvmName("toItemMediaListContinueWatchingItem")
|
||||
fun Collection<ContinueWatchingItem>.toItemMediaList(): List<ItemMedia> {
|
||||
return items.map {
|
||||
ItemMedia(it.panel.episodeMetadata.seriesId, it.panel.title, it.panel.images.thumbnail[0][0].source)
|
||||
}
|
||||
}
|
||||
|
||||
fun List<ContinueWatchingItem>.toItemMediaList(): List<ItemMedia> {
|
||||
return this.map {
|
||||
ItemMedia(it.panel.episodeMetadata.seriesId, it.panel.title, it.panel.images.thumbnail[0][0].source)
|
||||
}
|
||||
}
|
||||
|
||||
fun Locale.toDisplayString(fallback: String): String {
|
||||
return if (this.displayLanguage.isNotEmpty() && this.displayCountry.isNotEmpty()) {
|
||||
"${this.displayLanguage} (${this.displayCountry})"
|
||||
} else if (this.displayCountry.isNotEmpty()) {
|
||||
this.displayLanguage
|
||||
} else {
|
||||
fallback
|
||||
}
|
||||
}
|
||||
|
||||
fun hideBars(window: Window?, root: View) {
|
||||
if (window != null) {
|
||||
WindowCompat.setDecorFitsSystemWindows(window, false)
|
||||
WindowInsetsControllerCompat(window, root).let { controller ->
|
||||
controller.hide(WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.navigationBars())
|
||||
controller.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,9 @@ package org.mosad.teapod.util.adapter
|
||||
|
||||
import android.graphics.Color
|
||||
import android.graphics.drawable.ColorDrawable
|
||||
import android.graphics.drawable.Drawable
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
@ -11,42 +13,53 @@ import com.bumptech.glide.request.RequestOptions
|
||||
import jp.wasabeef.glide.transformations.RoundedCornersTransformation
|
||||
import org.mosad.teapod.R
|
||||
import org.mosad.teapod.databinding.ItemEpisodeBinding
|
||||
import org.mosad.teapod.util.Episode
|
||||
import org.mosad.teapod.databinding.ItemEpisodePlayerBinding
|
||||
import org.mosad.teapod.parser.crunchyroll.Episode
|
||||
import org.mosad.teapod.parser.crunchyroll.PlayheadObject
|
||||
import org.mosad.teapod.parser.crunchyroll.PlayheadsMap
|
||||
import org.mosad.teapod.util.tmdb.TMDBTVEpisode
|
||||
|
||||
class EpisodeItemAdapter(private val episodes: List<Episode>) : RecyclerView.Adapter<EpisodeItemAdapter.EpisodeViewHolder>() {
|
||||
class EpisodeItemAdapter(
|
||||
private val episodes: List<Episode>,
|
||||
private val tmdbEpisodes: List<TMDBTVEpisode>?,
|
||||
private val playheads: PlayheadsMap,
|
||||
private val onClickListener: OnClickListener,
|
||||
private val viewType: ViewType
|
||||
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
|
||||
|
||||
var onImageClick: ((String, Int) -> Unit)? = null
|
||||
var currentSelected: Int = -1 // -1, since position should never be < 0
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): EpisodeViewHolder {
|
||||
return EpisodeViewHolder(ItemEpisodeBinding.inflate(LayoutInflater.from(parent.context), parent, false))
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
|
||||
return when (viewType) {
|
||||
ViewType.PLAYER.ordinal -> {
|
||||
PlayerEpisodeViewHolder((ItemEpisodePlayerBinding.inflate(LayoutInflater.from(parent.context), parent, false)))
|
||||
}
|
||||
else -> {
|
||||
// media fragment episode list is default
|
||||
EpisodeViewHolder(ItemEpisodeBinding.inflate(LayoutInflater.from(parent.context), parent, false))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: EpisodeViewHolder, position: Int) {
|
||||
val context = holder.binding.root.context
|
||||
val ep = episodes[position]
|
||||
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
|
||||
val episode = episodes[position]
|
||||
val playhead = playheads[episode.id]
|
||||
val tmdbEpisode = tmdbEpisodes?.getOrNull(position)
|
||||
|
||||
val titleText = if (ep.hasDub()) {
|
||||
context.getString(R.string.component_episode_title, ep.number, ep.description)
|
||||
} else {
|
||||
context.getString(R.string.component_episode_title_sub, ep.number, ep.description)
|
||||
when (holder.itemViewType) {
|
||||
ViewType.MEDIA_FRAGMENT.ordinal -> {
|
||||
(holder as EpisodeViewHolder).bind(episode, playhead, tmdbEpisode)
|
||||
}
|
||||
ViewType.PLAYER.ordinal -> {
|
||||
(holder as PlayerEpisodeViewHolder).bind(episode, playhead, currentSelected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
holder.binding.textEpisodeTitle.text = titleText
|
||||
holder.binding.textEpisodeDesc.text = ep.shortDesc
|
||||
|
||||
if (episodes[position].posterUrl.isNotEmpty()) {
|
||||
Glide.with(context).load(ep.posterUrl)
|
||||
.apply(RequestOptions.placeholderOf(ColorDrawable(Color.DKGRAY)))
|
||||
.apply(RequestOptions.bitmapTransform(RoundedCornersTransformation(10, 0)))
|
||||
.into(holder.binding.imageEpisode)
|
||||
}
|
||||
|
||||
if (ep.watched) {
|
||||
holder.binding.imageWatched.setImageDrawable(
|
||||
ContextCompat.getDrawable(context, R.drawable.ic_baseline_check_circle_24)
|
||||
)
|
||||
} else {
|
||||
holder.binding.imageWatched.setImageDrawable(null)
|
||||
override fun getItemViewType(position: Int): Int {
|
||||
return when (viewType) {
|
||||
ViewType.MEDIA_FRAGMENT -> ViewType.MEDIA_FRAGMENT.ordinal
|
||||
ViewType.PLAYER -> ViewType.PLAYER.ordinal
|
||||
}
|
||||
}
|
||||
|
||||
@ -54,16 +67,113 @@ class EpisodeItemAdapter(private val episodes: List<Episode>) : RecyclerView.Ada
|
||||
return episodes.size
|
||||
}
|
||||
|
||||
fun updateWatchedState(watched: Boolean, position: Int) {
|
||||
// use getOrNull as there could be a index out of bound when running this in onResume()
|
||||
episodes.getOrNull(position)?.watched = watched
|
||||
}
|
||||
inner class EpisodeViewHolder(val binding: ItemEpisodeBinding) :
|
||||
RecyclerView.ViewHolder(binding.root) {
|
||||
|
||||
fun bind(episode: Episode, playhead: PlayheadObject?, tmdbEpisode: TMDBTVEpisode?) {
|
||||
val context = binding.root.context
|
||||
|
||||
val titleText = if (episode.episodeNumber != null) {
|
||||
// for tv shows add ep prefix and episode number
|
||||
if (episode.isDubbed) {
|
||||
context.getString(R.string.component_episode_title, episode.episode, episode.title)
|
||||
} else {
|
||||
context.getString(R.string.component_episode_title_sub, episode.episode, episode.title)
|
||||
}
|
||||
} else {
|
||||
episode.title
|
||||
}
|
||||
|
||||
binding.textEpisodeTitle.text = titleText
|
||||
binding.textEpisodeDesc.text = episode.description.ifEmpty {
|
||||
tmdbEpisode?.overview ?: ""
|
||||
}
|
||||
|
||||
if (episode.images.thumbnail[0][0].source.isNotEmpty()) {
|
||||
Glide.with(context).load(episode.images.thumbnail[0][0].source)
|
||||
.apply(RequestOptions.placeholderOf(ColorDrawable(Color.DKGRAY)))
|
||||
.apply(RequestOptions.bitmapTransform(RoundedCornersTransformation(10, 0)))
|
||||
.into(binding.imageEpisode)
|
||||
}
|
||||
|
||||
// add watched progress
|
||||
val playheadProgress = playhead?.playhead?.let {
|
||||
((it.toFloat() / (episode.durationMs / 1000)) * 100).toInt()
|
||||
} ?: 0
|
||||
binding.progressPlayhead.setProgressCompat(playheadProgress, false)
|
||||
binding.progressPlayhead.visibility = if (playheadProgress <= 0)
|
||||
View.GONE else View.VISIBLE
|
||||
|
||||
// add watched icon to episode, if the episode id is present in playheads and fullyWatched
|
||||
val watchedImage: Drawable? = if (playhead?.fullyWatched == true) {
|
||||
ContextCompat.getDrawable(context, R.drawable.ic_baseline_check_circle_24)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
binding.imageWatched.setImageDrawable(watchedImage)
|
||||
|
||||
inner class EpisodeViewHolder(val binding: ItemEpisodeBinding) : RecyclerView.ViewHolder(binding.root) {
|
||||
init {
|
||||
binding.imageEpisode.setOnClickListener {
|
||||
onImageClick?.invoke(episodes[adapterPosition].title, adapterPosition)
|
||||
onClickListener.onClick(episode)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inner class PlayerEpisodeViewHolder(val binding: ItemEpisodePlayerBinding) :
|
||||
RecyclerView.ViewHolder(binding.root) {
|
||||
|
||||
// -1, since position should never be < 0
|
||||
fun bind(episode: Episode, playhead: PlayheadObject?, currentSelected: Int) {
|
||||
val context = binding.root.context
|
||||
|
||||
val titleText = if (episode.episodeNumber != null) {
|
||||
// for tv shows add ep prefix and episode number
|
||||
if (episode.isDubbed) {
|
||||
context.getString(R.string.component_episode_title, episode.episode, episode.title)
|
||||
} else {
|
||||
context.getString(R.string.component_episode_title_sub, episode.episode, episode.title)
|
||||
}
|
||||
} else {
|
||||
episode.title
|
||||
}
|
||||
|
||||
binding.textEpisodeTitle2.text = titleText
|
||||
binding.textEpisodeDesc2.text = episode.description.ifEmpty { "" }
|
||||
|
||||
if (episode.images.thumbnail[0][0].source.isNotEmpty()) {
|
||||
Glide.with(context).load(episode.images.thumbnail[0][0].source)
|
||||
.apply(RequestOptions.bitmapTransform(RoundedCornersTransformation(10, 0)))
|
||||
.into(binding.imageEpisode)
|
||||
}
|
||||
|
||||
// add watched progress
|
||||
val playheadProgress = playhead?.playhead?.let {
|
||||
((it.toFloat() / (episode.durationMs / 1000)) * 100).toInt()
|
||||
} ?: 0
|
||||
binding.progressPlayhead.setProgressCompat(playheadProgress, false)
|
||||
binding.progressPlayhead.visibility = if (playheadProgress <= 0)
|
||||
View.GONE else View.VISIBLE
|
||||
|
||||
// hide the play icon, if it's the current episode
|
||||
binding.imageEpisodePlay.visibility = if (currentSelected == bindingAdapterPosition) {
|
||||
View.GONE
|
||||
} else {
|
||||
View.VISIBLE
|
||||
}
|
||||
|
||||
if (currentSelected != bindingAdapterPosition) {
|
||||
binding.imageEpisode.setOnClickListener {
|
||||
onClickListener.onClick(episode)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class OnClickListener(val clickListener: (episode: Episode) -> Unit) {
|
||||
fun onClick(episode: Episode) = clickListener(episode)
|
||||
}
|
||||
|
||||
enum class ViewType {
|
||||
MEDIA_FRAGMENT,
|
||||
PLAYER
|
||||
}
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
package org.mosad.teapod.util.adapter
|
||||
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.recyclerview.widget.DiffUtil
|
||||
import androidx.recyclerview.widget.ListAdapter
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.bumptech.glide.Glide
|
||||
import org.mosad.teapod.R
|
||||
import org.mosad.teapod.databinding.ItemMediaBinding
|
||||
import org.mosad.teapod.parser.crunchyroll.ContinueWatchingItem
|
||||
|
||||
class MediaEpisodeListAdapter(private val onClickListener: OnClickListener) : ListAdapter<ContinueWatchingItem, MediaEpisodeListAdapter.MediaViewHolder>(DiffCallback) {
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MediaViewHolder {
|
||||
return MediaViewHolder(
|
||||
ItemMediaBinding.inflate(
|
||||
LayoutInflater.from(parent.context),
|
||||
parent,
|
||||
false
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: MediaViewHolder, position: Int) {
|
||||
val item = getItem(position)
|
||||
holder.binding.root.setOnClickListener {
|
||||
onClickListener.onClick(item)
|
||||
}
|
||||
holder.bind(item)
|
||||
}
|
||||
|
||||
inner class MediaViewHolder(val binding: ItemMediaBinding) :
|
||||
RecyclerView.ViewHolder(binding.root) {
|
||||
|
||||
fun bind(item: ContinueWatchingItem) {
|
||||
val metadata = item.panel.episodeMetadata
|
||||
|
||||
binding.textTitle.text = binding.root.context.getString(R.string.season_episode_title,
|
||||
metadata.seasonNumber, metadata.episodeNumber, metadata.seriesTitle
|
||||
)
|
||||
|
||||
Glide.with(binding.imagePoster)
|
||||
.load(item.panel.images.thumbnail[0][0].source)
|
||||
.into(binding.imagePoster)
|
||||
|
||||
// add watched progress
|
||||
val playheadProgress = ((item.playhead.toFloat() / (metadata.durationMs / 1000)) * 100)
|
||||
.toInt()
|
||||
binding.progressPlayhead.setProgressCompat(playheadProgress, false)
|
||||
binding.progressPlayhead.visibility = if (playheadProgress <= 0)
|
||||
View.GONE else View.VISIBLE
|
||||
}
|
||||
}
|
||||
|
||||
companion object DiffCallback : DiffUtil.ItemCallback<ContinueWatchingItem>() {
|
||||
override fun areItemsTheSame(oldItem: ContinueWatchingItem, newItem: ContinueWatchingItem): Boolean {
|
||||
return oldItem.panel.id == newItem.panel.id
|
||||
}
|
||||
|
||||
override fun areContentsTheSame(oldItem: ContinueWatchingItem, newItem: ContinueWatchingItem): Boolean {
|
||||
return oldItem == newItem
|
||||
}
|
||||
}
|
||||
|
||||
class OnClickListener(val clickListener: (item: ContinueWatchingItem) -> Unit) {
|
||||
fun onClick(item: ContinueWatchingItem) = clickListener(item)
|
||||
}
|
||||
}
|
@ -2,19 +2,16 @@ package org.mosad.teapod.util.adapter
|
||||
|
||||
import android.view.LayoutInflater
|
||||
import android.view.ViewGroup
|
||||
import android.widget.Filter
|
||||
import android.widget.Filterable
|
||||
import androidx.core.view.isVisible
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.bumptech.glide.Glide
|
||||
import org.mosad.teapod.databinding.ItemMediaBinding
|
||||
import org.mosad.teapod.util.ItemMedia
|
||||
import java.util.*
|
||||
|
||||
class MediaItemAdapter(private val initMedia: List<ItemMedia>) : RecyclerView.Adapter<MediaItemAdapter.MediaViewHolder>(), Filterable {
|
||||
@Deprecated("Use MediaItemListAdapter instead")
|
||||
class MediaItemAdapter(private val items: List<ItemMedia>) : RecyclerView.Adapter<MediaItemAdapter.MediaViewHolder>() {
|
||||
|
||||
var onItemClick: ((Int, Int) -> Unit)? = null
|
||||
private val filter = MediaFilter()
|
||||
private var filteredMedia = initMedia.map { it.copy() }
|
||||
var onItemClick: ((id: String, position: Int) -> Unit)? = null
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MediaItemAdapter.MediaViewHolder {
|
||||
return MediaViewHolder(ItemMediaBinding.inflate(LayoutInflater.from(parent.context), parent, false))
|
||||
@ -22,58 +19,26 @@ class MediaItemAdapter(private val initMedia: List<ItemMedia>) : RecyclerView.Ad
|
||||
|
||||
override fun onBindViewHolder(holder: MediaItemAdapter.MediaViewHolder, position: Int) {
|
||||
holder.binding.root.apply {
|
||||
holder.binding.textTitle.text = filteredMedia[position].title
|
||||
Glide.with(context).load(filteredMedia[position].posterUrl).into(holder.binding.imagePoster)
|
||||
holder.binding.textTitle.text = items[position].title
|
||||
Glide.with(context).load(items[position].posterUrl).into(holder.binding.imagePoster)
|
||||
}
|
||||
}
|
||||
|
||||
override fun getItemCount(): Int {
|
||||
return filteredMedia.size
|
||||
return items.size
|
||||
}
|
||||
|
||||
override fun getFilter(): Filter {
|
||||
return filter
|
||||
}
|
||||
|
||||
fun updateMediaList(mediaList: List<ItemMedia>) {
|
||||
filteredMedia = mediaList
|
||||
}
|
||||
|
||||
inner class MediaViewHolder(val binding: ItemMediaBinding) : RecyclerView.ViewHolder(binding.root) {
|
||||
inner class MediaViewHolder(val binding: ItemMediaBinding) :
|
||||
RecyclerView.ViewHolder(binding.root) {
|
||||
init {
|
||||
binding.imageEpisodePlay.isVisible = false // hide the play button for media items
|
||||
binding.root.setOnClickListener {
|
||||
onItemClick?.invoke(filteredMedia[adapterPosition].id, adapterPosition)
|
||||
onItemClick?.invoke(
|
||||
items[bindingAdapterPosition].id,
|
||||
bindingAdapterPosition
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inner class MediaFilter : Filter() {
|
||||
override fun performFiltering(constraint: CharSequence?): FilterResults {
|
||||
val filterTerm = constraint.toString().lowercase(Locale.ROOT)
|
||||
val results = FilterResults()
|
||||
|
||||
val filteredList = if (filterTerm.isEmpty()) {
|
||||
initMedia
|
||||
} else {
|
||||
initMedia.filter {
|
||||
it.title.lowercase(Locale.ROOT).contains(filterTerm)
|
||||
}
|
||||
}
|
||||
|
||||
results.values = filteredList
|
||||
results.count = filteredList.size
|
||||
|
||||
return results
|
||||
}
|
||||
|
||||
@Suppress("unchecked_cast")
|
||||
/**
|
||||
* suppressing unchecked cast is safe, since we only use Media
|
||||
*/
|
||||
override fun publishResults(constraint: CharSequence?, results: FilterResults?) {
|
||||
filteredMedia = results?.values as List<ItemMedia>
|
||||
notifyDataSetChanged()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
package org.mosad.teapod.util.adapter
|
||||
|
||||
import android.view.LayoutInflater
|
||||
import android.view.ViewGroup
|
||||
import androidx.core.view.isVisible
|
||||
import androidx.recyclerview.widget.DiffUtil
|
||||
import androidx.recyclerview.widget.ListAdapter
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.bumptech.glide.Glide
|
||||
import org.mosad.teapod.databinding.ItemMediaBinding
|
||||
import org.mosad.teapod.util.ItemMedia
|
||||
|
||||
class MediaItemListAdapter(private val onClickListener: OnClickListener) : ListAdapter<ItemMedia, MediaItemListAdapter.MediaViewHolder>(DiffCallback) {
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MediaViewHolder {
|
||||
return MediaViewHolder(
|
||||
ItemMediaBinding.inflate(
|
||||
LayoutInflater.from(parent.context),
|
||||
parent,
|
||||
false
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: MediaViewHolder, position: Int) {
|
||||
val item = getItem(position)
|
||||
holder.binding.root.setOnClickListener {
|
||||
onClickListener.onClick(item)
|
||||
}
|
||||
holder.bind(item)
|
||||
}
|
||||
|
||||
inner class MediaViewHolder(val binding: ItemMediaBinding) :
|
||||
RecyclerView.ViewHolder(binding.root) {
|
||||
|
||||
fun bind(item: ItemMedia) {
|
||||
binding.textTitle.text = item.title
|
||||
|
||||
Glide.with(binding.imagePoster)
|
||||
.load(item.posterUrl)
|
||||
.into(binding.imagePoster)
|
||||
|
||||
binding.imageEpisodePlay.isVisible = false
|
||||
binding.progressPlayhead.isVisible = false
|
||||
}
|
||||
}
|
||||
|
||||
companion object DiffCallback : DiffUtil.ItemCallback<ItemMedia>() {
|
||||
override fun areItemsTheSame(oldItem: ItemMedia, newItem: ItemMedia): Boolean {
|
||||
return oldItem.id == newItem.id
|
||||
}
|
||||
|
||||
override fun areContentsTheSame(oldItem: ItemMedia, newItem: ItemMedia): Boolean {
|
||||
return oldItem == newItem
|
||||
}
|
||||
}
|
||||
|
||||
class OnClickListener(val clickListener: (item: ItemMedia) -> Unit) {
|
||||
fun onClick(item: ItemMedia) = clickListener(item)
|
||||
}
|
||||
}
|
@ -1,64 +0,0 @@
|
||||
package org.mosad.teapod.util.adapter
|
||||
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.bumptech.glide.Glide
|
||||
import com.bumptech.glide.request.RequestOptions
|
||||
import jp.wasabeef.glide.transformations.RoundedCornersTransformation
|
||||
import org.mosad.teapod.R
|
||||
import org.mosad.teapod.databinding.ItemEpisodePlayerBinding
|
||||
import org.mosad.teapod.util.Episode
|
||||
|
||||
class PlayerEpisodeItemAdapter(private val episodes: List<Episode>) : RecyclerView.Adapter<PlayerEpisodeItemAdapter.EpisodeViewHolder>() {
|
||||
|
||||
var onImageClick: ((String, Int) -> Unit)? = null
|
||||
var currentSelected: Int = -1 // -1, since position should never be < 0
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): EpisodeViewHolder {
|
||||
return EpisodeViewHolder(ItemEpisodePlayerBinding.inflate(LayoutInflater.from(parent.context), parent, false))
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: EpisodeViewHolder, position: Int) {
|
||||
val context = holder.binding.root.context
|
||||
val ep = episodes[position]
|
||||
|
||||
val titleText = if (ep.hasDub()) {
|
||||
context.getString(R.string.component_episode_title, ep.number, ep.description)
|
||||
} else {
|
||||
context.getString(R.string.component_episode_title_sub, ep.number, ep.description)
|
||||
}
|
||||
|
||||
holder.binding.textEpisodeTitle2.text = titleText
|
||||
holder.binding.textEpisodeDesc2.text = ep.shortDesc
|
||||
|
||||
if (episodes[position].posterUrl.isNotEmpty()) {
|
||||
Glide.with(context).load(ep.posterUrl)
|
||||
.apply(RequestOptions.bitmapTransform(RoundedCornersTransformation(10, 0)))
|
||||
.into(holder.binding.imageEpisode)
|
||||
}
|
||||
|
||||
// hide the play icon, if it's the current episode
|
||||
|