<commit_before>package ee.ria.DigiDoc.android.crypto.create;

import android.support.annotation.Nullable;

import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;

import java.io.File;

import ee.ria.DigiDoc.android.model.idcard.IdCardData;
import ee.ria.DigiDoc.android.model.idcard.IdCardDataResponse;
import ee.ria.DigiDoc.android.utils.mvi.MviResult;
import ee.ria.DigiDoc.android.utils.mvi.State;
import ee.ria.DigiDoc.common.Certificate;
import ee.ria.DigiDoc.crypto.CryptoContainer;
import ee.ria.DigiDoc.crypto.Pin1InvalidException;

interface Result extends MviResult<ViewState> {

    @AutoValue
    abstract class InitialResult implements Result {

        @State abstract String state();

        @Nullable abstract File containerFile();
        @Nullable abstract ImmutableList<File> dataFiles();

        @Nullable abstract CryptoContainer container();

        @Nullable abstract Throwable error();

        @Override
        public ViewState reduce(ViewState state) {
            File containerFile = containerFile();
            ImmutableList<File> dataFiles = dataFiles();
            CryptoContainer container = container();

            ViewState.Builder builder = state.buildWith()
                    .dataFilesAddState(state())
                    .dataFilesAddError(error())
                    .dataFilesViewEnabled(false)
                    .dataFilesAddEnabled(false)
                    .dataFilesRemoveEnabled(false)
                    .recipientsAddEnabled(false)
                    .recipientsRemoveEnabled(false)
                    .encryptButtonVisible(false)
                    .decryptButtonVisible(false)
                    .sendButtonVisible(false);

            if (containerFile != null && dataFiles != null) {
                builder
                        .name(containerFile.getName())
                        .dataFiles(dataFiles)
                        .dataFilesViewEnabled(true)
                        .dataFilesAddEnabled(true)
                        .dataFilesRemoveEnabled(true)
                        .recipientsAddEnabled(true)
                        .recipientsRemoveEnabled(true)
                        .encryptButtonVisible(true);
            } else if (container != null) {
                builder
                        .containerFile(container.file())
                        .name(container.file().getName())
                        .dataFiles(container.dataFiles())
                        .recipients(container.recipients())
                        .decryptButtonVisible(true)
                        .sendButtonVisible(true);
            }

            return builder.build();
        }

        static InitialResult activity() {
            return create(State.ACTIVE, null, null, null, null);
        }

        static InitialResult success(File containerFile, ImmutableList<File> dataFiles) {
            return create(State.IDLE, containerFile, dataFiles, null, null);
        }

        static InitialResult success(CryptoContainer container) {
            return create(State.IDLE, null, null, container, null);
        }

        static InitialResult failure(Throwable error) {
            return create(State.IDLE, null, null, null, error);
        }

        static InitialResult clear() {
            return create(State.IDLE, null, null, null, null);
        }

        private static InitialResult create(@State String state, @Nullable File containerFile,
                                            @Nullable ImmutableList<File> dataFiles,
                                            @Nullable CryptoContainer container,
                                            @Nullable Throwable error) {
            return new AutoValue_Result_InitialResult(state, containerFile, dataFiles, container,
                    error);
        }
    }

    @AutoValue
    abstract class DataFilesAddResult implements Result {

        @State abstract String state();

        @Nullable abstract ImmutableList<File> dataFiles();

        @Nullable abstract Throwable error();

        @Override
        public ViewState reduce(ViewState state) {
            ViewState.Builder builder = state.buildWith()
                    .dataFilesAddState(state())
                    .dataFilesAddError(error());
            if (dataFiles() != null) {
                builder.dataFiles(dataFiles());
            }
            return builder.build();
        }

        static DataFilesAddResult activity() {
            return create(State.ACTIVE, null, null);
        }

        static DataFilesAddResult success(ImmutableList<File> dataFiles) {
            return create(State.IDLE, dataFiles, null);
        }

        static DataFilesAddResult failure(Throwable error) {
            return create(State.IDLE, null, error);
        }

        static DataFilesAddResult clear() {
            return create(State.IDLE, null, null);
        }

        private static DataFilesAddResult create(@State String state,
                                                 @Nullable ImmutableList<File> dataFiles,
                                                 @Nullable Throwable error) {
            return new AutoValue_Result_DataFilesAddResult(state, dataFiles, error);
        }
    }

    @AutoValue
    abstract class DataFileRemoveResult implements Result {

        abstract ImmutableList<File> dataFiles();

        @Override
        public ViewState reduce(ViewState state) {
            return state.buildWith().dataFiles(dataFiles()).build();
        }

        static DataFileRemoveResult create(ImmutableList<File> dataFiles) {
            return new AutoValue_Result_DataFileRemoveResult(dataFiles);
        }
    }

    @AutoValue
    abstract class RecipientsAddButtonClickResult implements Result {

        @Override
        public ViewState reduce(ViewState state) {
            return state.buildWith().recipientsSearchResult(null).build();
        }

        static RecipientsAddButtonClickResult create() {
            return new AutoValue_Result_RecipientsAddButtonClickResult();
        }
    }

    @AutoValue
    abstract class RecipientsSearchResult implements Result {

        @State abstract String state();

        abstract ImmutableList<Certificate> result();

        @Nullable abstract Throwable error();

        @Override
        public ViewState reduce(ViewState state) {
            return state.buildWith()
                    .recipientsSearchState(state())
                    .recipientsSearchResult(result())
                    .recipientsSearchError(error())
                    .build();
        }

        static RecipientsSearchResult activity() {
            return create(State.ACTIVE, ImmutableList.of(), null);
        }

        static RecipientsSearchResult success(ImmutableList<Certificate> result) {
            return create(State.IDLE, result, null);
        }

        static RecipientsSearchResult failure(Throwable error) {
            return create(State.IDLE, ImmutableList.of(), error);
        }

        static RecipientsSearchResult clear() {
            return create(State.IDLE, ImmutableList.of(), null);
        }

        private static RecipientsSearchResult create(
                @State String state, ImmutableList<Certificate> result, @Nullable Throwable error) {
            return new AutoValue_Result_RecipientsSearchResult(state, result, error);
        }
    }

    @AutoValue
    abstract class RecipientAddResult implements Result {

        abstract ImmutableList<Certificate> recipients();

        @Override
        public ViewState reduce(ViewState state) {
            return state.buildWith().recipients(recipients()).build();
        }

        static RecipientAddResult create(ImmutableList<Certificate> recipients) {
            return new AutoValue_Result_RecipientAddResult(recipients);
        }
    }

    @AutoValue
    abstract class RecipientRemoveResult implements Result {

        abstract ImmutableList<Certificate> recipients();

        @Override
        public ViewState reduce(ViewState state) {
            return state.buildWith().recipients(recipients()).build();
        }

        static RecipientRemoveResult create(ImmutableList<Certificate> recipients) {
            return new AutoValue_Result_RecipientRemoveResult(recipients);
        }
    }

    @AutoValue
    abstract class EncryptResult implements Result {

        @State abstract String state();

        abstract boolean successMessageVisible();

        @Nullable abstract File containerFile();

        @Nullable abstract Throwable error();

        @Override
        public ViewState reduce(ViewState state) {
            ViewState.Builder builder = state.buildWith()
                    .encryptState(state())
                    .encryptSuccessMessageVisible(successMessageVisible())
                    .encryptError(error());
            if (containerFile() != null) {
                builder
                        .containerFile(containerFile())
                        .dataFilesViewEnabled(false)
                        .dataFilesAddEnabled(false)
                        .dataFilesRemoveEnabled(false)
                        .recipientsAddEnabled(false)
                        .recipientsRemoveEnabled(false)
                        .encryptButtonVisible(false)
                        .decryptButtonVisible(true)
                        .sendButtonVisible(true);
            }
            return builder.build();
        }

        static EncryptResult activity() {
            return create(State.ACTIVE, false, null, null);
        }

        static EncryptResult successMessage(File containerFile) {
            return create(State.IDLE, true, containerFile, null);
        }

        static EncryptResult success(File containerFile) {
            return create(State.IDLE, false, containerFile, null);
        }

        static EncryptResult failure(Throwable error) {
            return create(State.IDLE, false, null, error);
        }

        static EncryptResult clear() {
            return create(State.IDLE, false, null, null);
        }

        private static EncryptResult create(@State String state, boolean successMessageVisible,
                                            @Nullable File containerFile,
                                            @Nullable Throwable error) {
            return new AutoValue_Result_EncryptResult(state, successMessageVisible, containerFile,
                    error);
        }
    }

    @AutoValue
    abstract class DecryptionResult implements Result {

        @Nullable abstract IdCardDataResponse idCardDataResponse();

        @Override
        public ViewState reduce(ViewState state) {
            return state.buildWith().decryptionIdCardDataResponse(idCardDataResponse()).build();
        }

        static DecryptionResult show(IdCardDataResponse idCardDataResponse) {
            return create(idCardDataResponse);
        }<commit_msg>Clear decrypt errors when dismissing decrypt dialog<commit_after>package ee.ria.DigiDoc.android.crypto.create;

import android.support.annotation.Nullable;

import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;

import java.io.File;

import ee.ria.DigiDoc.android.model.idcard.IdCardData;
import ee.ria.DigiDoc.android.model.idcard.IdCardDataResponse;
import ee.ria.DigiDoc.android.utils.mvi.MviResult;
import ee.ria.DigiDoc.android.utils.mvi.State;
import ee.ria.DigiDoc.common.Certificate;
import ee.ria.DigiDoc.crypto.CryptoContainer;
import ee.ria.DigiDoc.crypto.Pin1InvalidException;

interface Result extends MviResult<ViewState> {

    @AutoValue
    abstract class InitialResult implements Result {

        @State abstract String state();

        @Nullable abstract File containerFile();
        @Nullable abstract ImmutableList<File> dataFiles();

        @Nullable abstract CryptoContainer container();

        @Nullable abstract Throwable error();

        @Override
        public ViewState reduce(ViewState state) {
            File containerFile = containerFile();
            ImmutableList<File> dataFiles = dataFiles();
            CryptoContainer container = container();

            ViewState.Builder builder = state.buildWith()
                    .dataFilesAddState(state())
                    .dataFilesAddError(error())
                    .dataFilesViewEnabled(false)
                    .dataFilesAddEnabled(false)
                    .dataFilesRemoveEnabled(false)
                    .recipientsAddEnabled(false)
                    .recipientsRemoveEnabled(false)
                    .encryptButtonVisible(false)
                    .decryptButtonVisible(false)
                    .sendButtonVisible(false);

            if (containerFile != null && dataFiles != null) {
                builder
                        .name(containerFile.getName())
                        .dataFiles(dataFiles)
                        .dataFilesViewEnabled(true)
                        .dataFilesAddEnabled(true)
                        .dataFilesRemoveEnabled(true)
                        .recipientsAddEnabled(true)
                        .recipientsRemoveEnabled(true)
                        .encryptButtonVisible(true);
            } else if (container != null) {
                builder
                        .containerFile(container.file())
                        .name(container.file().getName())
                        .dataFiles(container.dataFiles())
                        .recipients(container.recipients())
                        .decryptButtonVisible(true)
                        .sendButtonVisible(true);
            }

            return builder.build();
        }

        static InitialResult activity() {
            return create(State.ACTIVE, null, null, null, null);
        }

        static InitialResult success(File containerFile, ImmutableList<File> dataFiles) {
            return create(State.IDLE, containerFile, dataFiles, null, null);
        }

        static InitialResult success(CryptoContainer container) {
            return create(State.IDLE, null, null, container, null);
        }

        static InitialResult failure(Throwable error) {
            return create(State.IDLE, null, null, null, error);
        }

        static InitialResult clear() {
            return create(State.IDLE, null, null, null, null);
        }

        private static InitialResult create(@State String state, @Nullable File containerFile,
                                            @Nullable ImmutableList<File> dataFiles,
                                            @Nullable CryptoContainer container,
                                            @Nullable Throwable error) {
            return new AutoValue_Result_InitialResult(state, containerFile, dataFiles, container,
                    error);
        }
    }

    @AutoValue
    abstract class DataFilesAddResult implements Result {

        @State abstract String state();

        @Nullable abstract ImmutableList<File> dataFiles();

        @Nullable abstract Throwable error();

        @Override
        public ViewState reduce(ViewState state) {
            ViewState.Builder builder = state.buildWith()
                    .dataFilesAddState(state())
                    .dataFilesAddError(error());
            if (dataFiles() != null) {
                builder.dataFiles(dataFiles());
            }
            return builder.build();
        }

        static DataFilesAddResult activity() {
            return create(State.ACTIVE, null, null);
        }

        static DataFilesAddResult success(ImmutableList<File> dataFiles) {
            return create(State.IDLE, dataFiles, null);
        }

        static DataFilesAddResult failure(Throwable error) {
            return create(State.IDLE, null, error);
        }

        static DataFilesAddResult clear() {
            return create(State.IDLE, null, null);
        }

        private static DataFilesAddResult create(@State String state,
                                                 @Nullable ImmutableList<File> dataFiles,
                                                 @Nullable Throwable error) {
            return new AutoValue_Result_DataFilesAddResult(state, dataFiles, error);
        }
    }

    @AutoValue
    abstract class DataFileRemoveResult implements Result {

        abstract ImmutableList<File> dataFiles();

        @Override
        public ViewState reduce(ViewState state) {
            return state.buildWith().dataFiles(dataFiles()).build();
        }

        static DataFileRemoveResult create(ImmutableList<File> dataFiles) {
            return new AutoValue_Result_DataFileRemoveResult(dataFiles);
        }
    }

    @AutoValue
    abstract class RecipientsAddButtonClickResult implements Result {

        @Override
        public ViewState reduce(ViewState state) {
            return state.buildWith().recipientsSearchResult(null).build();
        }

        static RecipientsAddButtonClickResult create() {
            return new AutoValue_Result_RecipientsAddButtonClickResult();
        }
    }

    @AutoValue
    abstract class RecipientsSearchResult implements Result {

        @State abstract String state();

        abstract ImmutableList<Certificate> result();

        @Nullable abstract Throwable error();

        @Override
        public ViewState reduce(ViewState state) {
            return state.buildWith()
                    .recipientsSearchState(state())
                    .recipientsSearchResult(result())
                    .recipientsSearchError(error())
                    .build();
        }

        static RecipientsSearchResult activity() {
            return create(State.ACTIVE, ImmutableList.of(), null);
        }

        static RecipientsSearchResult success(ImmutableList<Certificate> result) {
            return create(State.IDLE, result, null);
        }

        static RecipientsSearchResult failure(Throwable error) {
            return create(State.IDLE, ImmutableList.of(), error);
        }

        static RecipientsSearchResult clear() {
            return create(State.IDLE, ImmutableList.of(), null);
        }

        private static RecipientsSearchResult create(
                @State String state, ImmutableList<Certificate> result, @Nullable Throwable error) {
            return new AutoValue_Result_RecipientsSearchResult(state, result, error);
        }
    }

    @AutoValue
    abstract class RecipientAddResult implements Result {

        abstract ImmutableList<Certificate> recipients();

        @Override
        public ViewState reduce(ViewState state) {
            return state.buildWith().recipients(recipients()).build();
        }

        static RecipientAddResult create(ImmutableList<Certificate> recipients) {
            return new AutoValue_Result_RecipientAddResult(recipients);
        }
    }

    @AutoValue
    abstract class RecipientRemoveResult implements Result {

        abstract ImmutableList<Certificate> recipients();

        @Override
        public ViewState reduce(ViewState state) {
            return state.buildWith().recipients(recipients()).build();
        }

        static RecipientRemoveResult create(ImmutableList<Certificate> recipients) {
            return new AutoValue_Result_RecipientRemoveResult(recipients);
        }
    }

    @AutoValue
    abstract class EncryptResult implements Result {

        @State abstract String state();

        abstract boolean successMessageVisible();

        @Nullable abstract File containerFile();

        @Nullable abstract Throwable error();

        @Override
        public ViewState reduce(ViewState state) {
            ViewState.Builder builder = state.buildWith()
                    .encryptState(state())
                    .encryptSuccessMessageVisible(successMessageVisible())
                    .encryptError(error());
            if (containerFile() != null) {
                builder
                        .containerFile(containerFile())
                        .dataFilesViewEnabled(false)
                        .dataFilesAddEnabled(false)
                        .dataFilesRemoveEnabled(false)
                        .recipientsAddEnabled(false)
                        .recipientsRemoveEnabled(false)
                        .encryptButtonVisible(false)
                        .decryptButtonVisible(true)
                        .sendButtonVisible(true);
            }
            return builder.build();
        }

        static EncryptResult activity() {
            return create(State.ACTIVE, false, null, null);
        }

        static EncryptResult successMessage(File containerFile) {
            return create(State.IDLE, true, containerFile, null);
        }

        static EncryptResult success(File containerFile) {
            return create(State.IDLE, false, containerFile, null);
        }

        static EncryptResult failure(Throwable error) {
            return create(State.IDLE, false, null, error);
        }

        static EncryptResult clear() {
            return create(State.IDLE, false, null, null);
        }

        private static EncryptResult create(@State String state, boolean successMessageVisible,
                                            @Nullable File containerFile,
                                            @Nullable Throwable error) {
            return new AutoValue_Result_EncryptResult(state, successMessageVisible, containerFile,
                    error);
        }
    }

    @AutoValue
    abstract class DecryptionResult implements Result {

        @Nullable abstract IdCardDataResponse idCardDataResponse();

        @Override
        public ViewState reduce(ViewState state) {
            ViewState.Builder builder = state.buildWith()
                    .decryptionIdCardDataResponse(idCardDataResponse());
            if (idCardDataResponse() == null) {
                builder.decryptError(null);
            }
            return builder.build();
        }

        static DecryptionResult show(IdCardDataResponse idCardDataResponse) {
            return create(idCardDataResponse);
        }
{'fraction_non_alphanumeric': 0.07821657199531151, 'fraction_numerical': 9.01631953836444e-05, 'mean_word_length': 1.92073732718894, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3522752', 'n_tokens_mistral': 5215, 'n_tokens_neox': 4936, 'n_words': 1134}