use of com.github.dedis.popstellar.model.objects.Election in project popstellar by dedis.
the class ElectionHandlerTest method testHandleElectionEnd.
@Test
public void testHandleElectionEnd() throws DataHandlingException {
// Create the end Election message
ElectionEnd electionEnd = new ElectionEnd(election.getId(), lao.getId(), "");
MessageGeneral message = new MessageGeneral(SENDER_KEY, electionEnd, GSON);
// Call the message handler
messageHandler.handleMessage(laoRepository, messageSender, LAO_CHANNEL.subChannel(election.getId()), message);
// Check the Election is present with state CLOSED and the results
Optional<Election> electionOpt = laoRepository.getLaoByChannel(LAO_CHANNEL).getElection(election.getId());
assertTrue(electionOpt.isPresent());
assertEquals(EventState.CLOSED, electionOpt.get().getState());
}
use of com.github.dedis.popstellar.model.objects.Election in project popstellar by dedis.
the class ElectionStartFragment method onCreateView.
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ElectionStartFragmentBinding binding = ElectionStartFragmentBinding.inflate(inflater, container, false);
electionStart = binding.electionStart;
electionStatus = binding.electionStatus;
LaoDetailViewModel mLaoDetailViewModel = LaoDetailActivity.obtainViewModel(requireActivity());
Election election = mLaoDetailViewModel.getCurrentElection();
if (election == null) {
Log.e(TAG, "The current election of the LaoDetailViewModel is null");
return null;
}
String scheduledDate = dateFormat.format(new Date(election.getStartTimestampInMillis()));
String electionId = election.getId();
String instanceId = ElectInstance.generateConsensusId("election", electionId, "state");
binding.electionTitle.setText(getString(R.string.election_start_title, election.getName()));
electionStatus.setText(R.string.waiting_scheduled_time);
electionStart.setText(getString(R.string.election_scheduled, scheduledDate));
electionStart.setEnabled(false);
setupTimerUpdate(election);
setupButtonListeners(binding, mLaoDetailViewModel, electionId);
Lao lao = mLaoDetailViewModel.getCurrentLaoValue();
List<ConsensusNode> nodes = lao.getNodes();
ownNode = lao.getNode(mLaoDetailViewModel.getPublicKey());
if (ownNode == null) {
// Only possible if the user wasn't an acceptor, but shouldn't have access to this fragment
Log.e(TAG, "Couldn't find the Node with public key : " + mLaoDetailViewModel.getPublicKey());
throw new IllegalStateException("Only acceptors are allowed to access ElectionStartFragment");
}
NodesAcceptorAdapter adapter = new NodesAcceptorAdapter(nodes, ownNode, instanceId, getViewLifecycleOwner(), mLaoDetailViewModel);
GridView gridView = binding.nodesGrid;
gridView.setAdapter(adapter);
if (isElectionStartTimePassed(election)) {
updateStartAndStatus(nodes, election, instanceId);
}
mLaoDetailViewModel.getNodes().observe(getViewLifecycleOwner(), consensusNodes -> {
Log.d(TAG, "got an update for nodes : " + consensusNodes);
adapter.setList(consensusNodes);
if (isElectionStartTimePassed(election)) {
updateStartAndStatus(consensusNodes, election, instanceId);
}
});
binding.setLifecycleOwner(getViewLifecycleOwner());
return binding.getRoot();
}
use of com.github.dedis.popstellar.model.objects.Election in project popstellar by dedis.
the class ElectionResultPagerAdapter method onBindViewHolder.
@Override
public void onBindViewHolder(@NonNull Pager2ViewHolder holder, int position) {
Election election = mLaoDetailViewModel.getCurrentElection();
// setting the question
ElectionQuestion electionQuestion = election.getElectionQuestions().get(position);
String question = electionQuestion.getQuestion();
holder.questionView.setText(question);
List<QuestionResult> questionResults = election.getResultsForQuestionId(electionQuestion.getId());
List<ElectionResultListAdapter.ElectionResult> electionResults = new ArrayList<>();
for (int i = 0; i < questionResults.size(); i++) {
electionResults.add(new ElectionResultListAdapter.ElectionResult(questionResults.get(i).getBallot(), questionResults.get(i).getCount()));
}
adapter.clear();
adapter.addAll(electionResults);
holder.resultListView.setAdapter(adapter);
}
use of com.github.dedis.popstellar.model.objects.Election in project popstellar by dedis.
the class ElectionResultFragment method onCreateView.
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
ElectionResultFragmentBinding mElectionResultFragBinding = ElectionResultFragmentBinding.inflate(inflater, container, false);
mLaoDetailViewModel = LaoDetailActivity.obtainViewModel(requireActivity());
TextView laoNameView = mElectionResultFragBinding.electionResultLaoName;
TextView electionNameView = mElectionResultFragBinding.electionResultPresentationTitle;
// Getting election
Election election = mLaoDetailViewModel.getCurrentElection();
// Setting the Lao Name
laoNameView.setText(mLaoDetailViewModel.getCurrentLaoName().getValue());
// Setting election name
electionNameView.setText(election.getName());
ElectionResultPagerAdapter adapter = new ElectionResultPagerAdapter(mLaoDetailViewModel);
ViewPager2 viewPager2 = mElectionResultFragBinding.electionResultPager;
viewPager2.setAdapter(adapter);
// Setting the circle indicator
CircleIndicator3 circleIndicator = mElectionResultFragBinding.swipeIndicatorElectionResults;
circleIndicator.setViewPager(viewPager2);
mElectionResultFragBinding.setLifecycleOwner(getActivity());
return mElectionResultFragBinding.getRoot();
}
use of com.github.dedis.popstellar.model.objects.Election in project popstellar by dedis.
the class LaoDetailViewModel method sendVote.
/**
* Sends a ElectionCastVotes message .
*
* <p>Publish a GeneralMessage containing ElectionCastVotes data.
*
* @param votes the corresponding votes for that election
*/
public void sendVote(List<ElectionVote> votes) {
Election election = mCurrentElection.getValue();
if (election == null) {
Log.d(TAG, "failed to retrieve current election");
return;
}
Log.d(TAG, "sending a new vote in election : " + election + " with election start time" + election.getStartTimestamp());
Lao lao = getCurrentLaoValue();
if (lao == null) {
Log.d(TAG, LAO_FAILURE_MESSAGE);
return;
}
try {
PoPToken token = keyManager.getValidPoPToken(lao);
CastVote castVote = new CastVote(votes, election.getId(), lao.getId());
Channel electionChannel = election.getChannel();
Log.d(TAG, PUBLISH_MESSAGE);
Disposable disposable = networkManager.getMessageSender().publish(token, electionChannel, castVote).doFinally(this::openLaoDetail).subscribe(() -> {
Log.d(TAG, "sent a vote successfully");
// Toast ? + send back to election screen or details screen ?
Toast.makeText(getApplication(), "vote successfully sent !", Toast.LENGTH_LONG).show();
}, error -> ErrorUtils.logAndShow(getApplication(), TAG, error, R.string.error_send_vote));
disposables.add(disposable);
} catch (KeyException e) {
ErrorUtils.logAndShow(getApplication(), TAG, e, R.string.error_retrieve_own_token);
}
}
Aggregations