Search in sources :

Example 1 with ConsensusNodeLayoutBinding

use of com.github.dedis.popstellar.databinding.ConsensusNodeLayoutBinding 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)

Aggregations

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 State (com.github.dedis.popstellar.model.objects.ElectInstance.State)1 LaoDetailViewModel (com.github.dedis.popstellar.ui.detail.LaoDetailViewModel)1 List (java.util.List)1 Optional (java.util.Optional)1