Search in sources :

Example 1 with ElectionSetupViewPagerAdapter

use of com.github.dedis.popstellar.ui.detail.event.election.adapters.ElectionSetupViewPagerAdapter in project popstellar by dedis.

the class ElectionSetupFragment method onCreateView.

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    mSetupElectionFragBinding = ElectionSetupFragmentBinding.inflate(inflater, container, false);
    mLaoDetailViewModel = LaoDetailActivity.obtainViewModel(requireActivity());
    // Set the view for the date and time
    setDateAndTimeView(mSetupElectionFragBinding.getRoot());
    // Make the textWatcher listen to changes in the start and end date/time
    addEndDateAndTimeListener(submitTextWatcher);
    addStartDateAndTimeListener(submitTextWatcher);
    cancelButton = mSetupElectionFragBinding.electionCancelButton;
    submitButton = mSetupElectionFragBinding.electionSubmitButton;
    electionNameText = mSetupElectionFragBinding.electionSetupName;
    // Add text watchers on the fields that need to be filled
    electionNameText.addTextChangedListener(submitTextWatcher);
    // Set the text widget in layout to current LAO name
    TextView laoNameTextView = mSetupElectionFragBinding.electionSetupLaoName;
    laoNameTextView.setText(mLaoDetailViewModel.getCurrentLaoName().getValue());
    // Set viewPager adapter
    viewPagerAdapter = new ElectionSetupViewPagerAdapter(mLaoDetailViewModel);
    // Set ViewPager
    ViewPager2 viewPager2 = mSetupElectionFragBinding.electionSetupViewPager2;
    viewPager2.setAdapter(viewPagerAdapter);
    // Sets animation on swipe
    viewPager2.setPageTransformer(new ZoomOutTransformer());
    // This sets the indicator of which page we are on
    CircleIndicator3 circleIndicator = mSetupElectionFragBinding.electionSetupSwipeIndicator;
    circleIndicator.setViewPager(viewPager2);
    // This observes if at least one of the question has the minimal information
    viewPagerAdapter.isAnInputValid().observe(getViewLifecycleOwner(), aBoolean -> submitButton.setEnabled(aBoolean && isElectionLevelInputValid()));
    Button addQuestion = mSetupElectionFragBinding.addQuestion;
    addQuestion.setOnClickListener(v -> {
        addQuestion.setEnabled(false);
        viewPagerAdapter.addQuestion();
        // This scales for a few dozens of questions but this is dangerous and  greedy in
        // resources
        // TODO delete this and find a way to keep data on left swipe
        viewPager2.setOffscreenPageLimit(viewPagerAdapter.getNumberOfQuestions());
        // This swipes automatically to new question
        viewPager2.setCurrentItem(viewPager2.getCurrentItem() + 1);
        // Updates the number of circles in the indicator
        circleIndicator.setViewPager(viewPager2);
        addQuestion.setEnabled(true);
    });
    mSetupElectionFragBinding.setLifecycleOwner(getActivity());
    return mSetupElectionFragBinding.getRoot();
}
Also used : ViewPager2(androidx.viewpager2.widget.ViewPager2) CircleIndicator3(me.relex.circleindicator.CircleIndicator3) Button(android.widget.Button) ZoomOutTransformer(com.github.dedis.popstellar.ui.detail.event.election.ZoomOutTransformer) TextView(android.widget.TextView) ElectionSetupViewPagerAdapter(com.github.dedis.popstellar.ui.detail.event.election.adapters.ElectionSetupViewPagerAdapter) Nullable(androidx.annotation.Nullable)

Aggregations

Button (android.widget.Button)1 TextView (android.widget.TextView)1 Nullable (androidx.annotation.Nullable)1 ViewPager2 (androidx.viewpager2.widget.ViewPager2)1 ZoomOutTransformer (com.github.dedis.popstellar.ui.detail.event.election.ZoomOutTransformer)1 ElectionSetupViewPagerAdapter (com.github.dedis.popstellar.ui.detail.event.election.adapters.ElectionSetupViewPagerAdapter)1 CircleIndicator3 (me.relex.circleindicator.CircleIndicator3)1