Disable downvote button when downvotes are disable on the account's instance

This commit hides the downvote button from account where the instance disabled the downvote functionality.

Note: You need to switch accounts to take effect!
This commit is contained in:
Bazsalanszky 2023-08-05 17:09:48 +02:00
parent 279820c1be
commit 6cf69ee26d
11 changed files with 197 additions and 12 deletions

View File

@ -38,7 +38,7 @@ import eu.toldi.infinityforlemmy.user.UserData;
@Database(entities = {Account.class, SubredditData.class, SubscribedSubredditData.class, UserData.class,
SubscribedUserData.class, MultiReddit.class, CustomTheme.class, RecentSearchQuery.class,
ReadPost.class, PostFilter.class, PostFilterUsage.class, AnonymousMultiredditSubreddit.class}, version = 23)
ReadPost.class, PostFilter.class, PostFilterUsage.class, AnonymousMultiredditSubreddit.class}, version = 24)
public abstract class RedditDataRoomDatabase extends RoomDatabase {
public static RedditDataRoomDatabase create(final Context context) {
@ -49,7 +49,7 @@ public abstract class RedditDataRoomDatabase extends RoomDatabase {
MIGRATION_9_10, MIGRATION_10_11, MIGRATION_11_12, MIGRATION_12_13,
MIGRATION_13_14, MIGRATION_14_15, MIGRATION_15_16, MIGRATION_16_17,
MIGRATION_17_18, MIGRATION_18_19, MIGRATION_19_20, MIGRATION_20_21,
MIGRATION_21_22, MIGRATION_22_23)
MIGRATION_21_22, MIGRATION_22_23, MIGRATION_23_24)
.build();
}
@ -383,4 +383,11 @@ public abstract class RedditDataRoomDatabase extends RoomDatabase {
}
}
};
private static final Migration MIGRATION_23_24 = new Migration(23, 24) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {
database.execSQL("ALTER TABLE accounts ADD COLUMN can_downvote INTEGER DEFAULT 1 NOT NULL");
}
};
}

View File

@ -32,6 +32,9 @@ public class Account implements Parcelable {
@ColumnInfo(name = "instance_url")
private String instance_url;
@ColumnInfo(name = "can_downvote")
private boolean canDownvote = true;
@Ignore
protected Account(Parcel in) {
accountName = in.readString();
@ -42,6 +45,7 @@ public class Account implements Parcelable {
code = in.readString();
isCurrentUser = in.readByte() != 0;
instance_url = in.readString();
canDownvote = in.readByte() != 0;
}
public static final Creator<Account> CREATOR = new Creator<Account>() {
@ -58,11 +62,11 @@ public class Account implements Parcelable {
@Ignore
public static Account getAnonymousAccount() {
return new Account("-",null, null, null, null, null, false,null);
return new Account("-",null, null, null, null, null, false,null,true);
}
public Account(@NonNull String accountName, String display_name, String accessToken, String code,
String profileImageUrl, String bannerImageUrl, boolean isCurrentUser,String instance_url) {
String profileImageUrl, String bannerImageUrl, boolean isCurrentUser,String instance_url, boolean canDownvote) {
this.accountName = accountName;
this.display_name = display_name;
this.accessToken = accessToken;
@ -71,6 +75,7 @@ public class Account implements Parcelable {
this.bannerImageUrl = bannerImageUrl;
this.isCurrentUser = isCurrentUser;
this.instance_url = instance_url;
this.canDownvote = canDownvote;
}
@NonNull
@ -116,6 +121,10 @@ public class Account implements Parcelable {
return instance_url;
}
public boolean canDownvote() {
return canDownvote;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(accountName);
@ -126,5 +135,6 @@ public class Account implements Parcelable {
dest.writeString(code);
dest.writeByte((byte) (isCurrentUser ? 1 : 0));
dest.writeString(instance_url);
dest.writeByte((byte) (canDownvote ? 1 : 0));
}
}

View File

@ -43,6 +43,8 @@ import eu.toldi.infinityforlemmy.asynctasks.ParseAndInsertNewAccount;
import eu.toldi.infinityforlemmy.customtheme.CustomThemeWrapper;
import eu.toldi.infinityforlemmy.customviews.slidr.Slidr;
import eu.toldi.infinityforlemmy.dto.AccountLoginDTO;
import eu.toldi.infinityforlemmy.site.FetchSiteInfo;
import eu.toldi.infinityforlemmy.site.SiteInfo;
import eu.toldi.infinityforlemmy.utils.SharedPreferencesUtils;
import eu.toldi.infinityforlemmy.utils.Utils;
import retrofit2.Call;
@ -176,17 +178,35 @@ public class LoginActivity extends BaseActivity {
accessToken, new FetchMyInfo.FetchMyInfoListener() {
@Override
public void onFetchMyInfoSuccess(String name, String display_name, String profileImageUrl, String bannerImageUrl) {
FetchSiteInfo.fetchSiteInfo(mRetrofit.getRetrofit(), accessToken, new FetchSiteInfo.FetchSiteInfoListener() {
@Override
public void onFetchSiteInfoSuccess(SiteInfo siteInfo) {
boolean canDownvote = siteInfo.isEnable_downvotes();
ParseAndInsertNewAccount.parseAndInsertNewAccount(mExecutor, new Handler(), name,display_name, accessToken, profileImageUrl, bannerImageUrl, authCode,instance,canDownvote, mRedditDataRoomDatabase.accountDao(),
() -> {
Intent resultIntent = new Intent();
setResult(Activity.RESULT_OK, resultIntent);
finish();
});
mCurrentAccountSharedPreferences.edit().putBoolean(SharedPreferencesUtils.CAN_DOWNVOTE, canDownvote).apply();
}
@Override
public void onFetchSiteInfoFailed() {
ParseAndInsertNewAccount.parseAndInsertNewAccount(mExecutor, new Handler(), name,display_name, accessToken, profileImageUrl, bannerImageUrl, authCode,instance,true, mRedditDataRoomDatabase.accountDao(),
() -> {
Intent resultIntent = new Intent();
setResult(Activity.RESULT_OK, resultIntent);
finish();
});
mCurrentAccountSharedPreferences.edit().putBoolean(SharedPreferencesUtils.CAN_DOWNVOTE, true).apply();
}
});
mCurrentAccountSharedPreferences.edit().putString(SharedPreferencesUtils.ACCESS_TOKEN, accessToken)
.putString(SharedPreferencesUtils.ACCOUNT_NAME, display_name)
.putString(SharedPreferencesUtils.ACCOUNT_QUALIFIED_NAME, name)
.putString(SharedPreferencesUtils.ACCOUNT_INSTANCE,instance)
.putString(SharedPreferencesUtils.ACCOUNT_IMAGE_URL, profileImageUrl).apply();
ParseAndInsertNewAccount.parseAndInsertNewAccount(mExecutor, new Handler(), name,display_name, accessToken, profileImageUrl, bannerImageUrl, authCode,instance, mRedditDataRoomDatabase.accountDao(),
() -> {
Intent resultIntent = new Intent();
setResult(Activity.RESULT_OK, resultIntent);
finish();
});
}
@Override

View File

@ -1219,6 +1219,10 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter<Recycler
this.mSaveButton = mSaveButton;
this.mShareButton = mShareButton;
if(!mCurrentAccountSharedPreferences.getBoolean(SharedPreferencesUtils.CAN_DOWNVOTE,true)){
mDownvoteButton.setVisibility(View.GONE);
}
mIconGifImageView.setOnClickListener(view -> mSubredditTextView.performClick());
mSubredditTextView.setOnClickListener(view -> {

View File

@ -2370,6 +2370,9 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter<Post, RecyclerVie
this.shareButton = shareButton;
scoreTextView.setOnClickListener(null);
if(!mCurrentAccountSharedPreferences.getBoolean(SharedPreferencesUtils.CAN_DOWNVOTE,true)){
downvoteButton.setVisibility(View.GONE);
}
if (mVoteButtonsOnTheRight) {
ConstraintSet constraintSet = new ConstraintSet();
@ -3727,6 +3730,10 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter<Post, RecyclerVie
scoreTextView.setOnClickListener(null);
if(!mCurrentAccountSharedPreferences.getBoolean(SharedPreferencesUtils.CAN_DOWNVOTE,true)){
downvoteButton.setVisibility(View.GONE);
}
if (mVoteButtonsOnTheRight) {
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(bottomConstraintLayout);

View File

@ -221,4 +221,9 @@ public interface LemmyAPI {
@Query("id") int commentId,
@Query("auth") String auth
);
@GET("api/v3/site")
Call<String> getSiteInfo(
@Query("auth") String auth
);
}

View File

@ -11,11 +11,11 @@ public class ParseAndInsertNewAccount {
public static void parseAndInsertNewAccount(Executor executor, Handler handler, String username,
String display_name,String accessToken, String profileImageUrl,
String bannerImageUrl, String code,String instance, AccountDao accountDao,
String bannerImageUrl, String code,String instance,boolean can_downvote, AccountDao accountDao,
ParseAndInsertAccountListener parseAndInsertAccountListener) {
executor.execute(() -> {
Account account = new Account(username,display_name, accessToken, code, profileImageUrl,
bannerImageUrl, true,instance);
bannerImageUrl, true,instance,true);
accountDao.markAllAccountsNonCurrent();
accountDao.insert(account);

View File

@ -8,6 +8,8 @@ import java.util.concurrent.Executor;
import eu.toldi.infinityforlemmy.RedditDataRoomDatabase;
import eu.toldi.infinityforlemmy.RetrofitHolder;
import eu.toldi.infinityforlemmy.account.Account;
import eu.toldi.infinityforlemmy.site.FetchSiteInfo;
import eu.toldi.infinityforlemmy.site.SiteInfo;
import eu.toldi.infinityforlemmy.utils.SharedPreferencesUtils;
public class SwitchAccount {
@ -26,6 +28,18 @@ public class SwitchAccount {
.putString(SharedPreferencesUtils.ACCOUNT_INSTANCE,account.getInstance_url())
.putString(SharedPreferencesUtils.ACCOUNT_IMAGE_URL, account.getProfileImageUrl()).apply();
retrofitHolder.setBaseURL(account.getInstance_url());
FetchSiteInfo.fetchSiteInfo(retrofitHolder.getRetrofit(), account.getAccessToken(), new FetchSiteInfo.FetchSiteInfoListener() {
@Override
public void onFetchSiteInfoSuccess(SiteInfo siteInfo) {
boolean canDownvote = siteInfo.isEnable_downvotes();
currentAccountSharedPreferences.edit().putBoolean(SharedPreferencesUtils.CAN_DOWNVOTE, canDownvote).apply();
}
@Override
public void onFetchSiteInfoFailed() {
currentAccountSharedPreferences.edit().putBoolean(SharedPreferencesUtils.CAN_DOWNVOTE, true).apply();
}
});
handler.post(() -> switchAccountListener.switched(account));
});

View File

@ -0,0 +1,34 @@
package eu.toldi.infinityforlemmy.site;
import eu.toldi.infinityforlemmy.apis.LemmyAPI;
import retrofit2.Retrofit;
public class FetchSiteInfo {
public static void fetchSiteInfo(Retrofit retrofit, String accesToken, FetchSiteInfoListener fetchSiteInfoListener) {
retrofit.create(LemmyAPI.class).getSiteInfo(accesToken).enqueue(
new retrofit2.Callback<String>() {
@Override
public void onResponse(retrofit2.Call<String> call, retrofit2.Response<String> response) {
if (response.isSuccessful()) {
String siteInfoJson = response.body();
SiteInfo siteInfo = SiteInfo.parseSiteInfo(siteInfoJson);
fetchSiteInfoListener.onFetchSiteInfoSuccess(siteInfo);
} else {
fetchSiteInfoListener.onFetchSiteInfoFailed();
}
}
@Override
public void onFailure(retrofit2.Call<String> call, Throwable t) {
fetchSiteInfoListener.onFetchSiteInfoFailed();
}
}
);
}
public interface FetchSiteInfoListener {
void onFetchSiteInfoSuccess(SiteInfo siteInfo);
void onFetchSiteInfoFailed();
}
}

View File

@ -0,0 +1,83 @@
package eu.toldi.infinityforlemmy.site;
import org.json.JSONException;
import org.json.JSONObject;
public class SiteInfo {
private int id;
private String name;
private String sidebar;
private String description;
private boolean enable_downvotes;
private boolean enable_nsfw;
private boolean community_creation_admin_only;
public SiteInfo(int id, String name, String sidebar, String description, boolean enable_downvotes, boolean enable_nsfw, boolean community_creation_admin_only) {
this.id = id;
this.name = name;
this.sidebar = sidebar;
this.description = description;
this.enable_downvotes = enable_downvotes;
this.enable_nsfw = enable_nsfw;
this.community_creation_admin_only = community_creation_admin_only;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getSidebar() {
return sidebar;
}
public String getDescription() {
return description;
}
public boolean isEnable_downvotes() {
return enable_downvotes;
}
public boolean isEnable_nsfw() {
return enable_nsfw;
}
public boolean isCommunity_creation_admin_only() {
return community_creation_admin_only;
}
public static SiteInfo parseSiteInfo(String siteInfoJson) {
try {
JSONObject siteInfo = new JSONObject(siteInfoJson);
JSONObject siteView = siteInfo.getJSONObject("site_view");
JSONObject site = siteView.getJSONObject("site");
JSONObject localSite = siteView.getJSONObject("local_site");
int id = site.getInt("id");
String name = site.getString("name");
String sidebar = null;
if (site.has("sidebar"))
sidebar = site.getString("sidebar");
String description = null;
if (site.has("description"))
description = site.getString("description");
boolean enable_downvotes = localSite.getBoolean("enable_downvotes");
boolean enable_nsfw = localSite.getBoolean("enable_nsfw");
boolean community_creation_admin_only = localSite.getBoolean("community_creation_admin_only");
SiteInfo si = new SiteInfo(id, name, sidebar, description, enable_downvotes, enable_nsfw, community_creation_admin_only);
return si;
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}
}

View File

@ -400,4 +400,5 @@ public class SharedPreferencesUtils {
public static final String OPEN_LINK_IN_APP_LEGACY = "open_link_in_app";
public static final String ACCOUNT_INSTANCE = "account_instance";
public static final String ACCOUNT_QUALIFIED_NAME = "account_qualified_name";
public static final String CAN_DOWNVOTE = "can_downvote";
}