Search in sources :

Example 1 with ElectionQuestion

use of com.github.dedis.popstellar.model.network.method.message.data.election.ElectionQuestion in project popstellar by dedis.

the class ElectionHandlerTest method setup.

@Before
public void setup() throws GeneralSecurityException, IOException {
    lenient().when(keyManager.getMainKeyPair()).thenReturn(SENDER_KEY);
    lenient().when(keyManager.getMainPublicKey()).thenReturn(SENDER);
    laoRepository = new LAORepository();
    when(messageSender.subscribe(any())).then(args -> Completable.complete());
    laoRepository = new LAORepository();
    messageHandler = new MessageHandler(DataRegistryModule.provideDataRegistry(), keyManager);
    // Create one LAO
    lao = new Lao(CREATE_LAO.getName(), CREATE_LAO.getOrganizer(), CREATE_LAO.getCreation());
    lao.setLastModified(lao.getCreation());
    // Create one Roll Call and add it to the LAO
    rollCall = new RollCall(lao.getId(), Instant.now().getEpochSecond(), "roll call 1");
    lao.setRollCalls(new HashMap<String, RollCall>() {

        {
            put(rollCall.getId(), rollCall);
        }
    });
    // Create one Election and add it to the LAO
    election = new Election(lao.getId(), Instant.now().getEpochSecond(), "election 1");
    election.setStart(Instant.now().getEpochSecond());
    election.setEnd(Instant.now().getEpochSecond() + 20L);
    election.setChannel(lao.getChannel().subChannel(election.getId()));
    electionQuestion = new ElectionQuestion("question", "Plurality", false, Arrays.asList("a", "b"), election.getId());
    election.setElectionQuestions(Collections.singletonList(electionQuestion));
    lao.setElections(new HashMap<String, Election>() {

        {
            put(election.getId(), election);
        }
    });
    // Add the LAO to the LAORepository
    laoRepository.getLaoById().put(lao.getId(), new LAOState(lao));
    laoRepository.setAllLaoSubject();
    // Add the CreateLao message to the LAORepository
    MessageGeneral createLaoMessage = new MessageGeneral(SENDER_KEY, CREATE_LAO, GSON);
    laoRepository.getMessageById().put(createLaoMessage.getMessageId(), createLaoMessage);
}
Also used : LAOState(com.github.dedis.popstellar.repository.LAOState) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) LAORepository(com.github.dedis.popstellar.repository.LAORepository) ElectionQuestion(com.github.dedis.popstellar.model.network.method.message.data.election.ElectionQuestion) RollCall(com.github.dedis.popstellar.model.objects.RollCall) CreateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao) Lao(com.github.dedis.popstellar.model.objects.Lao) Election(com.github.dedis.popstellar.model.objects.Election) Before(org.junit.Before)

Example 2 with ElectionQuestion

use of com.github.dedis.popstellar.model.network.method.message.data.election.ElectionQuestion in project popstellar by dedis.

the class ElectionResultPagerAdapter method onBindViewHolder.

@Override
public void onBindViewHolder(@NonNull Pager2ViewHolder holder, int position) {
    Election election = mLaoDetailViewModel.getCurrentElection();
    // setting the question
    ElectionQuestion electionQuestion = election.getElectionQuestions().get(position);
    String question = electionQuestion.getQuestion();
    holder.questionView.setText(question);
    List<QuestionResult> questionResults = election.getResultsForQuestionId(electionQuestion.getId());
    List<ElectionResultListAdapter.ElectionResult> electionResults = new ArrayList<>();
    for (int i = 0; i < questionResults.size(); i++) {
        electionResults.add(new ElectionResultListAdapter.ElectionResult(questionResults.get(i).getBallot(), questionResults.get(i).getCount()));
    }
    adapter.clear();
    adapter.addAll(electionResults);
    holder.resultListView.setAdapter(adapter);
}
Also used : ArrayList(java.util.ArrayList) ElectionQuestion(com.github.dedis.popstellar.model.network.method.message.data.election.ElectionQuestion) Election(com.github.dedis.popstellar.model.objects.Election) QuestionResult(com.github.dedis.popstellar.model.network.method.message.data.election.QuestionResult)

Example 3 with ElectionQuestion

use of com.github.dedis.popstellar.model.network.method.message.data.election.ElectionQuestion in project popstellar by dedis.

the class ManageElectionFragment method onCreateView.

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    ElectionManageFragmentBinding mManageElectionFragBinding = ElectionManageFragmentBinding.inflate(inflater, container, false);
    laoDetailViewModel = LaoDetailActivity.obtainViewModel(requireActivity());
    terminate = mManageElectionFragBinding.terminateElection;
    editStartTimeButton = mManageElectionFragBinding.editStartTime;
    editEndTimeButton = mManageElectionFragBinding.editEndTime;
    editName = mManageElectionFragBinding.editName;
    editQuestion = mManageElectionFragBinding.editQuestion;
    TextView currentTime = mManageElectionFragBinding.displayedCurrentTime;
    TextView startTime = mManageElectionFragBinding.displayedStartTime;
    TextView endTime = mManageElectionFragBinding.displayedEndTime;
    editStartDateButton = mManageElectionFragBinding.editStartDate;
    editEndDateButton = mManageElectionFragBinding.editEndDate;
    TextView question = mManageElectionFragBinding.electionQuestion;
    TextView laoName = mManageElectionFragBinding.manageElectionLaoName;
    TextView electionName = mManageElectionFragBinding.manageElectionTitle;
    Date dCurrent = new java.util.Date(// Get's the date based on the unix time stamp
    System.currentTimeMillis());
    Date dStart = new java.util.Date(laoDetailViewModel.getCurrentElection().getStartTimestampInMillis());
    Date dEnd = new java.util.Date(laoDetailViewModel.getCurrentElection().getEndTimestampInMillis());
    currentTime.setText(// Set's the start time in the form dd/MM/yyyy HH:mm z
    DATE_FORMAT.format(dCurrent));
    startTime.setText(DATE_FORMAT.format(dStart));
    endTime.setText(DATE_FORMAT.format(dEnd));
    laoName.setText(laoDetailViewModel.getCurrentLaoName().getValue());
    electionName.setText(laoDetailViewModel.getCurrentElection().getName());
    List<ElectionQuestion> electionQuestions = laoDetailViewModel.getCurrentElection().getElectionQuestions();
    if (electionQuestions.isEmpty()) {
        question.setText("No election question !");
    } else {
        question.setText("Election Question : " + electionQuestions.get(0).getQuestion());
    }
    mManageElectionFragBinding.setLifecycleOwner(getActivity());
    return mManageElectionFragBinding.getRoot();
}
Also used : ElectionManageFragmentBinding(com.github.dedis.popstellar.databinding.ElectionManageFragmentBinding) ElectionQuestion(com.github.dedis.popstellar.model.network.method.message.data.election.ElectionQuestion) TextView(android.widget.TextView) Date(java.util.Date) Nullable(androidx.annotation.Nullable)

Example 4 with ElectionQuestion

use of com.github.dedis.popstellar.model.network.method.message.data.election.ElectionQuestion in project popstellar by dedis.

the class ElectionSetupFragmentTest method multiplePluralityQuestionsTest.

@Test
public void multiplePluralityQuestionsTest() {
    setupViewModel();
    electionName().perform(click(), typeText(ELECTION_NAME), closeSoftKeyboard());
    pickValidDateAndTime();
    // Add Question 1, with 3 ballots options, no write in
    questionText().perform(click(), typeText("Question 1"), closeSoftKeyboard());
    addBallot().perform(click());
    for (int i = 0; i < 3; ++i) {
        ballotOptionAtPosition(i).perform(click(), typeText("answer 1." + i), closeSoftKeyboard());
    }
    // Add Question 2, with 2 ballots options, with write in
    addQuestion().perform(click());
    questionText().perform(click(), typeText("Question 2"), closeSoftKeyboard());
    writeIn().perform(click());
    for (int i = 0; i < 2; ++i) {
        ballotOptionAtPosition(i).perform(click(), typeText("answer 2." + i), closeSoftKeyboard());
    }
    // Submit and intercept the ElectionSetup message
    long minCreation = Instant.now().getEpochSecond();
    submit().perform(click());
    ElectionSetup electionSetup = getInterceptedElectionSetupMsg();
    long maxCreation = Instant.now().getEpochSecond();
    // Check that the creation time was when we submit the ElectionSetup
    assertTrue(minCreation <= electionSetup.getCreation());
    assertTrue(maxCreation >= electionSetup.getCreation());
    // Check the start/end time
    Calendar calendar = Calendar.getInstance();
    calendar.set(YEAR, MONTH_OF_YEAR, DAY_OF_MONTH, HOURS, MINUTES, 0);
    long expectedStartTime = calendar.toInstant().getEpochSecond();
    calendar.set(YEAR, MONTH_OF_YEAR, DAY_OF_MONTH, HOURS + 1, MINUTES, 0);
    long expectedEndTime = calendar.toInstant().getEpochSecond();
    assertEquals(expectedStartTime, electionSetup.getStartTime());
    assertEquals(expectedEndTime, electionSetup.getEndTime());
    assertEquals(ELECTION_NAME, electionSetup.getName());
    assertEquals(LAO.getId(), electionSetup.getLao());
    List<ElectionQuestion> questionList = electionSetup.getQuestions();
    assertEquals(2, questionList.size());
    ElectionQuestion question1 = questionList.get(0);
    ElectionQuestion question2 = questionList.get(1);
    // Check the Questions 1
    assertEquals("Question 1", question1.getQuestion());
    assertFalse(question1.getWriteIn());
    List<String> ballotOptions1 = question1.getBallotOptions();
    assertEquals(3, ballotOptions1.size());
    for (int i = 0; i < 3; ++i) {
        assertEquals("answer 1." + i, ballotOptions1.get(i));
    }
    // Check the Questions 2
    assertEquals("Question 2", question2.getQuestion());
    // assertTrue(question2.getWriteIn());
    List<String> ballotOptions2 = question2.getBallotOptions();
    assertEquals(2, ballotOptions2.size());
    for (int i = 0; i < 2; ++i) {
        assertEquals("answer 2." + i, ballotOptions2.get(i));
    }
}
Also used : Calendar(java.util.Calendar) ElectionQuestion(com.github.dedis.popstellar.model.network.method.message.data.election.ElectionQuestion) ElectionSetup(com.github.dedis.popstellar.model.network.method.message.data.election.ElectionSetup) LargeTest(androidx.test.filters.LargeTest) HiltAndroidTest(dagger.hilt.android.testing.HiltAndroidTest) Test(org.junit.Test)

Example 5 with ElectionQuestion

use of com.github.dedis.popstellar.model.network.method.message.data.election.ElectionQuestion 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)

Aggregations

ElectionQuestion (com.github.dedis.popstellar.model.network.method.message.data.election.ElectionQuestion)5 Election (com.github.dedis.popstellar.model.objects.Election)3 TextView (android.widget.TextView)2 ArrayList (java.util.ArrayList)2 LayoutInflater (android.view.LayoutInflater)1 View (android.view.View)1 ViewGroup (android.view.ViewGroup)1 AbsListView (android.widget.AbsListView)1 AdapterView (android.widget.AdapterView)1 ArrayAdapter (android.widget.ArrayAdapter)1 Button (android.widget.Button)1 ListView (android.widget.ListView)1 NonNull (androidx.annotation.NonNull)1 Nullable (androidx.annotation.Nullable)1 RecyclerView (androidx.recyclerview.widget.RecyclerView)1 LargeTest (androidx.test.filters.LargeTest)1 R (com.github.dedis.popstellar.R)1 CastVoteFragmentBinding (com.github.dedis.popstellar.databinding.CastVoteFragmentBinding)1 ElectionManageFragmentBinding (com.github.dedis.popstellar.databinding.ElectionManageFragmentBinding)1 MessageGeneral (com.github.dedis.popstellar.model.network.method.message.MessageGeneral)1