Search in sources :

Example 6 with Election

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());
}
Also used : ElectionEnd(com.github.dedis.popstellar.model.network.method.message.data.election.ElectionEnd) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) Election(com.github.dedis.popstellar.model.objects.Election) Test(org.junit.Test)

Example 7 with Election

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();
}
Also used : LaoDetailViewModel(com.github.dedis.popstellar.ui.detail.LaoDetailViewModel) ElectionStartFragmentBinding(com.github.dedis.popstellar.databinding.ElectionStartFragmentBinding) Lao(com.github.dedis.popstellar.model.objects.Lao) ConsensusNode(com.github.dedis.popstellar.model.objects.ConsensusNode) GridView(android.widget.GridView) Election(com.github.dedis.popstellar.model.objects.Election) Date(java.util.Date)

Example 8 with Election

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);
}
Also used : ArrayList(java.util.ArrayList) ElectionQuestion(com.github.dedis.popstellar.model.network.method.message.data.election.ElectionQuestion) Election(com.github.dedis.popstellar.model.objects.Election) QuestionResult(com.github.dedis.popstellar.model.network.method.message.data.election.QuestionResult)

Example 9 with Election

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();
}
Also used : ViewPager2(androidx.viewpager2.widget.ViewPager2) ElectionResultFragmentBinding(com.github.dedis.popstellar.databinding.ElectionResultFragmentBinding) ElectionResultPagerAdapter(com.github.dedis.popstellar.ui.detail.event.election.adapters.ElectionResultPagerAdapter) CircleIndicator3(me.relex.circleindicator.CircleIndicator3) TextView(android.widget.TextView) Election(com.github.dedis.popstellar.model.objects.Election)

Example 10 with Election

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);
    }
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) PoPToken(com.github.dedis.popstellar.model.objects.security.PoPToken) Channel(com.github.dedis.popstellar.model.objects.Channel) CastVote(com.github.dedis.popstellar.model.network.method.message.data.election.CastVote) StateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao) Lao(com.github.dedis.popstellar.model.objects.Lao) UpdateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao) Election(com.github.dedis.popstellar.model.objects.Election) KeyException(com.github.dedis.popstellar.utility.error.keys.KeyException)

Aggregations

Election (com.github.dedis.popstellar.model.objects.Election)16 Lao (com.github.dedis.popstellar.model.objects.Lao)8 Channel (com.github.dedis.popstellar.model.objects.Channel)5 LAORepository (com.github.dedis.popstellar.repository.LAORepository)5 MessageGeneral (com.github.dedis.popstellar.model.network.method.message.MessageGeneral)4 TextView (android.widget.TextView)3 ElectionQuestion (com.github.dedis.popstellar.model.network.method.message.data.election.ElectionQuestion)3 LaoDetailViewModel (com.github.dedis.popstellar.ui.detail.LaoDetailViewModel)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 LayoutInflater (android.view.LayoutInflater)2 View (android.view.View)2 ViewGroup (android.view.ViewGroup)2 ArrayAdapter (android.widget.ArrayAdapter)2 ViewPager2 (androidx.viewpager2.widget.ViewPager2)2 R (com.github.dedis.popstellar.R)2 CastVoteFragmentBinding (com.github.dedis.popstellar.databinding.CastVoteFragmentBinding)2 CastVote (com.github.dedis.popstellar.model.network.method.message.data.election.CastVote)2 ElectionResultQuestion (com.github.dedis.popstellar.model.network.method.message.data.election.ElectionResultQuestion)2 QuestionResult (com.github.dedis.popstellar.model.network.method.message.data.election.QuestionResult)2