Search in sources :

Example 1 with ElectionDisplayLayoutBinding

use of com.github.dedis.popstellar.databinding.ElectionDisplayLayoutBinding in project popstellar by dedis.

the class EventExpandableListViewAdapter method setupElectionElement.

/**
 * A helper method used to handle the display of the elections
 *
 * @param election the election Event
 * @param category the event category ( Past,Present or Future)
 * @param layoutEventBinding the binding of the generic layoutEvent that we use to display events
 * @return the View corresponding to the child at the specified position
 */
private View setupElectionElement(Election election, EventCategory category, EventLayoutBinding layoutEventBinding) {
    ElectionDisplayLayoutBinding electionBinding = layoutEventBinding.includeLayoutElection;
    electionBinding.setElection(election);
    Date dStart = new java.util.Date(election.getStartTimestampInMillis());
    String dateStart = DATE_FORMAT.format(dStart);
    electionBinding.electionStartDate.setText("Start date : " + dateStart);
    Date dEnd = new java.util.Date(election.getEndTimestampInMillis());
    String dateEnd = DATE_FORMAT.format(dEnd);
    electionBinding.electionEndDate.setText("End Date : " + dateEnd);
    viewModel.setCurrentElection(election);
    viewModel.getEndElectionEvent().observe(lifecycleOwner, end -> {
        electionBinding.electionActionButton.setText(R.string.election_ended);
        electionBinding.electionActionButton.setEnabled(false);
    });
    if (category == PRESENT) {
        electionBinding.electionActionButton.setText(R.string.cast_vote);
        electionBinding.electionActionButton.setEnabled(true);
        electionBinding.electionActionButton.setOnClickListener(clicked -> {
            viewModel.setCurrentElection(election);
            viewModel.openCastVotes();
        });
    } else if (category == PAST) {
        electionBinding.electionActionButton.setEnabled(true);
        if (!viewModel.isOrganizer().getValue()) {
            electionBinding.electionActionButton.setEnabled(false);
        }
        if (election.getState() == CLOSED) {
            electionBinding.electionActionButton.setText(R.string.election_ended);
            electionBinding.electionActionButton.setEnabled(false);
        } else if (election.getState() == RESULTS_READY) {
            electionBinding.electionActionButton.setText(R.string.show_results);
            electionBinding.electionActionButton.setEnabled(true);
            electionBinding.electionActionButton.setOnClickListener(clicked -> {
                viewModel.setCurrentElection(election);
                viewModel.openElectionResults(true);
            });
        } else {
            electionBinding.electionActionButton.setText(R.string.tally_votes);
            electionBinding.electionActionButton.setOnClickListener(clicked -> {
                viewModel.setCurrentElection(election);
                viewModel.endElection(election);
            });
        }
    }
    electionBinding.electionEditButton.setOnClickListener(clicked -> {
        viewModel.setCurrentElection(election);
        viewModel.openManageElection(true);
    });
    electionBinding.detailsButton.setOnClickListener(clicked -> {
        viewModel.setCurrentElection(election);
        viewModel.openStartElection(true);
    });
    electionBinding.detailsButton.setEnabled(viewModel.isWitness().getValue() || viewModel.isOrganizer().getValue());
    electionBinding.setEventCategory(category);
    electionBinding.setViewModel(viewModel);
    electionBinding.setLifecycleOwner(lifecycleOwner);
    electionBinding.executePendingBindings();
    return layoutEventBinding.getRoot();
}
Also used : ElectionDisplayLayoutBinding(com.github.dedis.popstellar.databinding.ElectionDisplayLayoutBinding) Date(java.util.Date)

Aggregations

ElectionDisplayLayoutBinding (com.github.dedis.popstellar.databinding.ElectionDisplayLayoutBinding)1 Date (java.util.Date)1