From c49b081984f1cfcba68901638758bdf926a10ab4 Mon Sep 17 00:00:00 2001 From: Nicolas Pomepuy Date: Mon, 4 Nov 2024 08:58:41 +0100 Subject: [PATCH] Save and display API limits --- .../resources/opensubtitles/Models.kt | 4 +- .../opensubtitles/OpenSubtitleService.kt | 9 +- .../opensubtitles/OpenSubtitlesLimit.kt | 42 ++++++++++ .../opensubtitles/OpenSubtitlesUser.kt | 22 ----- .../opensubtitles/OpenSubtitlesUtils.kt | 83 +++++++++++++++++++ .../main/java/org/videolan/tools/Settings.kt | 1 + .../res/layout/subtitle_downloader_dialog.xml | 8 ++ .../SubtitleDownloaderDialogFragment.kt | 15 +++- .../videolan/vlc/viewmodels/SubtitlesModel.kt | 6 +- 9 files changed, 160 insertions(+), 30 deletions(-) create mode 100644 application/resources/src/main/java/org/videolan/resources/opensubtitles/OpenSubtitlesLimit.kt create mode 100644 application/resources/src/main/java/org/videolan/resources/opensubtitles/OpenSubtitlesUtils.kt diff --git a/application/resources/src/main/java/org/videolan/resources/opensubtitles/Models.kt b/application/resources/src/main/java/org/videolan/resources/opensubtitles/Models.kt index cfc4ab987..c2da1b3cb 100644 --- a/application/resources/src/main/java/org/videolan/resources/opensubtitles/Models.kt +++ b/application/resources/src/main/java/org/videolan/resources/opensubtitles/Models.kt @@ -1,7 +1,7 @@ package org.videolan.resources.opensubtitles import com.squareup.moshi.Json -import com.squareup.moshi.JsonClass +import java.util.Date data class OpenSubV1( @field:Json(name = "data") @@ -156,7 +156,7 @@ data class DownloadLink( @field:Json(name = "reset_time") val resetTime: String, @field:Json(name = "reset_time_utc") - val resetTimeUtc: String + val resetTimeUtc: Date ) data class OpenSubtitleAccount( diff --git a/application/resources/src/main/java/org/videolan/resources/opensubtitles/OpenSubtitleService.kt b/application/resources/src/main/java/org/videolan/resources/opensubtitles/OpenSubtitleService.kt index ba60e8624..59e0ba3d8 100644 --- a/application/resources/src/main/java/org/videolan/resources/opensubtitles/OpenSubtitleService.kt +++ b/application/resources/src/main/java/org/videolan/resources/opensubtitles/OpenSubtitleService.kt @@ -3,6 +3,8 @@ package org.videolan.resources.opensubtitles import android.util.Log import com.moczul.ok2curl.CurlInterceptor import com.moczul.ok2curl.logger.Logger +import com.squareup.moshi.Moshi +import com.squareup.moshi.adapters.Rfc3339DateJsonAdapter import okhttp3.Interceptor import okhttp3.OkHttpClient import okhttp3.Request @@ -13,6 +15,7 @@ import org.videolan.resources.BuildConfig import org.videolan.resources.util.ConnectivityInterceptor import retrofit2.Retrofit import retrofit2.converter.moshi.MoshiConverterFactory +import java.util.Date import java.util.concurrent.TimeUnit @@ -35,7 +38,11 @@ private fun buildClient() = Retrofit.Builder() .readTimeout(10, TimeUnit.SECONDS) .connectTimeout(5, TimeUnit.SECONDS) .build()) - .addConverterFactory(MoshiConverterFactory.create()) + .addConverterFactory(MoshiConverterFactory.create( + Moshi.Builder() + .add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe()) + .build() + )) .build() .create(IOpenSubtitleService::class.java) diff --git a/application/resources/src/main/java/org/videolan/resources/opensubtitles/OpenSubtitlesLimit.kt b/application/resources/src/main/java/org/videolan/resources/opensubtitles/OpenSubtitlesLimit.kt new file mode 100644 index 000000000..054c9da3b --- /dev/null +++ b/application/resources/src/main/java/org/videolan/resources/opensubtitles/OpenSubtitlesLimit.kt @@ -0,0 +1,42 @@ +/* + * ************************************************************************ + * OpenSubtitlesLimit.kt + * ************************************************************************* + * Copyright © 2024 VLC authors and VideoLAN + * Author: Nicolas POMEPUY + * 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 2 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 main.java.org.videolan.resources.opensubtitles + +import java.util.Date + +data class OpenSubtitlesLimit ( + val requests: Int = 0, + val max: Int = 5, + val resetTime: Date? = null +) { + private fun getRemaining(): Int { + if (resetTime != null && Date().after(resetTime)) return max + return max - requests + } + fun getRemainingText(): String { + val remaining = getRemaining() + return "$remaining/$max" + } +} \ No newline at end of file diff --git a/application/resources/src/main/java/org/videolan/resources/opensubtitles/OpenSubtitlesUser.kt b/application/resources/src/main/java/org/videolan/resources/opensubtitles/OpenSubtitlesUser.kt index 60c2faaf3..83130e5f6 100644 --- a/application/resources/src/main/java/org/videolan/resources/opensubtitles/OpenSubtitlesUser.kt +++ b/application/resources/src/main/java/org/videolan/resources/opensubtitles/OpenSubtitlesUser.kt @@ -39,26 +39,4 @@ data class OpenSubtitlesUser( val errorMessage: String? = null ) { fun isVip() = account?.user?.vip ?: false -} - -object OpenSubtitlesUserUtil { - fun get(settings: SharedPreferences): OpenSubtitlesUser { - settings.getString(KEY_OPEN_SUBTITLES_USER, "")?.let { userString -> - val moshi = Moshi.Builder().build() - val type = Types.newParameterizedType(OpenSubtitlesUser::class.java) - val jsonAdapter: JsonAdapter = moshi.adapter(type) - return try { - jsonAdapter.fromJson(userString) ?: OpenSubtitlesUser() - } catch (e: Exception) { - OpenSubtitlesUser() - } - } - return OpenSubtitlesUser() - } - - fun save(settings: SharedPreferences, user: OpenSubtitlesUser) { - val moshi = Moshi.Builder().build() - val jsonAdapter = moshi.adapter(OpenSubtitlesUser::class.java) - settings.putSingle(KEY_OPEN_SUBTITLES_USER, jsonAdapter.toJson(user)) - } } \ No newline at end of file diff --git a/application/resources/src/main/java/org/videolan/resources/opensubtitles/OpenSubtitlesUtils.kt b/application/resources/src/main/java/org/videolan/resources/opensubtitles/OpenSubtitlesUtils.kt new file mode 100644 index 000000000..7cb326cff --- /dev/null +++ b/application/resources/src/main/java/org/videolan/resources/opensubtitles/OpenSubtitlesUtils.kt @@ -0,0 +1,83 @@ +/* + * ************************************************************************ + * OpenSubtitlesUtils.kt + * ************************************************************************* + * Copyright © 2024 VLC authors and VideoLAN + * Author: Nicolas POMEPUY + * 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 2 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 main.java.org.videolan.resources.opensubtitles + +import android.content.SharedPreferences +import com.squareup.moshi.JsonAdapter +import com.squareup.moshi.Moshi +import com.squareup.moshi.adapters.Rfc3339DateJsonAdapter +import org.videolan.tools.KEY_OPEN_SUBTITLES_LIMIT +import org.videolan.tools.KEY_OPEN_SUBTITLES_USER +import org.videolan.tools.putSingle +import java.util.Date + +object OpenSubtitlesUtils { + fun getUser(settings: SharedPreferences): OpenSubtitlesUser { + settings.getString(KEY_OPEN_SUBTITLES_USER, "")?.let { userString -> + val jsonAdapter = getUserAdapter() + return try { + jsonAdapter.fromJson(userString) ?: OpenSubtitlesUser() + } catch (e: Exception) { + OpenSubtitlesUser() + } + } + return OpenSubtitlesUser() + } + + fun getLimit(settings: SharedPreferences): OpenSubtitlesLimit { + settings.getString(KEY_OPEN_SUBTITLES_LIMIT, "")?.let { limitString -> + val jsonAdapter = getLimitAdapter() + return try { + jsonAdapter.fromJson(limitString) ?: OpenSubtitlesLimit() + } catch (e: Exception) { + OpenSubtitlesLimit() + } + } + return OpenSubtitlesLimit() + } + + fun saveUser(settings: SharedPreferences, user: OpenSubtitlesUser) { + val jsonAdapter = getUserAdapter() + settings.putSingle(KEY_OPEN_SUBTITLES_USER, jsonAdapter.toJson(user)) + } + + private fun getUserAdapter(): JsonAdapter { + val moshi = Moshi.Builder().build() + val jsonAdapter = moshi.adapter(OpenSubtitlesUser::class.java) + return jsonAdapter + } + + fun saveLimit(settings: SharedPreferences, limit: OpenSubtitlesLimit) { + val jsonAdapter = getLimitAdapter() + settings.putSingle(KEY_OPEN_SUBTITLES_LIMIT, jsonAdapter.toJson(limit)) + } + + private fun getLimitAdapter(): JsonAdapter { + val moshi = + Moshi.Builder().add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe()).build() + val jsonAdapter = moshi.adapter(OpenSubtitlesLimit::class.java) + return jsonAdapter + } +} \ No newline at end of file diff --git a/application/tools/src/main/java/org/videolan/tools/Settings.kt b/application/tools/src/main/java/org/videolan/tools/Settings.kt index d3bc10c46..9e84ff5f8 100644 --- a/application/tools/src/main/java/org/videolan/tools/Settings.kt +++ b/application/tools/src/main/java/org/videolan/tools/Settings.kt @@ -248,6 +248,7 @@ const val WIDGETS_PREVIEW_PLAYING = "widgets_preview_playing" //OpenSubtitles const val KEY_OPEN_SUBTITLES_USER = "open_subtitles_user" +const val KEY_OPEN_SUBTITLES_LIMIT = "open_subtitles_limit" const val KEY_SAFE_MODE_PIN = "safe_mode_pin" diff --git a/application/vlc-android/res/layout/subtitle_downloader_dialog.xml b/application/vlc-android/res/layout/subtitle_downloader_dialog.xml index 147825af3..1c52bec7b 100644 --- a/application/vlc-android/res/layout/subtitle_downloader_dialog.xml +++ b/application/vlc-android/res/layout/subtitle_downloader_dialog.xml @@ -49,6 +49,14 @@ app:layout_constraintTop_toTopOf="parent" tools:text="Game of Thrones - S01e01" /> + + >() val observableSearchHearingImpaired = ObservableField() val observableUser = ObservableField() + val observableLimit = ObservableField() private var previousSearchLanguage: List? = null val manualSearchEnabled = ObservableBoolean(false) @@ -272,7 +274,7 @@ class SubtitlesModel(private val context: Context, private val mediaUri: Uri, pr val userResult = call.body() if (userResult != null) { val openSubtitlesUser = OpenSubtitlesUser(true, userResult, username = username) - OpenSubtitlesUserUtil.save(settings, openSubtitlesUser) + OpenSubtitlesUtils.saveUser(settings, openSubtitlesUser) observableUser.set(openSubtitlesUser) return@withContext }