Search in sources :

Example 1 with LaoDetailViewModel

use of com.github.dedis.popstellar.ui.detail.LaoDetailViewModel in project popstellar by dedis.

the class ElectionStartFragmentTest method setupViewModel.

private void setupViewModel(long electionStart) {
    election.setStart(electionStart);
    fragmentRule.getScenario().onFragment(electionStartFragment -> {
        FragmentActivity fragmentActivity = electionStartFragment.requireActivity();
        LaoDetailViewModel laoDetailViewModel = LaoDetailActivity.obtainViewModel(fragmentActivity);
        laoDetailViewModel.setCurrentElection(election);
        laoDetailViewModel.setCurrentLao(lao);
    });
    // Recreate the fragment because the viewModel needed to be modified before start
    fragmentRule.getScenario().recreate();
}
Also used : FragmentActivity(androidx.fragment.app.FragmentActivity) LaoDetailViewModel(com.github.dedis.popstellar.ui.detail.LaoDetailViewModel)

Example 2 with LaoDetailViewModel

use of com.github.dedis.popstellar.ui.detail.LaoDetailViewModel 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 3 with LaoDetailViewModel

use of com.github.dedis.popstellar.ui.detail.LaoDetailViewModel in project popstellar by dedis.

the class ElectionSetupFragmentTest method setupViewModel.

private void setupViewModel() {
    fragmentRule.getScenario().onActivity(activity -> {
        LaoDetailViewModel laoDetailViewModel = LaoDetailActivity.obtainViewModel(activity);
        laoDetailViewModel.setCurrentLao(LAO);
    });
}
Also used : LaoDetailViewModel(com.github.dedis.popstellar.ui.detail.LaoDetailViewModel)

Aggregations

LaoDetailViewModel (com.github.dedis.popstellar.ui.detail.LaoDetailViewModel)3 GridView (android.widget.GridView)1 FragmentActivity (androidx.fragment.app.FragmentActivity)1 ElectionStartFragmentBinding (com.github.dedis.popstellar.databinding.ElectionStartFragmentBinding)1 ConsensusNode (com.github.dedis.popstellar.model.objects.ConsensusNode)1 Election (com.github.dedis.popstellar.model.objects.Election)1 Lao (com.github.dedis.popstellar.model.objects.Lao)1 Date (java.util.Date)1