Search in sources :

Example 1 with LAOState

use of com.github.dedis.popstellar.repository.LAOState in project popstellar by dedis.

the class HomeViewModel method onQRCodeDetected.

@Override
public void onQRCodeDetected(Barcode barcode) {
    Log.d(TAG, "Detected barcode with value: " + barcode.rawValue);
    ConnectToLao data;
    try {
        data = ConnectToLao.extractFrom(gson, barcode.rawValue);
    } catch (JsonParseException e) {
        Log.e(TAG, "Invalid QRCode data", e);
        Toast.makeText(getApplication().getApplicationContext(), "Invalid QRCode data", Toast.LENGTH_LONG).show();
        return;
    }
    networkManager.connect(data.server);
    Lao lao = new Lao(data.lao);
    disposables.add(networkManager.getMessageSender().subscribe(lao.getChannel()).doFinally(this::openHome).subscribe(() -> {
        Log.d(TAG, "subscribing to LAO with id " + lao.getId());
        // Create the new LAO and add it to the LAORepository LAO lists
        laoRepository.getLaoById().put(lao.getId(), new LAOState(lao));
        laoRepository.setAllLaoSubject();
        Log.d(TAG, "got success result for subscribe to lao");
    }, error -> ErrorUtils.logAndShow(getApplication(), TAG, error, R.string.error_subscribe_lao)));
    setConnectingLao(lao.getId());
    openConnecting();
}
Also used : MutableLiveData(androidx.lifecycle.MutableLiveData) JsonParseException(com.google.gson.JsonParseException) R(com.github.dedis.popstellar.R) PackageManager(android.content.pm.PackageManager) NonNull(androidx.annotation.NonNull) ErrorUtils(com.github.dedis.popstellar.utility.error.ErrorUtils) SeedValidationException(com.github.dedis.popstellar.utility.error.keys.SeedValidationException) LiveDataReactiveStreams(androidx.lifecycle.LiveDataReactiveStreams) LAORepository(com.github.dedis.popstellar.repository.LAORepository) Inject(javax.inject.Inject) ConnectToLao(com.github.dedis.popstellar.model.qrcode.ConnectToLao) Manifest(android.Manifest) GeneralSecurityException(java.security.GeneralSecurityException) CreateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao) Lao(com.github.dedis.popstellar.model.objects.Lao) Gson(com.google.gson.Gson) Toast(android.widget.Toast) AndroidViewModel(androidx.lifecycle.AndroidViewModel) Log(android.util.Log) LiveData(androidx.lifecycle.LiveData) Channel(com.github.dedis.popstellar.model.objects.Channel) BackpressureStrategy(io.reactivex.BackpressureStrategy) KeyManager(com.github.dedis.popstellar.utility.security.KeyManager) ActivityCompat(androidx.core.app.ActivityCompat) LAOState(com.github.dedis.popstellar.repository.LAOState) Barcode(com.google.android.gms.vision.barcode.Barcode) HiltViewModel(dagger.hilt.android.lifecycle.HiltViewModel) QRCodeScanningViewModel(com.github.dedis.popstellar.ui.qrcode.QRCodeScanningViewModel) ScanningAction(com.github.dedis.popstellar.ui.qrcode.ScanningAction) List(java.util.List) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Application(android.app.Application) SingleEvent(com.github.dedis.popstellar.SingleEvent) Wallet(com.github.dedis.popstellar.model.objects.Wallet) GlobalNetworkManager(com.github.dedis.popstellar.repository.remote.GlobalNetworkManager) CameraPermissionViewModel(com.github.dedis.popstellar.ui.qrcode.CameraPermissionViewModel) LAOState(com.github.dedis.popstellar.repository.LAOState) ConnectToLao(com.github.dedis.popstellar.model.qrcode.ConnectToLao) ConnectToLao(com.github.dedis.popstellar.model.qrcode.ConnectToLao) CreateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao) Lao(com.github.dedis.popstellar.model.objects.Lao) JsonParseException(com.google.gson.JsonParseException)

Example 2 with LAOState

use of com.github.dedis.popstellar.repository.LAOState in project popstellar by dedis.

the class ChirpHandlerTest method setup.

@Before
public void setup() throws GeneralSecurityException, DataHandlingException, IOException {
    lenient().when(keyManager.getMainKeyPair()).thenReturn(SENDER_KEY);
    lenient().when(keyManager.getMainPublicKey()).thenReturn(SENDER);
    when(messageSender.subscribe(any())).then(args -> Completable.complete());
    laoRepository = new LAORepository();
    messageHandler = new MessageHandler(DataRegistryModule.provideDataRegistry(), keyManager);
    laoRepository.getLaoById().put(LAO.getId(), new LAOState(LAO));
    MessageGeneral createLaoMessage = new MessageGeneral(SENDER_KEY, CREATE_LAO, GSON);
    messageHandler.handleMessage(laoRepository, messageSender, LAO.getChannel(), 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) Before(org.junit.Before)

Example 3 with LAOState

use of com.github.dedis.popstellar.repository.LAOState 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 4 with LAOState

use of com.github.dedis.popstellar.repository.LAOState in project popstellar by dedis.

the class HomeViewModel method launchLao.

/**
 * launchLao is invoked when the user tries to create a new LAO. The method creates a `CreateLAO`
 * message and publishes it to the root channel. It observers the response in the background and
 * switches to the home screen on success.
 */
public void launchLao() {
    String laoName = mLaoName.getValue();
    Log.d(TAG, "creating lao with name " + laoName);
    CreateLao createLao = new CreateLao(laoName, keyManager.getMainPublicKey());
    disposables.add(networkManager.getMessageSender().publish(keyManager.getMainKeyPair(), Channel.ROOT, createLao).subscribe(() -> {
        Log.d(TAG, "got success result for create lao");
        // Create new LAO and add it to the LAORepository LAO lists
        Lao lao = new Lao(createLao.getId());
        laoRepository.getLaoById().put(lao.getId(), new LAOState(lao));
        laoRepository.setAllLaoSubject();
        // Send subscribe and catchup after creating a LAO
        networkManager.getMessageSender().subscribe(lao.getChannel()).subscribe();
        openHome();
    }, error -> ErrorUtils.logAndShow(getApplication(), TAG, error, R.string.error_create_lao)));
}
Also used : MutableLiveData(androidx.lifecycle.MutableLiveData) JsonParseException(com.google.gson.JsonParseException) R(com.github.dedis.popstellar.R) PackageManager(android.content.pm.PackageManager) NonNull(androidx.annotation.NonNull) ErrorUtils(com.github.dedis.popstellar.utility.error.ErrorUtils) SeedValidationException(com.github.dedis.popstellar.utility.error.keys.SeedValidationException) LiveDataReactiveStreams(androidx.lifecycle.LiveDataReactiveStreams) LAORepository(com.github.dedis.popstellar.repository.LAORepository) Inject(javax.inject.Inject) ConnectToLao(com.github.dedis.popstellar.model.qrcode.ConnectToLao) Manifest(android.Manifest) GeneralSecurityException(java.security.GeneralSecurityException) CreateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao) Lao(com.github.dedis.popstellar.model.objects.Lao) Gson(com.google.gson.Gson) Toast(android.widget.Toast) AndroidViewModel(androidx.lifecycle.AndroidViewModel) Log(android.util.Log) LiveData(androidx.lifecycle.LiveData) Channel(com.github.dedis.popstellar.model.objects.Channel) BackpressureStrategy(io.reactivex.BackpressureStrategy) KeyManager(com.github.dedis.popstellar.utility.security.KeyManager) ActivityCompat(androidx.core.app.ActivityCompat) LAOState(com.github.dedis.popstellar.repository.LAOState) Barcode(com.google.android.gms.vision.barcode.Barcode) HiltViewModel(dagger.hilt.android.lifecycle.HiltViewModel) QRCodeScanningViewModel(com.github.dedis.popstellar.ui.qrcode.QRCodeScanningViewModel) ScanningAction(com.github.dedis.popstellar.ui.qrcode.ScanningAction) List(java.util.List) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Application(android.app.Application) SingleEvent(com.github.dedis.popstellar.SingleEvent) Wallet(com.github.dedis.popstellar.model.objects.Wallet) GlobalNetworkManager(com.github.dedis.popstellar.repository.remote.GlobalNetworkManager) CameraPermissionViewModel(com.github.dedis.popstellar.ui.qrcode.CameraPermissionViewModel) CreateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao) LAOState(com.github.dedis.popstellar.repository.LAOState) ConnectToLao(com.github.dedis.popstellar.model.qrcode.ConnectToLao) CreateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao) Lao(com.github.dedis.popstellar.model.objects.Lao)

Example 5 with LAOState

use of com.github.dedis.popstellar.repository.LAOState in project popstellar by dedis.

the class MessageHandler method notifyLaoUpdate.

/**
 * Keep the UI up to date by notifying all observers the updated LAO state.
 *
 * <p>The LAO is updated if the channel of the message is a LAO channel and the message is not a
 * WitnessSignatureMessage.
 *
 * <p>If a LAO has been created or modified then the LAO lists in the LAORepository are updated.
 *
 * @param laoRepository the repository to access the LAO lists
 * @param data the data received
 * @param channel the channel of the message received
 */
private void notifyLaoUpdate(LAORepository laoRepository, Data data, Channel channel) {
    if (!(data instanceof WitnessMessageSignature) && channel.isLaoChannel()) {
        LAOState laoState = laoRepository.getLaoById().get(channel.extractLaoId());
        // Trigger an onNext
        laoState.publish();
        if (data instanceof StateLao || data instanceof CreateLao) {
            laoRepository.setAllLaoSubject();
        }
    }
}
Also used : WitnessMessageSignature(com.github.dedis.popstellar.model.network.method.message.data.message.WitnessMessageSignature) LAOState(com.github.dedis.popstellar.repository.LAOState) CreateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao) StateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao)

Aggregations

LAOState (com.github.dedis.popstellar.repository.LAOState)8 CreateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao)7 LAORepository (com.github.dedis.popstellar.repository.LAORepository)7 Lao (com.github.dedis.popstellar.model.objects.Lao)6 MessageGeneral (com.github.dedis.popstellar.model.network.method.message.MessageGeneral)5 Before (org.junit.Before)5 Manifest (android.Manifest)2 Application (android.app.Application)2 PackageManager (android.content.pm.PackageManager)2 Log (android.util.Log)2 Toast (android.widget.Toast)2 NonNull (androidx.annotation.NonNull)2 ActivityCompat (androidx.core.app.ActivityCompat)2 AndroidViewModel (androidx.lifecycle.AndroidViewModel)2 LiveData (androidx.lifecycle.LiveData)2 LiveDataReactiveStreams (androidx.lifecycle.LiveDataReactiveStreams)2 MutableLiveData (androidx.lifecycle.MutableLiveData)2 R (com.github.dedis.popstellar.R)2 SingleEvent (com.github.dedis.popstellar.SingleEvent)2 StateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao)2