From 5e486d47940d787b9a42b37d143cdd9cf7275208 Mon Sep 17 00:00:00 2001 From: antonio Date: Sun, 17 Sep 2023 16:38:08 +0200 Subject: [PATCH] feat: implemented client, service, and repository for sharing APIs --- .../tempo/repository/SharingRepository.java | 97 +++++++++++++++++++ .../tempo/subsonic/Subsonic.java | 9 ++ .../subsonic/api/sharing/SharingClient.java | 41 ++++++++ .../subsonic/api/sharing/SharingService.java | 24 +++++ .../tempo/subsonic/models/Share.kt | 7 +- .../tempo/subsonic/models/Shares.kt | 2 + 6 files changed, 179 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/com/cappielloantonio/tempo/repository/SharingRepository.java create mode 100644 app/src/main/java/com/cappielloantonio/tempo/subsonic/api/sharing/SharingClient.java create mode 100644 app/src/main/java/com/cappielloantonio/tempo/subsonic/api/sharing/SharingService.java diff --git a/app/src/main/java/com/cappielloantonio/tempo/repository/SharingRepository.java b/app/src/main/java/com/cappielloantonio/tempo/repository/SharingRepository.java new file mode 100644 index 00000000..5c5edd04 --- /dev/null +++ b/app/src/main/java/com/cappielloantonio/tempo/repository/SharingRepository.java @@ -0,0 +1,97 @@ +package com.cappielloantonio.tempo.repository; + +import androidx.annotation.NonNull; +import androidx.lifecycle.MutableLiveData; + +import com.cappielloantonio.tempo.App; +import com.cappielloantonio.tempo.subsonic.base.ApiResponse; +import com.cappielloantonio.tempo.subsonic.models.Share; + +import java.util.ArrayList; +import java.util.List; + +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; + +public class SharingRepository { + public MutableLiveData> getShares() { + MutableLiveData> shares = new MutableLiveData<>(new ArrayList<>()); + + App.getSubsonicClientInstance(false) + .getSharingClient() + .getShares() + .enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if (response.isSuccessful() && response.body() != null && response.body().getSubsonicResponse().getShares() != null && response.body().getSubsonicResponse().getShares().getShares() != null) { + shares.setValue(response.body().getSubsonicResponse().getShares().getShares()); + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + + } + }); + + return shares; + } + + public MutableLiveData createShare(String id, String description, Long expires) { + MutableLiveData share = new MutableLiveData<>(); + + App.getSubsonicClientInstance(false) + .getSharingClient() + .createShare(id, description, expires) + .enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if (response.isSuccessful() && response.body() != null && response.body().getSubsonicResponse().getShares() != null && response.body().getSubsonicResponse().getShares().getShares() != null && response.body().getSubsonicResponse().getShares().getShares().get(0) != null) { + share.setValue(response.body().getSubsonicResponse().getShares().getShares().get(0)); + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + + } + }); + + return share; + } + + public void updateShare(String id, String description, Long expires) { + App.getSubsonicClientInstance(false) + .getSharingClient() + .updateShare(id, description, expires) + .enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + + } + }); + } + + public void deleteShare(String id) { + App.getSubsonicClientInstance(false) + .getSharingClient() + .deleteShare(id) + .enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + + } + }); + } +} diff --git a/app/src/main/java/com/cappielloantonio/tempo/subsonic/Subsonic.java b/app/src/main/java/com/cappielloantonio/tempo/subsonic/Subsonic.java index e3d90a82..c401bca7 100644 --- a/app/src/main/java/com/cappielloantonio/tempo/subsonic/Subsonic.java +++ b/app/src/main/java/com/cappielloantonio/tempo/subsonic/Subsonic.java @@ -10,6 +10,7 @@ import com.cappielloantonio.tempo.subsonic.api.mediaretrieval.MediaRetrievalClie import com.cappielloantonio.tempo.subsonic.api.playlist.PlaylistClient; import com.cappielloantonio.tempo.subsonic.api.podcast.PodcastClient; import com.cappielloantonio.tempo.subsonic.api.searching.SearchingClient; +import com.cappielloantonio.tempo.subsonic.api.sharing.SharingClient; import com.cappielloantonio.tempo.subsonic.api.system.SystemClient; import com.cappielloantonio.tempo.subsonic.base.Version; @@ -33,6 +34,7 @@ public class Subsonic { private MediaLibraryScanningClient mediaLibraryScanningClient; private BookmarksClient bookmarksClient; private InternetRadioClient internetRadioClient; + private SharingClient sharingClient; public Subsonic(SubsonicPreferences preferences) { this.preferences = preferences; @@ -119,6 +121,13 @@ public class Subsonic { return internetRadioClient; } + public SharingClient getSharingClient() { + if (sharingClient == null) { + sharingClient = new SharingClient(this); + } + return sharingClient; + } + public String getUrl() { String url = preferences.getServerUrl() + "/rest/"; return url.replace("//rest", "/rest"); diff --git a/app/src/main/java/com/cappielloantonio/tempo/subsonic/api/sharing/SharingClient.java b/app/src/main/java/com/cappielloantonio/tempo/subsonic/api/sharing/SharingClient.java new file mode 100644 index 00000000..a4060361 --- /dev/null +++ b/app/src/main/java/com/cappielloantonio/tempo/subsonic/api/sharing/SharingClient.java @@ -0,0 +1,41 @@ +package com.cappielloantonio.tempo.subsonic.api.sharing; + +import android.util.Log; + +import com.cappielloantonio.tempo.subsonic.RetrofitClient; +import com.cappielloantonio.tempo.subsonic.Subsonic; +import com.cappielloantonio.tempo.subsonic.base.ApiResponse; + +import retrofit2.Call; + +public class SharingClient { + private static final String TAG = "BrowsingClient"; + + private final Subsonic subsonic; + private final SharingService sharingService; + + public SharingClient(Subsonic subsonic) { + this.subsonic = subsonic; + this.sharingService = new RetrofitClient(subsonic).getRetrofit().create(SharingService.class); + } + + public Call getShares() { + Log.d(TAG, "getShares()"); + return sharingService.getShares(subsonic.getParams()); + } + + public Call createShare(String id, String description, Long expires) { + Log.d(TAG, "createShare()"); + return sharingService.createShare(subsonic.getParams(), id, description, expires); + } + + public Call updateShare(String id, String description, Long expires) { + Log.d(TAG, "updateShare()"); + return sharingService.updateShare(subsonic.getParams(), id, description, expires); + } + + public Call deleteShare(String id) { + Log.d(TAG, "deleteShare()"); + return sharingService.deleteShare(subsonic.getParams(), id); + } +} diff --git a/app/src/main/java/com/cappielloantonio/tempo/subsonic/api/sharing/SharingService.java b/app/src/main/java/com/cappielloantonio/tempo/subsonic/api/sharing/SharingService.java new file mode 100644 index 00000000..0a18f33c --- /dev/null +++ b/app/src/main/java/com/cappielloantonio/tempo/subsonic/api/sharing/SharingService.java @@ -0,0 +1,24 @@ +package com.cappielloantonio.tempo.subsonic.api.sharing; + +import com.cappielloantonio.tempo.subsonic.base.ApiResponse; + +import java.util.Map; + +import retrofit2.Call; +import retrofit2.http.GET; +import retrofit2.http.Query; +import retrofit2.http.QueryMap; + +public interface SharingService { + @GET("getShares") + Call getShares(@QueryMap Map params); + + @GET("createShare") + Call createShare(@QueryMap Map params, @Query("id") String id, @Query("description") String description, @Query("expires") Long expires); + + @GET("updateShare") + Call updateShare(@QueryMap Map params, @Query("id") String id, @Query("description") String description, @Query("expires") Long expires); + + @GET("deleteShare") + Call deleteShare(@QueryMap Map params, @Query("id") String id); +} diff --git a/app/src/main/java/com/cappielloantonio/tempo/subsonic/models/Share.kt b/app/src/main/java/com/cappielloantonio/tempo/subsonic/models/Share.kt index 1c5d7d0a..193cefc8 100644 --- a/app/src/main/java/com/cappielloantonio/tempo/subsonic/models/Share.kt +++ b/app/src/main/java/com/cappielloantonio/tempo/subsonic/models/Share.kt @@ -1,10 +1,15 @@ package com.cappielloantonio.tempo.subsonic.models +import android.os.Parcelable import androidx.annotation.Keep +import com.google.gson.annotations.SerializedName +import kotlinx.parcelize.Parcelize import java.util.* @Keep -class Share { +@Parcelize +class Share : Parcelable { + @SerializedName("entry") var entries: List? = null var id: String? = null var url: String? = null diff --git a/app/src/main/java/com/cappielloantonio/tempo/subsonic/models/Shares.kt b/app/src/main/java/com/cappielloantonio/tempo/subsonic/models/Shares.kt index 78e79ec1..4c0f30ea 100644 --- a/app/src/main/java/com/cappielloantonio/tempo/subsonic/models/Shares.kt +++ b/app/src/main/java/com/cappielloantonio/tempo/subsonic/models/Shares.kt @@ -1,8 +1,10 @@ package com.cappielloantonio.tempo.subsonic.models import androidx.annotation.Keep +import com.google.gson.annotations.SerializedName @Keep class Shares { + @SerializedName("share") var shares: List? = null } \ No newline at end of file