From 25d3803e6923f87b02388ebfa0c78115208e6deb Mon Sep 17 00:00:00 2001 From: Thomas Date: Sat, 28 Jan 2023 16:26:55 +0100 Subject: [PATCH] Fix peertube support --- .../android/mastodon/helper/Helper.java | 25 ++++++++++++++++--- .../peertube/client/RetrofitPeertubeAPI.java | 15 ++++++++++- .../peertube/client/entities/UserMe.java | 8 +++--- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/app/fedilab/android/mastodon/helper/Helper.java b/app/src/main/java/app/fedilab/android/mastodon/helper/Helper.java index 16cda221..5ada9c46 100644 --- a/app/src/main/java/app/fedilab/android/mastodon/helper/Helper.java +++ b/app/src/main/java/app/fedilab/android/mastodon/helper/Helper.java @@ -18,6 +18,7 @@ import static android.content.Context.DOWNLOAD_SERVICE; import static app.fedilab.android.BaseMainActivity.currentAccount; import static app.fedilab.android.mastodon.activities.BaseActivity.currentThemeId; import static app.fedilab.android.mastodon.helper.LogoHelper.getNotificationIcon; +import static app.fedilab.android.mastodon.helper.ThemeHelper.fetchAccentColor; import android.annotation.SuppressLint; import android.app.Activity; @@ -42,6 +43,7 @@ import android.graphics.Color; import android.graphics.Paint; import android.graphics.PorterDuff; import android.graphics.PorterDuffColorFilter; +import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.media.RingtoneManager; import android.net.ConnectivityManager; @@ -89,6 +91,7 @@ import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.preference.PreferenceManager; import androidx.recyclerview.widget.LinearLayoutManager; +import com.avatarfirst.avatargenlib.AvatarGenerator; import com.bumptech.glide.Glide; import com.bumptech.glide.RequestBuilder; import com.bumptech.glide.load.resource.bitmap.CenterCrop; @@ -1062,9 +1065,25 @@ public class Helper { if (account.mastodon_account != null) { targetedUrl = disableGif ? account.mastodon_account.avatar_static : account.mastodon_account.avatar; } else if (account.peertube_account != null) { - targetedUrl = account.peertube_account.getAvatar().getPath(); - if (targetedUrl != null && targetedUrl.startsWith("/")) { - targetedUrl = "https://" + account.instance + account.peertube_account.getAvatar().getPath(); + if (account.peertube_account.getAvatar() != null) { + targetedUrl = account.peertube_account.getAvatar().getPath(); + if (targetedUrl != null && targetedUrl.startsWith("/")) { + targetedUrl = "https://" + account.instance + account.peertube_account.getAvatar().getPath(); + } + } else { + BitmapDrawable avatar = new AvatarGenerator.AvatarBuilder(activity) + .setLabel(account.peertube_account.getAcct()) + .setAvatarSize(120) + .setTextSize(30) + .toSquare() + .setBackgroundColor(fetchAccentColor(activity)) + .build(); + Glide.with(activity) + .asDrawable() + .load(avatar) + .apply(new RequestOptions().transform(new CenterCrop(), new RoundedCorners(10))) + .into(view); + return; } } diff --git a/app/src/main/java/app/fedilab/android/peertube/client/RetrofitPeertubeAPI.java b/app/src/main/java/app/fedilab/android/peertube/client/RetrofitPeertubeAPI.java index 9de1ee43..6476bdac 100644 --- a/app/src/main/java/app/fedilab/android/peertube/client/RetrofitPeertubeAPI.java +++ b/app/src/main/java/app/fedilab/android/peertube/client/RetrofitPeertubeAPI.java @@ -33,6 +33,7 @@ import android.webkit.URLUtil; import androidx.documentfile.provider.DocumentFile; import androidx.preference.PreferenceManager; +import com.google.android.material.button.MaterialButton; import com.google.gson.JsonSyntaxException; import org.jetbrains.annotations.NotNull; @@ -94,6 +95,7 @@ import app.fedilab.android.peertube.viewmodel.CommentVM; import app.fedilab.android.peertube.viewmodel.PlaylistsVM; import app.fedilab.android.peertube.viewmodel.TimelineVM; import app.fedilab.android.sqlite.Sqlite; +import es.dmoral.toasty.Toasty; import okhttp3.MediaType; import okhttp3.MultipartBody; import okhttp3.OkHttpClient; @@ -158,7 +160,18 @@ public class RetrofitPeertubeAPI { String instance = host; try { UserMe userMe = new RetrofitPeertubeAPI(activity, instance, token).verifyCredentials(); - + if (userMe == null) { + Handler mainHandler = new Handler(Looper.getMainLooper()); + Runnable myRunnable = () -> { + MaterialButton login = activity.findViewById(R.id.login_button); + if (login != null) { + login.setEnabled(true); + } + Toasty.error(activity, activity.getString(R.string.toast_error_peertube_not_supported), Toasty.LENGTH_SHORT).show(); + }; + mainHandler.post(myRunnable); + return; + } peertubeAccount = userMe.getAccount(); } catch (Error error) { Error.displayError(activity, error); diff --git a/app/src/main/java/app/fedilab/android/peertube/client/entities/UserMe.java b/app/src/main/java/app/fedilab/android/peertube/client/entities/UserMe.java index 98f6c1d0..296e8a7d 100644 --- a/app/src/main/java/app/fedilab/android/peertube/client/entities/UserMe.java +++ b/app/src/main/java/app/fedilab/android/peertube/client/entities/UserMe.java @@ -55,8 +55,8 @@ public class UserMe { private NotificationSettings notificationSettings; @SerializedName("nsfwPolicy") private String nsfwPolicy; - @SerializedName("role") - private Role role; + /* @SerializedName("role") + private Role role;*/ @SerializedName("roleLabel") private String roleLabel; @SerializedName("username") @@ -74,13 +74,13 @@ public class UserMe { @SerializedName("webTorrentEnabled") private boolean webTorrentEnabled; - public Role getRole() { + /* public Role getRole() { return role; } public void setRole(Role role) { this.role = role; - } + }*/ public PeertubeAccount getAccount() { return account;