Search in sources :

Example 1 with State

use of com.github.dedis.popstellar.model.objects.ElectInstance.State in project popstellar by dedis.

the class NodesAcceptorAdapter method getView.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ConsensusNodeLayoutBinding binding;
    if (convertView == null) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        binding = ConsensusNodeLayoutBinding.inflate(inflater);
    } else {
        binding = DataBindingUtil.getBinding(convertView);
    }
    if (binding == null) {
        throw new IllegalStateException("Binding could not be find in the view");
    }
    ConsensusNode node = getItem(position);
    Optional<ElectInstance> lastElectInstance = node.getLastElectInstance(instanceId);
    State state = node.getState(instanceId);
    boolean alreadyAccepted = lastElectInstance.map(ElectInstance::getMessageId).map(ownNode.getAcceptedMessageIds()::contains).orElse(false);
    String text = "";
    switch(state) {
        case FAILED:
            text = "Start Failed\n";
            break;
        case STARTING:
            text = "Approve Start by\n";
            break;
        case WAITING:
            text = "Waiting\n";
            break;
        case ACCEPTED:
            text = "Started by\n";
    }
    text += node.getPublicKey().getEncoded();
    binding.nodeButton.setText(text);
    binding.nodeButton.setEnabled(state == State.STARTING && !alreadyAccepted);
    lastElectInstance.ifPresent(electInstance -> binding.nodeButton.setOnClickListener(clicked -> laoDetailViewModel.sendConsensusElectAccept(electInstance, true)));
    binding.setLifecycleOwner(lifecycleOwner);
    binding.executePendingBindings();
    return binding.getRoot();
}
Also used : ConsensusNodeLayoutBinding(com.github.dedis.popstellar.databinding.ConsensusNodeLayoutBinding) LayoutInflater(android.view.LayoutInflater) ConsensusNode(com.github.dedis.popstellar.model.objects.ConsensusNode) ElectInstance(com.github.dedis.popstellar.model.objects.ElectInstance) ViewGroup(android.view.ViewGroup) LaoDetailViewModel(com.github.dedis.popstellar.ui.detail.LaoDetailViewModel) List(java.util.List) BaseAdapter(android.widget.BaseAdapter) State(com.github.dedis.popstellar.model.objects.ElectInstance.State) LifecycleOwner(androidx.lifecycle.LifecycleOwner) View(android.view.View) Optional(java.util.Optional) DataBindingUtil(androidx.databinding.DataBindingUtil) ElectInstance(com.github.dedis.popstellar.model.objects.ElectInstance) State(com.github.dedis.popstellar.model.objects.ElectInstance.State) LayoutInflater(android.view.LayoutInflater) ConsensusNode(com.github.dedis.popstellar.model.objects.ConsensusNode) ConsensusNodeLayoutBinding(com.github.dedis.popstellar.databinding.ConsensusNodeLayoutBinding)

Example 2 with State

use of com.github.dedis.popstellar.model.objects.ElectInstance.State in project popstellar by dedis.

the class ElectionStartFragment method updateStartAndStatus.

private void updateStartAndStatus(List<ConsensusNode> nodes, Election election, String instanceId) {
    boolean isAnyElectInstanceAccepted = nodes.stream().map(node -> node.getLastElectInstance(instanceId)).filter(Optional::isPresent).map(Optional::get).map(ElectInstance::getState).anyMatch(State.ACCEPTED::equals);
    if (isAnyElectInstanceAccepted) {
        // assuming the election start time was updated from scheduled to real start time
        String startedDate = dateFormat.format(new Date(election.getStartTimestampInMillis()));
        electionStatus.setText(R.string.started);
        electionStart.setText(getString(R.string.election_started_at, startedDate));
        electionStart.setEnabled(false);
    } else {
        State ownState = ownNode.getState(instanceId);
        boolean canClick = ownState == State.WAITING || ownState == State.FAILED;
        electionStart.setEnabled(canClick);
    }
}
Also used : Optional(java.util.Optional) State(com.github.dedis.popstellar.model.objects.ElectInstance.State) Date(java.util.Date)

Aggregations

State (com.github.dedis.popstellar.model.objects.ElectInstance.State)2 Optional (java.util.Optional)2 LayoutInflater (android.view.LayoutInflater)1 View (android.view.View)1 ViewGroup (android.view.ViewGroup)1 BaseAdapter (android.widget.BaseAdapter)1 DataBindingUtil (androidx.databinding.DataBindingUtil)1 LifecycleOwner (androidx.lifecycle.LifecycleOwner)1 ConsensusNodeLayoutBinding (com.github.dedis.popstellar.databinding.ConsensusNodeLayoutBinding)1 ConsensusNode (com.github.dedis.popstellar.model.objects.ConsensusNode)1 ElectInstance (com.github.dedis.popstellar.model.objects.ElectInstance)1 LaoDetailViewModel (com.github.dedis.popstellar.ui.detail.LaoDetailViewModel)1 Date (java.util.Date)1 List (java.util.List)1