use of com.github.dedis.popstellar.model.objects.Election in project popstellar by dedis.
the class LAORepository method getElectionByChannel.
/**
* Retrieves the Election in a given channel
*
* @param channel the channel on which the election was created
* @return the election corresponding to this channel
*/
public Election getElectionByChannel(Channel channel) {
Log.d(TAG, "querying election for channel " + channel);
if (!channel.isElectionChannel())
throw new IllegalArgumentException("invalid channel for an election : " + channel);
Lao lao = getLaoByChannel(channel);
Optional<Election> electionOption = lao.getElection(channel.extractElectionId());
if (!electionOption.isPresent()) {
throw new IllegalArgumentException("the election should be present when receiving a result");
}
return electionOption.get();
}
use of com.github.dedis.popstellar.model.objects.Election in project popstellar by dedis.
the class EventExpandableListViewAdapter method getGroupView.
/**
* Gets a View that displays the given group. This View is only for the group--the Views for the
* group's children will be fetched using {@link #getChildView(int, int, boolean, View,
* ViewGroup)}.
*
* @param groupPosition the position of the group for which the View is returned
* @param isExpanded whether the group is expanded or collapsed
* @param convertView the old view to reuse, if possible. You should check that this view is
* non-null and of an appropriate type before using. If it is not possible to convert this
* view to display the correct data, this method can create a new view. It is not guaranteed
* that the convertView will have been previously created by {@link #getGroupView(int,
* boolean, View, ViewGroup)}.
* @param parent the parent that this view will eventually be attached to
* @return the View corresponding to the group at the specified position
*/
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
EventCategory eventCategory = (EventCategory) getGroup(groupPosition);
EventCategoryLayoutBinding binding;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
binding = EventCategoryLayoutBinding.inflate(inflater, parent, false);
} else {
binding = DataBindingUtil.getBinding(convertView);
}
binding.setCategoryName(eventCategory.name());
Context context = parent.getContext();
AddEventListener addEventOnClickListener = () -> {
Builder builder = new Builder(context);
builder.setTitle("Select Event Type");
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(context, android.R.layout.select_dialog_singlechoice);
arrayAdapter.add("Roll-Call Event");
arrayAdapter.add("Election Event");
builder.setNegativeButton("Cancel", (dialog, which) -> dialog.dismiss());
builder.setAdapter(arrayAdapter, ((dialog, which) -> {
dialog.dismiss();
viewModel.chooseEventType(EventType.values()[which]);
}));
builder.show();
};
binding.setIsFutureCategory(eventCategory.equals(FUTURE));
binding.setViewmodel(viewModel);
binding.setLifecycleOwner(lifecycleOwner);
binding.setAddEventListener(addEventOnClickListener);
binding.executePendingBindings();
binding.addFutureEventButton.setFocusable(false);
return binding.getRoot();
}
use of com.github.dedis.popstellar.model.objects.Election in project popstellar by dedis.
the class CastVoteViewPagerAdapter method onBindViewHolder.
@Override
public void onBindViewHolder(@NonNull Pager2ViewHolder holder, int position) {
Election election = mLaoDetailViewModel.getCurrentElection();
// setting the question
ElectionQuestion question = election.getElectionQuestions().get(position);
holder.questionView.setText(question.getQuestion());
// this will determine the number of option the user can select and vote for
// If another voting method is implemented in setUp, this can be adapted
// by default
int numberOfChoices = 1;
List<Integer> votes = new ArrayList<>();
// setting the list view with ballot options
List<String> ballotOptions = question.getBallotOptions();
ballotAdapter.clear();
ballotAdapter.addAll(ballotOptions);
ListView ballotsListView = holder.ballotsListView;
ballotsListView.setAdapter(ballotAdapter);
ballotsListView.setChoiceMode(numberOfChoices > 1 ? AbsListView.CHOICE_MODE_MULTIPLE : AbsListView.CHOICE_MODE_SINGLE);
AdapterView.OnItemClickListener itemListener = (parent, view, listPosition, id) -> {
// in this listener the position refers to the index of the question and
// the list position is the index of the ballot that was clicked on
ballotsListView.setClickable(false);
if (numberOfChoices > 1) {
if (votes.contains(listPosition)) {
// without the cast listPosition is treated as the index rather than the list element
votes.remove((Integer) listPosition);
ballotsListView.setItemChecked(listPosition, false);
} else if (votes.size() < numberOfChoices) {
votes.add(listPosition);
ballotsListView.setItemChecked(listPosition, true);
} else {
ballotsListView.setItemChecked(listPosition, false);
}
} else {
if (votes.contains(listPosition)) {
votes.clear();
ballotsListView.setItemChecked(listPosition, false);
} else {
votes.clear();
votes.add(listPosition);
}
}
mLaoDetailViewModel.setCurrentElectionQuestionVotes(votes, position);
ballotsListView.setClickable(true);
voteButton.setEnabled(checkEachQuestion());
};
ballotsListView.setOnItemClickListener(itemListener);
}
use of com.github.dedis.popstellar.model.objects.Election in project popstellar by dedis.
the class CastVoteFragment method onCreateView.
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
CastVoteFragmentBinding mCastVoteFragBinding = CastVoteFragmentBinding.inflate(inflater, container, false);
mLaoDetailViewModel = LaoDetailActivity.obtainViewModel(requireActivity());
TextView laoNameView = mCastVoteFragBinding.castVoteLaoName;
TextView electionNameView = mCastVoteFragBinding.castVoteElectionName;
// setUp the cast Vote button
voteButton = mCastVoteFragBinding.castVoteButton;
voteButton.setEnabled(false);
// Getting election
Election election = mLaoDetailViewModel.getCurrentElection();
// Setting the Lao Name
laoNameView.setText(mLaoDetailViewModel.getCurrentLaoName().getValue());
// Setting election name
electionNameView.setText(election.getName());
int numberOfQuestions = election.getElectionQuestions().size();
// Setting up the votes for the adapter
mLaoDetailViewModel.setCurrentElectionVotes(setEmptyVoteList(numberOfQuestions));
// Setting the viewPager and its adapter
ViewPager2 viewPager2 = mCastVoteFragBinding.castVotePager;
CastVoteViewPagerAdapter adapter = new CastVoteViewPagerAdapter(mLaoDetailViewModel, mCastVoteFragBinding);
viewPager2.setAdapter(adapter);
viewPager2.setPageTransformer(new ZoomOutTransformer());
// Setting the indicator for horizontal swipe
CircleIndicator3 circleIndicator = mCastVoteFragBinding.swipeIndicator;
circleIndicator.setViewPager(viewPager2);
voteButton.setOnClickListener(buttonListener);
return mCastVoteFragBinding.getRoot();
}
use of com.github.dedis.popstellar.model.objects.Election in project popstellar by dedis.
the class ElectionHandlerTest method testHandleElectionSetup.
@Test
public void testHandleElectionSetup() throws DataHandlingException {
// Create the setup Election message
ElectionSetup electionSetup = new ElectionSetup("election 2", election.getCreation(), election.getStartTimestamp(), election.getEndTimestamp(), Collections.singletonList(electionQuestion.getVotingMethod()), Collections.singletonList(electionQuestion.getWriteIn()), Collections.singletonList(electionQuestion.getBallotOptions()), Collections.singletonList(electionQuestion.getQuestion()), lao.getId());
MessageGeneral message = new MessageGeneral(SENDER_KEY, electionSetup, GSON);
// Call the message handler
messageHandler.handleMessage(laoRepository, messageSender, LAO_CHANNEL, message);
// Check the Election is present with state OPENED and the correct ID
Optional<Election> electionOpt = laoRepository.getLaoByChannel(LAO_CHANNEL).getElection(electionSetup.getId());
assertTrue(electionOpt.isPresent());
assertEquals(EventState.OPENED, electionOpt.get().getState());
assertEquals(electionSetup.getId(), electionOpt.get().getId());
// Check the WitnessMessage has been created
Optional<WitnessMessage> witnessMessage = laoRepository.getLaoByChannel(LAO_CHANNEL).getWitnessMessage(message.getMessageId());
assertTrue(witnessMessage.isPresent());
// Check the Witness message contains the expected title and description
WitnessMessage expectedMessage = electionSetupWitnessMessage(message.getMessageId(), electionOpt.get());
assertEquals(expectedMessage.getTitle(), witnessMessage.get().getTitle());
assertEquals(expectedMessage.getDescription(), witnessMessage.get().getDescription());
}
Aggregations