1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-26 00:27:30 +00:00

Allow toggling approval requirement in group

This commit is contained in:
Ammar Githam 2021-01-19 22:43:51 +09:00
parent 10c6902c83
commit 17709c176c
5 changed files with 76 additions and 37 deletions

View File

@ -94,41 +94,6 @@ public class DirectMessageSettingsFragment extends Fragment implements ConfirmDi
viewModel = new ViewModelProvider(this).get(DirectSettingsViewModel.class);
viewModel.setViewer(inboxViewModel.getViewer());
viewModel.setThread(first.get());
// basicClickListener = v -> {
// final Object tag = v.getTag();
// if (tag instanceof ProfileModel) {
// ProfileModel model = (ProfileModel) tag;
// final Bundle bundle = new Bundle();
// bundle.putString("username", "@" + model.getUsername());
// NavHostFragment.findNavController(this).navigate(R.id.action_global_profileFragment, bundle);
// }
// };
//
// clickListener = v -> {
// final Object tag = v.getTag();
// if (tag instanceof ProfileModel) {
// ProfileModel model = (ProfileModel) tag;
// final Context context = getContext();
// if (context == null) return;
// final ArrayAdapter<String> adapter = new ArrayAdapter<>(context, android.R.layout.simple_list_item_1, new String[]{
// getString(R.string.open_profile),
// getString(R.string.dms_action_kick),
// });
// final DialogInterface.OnClickListener clickListener = (d, w) -> {
// if (w == 0) {
// final Bundle bundle = new Bundle();
// bundle.putString("username", "@" + model.getUsername());
// NavHostFragment.findNavController(this).navigate(R.id.action_global_profileFragment, bundle);
// } else if (w == 1) {
// new ChangeSettings(titleText.getText().toString()).execute("remove_users", model.getId());
// onRefresh();
// }
// };
// new AlertDialog.Builder(context)
// .setAdapter(adapter, clickListener)
// .show();
// }
// };
}
@NonNull
@ -327,7 +292,8 @@ public class DirectMessageSettingsFragment extends Fragment implements ConfirmDi
binding.approvalRequiredLabel.setVisibility(View.VISIBLE);
binding.approvalRequiredLabel.setOnClickListener(v -> binding.approvalRequired.toggle());
binding.approvalRequired.setOnCheckedChangeListener((buttonView, isChecked) -> {
final LiveData<Resource<Object>> resourceLiveData = isChecked ? viewModel.approvalRequired() : viewModel.approvalNotRequired();
handleSwitchChangeResource(resourceLiveData, buttonView);
});
}

View File

@ -112,4 +112,19 @@ public interface DirectMessagesRepository {
@POST("/api/v1/direct_v2/threads/{threadId}/deny_participant_requests/")
Call<DirectThreadDetailsChangeResponse> declineParticipantRequests(@Path("threadId") String threadId,
@FieldMap final Map<String, String> form);
@FormUrlEncoded
@POST("/api/v1/direct_v2/threads/{threadId}/approval_required_for_new_members/")
Call<DirectThreadDetailsChangeResponse> approvalRequired(@Path("threadId") String threadId,
@FieldMap final Map<String, String> form);
@FormUrlEncoded
@POST("/api/v1/direct_v2/threads/{threadId}/approval_not_required_for_new_members/")
Call<DirectThreadDetailsChangeResponse> approvalNotRequired(@Path("threadId") String threadId,
@FieldMap final Map<String, String> form);
@FormUrlEncoded
@POST("/api/v1/direct_v2/threads/{threadId}/leave/")
Call<DirectThreadDetailsChangeResponse> leave(@Path("threadId") String threadId,
@FieldMap final Map<String, String> form);
}

View File

@ -41,7 +41,7 @@ public class DirectThread implements Serializable {
private final boolean isSpam;
private final DirectItem lastPermanentItem;
private final DirectThreadDirectStory directStory;
private final boolean approvalRequiredForNewMembers;
private boolean approvalRequiredForNewMembers;
public DirectThread(final String threadId,
final String threadV2Id,
@ -245,6 +245,10 @@ public class DirectThread implements Serializable {
return approvalRequiredForNewMembers;
}
public void setApprovalRequiredForNewMembers(final boolean approvalRequiredForNewMembers) {
this.approvalRequiredForNewMembers = approvalRequiredForNewMembers;
}
@Nullable
public DirectItem getFirstDirectItem() {
DirectItem firstItem = null;

View File

@ -508,6 +508,36 @@ public class DirectSettingsViewModel extends AndroidViewModel {
return data;
}
public LiveData<Resource<Object>> approvalRequired() {
final MutableLiveData<Resource<Object>> data = new MutableLiveData<>();
data.postValue(Resource.loading(null));
if (thread.isApprovalRequiredForNewMembers()) {
data.postValue(Resource.success(new Object()));
return data;
}
final Call<DirectThreadDetailsChangeResponse> request = directMessagesService.approvalRequired(thread.getThreadId());
handleDetailsChangeRequest(data, request, () -> {
thread.setApprovalRequiredForNewMembers(true);
approvalRequiredToJoin.postValue(true);
});
return data;
}
public LiveData<Resource<Object>> approvalNotRequired() {
final MutableLiveData<Resource<Object>> data = new MutableLiveData<>();
data.postValue(Resource.loading(null));
if (!thread.isApprovalRequiredForNewMembers()) {
data.postValue(Resource.success(new Object()));
return data;
}
final Call<DirectThreadDetailsChangeResponse> request = directMessagesService.approvalNotRequired(thread.getThreadId());
handleDetailsChangeRequest(data, request, () -> {
thread.setApprovalRequiredForNewMembers(false);
approvalRequiredToJoin.postValue(false);
});
return data;
}
private interface OnSuccessAction {
void onSuccess();
}

View File

@ -377,4 +377,28 @@ public class DirectMessagesService extends BaseService {
);
return repository.declineParticipantRequests(threadId, form);
}
public Call<DirectThreadDetailsChangeResponse> approvalRequired(@NonNull final String threadId) {
final ImmutableMap<String, String> form = ImmutableMap.of(
"_csrftoken", csrfToken,
"_uuid", deviceUuid
);
return repository.approvalRequired(threadId, form);
}
public Call<DirectThreadDetailsChangeResponse> approvalNotRequired(@NonNull final String threadId) {
final ImmutableMap<String, String> form = ImmutableMap.of(
"_csrftoken", csrfToken,
"_uuid", deviceUuid
);
return repository.approvalNotRequired(threadId, form);
}
public Call<DirectThreadDetailsChangeResponse> leave(@NonNull final String threadId) {
final ImmutableMap<String, String> form = ImmutableMap.of(
"_csrftoken", csrfToken,
"_uuid", deviceUuid
);
return repository.leave(threadId, form);
}
}