Make subscribe/unsubscribe buttons functional

This commit makes the subscribe/unsubscribe buttons functional, so you can manage subscriptions in the app!
Signed-off-by: Balazs Toldi <balazs@toldi.eu>
This commit is contained in:
Balazs Toldi 2023-07-27 09:26:12 +02:00
parent f3bff30ef0
commit e2de8b8160
No known key found for this signature in database
GPG Key ID: 6C7D440036F99D58
11 changed files with 214 additions and 181 deletions

View File

@ -788,7 +788,7 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect
bundle.putString(SubredditListingFragment.EXTRA_QUERY, mQuery); bundle.putString(SubredditListingFragment.EXTRA_QUERY, mQuery);
bundle.putBoolean(SubredditListingFragment.EXTRA_IS_GETTING_SUBREDDIT_INFO, false); bundle.putBoolean(SubredditListingFragment.EXTRA_IS_GETTING_SUBREDDIT_INFO, false);
bundle.putString(SubredditListingFragment.EXTRA_ACCESS_TOKEN, mAccessToken); bundle.putString(SubredditListingFragment.EXTRA_ACCESS_TOKEN, mAccessToken);
bundle.putString(SubredditListingFragment.EXTRA_ACCOUNT_NAME, mAccountName); bundle.putString(SubredditListingFragment.EXTRA_ACCOUNT_NAME, mAccountQualifiedName);
mFragment.setArguments(bundle); mFragment.setArguments(bundle);
return mFragment; return mFragment;
} }

View File

@ -64,6 +64,8 @@ public class SearchSubredditsResultActivity extends BaseActivity implements Acti
private String mAccessToken; private String mAccessToken;
private String mAccountName; private String mAccountName;
private String mAccountQualifiedName;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
((Infinity) getApplication()).getAppComponent().inject(this); ((Infinity) getApplication()).getAppComponent().inject(this);
@ -107,6 +109,7 @@ public class SearchSubredditsResultActivity extends BaseActivity implements Acti
mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null);
mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null); mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null);
mAccountQualifiedName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_QUALIFIED_NAME, null);
if (savedInstanceState == null) { if (savedInstanceState == null) {
mFragment = new SubredditListingFragment(); mFragment = new SubredditListingFragment();
@ -114,7 +117,7 @@ public class SearchSubredditsResultActivity extends BaseActivity implements Acti
bundle.putString(SubredditListingFragment.EXTRA_QUERY, query); bundle.putString(SubredditListingFragment.EXTRA_QUERY, query);
bundle.putBoolean(SubredditListingFragment.EXTRA_IS_GETTING_SUBREDDIT_INFO, true); bundle.putBoolean(SubredditListingFragment.EXTRA_IS_GETTING_SUBREDDIT_INFO, true);
bundle.putString(SubredditListingFragment.EXTRA_ACCESS_TOKEN, mAccessToken); bundle.putString(SubredditListingFragment.EXTRA_ACCESS_TOKEN, mAccessToken);
bundle.putString(SubredditListingFragment.EXTRA_ACCOUNT_NAME, mAccountName); bundle.putString(SubredditListingFragment.EXTRA_ACCOUNT_NAME, mAccountQualifiedName);
bundle.putBoolean(SubredditListingFragment.EXTRA_IS_MULTI_SELECTION, getIntent().getBooleanExtra(EXTRA_IS_MULTI_SELECTION, false)); bundle.putBoolean(SubredditListingFragment.EXTRA_IS_MULTI_SELECTION, getIntent().getBooleanExtra(EXTRA_IS_MULTI_SELECTION, false));
mFragment.setArguments(bundle); mFragment.setArguments(bundle);
} else { } else {

View File

@ -96,10 +96,10 @@ import eu.toldi.infinityforlemmy.multireddit.MultiReddit;
import eu.toldi.infinityforlemmy.post.Post; import eu.toldi.infinityforlemmy.post.Post;
import eu.toldi.infinityforlemmy.post.PostPagingSource; import eu.toldi.infinityforlemmy.post.PostPagingSource;
import eu.toldi.infinityforlemmy.readpost.InsertReadPost; import eu.toldi.infinityforlemmy.readpost.InsertReadPost;
import eu.toldi.infinityforlemmy.subreddit.CommunitySubscription;
import eu.toldi.infinityforlemmy.subreddit.FetchSubredditData; import eu.toldi.infinityforlemmy.subreddit.FetchSubredditData;
import eu.toldi.infinityforlemmy.subreddit.ParseSubredditData; import eu.toldi.infinityforlemmy.subreddit.ParseSubredditData;
import eu.toldi.infinityforlemmy.subreddit.SubredditData; import eu.toldi.infinityforlemmy.subreddit.SubredditData;
import eu.toldi.infinityforlemmy.subreddit.SubredditSubscription;
import eu.toldi.infinityforlemmy.subreddit.SubredditViewModel; import eu.toldi.infinityforlemmy.subreddit.SubredditViewModel;
import eu.toldi.infinityforlemmy.utils.APIUtils; import eu.toldi.infinityforlemmy.utils.APIUtils;
import eu.toldi.infinityforlemmy.utils.LemmyUtils; import eu.toldi.infinityforlemmy.utils.LemmyUtils;
@ -116,7 +116,6 @@ import pl.droidsonroids.gif.GifImageView;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
import retrofit2.Response; import retrofit2.Response;
import retrofit2.Retrofit;
public class ViewSubredditDetailActivity extends BaseActivity implements SortTypeSelectionCallback, public class ViewSubredditDetailActivity extends BaseActivity implements SortTypeSelectionCallback,
PostTypeBottomSheetFragment.PostTypeSelectionCallback, PostLayoutBottomSheetFragment.PostLayoutSelectionCallback, PostTypeBottomSheetFragment.PostTypeSelectionCallback, PostLayoutBottomSheetFragment.PostLayoutSelectionCallback,
@ -174,9 +173,6 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
@Named("no_oauth") @Named("no_oauth")
RetrofitHolder mRetrofit; RetrofitHolder mRetrofit;
@Inject @Inject
@Named("oauth")
Retrofit mOauthRetrofit;
@Inject
RedditDataRoomDatabase mRedditDataRoomDatabase; RedditDataRoomDatabase mRedditDataRoomDatabase;
@Inject @Inject
@Named("default") @Named("default")
@ -207,7 +203,9 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
private String mAccessToken; private String mAccessToken;
private String mAccountName; private String mAccountName;
private String mAccountQualifiedName; private String mAccountQualifiedName;
private String subredditName; private String communityName;
private int communityId;
private String description; private String description;
private String qualifiedName; private String qualifiedName;
@ -340,8 +338,8 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
lockBottomAppBar = mSharedPreferences.getBoolean(SharedPreferencesUtils.LOCK_BOTTOM_APP_BAR, false); lockBottomAppBar = mSharedPreferences.getBoolean(SharedPreferencesUtils.LOCK_BOTTOM_APP_BAR, false);
hideSubredditDescription = mSharedPreferences.getBoolean(SharedPreferencesUtils.HIDE_SUBREDDIT_DESCRIPTION, false); hideSubredditDescription = mSharedPreferences.getBoolean(SharedPreferencesUtils.HIDE_SUBREDDIT_DESCRIPTION, false);
subredditName = getIntent().getStringExtra(EXTRA_SUBREDDIT_NAME_KEY); communityName = getIntent().getStringExtra(EXTRA_SUBREDDIT_NAME_KEY);
qualifiedName = getIntent().getStringExtra(EXTRA_COMMUNITY_FULL_NAME_KEY); qualifiedName = getIntent().getStringExtra(EXTRA_COMMUNITY_FULL_NAME_KEY);
fragmentManager = getSupportFragmentManager(); fragmentManager = getSupportFragmentManager();
@ -366,7 +364,7 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
checkNewAccountAndBindView(); checkNewAccountAndBindView();
fetchSubredditData(); fetchSubredditData();
if (subredditName != null) { if (communityName != null) {
setupVisibleElements(); setupVisibleElements();
} }
} }
@ -457,10 +455,10 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
} }
private void setupVisibleElements() { private void setupVisibleElements() {
subredditNameTextView.setText(subredditName); subredditNameTextView.setText(communityName);
communityFullNameTextView.setText(qualifiedName); communityFullNameTextView.setText(qualifiedName);
toolbar.setTitle(subredditName); toolbar.setTitle(communityName);
setSupportActionBar(toolbar); setSupportActionBar(toolbar);
setToolbarGoToTop(toolbar); setToolbarGoToTop(toolbar);
@ -514,8 +512,8 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
bannerImageView.setOnClickListener(view -> { bannerImageView.setOnClickListener(view -> {
Intent intent = new Intent(ViewSubredditDetailActivity.this, ViewImageOrGifActivity.class); Intent intent = new Intent(ViewSubredditDetailActivity.this, ViewImageOrGifActivity.class);
intent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, subredditData.getBannerUrl()); intent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, subredditData.getBannerUrl());
intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, subredditName + "-banner.jpg"); intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, communityName + "-banner.jpg");
intent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, subredditName); intent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, communityName);
startActivity(intent); startActivity(intent);
}); });
} }
@ -534,14 +532,14 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
iconGifImageView.setOnClickListener(view -> { iconGifImageView.setOnClickListener(view -> {
Intent intent = new Intent(ViewSubredditDetailActivity.this, ViewImageOrGifActivity.class); Intent intent = new Intent(ViewSubredditDetailActivity.this, ViewImageOrGifActivity.class);
intent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, subredditData.getIconUrl()); intent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, subredditData.getIconUrl());
intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, subredditName + "-icon.jpg"); intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, communityName + "-icon.jpg");
intent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, subredditName); intent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, communityName);
startActivity(intent); startActivity(intent);
}); });
} }
String subredditFullName = subredditData.getTitle(); String subredditFullName = subredditData.getTitle();
if (!subredditName.equals(subredditFullName)) { if (!communityName.equals(subredditFullName)) {
getSupportActionBar().setTitle(subredditFullName); getSupportActionBar().setTitle(subredditFullName);
} }
subredditNameTextView.setText(subredditFullName); subredditNameTextView.setText(subredditFullName);
@ -577,21 +575,130 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
}); });
} }
private void setupSubscribeChip() {
subscribeSubredditChip.setOnClickListener(view -> {
if (mAccessToken == null) {
if (subscriptionReady) {
subscriptionReady = false;
if (getResources().getString(R.string.subscribe).contentEquals(subscribeSubredditChip.getText())) {
CommunitySubscription.anonymousSubscribeToSubreddit(mExecutor, new Handler(),
mRetrofit.getRetrofit(), mRedditDataRoomDatabase, communityName,
new CommunitySubscription.SubredditSubscriptionListener() {
@Override
public void onSubredditSubscriptionSuccess() {
subscribeSubredditChip.setText(R.string.unsubscribe);
subscribeSubredditChip.setChipBackgroundColor(ColorStateList.valueOf(subscribedColor));
makeSnackbar(R.string.subscribed, false);
subscriptionReady = true;
}
@Override
public void onSubredditSubscriptionFail() {
makeSnackbar(R.string.subscribe_failed, false);
subscriptionReady = true;
}
});
} else {
CommunitySubscription.anonymousUnsubscribeToSubreddit(mExecutor, new Handler(),
mRedditDataRoomDatabase, communityName,
new CommunitySubscription.SubredditSubscriptionListener() {
@Override
public void onSubredditSubscriptionSuccess() {
subscribeSubredditChip.setText(R.string.subscribe);
subscribeSubredditChip.setChipBackgroundColor(ColorStateList.valueOf(unsubscribedColor));
makeSnackbar(R.string.unsubscribed, false);
subscriptionReady = true;
}
@Override
public void onSubredditSubscriptionFail() {
makeSnackbar(R.string.unsubscribe_failed, false);
subscriptionReady = true;
}
});
}
}
} else {
if (subscriptionReady) {
subscriptionReady = false;
if (getResources().getString(R.string.subscribe).contentEquals(subscribeSubredditChip.getText())) {
CommunitySubscription.subscribeToCommunity(mExecutor, new Handler(),
mRetrofit.getRetrofit(), mAccessToken, communityId, qualifiedName, mAccountQualifiedName, mRedditDataRoomDatabase,
new CommunitySubscription.SubredditSubscriptionListener() {
@Override
public void onSubredditSubscriptionSuccess() {
subscribeSubredditChip.setText(R.string.unsubscribe);
subscribeSubredditChip.setChipBackgroundColor(ColorStateList.valueOf(subscribedColor));
makeSnackbar(R.string.subscribed, false);
subscriptionReady = true;
}
@Override
public void onSubredditSubscriptionFail() {
makeSnackbar(R.string.subscribe_failed, false);
subscriptionReady = true;
}
});
} else {
CommunitySubscription.unsubscribeToCommunity(mExecutor, new Handler(),
mRetrofit.getRetrofit(), mAccessToken, communityId, qualifiedName, mAccountName, mRedditDataRoomDatabase,
new CommunitySubscription.SubredditSubscriptionListener() {
@Override
public void onSubredditSubscriptionSuccess() {
subscribeSubredditChip.setText(R.string.subscribe);
subscribeSubredditChip.setChipBackgroundColor(ColorStateList.valueOf(unsubscribedColor));
makeSnackbar(R.string.unsubscribed, false);
subscriptionReady = true;
}
@Override
public void onSubredditSubscriptionFail() {
makeSnackbar(R.string.unsubscribe_failed, false);
subscriptionReady = true;
}
});
}
}
}
});
CheckIsSubscribedToSubreddit.checkIsSubscribedToSubreddit(mExecutor, new Handler(),
mRedditDataRoomDatabase, qualifiedName, mAccountQualifiedName,
new CheckIsSubscribedToSubreddit.CheckIsSubscribedToSubredditListener() {
@Override
public void isSubscribed() {
subscribeSubredditChip.setText(R.string.unsubscribe);
subscribeSubredditChip.setChipBackgroundColor(ColorStateList.valueOf(subscribedColor));
subscriptionReady = true;
}
@Override
public void isNotSubscribed() {
subscribeSubredditChip.setText(R.string.subscribe);
subscribeSubredditChip.setChipBackgroundColor(ColorStateList.valueOf(unsubscribedColor));
subscriptionReady = true;
}
});
}
private void fetchSubredditData() { private void fetchSubredditData() {
if (!mFetchSubredditInfoSuccess) { if (!mFetchSubredditInfoSuccess) {
FetchSubredditData.fetchSubredditData(mOauthRetrofit, mRetrofit.getRetrofit(), qualifiedName, mAccessToken, new FetchSubredditData.FetchSubredditDataListener() { FetchSubredditData.fetchSubredditData(mRetrofit.getRetrofit(), qualifiedName, mAccessToken, new FetchSubredditData.FetchSubredditDataListener() {
@Override @Override
public void onFetchSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) { public void onFetchSubredditDataSuccess(SubredditData communityData, int nCurrentOnlineSubscribers) {
qualifiedName = LemmyUtils.actorID2FullName(subredditData.getActorId()); qualifiedName = LemmyUtils.actorID2FullName(communityData.getActorId());
if (subredditName == null) { if (communityName == null) {
subredditName = subredditData.getTitle(); communityName = communityData.getTitle();
setupVisibleElements(); setupVisibleElements();
} }
communityId = communityData.getId();
setupSubscribeChip();
mNCurrentOnlineSubscribers = nCurrentOnlineSubscribers; mNCurrentOnlineSubscribers = nCurrentOnlineSubscribers;
nOnlineSubscribersTextView.setText(getString(R.string.online_subscribers_number_detail, nCurrentOnlineSubscribers)); nOnlineSubscribersTextView.setText(getString(R.string.online_subscribers_number_detail, nCurrentOnlineSubscribers));
InsertSubredditData.insertSubredditData(mExecutor, new Handler(), mRedditDataRoomDatabase, InsertSubredditData.insertSubredditData(mExecutor, new Handler(), mRedditDataRoomDatabase,
subredditData, () -> mFetchSubredditInfoSuccess = true); communityData, () -> mFetchSubredditInfoSuccess = true);
} }
@Override @Override
@ -648,7 +755,7 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
} }
case SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_SEARCH: { case SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_SEARCH: {
Intent intent = new Intent(this, SearchActivity.class); Intent intent = new Intent(this, SearchActivity.class);
intent.putExtra(SearchActivity.EXTRA_SUBREDDIT_NAME, subredditName); intent.putExtra(SearchActivity.EXTRA_SUBREDDIT_NAME, communityName);
startActivity(intent); startActivity(intent);
break; break;
} }
@ -762,7 +869,7 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
private void bindView() { private void bindView() {
if (mMessageFullname != null) { if (mMessageFullname != null) {
ReadMessage.readMessage(mOauthRetrofit, mAccessToken, mMessageFullname, new ReadMessage.ReadMessageListener() { ReadMessage.readMessage(mRetrofit.getRetrofit(), mAccessToken, mMessageFullname, new ReadMessage.ReadMessageListener() {
@Override @Override
public void readSuccess() { public void readSuccess() {
mMessageFullname = null; mMessageFullname = null;
@ -920,7 +1027,7 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
} }
case SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_FAB_SEARCH: { case SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_FAB_SEARCH: {
Intent intent = new Intent(this, SearchActivity.class); Intent intent = new Intent(this, SearchActivity.class);
intent.putExtra(SearchActivity.EXTRA_SUBREDDIT_NAME, subredditName); intent.putExtra(SearchActivity.EXTRA_SUBREDDIT_NAME, communityName);
startActivity(intent); startActivity(intent);
break; break;
} }
@ -964,109 +1071,6 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
}); });
navigationWrapper.floatingActionButton.setVisibility(hideFab ? View.GONE : View.VISIBLE); navigationWrapper.floatingActionButton.setVisibility(hideFab ? View.GONE : View.VISIBLE);
subscribeSubredditChip.setOnClickListener(view -> {
if (mAccessToken == null) {
if (subscriptionReady) {
subscriptionReady = false;
if (getResources().getString(R.string.subscribe).contentEquals(subscribeSubredditChip.getText())) {
SubredditSubscription.anonymousSubscribeToSubreddit(mExecutor, new Handler(),
mRetrofit.getRetrofit(), mRedditDataRoomDatabase, subredditName,
new SubredditSubscription.SubredditSubscriptionListener() {
@Override
public void onSubredditSubscriptionSuccess() {
subscribeSubredditChip.setText(R.string.unsubscribe);
subscribeSubredditChip.setChipBackgroundColor(ColorStateList.valueOf(subscribedColor));
makeSnackbar(R.string.subscribed, false);
subscriptionReady = true;
}
@Override
public void onSubredditSubscriptionFail() {
makeSnackbar(R.string.subscribe_failed, false);
subscriptionReady = true;
}
});
} else {
SubredditSubscription.anonymousUnsubscribeToSubreddit(mExecutor, new Handler(),
mRedditDataRoomDatabase, subredditName,
new SubredditSubscription.SubredditSubscriptionListener() {
@Override
public void onSubredditSubscriptionSuccess() {
subscribeSubredditChip.setText(R.string.subscribe);
subscribeSubredditChip.setChipBackgroundColor(ColorStateList.valueOf(unsubscribedColor));
makeSnackbar(R.string.unsubscribed, false);
subscriptionReady = true;
}
@Override
public void onSubredditSubscriptionFail() {
makeSnackbar(R.string.unsubscribe_failed, false);
subscriptionReady = true;
}
});
}
}
} else {
if (subscriptionReady) {
subscriptionReady = false;
if (getResources().getString(R.string.subscribe).contentEquals(subscribeSubredditChip.getText())) {
SubredditSubscription.subscribeToSubreddit(mExecutor, new Handler(), mOauthRetrofit,
mRetrofit.getRetrofit(), mAccessToken, subredditName, mAccountName, mRedditDataRoomDatabase,
new SubredditSubscription.SubredditSubscriptionListener() {
@Override
public void onSubredditSubscriptionSuccess() {
subscribeSubredditChip.setText(R.string.unsubscribe);
subscribeSubredditChip.setChipBackgroundColor(ColorStateList.valueOf(subscribedColor));
makeSnackbar(R.string.subscribed, false);
subscriptionReady = true;
}
@Override
public void onSubredditSubscriptionFail() {
makeSnackbar(R.string.subscribe_failed, false);
subscriptionReady = true;
}
});
} else {
SubredditSubscription.unsubscribeToSubreddit(mExecutor, new Handler(), mOauthRetrofit,
mAccessToken, subredditName, mAccountName, mRedditDataRoomDatabase,
new SubredditSubscription.SubredditSubscriptionListener() {
@Override
public void onSubredditSubscriptionSuccess() {
subscribeSubredditChip.setText(R.string.subscribe);
subscribeSubredditChip.setChipBackgroundColor(ColorStateList.valueOf(unsubscribedColor));
makeSnackbar(R.string.unsubscribed, false);
subscriptionReady = true;
}
@Override
public void onSubredditSubscriptionFail() {
makeSnackbar(R.string.unsubscribe_failed, false);
subscriptionReady = true;
}
});
}
}
}
});
CheckIsSubscribedToSubreddit.checkIsSubscribedToSubreddit(mExecutor, new Handler(),
mRedditDataRoomDatabase, qualifiedName, mAccountQualifiedName,
new CheckIsSubscribedToSubreddit.CheckIsSubscribedToSubredditListener() {
@Override
public void isSubscribed() {
subscribeSubredditChip.setText(R.string.unsubscribe);
subscribeSubredditChip.setChipBackgroundColor(ColorStateList.valueOf(subscribedColor));
subscriptionReady = true;
}
@Override
public void isNotSubscribed() {
subscribeSubredditChip.setText(R.string.subscribe);
subscribeSubredditChip.setChipBackgroundColor(ColorStateList.valueOf(unsubscribedColor));
subscriptionReady = true;
}
});
sectionsPagerAdapter = new SectionsPagerAdapter(this); sectionsPagerAdapter = new SectionsPagerAdapter(this);
viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() { viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@ -1133,7 +1137,7 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
return true; return true;
} else if (itemId == R.id.action_search_view_subreddit_detail_activity) { } else if (itemId == R.id.action_search_view_subreddit_detail_activity) {
Intent intent = new Intent(this, SearchActivity.class); Intent intent = new Intent(this, SearchActivity.class);
intent.putExtra(SearchActivity.EXTRA_SUBREDDIT_NAME, subredditName); intent.putExtra(SearchActivity.EXTRA_SUBREDDIT_NAME, communityName);
intent.putExtra(SearchActivity.EXTRA_COMMUNITY_FULL_NAME, qualifiedName); intent.putExtra(SearchActivity.EXTRA_COMMUNITY_FULL_NAME, qualifiedName);
startActivity(intent); startActivity(intent);
return true; return true;
@ -1152,7 +1156,7 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
return true; return true;
} }
Intent selectUserFlairIntent = new Intent(this, SelectUserFlairActivity.class); Intent selectUserFlairIntent = new Intent(this, SelectUserFlairActivity.class);
selectUserFlairIntent.putExtra(SelectUserFlairActivity.EXTRA_SUBREDDIT_NAME, subredditName); selectUserFlairIntent.putExtra(SelectUserFlairActivity.EXTRA_SUBREDDIT_NAME, communityName);
startActivity(selectUserFlairIntent); startActivity(selectUserFlairIntent);
return true; return true;
} else if (itemId == R.id.action_add_to_multireddit_view_subreddit_detail_activity) { } else if (itemId == R.id.action_add_to_multireddit_view_subreddit_detail_activity) {
@ -1164,7 +1168,7 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
startActivityForResult(intent, ADD_TO_MULTIREDDIT_REQUEST_CODE); startActivityForResult(intent, ADD_TO_MULTIREDDIT_REQUEST_CODE);
} else if (itemId == R.id.action_add_to_post_filter_view_subreddit_detail_activity) { } else if (itemId == R.id.action_add_to_post_filter_view_subreddit_detail_activity) {
Intent intent = new Intent(this, PostFilterPreferenceActivity.class); Intent intent = new Intent(this, PostFilterPreferenceActivity.class);
intent.putExtra(PostFilterPreferenceActivity.EXTRA_SUBREDDIT_NAME, subredditName); intent.putExtra(PostFilterPreferenceActivity.EXTRA_SUBREDDIT_NAME, communityName);
startActivity(intent); startActivity(intent);
return true; return true;
} else if (itemId == R.id.action_share_view_subreddit_detail_activity) { } else if (itemId == R.id.action_share_view_subreddit_detail_activity) {
@ -1179,13 +1183,13 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
return true; return true;
} else if (itemId == R.id.action_go_to_wiki_view_subreddit_detail_activity) { } else if (itemId == R.id.action_go_to_wiki_view_subreddit_detail_activity) {
Intent wikiIntent = new Intent(this, WikiActivity.class); Intent wikiIntent = new Intent(this, WikiActivity.class);
wikiIntent.putExtra(WikiActivity.EXTRA_SUBREDDIT_NAME, subredditName); wikiIntent.putExtra(WikiActivity.EXTRA_SUBREDDIT_NAME, communityName);
wikiIntent.putExtra(WikiActivity.EXTRA_WIKI_PATH, "index"); wikiIntent.putExtra(WikiActivity.EXTRA_WIKI_PATH, "index");
startActivity(wikiIntent); startActivity(wikiIntent);
return true; return true;
} else if (itemId == R.id.action_contact_mods_view_subreddit_detail_activity) { } else if (itemId == R.id.action_contact_mods_view_subreddit_detail_activity) {
Intent intent = new Intent(this, SendPrivateMessageActivity.class); Intent intent = new Intent(this, SendPrivateMessageActivity.class);
intent.putExtra(SendPrivateMessageActivity.EXTRA_RECIPIENT_USERNAME, "r/" + subredditName); intent.putExtra(SendPrivateMessageActivity.EXTRA_RECIPIENT_USERNAME, "r/" + communityName);
startActivity(intent); startActivity(intent);
return true; return true;
} }
@ -1199,19 +1203,19 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
if (data != null) { if (data != null) {
MultiReddit multiReddit = data.getParcelableExtra(MultiredditSelectionActivity.EXTRA_RETURN_MULTIREDDIT); MultiReddit multiReddit = data.getParcelableExtra(MultiredditSelectionActivity.EXTRA_RETURN_MULTIREDDIT);
if (multiReddit != null) { if (multiReddit != null) {
AddSubredditOrUserToMultiReddit.addSubredditOrUserToMultiReddit(mOauthRetrofit, AddSubredditOrUserToMultiReddit.addSubredditOrUserToMultiReddit(mRetrofit.getRetrofit(),
mAccessToken, multiReddit.getPath(), subredditName, mAccessToken, multiReddit.getPath(), communityName,
new AddSubredditOrUserToMultiReddit.AddSubredditOrUserToMultiRedditListener() { new AddSubredditOrUserToMultiReddit.AddSubredditOrUserToMultiRedditListener() {
@Override @Override
public void success() { public void success() {
Toast.makeText(ViewSubredditDetailActivity.this, Toast.makeText(ViewSubredditDetailActivity.this,
getString(R.string.add_community_or_user_to_multireddit_success, subredditName, multiReddit.getDisplayName()), Toast.LENGTH_LONG).show(); getString(R.string.add_community_or_user_to_multireddit_success, communityName, multiReddit.getDisplayName()), Toast.LENGTH_LONG).show();
} }
@Override @Override
public void failed(int code) { public void failed(int code) {
Toast.makeText(ViewSubredditDetailActivity.this, Toast.makeText(ViewSubredditDetailActivity.this,
getString(R.string.add_community_or_user_to_multireddit_failed, subredditName, multiReddit.getDisplayName()), Toast.LENGTH_LONG).show(); getString(R.string.add_community_or_user_to_multireddit_failed, communityName, multiReddit.getDisplayName()), Toast.LENGTH_LONG).show();
} }
}); });
} }
@ -1274,39 +1278,39 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
switch (postType) { switch (postType) {
case PostTypeBottomSheetFragment.TYPE_TEXT: case PostTypeBottomSheetFragment.TYPE_TEXT:
intent = new Intent(this, PostTextActivity.class); intent = new Intent(this, PostTextActivity.class);
intent.putExtra(PostTextActivity.EXTRA_SUBREDDIT_NAME, subredditName); intent.putExtra(PostTextActivity.EXTRA_SUBREDDIT_NAME, communityName);
startActivity(intent); startActivity(intent);
break; break;
case PostTypeBottomSheetFragment.TYPE_LINK: case PostTypeBottomSheetFragment.TYPE_LINK:
intent = new Intent(this, PostLinkActivity.class); intent = new Intent(this, PostLinkActivity.class);
intent.putExtra(PostLinkActivity.EXTRA_SUBREDDIT_NAME, subredditName); intent.putExtra(PostLinkActivity.EXTRA_SUBREDDIT_NAME, communityName);
startActivity(intent); startActivity(intent);
break; break;
case PostTypeBottomSheetFragment.TYPE_IMAGE: case PostTypeBottomSheetFragment.TYPE_IMAGE:
intent = new Intent(this, PostImageActivity.class); intent = new Intent(this, PostImageActivity.class);
intent.putExtra(PostImageActivity.EXTRA_SUBREDDIT_NAME, subredditName); intent.putExtra(PostImageActivity.EXTRA_SUBREDDIT_NAME, communityName);
startActivity(intent); startActivity(intent);
break; break;
case PostTypeBottomSheetFragment.TYPE_VIDEO: case PostTypeBottomSheetFragment.TYPE_VIDEO:
intent = new Intent(this, PostVideoActivity.class); intent = new Intent(this, PostVideoActivity.class);
intent.putExtra(PostVideoActivity.EXTRA_SUBREDDIT_NAME, subredditName); intent.putExtra(PostVideoActivity.EXTRA_SUBREDDIT_NAME, communityName);
startActivity(intent); startActivity(intent);
break; break;
case PostTypeBottomSheetFragment.TYPE_GALLERY: case PostTypeBottomSheetFragment.TYPE_GALLERY:
intent = new Intent(this, PostGalleryActivity.class); intent = new Intent(this, PostGalleryActivity.class);
intent.putExtra(PostGalleryActivity.EXTRA_SUBREDDIT_NAME, subredditName); intent.putExtra(PostGalleryActivity.EXTRA_SUBREDDIT_NAME, communityName);
startActivity(intent); startActivity(intent);
break; break;
case PostTypeBottomSheetFragment.TYPE_POLL: case PostTypeBottomSheetFragment.TYPE_POLL:
intent = new Intent(this, PostPollActivity.class); intent = new Intent(this, PostPollActivity.class);
intent.putExtra(PostPollActivity.EXTRA_SUBREDDIT_NAME, subredditName); intent.putExtra(PostPollActivity.EXTRA_SUBREDDIT_NAME, communityName);
startActivity(intent); startActivity(intent);
} }
} }
@Override @Override
public void postLayoutSelected(int postLayout) { public void postLayoutSelected(int postLayout) {
mPostLayoutSharedPreferences.edit().putInt(SharedPreferencesUtils.POST_LAYOUT_SUBREDDIT_POST_BASE + subredditName, postLayout).apply(); mPostLayoutSharedPreferences.edit().putInt(SharedPreferencesUtils.POST_LAYOUT_SUBREDDIT_POST_BASE + communityName, postLayout).apply();
sectionsPagerAdapter.changePostLayout(postLayout); sectionsPagerAdapter.changePostLayout(postLayout);
} }
@ -1382,7 +1386,7 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
break; break;
case FABMoreOptionsBottomSheetFragment.FAB_OPTION_SEARCH: case FABMoreOptionsBottomSheetFragment.FAB_OPTION_SEARCH:
Intent intent = new Intent(this, SearchActivity.class); Intent intent = new Intent(this, SearchActivity.class);
intent.putExtra(SearchActivity.EXTRA_SUBREDDIT_NAME, subredditName); intent.putExtra(SearchActivity.EXTRA_SUBREDDIT_NAME, communityName);
startActivity(intent); startActivity(intent);
break; break;
case FABMoreOptionsBottomSheetFragment.FAB_OPTION_GO_TO_SUBREDDIT: { case FABMoreOptionsBottomSheetFragment.FAB_OPTION_GO_TO_SUBREDDIT: {
@ -1460,7 +1464,7 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
if (subredditAutocompleteCall != null) { if (subredditAutocompleteCall != null) {
subredditAutocompleteCall.cancel(); subredditAutocompleteCall.cancel();
} }
subredditAutocompleteCall = mOauthRetrofit.create(RedditAPI.class).subredditAutocomplete(APIUtils.getOAuthHeader(mAccessToken), subredditAutocompleteCall = mRetrofit.getRetrofit().create(RedditAPI.class).subredditAutocomplete(APIUtils.getOAuthHeader(mAccessToken),
editable.toString(), nsfw); editable.toString(), nsfw);
subredditAutocompleteCall.enqueue(new Callback<String>() { subredditAutocompleteCall.enqueue(new Callback<String>() {
@Override @Override
@ -1596,7 +1600,7 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
SidebarFragment fragment = new SidebarFragment(); SidebarFragment fragment = new SidebarFragment();
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putString(SidebarFragment.EXTRA_ACCESS_TOKEN, mAccessToken); bundle.putString(SidebarFragment.EXTRA_ACCESS_TOKEN, mAccessToken);
bundle.putString(SidebarFragment.EXTRA_SUBREDDIT_NAME, subredditName); bundle.putString(SidebarFragment.EXTRA_SUBREDDIT_NAME, communityName);
bundle.putString(SidebarFragment.EXTRA_COMMUNITY_QUALIFIED_NAME, qualifiedName); bundle.putString(SidebarFragment.EXTRA_COMMUNITY_QUALIFIED_NAME, qualifiedName);
fragment.setArguments(bundle); fragment.setArguments(bundle);
return fragment; return fragment;

View File

@ -33,8 +33,8 @@ import eu.toldi.infinityforlemmy.RedditDataRoomDatabase;
import eu.toldi.infinityforlemmy.activities.BaseActivity; import eu.toldi.infinityforlemmy.activities.BaseActivity;
import eu.toldi.infinityforlemmy.asynctasks.CheckIsSubscribedToSubreddit; import eu.toldi.infinityforlemmy.asynctasks.CheckIsSubscribedToSubreddit;
import eu.toldi.infinityforlemmy.customtheme.CustomThemeWrapper; import eu.toldi.infinityforlemmy.customtheme.CustomThemeWrapper;
import eu.toldi.infinityforlemmy.subreddit.CommunitySubscription;
import eu.toldi.infinityforlemmy.subreddit.SubredditData; import eu.toldi.infinityforlemmy.subreddit.SubredditData;
import eu.toldi.infinityforlemmy.subreddit.SubredditSubscription;
import eu.toldi.infinityforlemmy.utils.LemmyUtils; import eu.toldi.infinityforlemmy.utils.LemmyUtils;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation; import jp.wasabeef.glide.transformations.RoundedCornersTransformation;
import pl.droidsonroids.gif.GifImageView; import pl.droidsonroids.gif.GifImageView;
@ -158,10 +158,10 @@ public class SubredditListingRecyclerViewAdapter extends PagedListAdapter<Subred
((DataViewHolder) holder).subscribeButton.setVisibility(View.VISIBLE); ((DataViewHolder) holder).subscribeButton.setVisibility(View.VISIBLE);
((DataViewHolder) holder).subscribeButton.setOnClickListener(view -> { ((DataViewHolder) holder).subscribeButton.setOnClickListener(view -> {
if (accessToken != null) { if (accessToken != null) {
SubredditSubscription.subscribeToSubreddit(executor, new Handler(), CommunitySubscription.subscribeToCommunity(executor, new Handler(),
oauthRetrofit, retrofit, accessToken, subredditData.getName(), retrofit, accessToken, subredditData.getId(), LemmyUtils.actorID2FullName(subredditData.getActorId()),
accountName, redditDataRoomDatabase, accountName, redditDataRoomDatabase,
new SubredditSubscription.SubredditSubscriptionListener() { new CommunitySubscription.SubredditSubscriptionListener() {
@Override @Override
public void onSubredditSubscriptionSuccess() { public void onSubredditSubscriptionSuccess() {
((DataViewHolder) holder).subscribeButton.setVisibility(View.GONE); ((DataViewHolder) holder).subscribeButton.setVisibility(View.GONE);
@ -174,9 +174,9 @@ public class SubredditListingRecyclerViewAdapter extends PagedListAdapter<Subred
} }
}); });
} else { } else {
SubredditSubscription.anonymousSubscribeToSubreddit(executor, new Handler(), CommunitySubscription.anonymousSubscribeToSubreddit(executor, new Handler(),
retrofit, redditDataRoomDatabase, subredditData.getName(), retrofit, redditDataRoomDatabase, subredditData.getName(),
new SubredditSubscription.SubredditSubscriptionListener() { new CommunitySubscription.SubredditSubscriptionListener() {
@Override @Override
public void onSubredditSubscriptionSuccess() { public void onSubredditSubscriptionSuccess() {
((DataViewHolder) holder).subscribeButton.setVisibility(View.GONE); ((DataViewHolder) holder).subscribeButton.setVisibility(View.GONE);

View File

@ -5,6 +5,7 @@ import com.google.common.util.concurrent.ListenableFuture;
import eu.toldi.infinityforlemmy.dto.AccountLoginDTO; import eu.toldi.infinityforlemmy.dto.AccountLoginDTO;
import eu.toldi.infinityforlemmy.dto.CommentDTO; import eu.toldi.infinityforlemmy.dto.CommentDTO;
import eu.toldi.infinityforlemmy.dto.CommentVoteDTO; import eu.toldi.infinityforlemmy.dto.CommentVoteDTO;
import eu.toldi.infinityforlemmy.dto.FollowCommunityDTO;
import eu.toldi.infinityforlemmy.dto.PostVoteDTO; import eu.toldi.infinityforlemmy.dto.PostVoteDTO;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Response; import retrofit2.Response;
@ -66,6 +67,10 @@ public interface LemmyAPI {
@POST("api/v3/comment/like") @POST("api/v3/comment/like")
Call<String> commentLike(@Body CommentVoteDTO params); Call<String> commentLike(@Body CommentVoteDTO params);
@Headers("Content-Type: application/json")
@POST("api/v3/community/follow")
Call<String> communityFollow(@Body FollowCommunityDTO params);
@GET("api/v3/comment/list") @GET("api/v3/comment/list")
Call<String> getComments( Call<String> getComments(
@Query("type_") String type, @Query("type_") String type,

View File

@ -23,7 +23,7 @@ public class LoadSubredditIcon {
String iconImageUrl = subredditDao.getSubredditData(subredditName).getIconUrl(); String iconImageUrl = subredditDao.getSubredditData(subredditName).getIconUrl();
handler.post(() -> loadSubredditIconAsyncTaskListener.loadIconSuccess(iconImageUrl)); handler.post(() -> loadSubredditIconAsyncTaskListener.loadIconSuccess(iconImageUrl));
} else { } else {
handler.post(() -> FetchSubredditData.fetchSubredditData(oauthRetrofit, retrofit, subredditName, accessToken, new FetchSubredditData.FetchSubredditDataListener() { handler.post(() -> FetchSubredditData.fetchSubredditData(retrofit, subredditName, accessToken, new FetchSubredditData.FetchSubredditDataListener() {
@Override @Override
public void onFetchSubredditDataSuccess(SubredditData subredditData1, int nCurrentOnlineSubscribers) { public void onFetchSubredditDataSuccess(SubredditData subredditData1, int nCurrentOnlineSubscribers) {
ArrayList<SubredditData> singleSubredditDataList = new ArrayList<>(); ArrayList<SubredditData> singleSubredditDataList = new ArrayList<>();

View File

@ -0,0 +1,26 @@
package eu.toldi.infinityforlemmy.dto;
public class FollowCommunityDTO {
private int community_id;
private boolean follow;
private String auth;
public FollowCommunityDTO(int community_id, boolean follow, String auth) {
this.community_id = community_id;
this.follow = follow;
this.auth = auth;
}
public int getCommunity_id() {
return community_id;
}
public boolean isFollow() {
return follow;
}
public String getAuth() {
return auth;
}
}

View File

@ -194,7 +194,7 @@ public class SidebarFragment extends Fragment {
public void fetchSubredditData() { public void fetchSubredditData() {
swipeRefreshLayout.setRefreshing(true); swipeRefreshLayout.setRefreshing(true);
FetchSubredditData.fetchSubredditData(mOauthRetrofit, mRetrofit.getRetrofit(), communityQualifiedName, mAccessToken, new FetchSubredditData.FetchSubredditDataListener() { FetchSubredditData.fetchSubredditData(mRetrofit.getRetrofit(), communityQualifiedName, mAccessToken, new FetchSubredditData.FetchSubredditDataListener() {
@Override @Override
public void onFetchSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) { public void onFetchSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) {
swipeRefreshLayout.setRefreshing(false); swipeRefreshLayout.setRefreshing(false);

View File

@ -1400,7 +1400,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
e.printStackTrace(); e.printStackTrace();
} }
} }
FetchSubredditData.fetchSubredditData(mOauthRetrofit, mRetrofit.getRetrofit(), mPost.getSubredditNamePrefixed(), mAccessToken, FetchSubredditData.fetchSubredditData(mRetrofit.getRetrofit(), mPost.getSubredditNamePrefixed(), mAccessToken,
new FetchSubredditData.FetchSubredditDataListener() { new FetchSubredditData.FetchSubredditDataListener() {
@Override @Override
public void onFetchSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) { public void onFetchSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) {

View File

@ -4,26 +4,24 @@ import android.os.Handler;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import eu.toldi.infinityforlemmy.RedditDataRoomDatabase; import eu.toldi.infinityforlemmy.RedditDataRoomDatabase;
import eu.toldi.infinityforlemmy.account.Account; import eu.toldi.infinityforlemmy.account.Account;
import eu.toldi.infinityforlemmy.apis.RedditAPI; import eu.toldi.infinityforlemmy.apis.LemmyAPI;
import eu.toldi.infinityforlemmy.dto.FollowCommunityDTO;
import eu.toldi.infinityforlemmy.subscribedsubreddit.SubscribedSubredditData; import eu.toldi.infinityforlemmy.subscribedsubreddit.SubscribedSubredditData;
import eu.toldi.infinityforlemmy.utils.APIUtils;
import eu.toldi.infinityforlemmy.utils.LemmyUtils; import eu.toldi.infinityforlemmy.utils.LemmyUtils;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
import retrofit2.Retrofit; import retrofit2.Retrofit;
public class SubredditSubscription { public class CommunitySubscription {
public static void subscribeToSubreddit(Executor executor, Handler handler, Retrofit oauthRetrofit, public static void subscribeToCommunity(Executor executor, Handler handler,
Retrofit retrofit, String accessToken, String subredditName, Retrofit retrofit, String accessToken, int communityId, String communityQualifiedName,
String accountName, RedditDataRoomDatabase redditDataRoomDatabase, String accountName, RedditDataRoomDatabase redditDataRoomDatabase,
SubredditSubscriptionListener subredditSubscriptionListener) { SubredditSubscriptionListener subredditSubscriptionListener) {
subredditSubscription(executor, handler, oauthRetrofit, retrofit, accessToken, subredditName, communitySubscription(executor, handler, retrofit, accessToken, communityId, communityQualifiedName,
accountName, "sub", redditDataRoomDatabase, subredditSubscriptionListener); accountName, "sub", redditDataRoomDatabase, subredditSubscriptionListener);
} }
@ -31,7 +29,7 @@ public class SubredditSubscription {
RedditDataRoomDatabase redditDataRoomDatabase, RedditDataRoomDatabase redditDataRoomDatabase,
String subredditName, String subredditName,
SubredditSubscriptionListener subredditSubscriptionListener) { SubredditSubscriptionListener subredditSubscriptionListener) {
FetchSubredditData.fetchSubredditData(null, retrofit, subredditName, "", new FetchSubredditData.FetchSubredditDataListener() { FetchSubredditData.fetchSubredditData(retrofit, subredditName, "", new FetchSubredditData.FetchSubredditDataListener() {
@Override @Override
public void onFetchSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) { public void onFetchSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) {
insertSubscription(executor, handler, redditDataRoomDatabase, insertSubscription(executor, handler, redditDataRoomDatabase,
@ -45,11 +43,11 @@ public class SubredditSubscription {
}); });
} }
public static void unsubscribeToSubreddit(Executor executor, Handler handler, Retrofit oauthRetrofit, public static void unsubscribeToCommunity(Executor executor, Handler handler,
String accessToken, String subredditName, String accountName, Retrofit retrofit, String accessToken, int communityId, String communityQualifiedName, String accountName,
RedditDataRoomDatabase redditDataRoomDatabase, RedditDataRoomDatabase redditDataRoomDatabase,
SubredditSubscriptionListener subredditSubscriptionListener) { SubredditSubscriptionListener subredditSubscriptionListener) {
subredditSubscription(executor, handler, oauthRetrofit, null, accessToken, subredditName, communitySubscription(executor, handler, retrofit, accessToken, communityId, communityQualifiedName,
accountName, "unsub", redditDataRoomDatabase, subredditSubscriptionListener); accountName, "unsub", redditDataRoomDatabase, subredditSubscriptionListener);
} }
@ -60,24 +58,21 @@ public class SubredditSubscription {
removeSubscription(executor, handler, redditDataRoomDatabase, subredditName, "-", subredditSubscriptionListener); removeSubscription(executor, handler, redditDataRoomDatabase, subredditName, "-", subredditSubscriptionListener);
} }
private static void subredditSubscription(Executor executor, Handler handler, Retrofit oauthRetrofit, private static void communitySubscription(Executor executor, Handler handler,
Retrofit retrofit, String accessToken, String subredditName, Retrofit retrofit, String accessToken, int communityId, String communityQualifiedName,
String accountName, String action, String accountName, String action,
RedditDataRoomDatabase redditDataRoomDatabase, RedditDataRoomDatabase redditDataRoomDatabase,
SubredditSubscriptionListener subredditSubscriptionListener) { SubredditSubscriptionListener subredditSubscriptionListener) {
RedditAPI api = oauthRetrofit.create(RedditAPI.class); LemmyAPI api = retrofit.create(LemmyAPI.class);
Map<String, String> params = new HashMap<>();
params.put(APIUtils.ACTION_KEY, action);
params.put(APIUtils.SR_NAME_KEY, subredditName);
Call<String> subredditSubscriptionCall = api.subredditSubscription(APIUtils.getOAuthHeader(accessToken), params); Call<String> subredditSubscriptionCall = api.communityFollow(new FollowCommunityDTO(communityId, action.equals("sub"), accessToken));
subredditSubscriptionCall.enqueue(new Callback<String>() { subredditSubscriptionCall.enqueue(new Callback<String>() {
@Override @Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
if (action.equals("sub")) { if (action.equals("sub")) {
FetchSubredditData.fetchSubredditData(oauthRetrofit, retrofit, subredditName, accessToken, new FetchSubredditData.FetchSubredditDataListener() { FetchSubredditData.fetchSubredditData(retrofit, communityQualifiedName, accessToken, new FetchSubredditData.FetchSubredditDataListener() {
@Override @Override
public void onFetchSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) { public void onFetchSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) {
insertSubscription(executor, handler, redditDataRoomDatabase, insertSubscription(executor, handler, redditDataRoomDatabase,
@ -90,7 +85,7 @@ public class SubredditSubscription {
} }
}); });
} else { } else {
removeSubscription(executor, handler, redditDataRoomDatabase, subredditName, removeSubscription(executor, handler, redditDataRoomDatabase, communityQualifiedName,
accountName, subredditSubscriptionListener); accountName, subredditSubscriptionListener);
} }
} else { } else {

View File

@ -12,11 +12,11 @@ import retrofit2.Response;
import retrofit2.Retrofit; import retrofit2.Retrofit;
public class FetchSubredditData { public class FetchSubredditData {
public static void fetchSubredditData(Retrofit oauthRetrofit, Retrofit retrofit, String subredditName, String accessToken, final FetchSubredditDataListener fetchSubredditDataListener) { public static void fetchSubredditData(Retrofit retrofit, String subredditName, String accessToken, final FetchSubredditDataListener fetchSubredditDataListener) {
LemmyAPI api = retrofit.create(LemmyAPI.class); LemmyAPI api = retrofit.create(LemmyAPI.class);
Call<String> subredditData; Call<String> subredditData;
subredditData = api.communityInfo(subredditName,accessToken); subredditData = api.communityInfo(subredditName, accessToken);
subredditData.enqueue(new Callback<>() { subredditData.enqueue(new Callback<>() {
@Override @Override