Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed social media reaction bugs #1607

Merged
merged 3 commits into from
May 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class Reaction implements Copyable<Reaction> {
public enum ReactionEmoji {
UPVOTE("\uD83D\uDC4D"),
DOWNVOTE("\uD83D\uDC4E"),
HEART("❤");
HEART("❤");

private final String code;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.util.*;
import java.util.stream.Collectors;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import timber.log.Timber;

import static android.text.format.DateUtils.getRelativeTimeSpanString;
Expand All @@ -39,6 +41,8 @@ public class ChirpListAdapter extends BaseAdapter {
private final LayoutInflater layoutInflater;
private List<Chirp> chirps;

private final CompositeDisposable disposables = new CompositeDisposable();

public ChirpListAdapter(
Context context, SocialMediaViewModel socialMediaViewModel, LaoViewModel viewModel) {
this.context = context;
Expand All @@ -55,6 +59,8 @@ public ChirpListAdapter(
}

public void replaceList(List<Chirp> chirps) {
// Dispose of previous observables
disposables.clear();
this.chirps = chirps;
notifyDataSetChanged();
}
Expand Down Expand Up @@ -94,6 +100,12 @@ public View getView(int position, View chirpView, ViewGroup viewGroup) {
chirpView.findViewById(R.id.chirp_card_buttons).setVisibility(View.GONE);
}

// Dispose of previous observables for the chirp at this position
Disposable previousDisposable = (Disposable) chirpView.getTag(R.id.chirp_card_buttons);
if (previousDisposable != null) {
previousDisposable.dispose();
}

PublicKey sender = chirp.getSender();
long timestamp = chirp.getTimestamp();
String text;
Expand All @@ -111,7 +123,7 @@ public View getView(int position, View chirpView, ViewGroup viewGroup) {

// Set dynamically the reaction buttons selection and counter
try {
laoViewModel.addDisposable(
Disposable reactionDisposable =
socialMediaViewModel
.getReactions(chirp.getId())
// Each time the observable changes the counter and the selection is notified
Expand Down Expand Up @@ -163,7 +175,10 @@ public View getView(int position, View chirpView, ViewGroup viewGroup) {
R.drawable.ic_social_media_heart);
},
err ->
ErrorUtils.logAndShow(context, TAG, err, R.string.unknown_chirp_exception)));
ErrorUtils.logAndShow(context, TAG, err, R.string.unknown_chirp_exception));
// Store the disposable as a tag
chirpView.setTag(R.id.chirp_card_buttons, reactionDisposable);
disposables.add(reactionDisposable);
} catch (UnknownChirpException e) {
throw new IllegalArgumentException("The chirp does not exist");
}
Expand Down Expand Up @@ -222,21 +237,21 @@ private void setupReactionButtons(
// Set the listener for the upvote button to add and delete reaction
upvoteChirp.setOnClickListener(
v -> {
reactionListener(upvoteChirp, UPVOTE, chirpId);
// Implement the exclusivity of upvote and downvote (i.e. disable downvote if upvote)
if (downvoteChirp.isSelected() && upvoteChirp.isSelected()) {
if (downvoteChirp.isSelected() && !upvoteChirp.isSelected()) {
reactionListener(downvoteChirp, DOWNVOTE, chirpId);
}
reactionListener(upvoteChirp, UPVOTE, chirpId);
});

// Set the listener for the downvote button to add and delete reaction
downvoteChirp.setOnClickListener(
v -> {
reactionListener(downvoteChirp, DOWNVOTE, chirpId);
// Implement the exclusivity of upvote and downvote (i.e. disable upvote if downvote)
if (downvoteChirp.isSelected() && upvoteChirp.isSelected()) {
if (!downvoteChirp.isSelected() && upvoteChirp.isSelected()) {
reactionListener(upvoteChirp, UPVOTE, chirpId);
}
reactionListener(downvoteChirp, DOWNVOTE, chirpId);
});

// Set the listener for the heart button to add and delete reaction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.github.dedis.popstellar.model.network.method.message.data.election.Vote;
import com.github.dedis.popstellar.model.objects.*;
import com.github.dedis.popstellar.model.objects.security.PublicKey;
import com.vdurmont.emoji.EmojiManager;

import java.time.Instant;
import java.util.*;
Expand Down Expand Up @@ -161,9 +160,6 @@ public MessageValidatorBuilder isNotEmptyBase64(String input, String field) {
* @throws IllegalArgumentException if the string is not representing a valid codepoint
*/
public MessageValidatorBuilder isValidEmoji(String input, String field) {
if (!EmojiManager.isEmoji(input)) {
throw new IllegalArgumentException(field + " must be a unicode emoji");
}
if (!Reaction.ReactionEmoji.isSupported(input)) {
throw new IllegalArgumentException(field + " is not a supported unicode emoji");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ protected void before() throws KeyException {
socialMediaRepository.addChirp(LAO_ID, CHIRP_2);
socialMediaRepository.addReaction(LAO_ID, REACTION);

messageSenderHelper.setupMock();
when(networkManager.getMessageSender()).thenReturn(messageSenderHelper.getMockedSender());
messageSenderHelper.setupMock();

when(keyManager.getMainPublicKey()).thenReturn(SENDER_KEY_1.getPublicKey());
when(keyManager.getValidPoPToken(anyString(), any(RollCall.class)))
Expand Down Expand Up @@ -322,43 +322,29 @@ public void getViewButtonTest() {
View view1 = chirpListAdapter.getView(0, null, layout);
assertNotNull(view1);

// Verify the upvote is set
// Verify the upvote is deselected
ImageButton upvoteButton = view1.findViewById(R.id.upvote_button);
assertNotNull(upvoteButton);
// Wait for the observable to be notified
InstrumentationRegistry.getInstrumentation().waitForIdleSync();
assertTrue(upvoteButton.isSelected());

upvoteButton.callOnClick();
// Remove the upvote reaction
socialMediaRepository.deleteReaction(LAO_ID, REACTION_ID);

// Wait for the observable to be notified
InstrumentationRegistry.getInstrumentation().waitForIdleSync();

assertFalse(upvoteButton.isSelected());

// Verify the downvote is not set
ImageButton downvoteButton = view1.findViewById(R.id.downvote_button);
assertNotNull(downvoteButton);
assertFalse(downvoteButton.isSelected());

// Add the downvote reaction
socialMediaRepository.addReaction(LAO_ID, REACTION2);

// Wait for the observable to be notified
InstrumentationRegistry.getInstrumentation().waitForIdleSync();
assertTrue(downvoteButton.isSelected());
downvoteButton.callOnClick();

// Verify the heart is not set
ImageButton heartButton = view1.findViewById(R.id.heart_button);
assertNotNull(heartButton);
assertFalse(heartButton.isSelected());

// Add the heart reaction
socialMediaRepository.addReaction(LAO_ID, REACTION3);

// Wait for the observable to be notified
InstrumentationRegistry.getInstrumentation().waitForIdleSync();
assertTrue(heartButton.isSelected());
heartButton.callOnClick();
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void equalsTest() {
assertEquals(ADD_REACTION, new AddReaction(CODE_POINT, CHIRP_ID, TIMESTAMP));

assertNotEquals(
ADD_REACTION, new AddReaction("❤", generateMessageIDOtherThan(CHIRP_ID), TIMESTAMP));
ADD_REACTION, new AddReaction("❤", generateMessageIDOtherThan(CHIRP_ID), TIMESTAMP));
assertNotEquals(
ADD_REACTION, new AddReaction(CODE_POINT, generateMessageIDOtherThan(CHIRP_ID), TIMESTAMP));
assertNotEquals(ADD_REACTION, new AddReaction(CODE_POINT, CHIRP_ID, TIMESTAMP + 1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public void testValidEmoji() {

validator.isValidEmoji("\uD83D\uDC4D", field);
validator.isValidEmoji("\uD83D\uDC4E", field);
validator.isValidEmoji("❤", field);
validator.isValidEmoji("❤", field);

assertThrows(
IllegalArgumentException.class, () -> validator.isValidEmoji("\uD83D\uDE00", field));
Expand Down