Search in sources :

Example 11 with Election

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

the class LAORepository method getElectionByChannel.

/**
 * Retrieves the Election in a given channel
 *
 * @param channel the channel on which the election was created
 * @return the election corresponding to this channel
 */
public Election getElectionByChannel(Channel channel) {
    Log.d(TAG, "querying election for channel " + channel);
    if (!channel.isElectionChannel())
        throw new IllegalArgumentException("invalid channel for an election : " + channel);
    Lao lao = getLaoByChannel(channel);
    Optional<Election> electionOption = lao.getElection(channel.extractElectionId());
    if (!electionOption.isPresent()) {
        throw new IllegalArgumentException("the election should be present when receiving a result");
    }
    return electionOption.get();
}
Also used : Lao(com.github.dedis.popstellar.model.objects.Lao) Election(com.github.dedis.popstellar.model.objects.Election)

Example 12 with Election

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

the class EventExpandableListViewAdapter method getGroupView.

/**
 * Gets a View that displays the given group. This View is only for the group--the Views for the
 * group's children will be fetched using {@link #getChildView(int, int, boolean, View,
 * ViewGroup)}.
 *
 * @param groupPosition the position of the group for which the View is returned
 * @param isExpanded whether the group is expanded or collapsed
 * @param convertView the old view to reuse, if possible. You should check that this view is
 *     non-null and of an appropriate type before using. If it is not possible to convert this
 *     view to display the correct data, this method can create a new view. It is not guaranteed
 *     that the convertView will have been previously created by {@link #getGroupView(int,
 *     boolean, View, ViewGroup)}.
 * @param parent the parent that this view will eventually be attached to
 * @return the View corresponding to the group at the specified position
 */
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    EventCategory eventCategory = (EventCategory) getGroup(groupPosition);
    EventCategoryLayoutBinding binding;
    if (convertView == null) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        binding = EventCategoryLayoutBinding.inflate(inflater, parent, false);
    } else {
        binding = DataBindingUtil.getBinding(convertView);
    }
    binding.setCategoryName(eventCategory.name());
    Context context = parent.getContext();
    AddEventListener addEventOnClickListener = () -> {
        Builder builder = new Builder(context);
        builder.setTitle("Select Event Type");
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(context, android.R.layout.select_dialog_singlechoice);
        arrayAdapter.add("Roll-Call Event");
        arrayAdapter.add("Election Event");
        builder.setNegativeButton("Cancel", (dialog, which) -> dialog.dismiss());
        builder.setAdapter(arrayAdapter, ((dialog, which) -> {
            dialog.dismiss();
            viewModel.chooseEventType(EventType.values()[which]);
        }));
        builder.show();
    };
    binding.setIsFutureCategory(eventCategory.equals(FUTURE));
    binding.setViewmodel(viewModel);
    binding.setLifecycleOwner(lifecycleOwner);
    binding.setAddEventListener(addEventOnClickListener);
    binding.executePendingBindings();
    binding.addFutureEventButton.setFocusable(false);
    return binding.getRoot();
}
Also used : Context(android.content.Context) Builder(android.app.AlertDialog.Builder) Context(android.content.Context) R(com.github.dedis.popstellar.R) EventType(com.github.dedis.popstellar.model.objects.event.EventType) EventState(com.github.dedis.popstellar.model.objects.event.EventState) Date(java.util.Date) EventCategory(com.github.dedis.popstellar.model.objects.event.EventCategory) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) BaseExpandableListAdapter(android.widget.BaseExpandableListAdapter) CLOSED(com.github.dedis.popstellar.model.objects.event.EventState.CLOSED) ArrayList(java.util.ArrayList) FUTURE(com.github.dedis.popstellar.model.objects.event.EventCategory.FUTURE) LifecycleOwner(androidx.lifecycle.LifecycleOwner) Locale(java.util.Locale) View(android.view.View) DataBindingUtil(androidx.databinding.DataBindingUtil) Election(com.github.dedis.popstellar.model.objects.Election) LayoutInflater(android.view.LayoutInflater) Event(com.github.dedis.popstellar.model.objects.event.Event) EventLayoutBinding(com.github.dedis.popstellar.databinding.EventLayoutBinding) EventCategoryLayoutBinding(com.github.dedis.popstellar.databinding.EventCategoryLayoutBinding) Instant(java.time.Instant) ViewGroup(android.view.ViewGroup) ArrayAdapter(android.widget.ArrayAdapter) LaoDetailViewModel(com.github.dedis.popstellar.ui.detail.LaoDetailViewModel) List(java.util.List) RESULTS_READY(com.github.dedis.popstellar.model.objects.event.EventState.RESULTS_READY) ElectionDisplayLayoutBinding(com.github.dedis.popstellar.databinding.ElectionDisplayLayoutBinding) RollCall(com.github.dedis.popstellar.model.objects.RollCall) RollCallEventLayoutBinding(com.github.dedis.popstellar.databinding.RollCallEventLayoutBinding) PRESENT(com.github.dedis.popstellar.model.objects.event.EventCategory.PRESENT) PAST(com.github.dedis.popstellar.model.objects.event.EventCategory.PAST) Collections(java.util.Collections) EventCategory(com.github.dedis.popstellar.model.objects.event.EventCategory) EventCategoryLayoutBinding(com.github.dedis.popstellar.databinding.EventCategoryLayoutBinding) LayoutInflater(android.view.LayoutInflater) Builder(android.app.AlertDialog.Builder) ArrayAdapter(android.widget.ArrayAdapter)

Example 13 with Election

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

the class CastVoteViewPagerAdapter method onBindViewHolder.

@Override
public void onBindViewHolder(@NonNull Pager2ViewHolder holder, int position) {
    Election election = mLaoDetailViewModel.getCurrentElection();
    // setting the question
    ElectionQuestion question = election.getElectionQuestions().get(position);
    holder.questionView.setText(question.getQuestion());
    // this will determine the number of option the user can select and vote for
    // If another voting method is implemented in setUp, this can be adapted
    // by default
    int numberOfChoices = 1;
    List<Integer> votes = new ArrayList<>();
    // setting the list view with ballot options
    List<String> ballotOptions = question.getBallotOptions();
    ballotAdapter.clear();
    ballotAdapter.addAll(ballotOptions);
    ListView ballotsListView = holder.ballotsListView;
    ballotsListView.setAdapter(ballotAdapter);
    ballotsListView.setChoiceMode(numberOfChoices > 1 ? AbsListView.CHOICE_MODE_MULTIPLE : AbsListView.CHOICE_MODE_SINGLE);
    AdapterView.OnItemClickListener itemListener = (parent, view, listPosition, id) -> {
        // in this listener the position refers to the index of the question and
        // the list position is the index of the ballot that was clicked on
        ballotsListView.setClickable(false);
        if (numberOfChoices > 1) {
            if (votes.contains(listPosition)) {
                // without the cast listPosition is treated as the index rather than the list element
                votes.remove((Integer) listPosition);
                ballotsListView.setItemChecked(listPosition, false);
            } else if (votes.size() < numberOfChoices) {
                votes.add(listPosition);
                ballotsListView.setItemChecked(listPosition, true);
            } else {
                ballotsListView.setItemChecked(listPosition, false);
            }
        } else {
            if (votes.contains(listPosition)) {
                votes.clear();
                ballotsListView.setItemChecked(listPosition, false);
            } else {
                votes.clear();
                votes.add(listPosition);
            }
        }
        mLaoDetailViewModel.setCurrentElectionQuestionVotes(votes, position);
        ballotsListView.setClickable(true);
        voteButton.setEnabled(checkEachQuestion());
    };
    ballotsListView.setOnItemClickListener(itemListener);
}
Also used : Election(com.github.dedis.popstellar.model.objects.Election) R(com.github.dedis.popstellar.R) LayoutInflater(android.view.LayoutInflater) NonNull(androidx.annotation.NonNull) ElectionQuestion(com.github.dedis.popstellar.model.network.method.message.data.election.ElectionQuestion) AbsListView(android.widget.AbsListView) CastVoteFragmentBinding(com.github.dedis.popstellar.databinding.CastVoteFragmentBinding) ViewGroup(android.view.ViewGroup) ArrayList(java.util.ArrayList) ArrayAdapter(android.widget.ArrayAdapter) LaoDetailViewModel(com.github.dedis.popstellar.ui.detail.LaoDetailViewModel) List(java.util.List) TextView(android.widget.TextView) View(android.view.View) Button(android.widget.Button) AdapterView(android.widget.AdapterView) ListView(android.widget.ListView) RecyclerView(androidx.recyclerview.widget.RecyclerView) AbsListView(android.widget.AbsListView) ListView(android.widget.ListView) ArrayList(java.util.ArrayList) ElectionQuestion(com.github.dedis.popstellar.model.network.method.message.data.election.ElectionQuestion) AdapterView(android.widget.AdapterView) Election(com.github.dedis.popstellar.model.objects.Election)

Example 14 with Election

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

the class CastVoteFragment method onCreateView.

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    CastVoteFragmentBinding mCastVoteFragBinding = CastVoteFragmentBinding.inflate(inflater, container, false);
    mLaoDetailViewModel = LaoDetailActivity.obtainViewModel(requireActivity());
    TextView laoNameView = mCastVoteFragBinding.castVoteLaoName;
    TextView electionNameView = mCastVoteFragBinding.castVoteElectionName;
    // setUp the cast Vote button
    voteButton = mCastVoteFragBinding.castVoteButton;
    voteButton.setEnabled(false);
    // Getting election
    Election election = mLaoDetailViewModel.getCurrentElection();
    // Setting the Lao Name
    laoNameView.setText(mLaoDetailViewModel.getCurrentLaoName().getValue());
    // Setting election name
    electionNameView.setText(election.getName());
    int numberOfQuestions = election.getElectionQuestions().size();
    // Setting up the votes for the adapter
    mLaoDetailViewModel.setCurrentElectionVotes(setEmptyVoteList(numberOfQuestions));
    // Setting the viewPager and its adapter
    ViewPager2 viewPager2 = mCastVoteFragBinding.castVotePager;
    CastVoteViewPagerAdapter adapter = new CastVoteViewPagerAdapter(mLaoDetailViewModel, mCastVoteFragBinding);
    viewPager2.setAdapter(adapter);
    viewPager2.setPageTransformer(new ZoomOutTransformer());
    // Setting the indicator for horizontal swipe
    CircleIndicator3 circleIndicator = mCastVoteFragBinding.swipeIndicator;
    circleIndicator.setViewPager(viewPager2);
    voteButton.setOnClickListener(buttonListener);
    return mCastVoteFragBinding.getRoot();
}
Also used : ViewPager2(androidx.viewpager2.widget.ViewPager2) CircleIndicator3(me.relex.circleindicator.CircleIndicator3) CastVoteViewPagerAdapter(com.github.dedis.popstellar.ui.detail.event.election.adapters.CastVoteViewPagerAdapter) ZoomOutTransformer(com.github.dedis.popstellar.ui.detail.event.election.ZoomOutTransformer) CastVoteFragmentBinding(com.github.dedis.popstellar.databinding.CastVoteFragmentBinding) TextView(android.widget.TextView) Election(com.github.dedis.popstellar.model.objects.Election) AndroidEntryPoint(dagger.hilt.android.AndroidEntryPoint)

Example 15 with Election

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

the class ElectionHandlerTest method testHandleElectionSetup.

@Test
public void testHandleElectionSetup() throws DataHandlingException {
    // Create the setup Election message
    ElectionSetup electionSetup = new ElectionSetup("election 2", election.getCreation(), election.getStartTimestamp(), election.getEndTimestamp(), Collections.singletonList(electionQuestion.getVotingMethod()), Collections.singletonList(electionQuestion.getWriteIn()), Collections.singletonList(electionQuestion.getBallotOptions()), Collections.singletonList(electionQuestion.getQuestion()), lao.getId());
    MessageGeneral message = new MessageGeneral(SENDER_KEY, electionSetup, GSON);
    // Call the message handler
    messageHandler.handleMessage(laoRepository, messageSender, LAO_CHANNEL, message);
    // Check the Election is present with state OPENED and the correct ID
    Optional<Election> electionOpt = laoRepository.getLaoByChannel(LAO_CHANNEL).getElection(electionSetup.getId());
    assertTrue(electionOpt.isPresent());
    assertEquals(EventState.OPENED, electionOpt.get().getState());
    assertEquals(electionSetup.getId(), electionOpt.get().getId());
    // Check the WitnessMessage has been created
    Optional<WitnessMessage> witnessMessage = laoRepository.getLaoByChannel(LAO_CHANNEL).getWitnessMessage(message.getMessageId());
    assertTrue(witnessMessage.isPresent());
    // Check the Witness message contains the expected title and description
    WitnessMessage expectedMessage = electionSetupWitnessMessage(message.getMessageId(), electionOpt.get());
    assertEquals(expectedMessage.getTitle(), witnessMessage.get().getTitle());
    assertEquals(expectedMessage.getDescription(), witnessMessage.get().getDescription());
}
Also used : MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) Election(com.github.dedis.popstellar.model.objects.Election) WitnessMessage(com.github.dedis.popstellar.model.objects.WitnessMessage) ElectionHandler.electionSetupWitnessMessage(com.github.dedis.popstellar.utility.handler.data.ElectionHandler.electionSetupWitnessMessage) ElectionSetup(com.github.dedis.popstellar.model.network.method.message.data.election.ElectionSetup) Test(org.junit.Test)

Aggregations

Election (com.github.dedis.popstellar.model.objects.Election)16 Lao (com.github.dedis.popstellar.model.objects.Lao)8 Channel (com.github.dedis.popstellar.model.objects.Channel)5 LAORepository (com.github.dedis.popstellar.repository.LAORepository)5 MessageGeneral (com.github.dedis.popstellar.model.network.method.message.MessageGeneral)4 TextView (android.widget.TextView)3 ElectionQuestion (com.github.dedis.popstellar.model.network.method.message.data.election.ElectionQuestion)3 LaoDetailViewModel (com.github.dedis.popstellar.ui.detail.LaoDetailViewModel)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 LayoutInflater (android.view.LayoutInflater)2 View (android.view.View)2 ViewGroup (android.view.ViewGroup)2 ArrayAdapter (android.widget.ArrayAdapter)2 ViewPager2 (androidx.viewpager2.widget.ViewPager2)2 R (com.github.dedis.popstellar.R)2 CastVoteFragmentBinding (com.github.dedis.popstellar.databinding.CastVoteFragmentBinding)2 CastVote (com.github.dedis.popstellar.model.network.method.message.data.election.CastVote)2 ElectionResultQuestion (com.github.dedis.popstellar.model.network.method.message.data.election.ElectionResultQuestion)2 QuestionResult (com.github.dedis.popstellar.model.network.method.message.data.election.QuestionResult)2