Browse Source

Allow toggling approval requirement in group

renovate/org.robolectric-robolectric-4.x
Ammar Githam 4 years ago
parent
commit
17709c176c
  1. 38
      app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageSettingsFragment.java
  2. 15
      app/src/main/java/awais/instagrabber/repositories/DirectMessagesRepository.java
  3. 6
      app/src/main/java/awais/instagrabber/repositories/responses/directmessages/DirectThread.java
  4. 30
      app/src/main/java/awais/instagrabber/viewmodels/DirectSettingsViewModel.java
  5. 24
      app/src/main/java/awais/instagrabber/webservices/DirectMessagesService.java

38
app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageSettingsFragment.java

@ -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);
});
}

15
app/src/main/java/awais/instagrabber/repositories/DirectMessagesRepository.java

@ -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);
}

6
app/src/main/java/awais/instagrabber/repositories/responses/directmessages/DirectThread.java

@ -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;

30
app/src/main/java/awais/instagrabber/viewmodels/DirectSettingsViewModel.java

@ -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();
}

24
app/src/main/java/awais/instagrabber/webservices/DirectMessagesService.java

@ -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);
}
}
Loading…
Cancel
Save