Search in sources :

Example 1 with IResponseCallback

use of it.niedermann.nextcloud.deck.api.IResponseCallback in project nextcloud-deck by stefan-niedermann.

the class SyncManager method createFullCard.

// public LiveData<FullCard> createCard(long accountId, long localBoardId, long localStackId, Card card) {
// 
// MutableLiveData<FullCard> liveData = new MutableLiveData<>();
// executor.submit(() -> {
// Account account = dataBaseAdapter.getAccountByIdDirectly(accountId);
// User owner = dataBaseAdapter.getUserByUidDirectly(accountId, account.getUserName());
// FullStack stack = dataBaseAdapter.getFullStackByLocalIdDirectly(localStackId);
// Board board = dataBaseAdapter.getBoardByLocalIdDirectly(localBoardId);
// card.setStackId(stack.getLocalId());
// FullCard fullCard = new FullCard();
// fullCard.setCard(card);
// fullCard.setOwner(owner);
// fullCard.setAccountId(accountId);
// new DataPropagationHelper(serverAdapter, dataBaseAdapter).createEntity(new CardPropagationDataProvider(null, board, stack), fullCard, new IResponseCallback<FullCard>(account) {
// @Override
// public void onResponse(FullCard response) {
// liveData.postValue(response);
// }
// }, (FullCard entity, FullCard response) -> {
// response.getCard().setUserId(entity.getCard().getUserId());
// response.getCard().setStackId(stack.getLocalId());
// });
// });
// return liveData;
// }
@AnyThread
public void createFullCard(long accountId, long localBoardId, long localStackId, @NonNull FullCard card, @NonNull IResponseCallback<FullCard> callback) {
    executor.submit(() -> {
        Account account = dataBaseAdapter.getAccountByIdDirectly(accountId);
        User owner = dataBaseAdapter.getUserByUidDirectly(accountId, account.getUserName());
        FullStack stack = dataBaseAdapter.getFullStackByLocalIdDirectly(localStackId);
        Board board = dataBaseAdapter.getBoardByLocalIdDirectly(localBoardId);
        card.getCard().setUserId(owner.getLocalId());
        card.getCard().setStackId(stack.getLocalId());
        card.getCard().setAccountId(accountId);
        card.getCard().setStatusEnum(DBStatus.LOCAL_EDITED);
        card.getCard().setOrder(dataBaseAdapter.getHighestCardOrderInStack(localStackId) + 1);
        long localCardId = dataBaseAdapter.createCardDirectly(accountId, card.getCard());
        card.getCard().setLocalId(localCardId);
        List<User> assignedUsers = card.getAssignedUsers();
        if (assignedUsers != null) {
            for (User assignedUser : assignedUsers) {
                dataBaseAdapter.createJoinCardWithUser(assignedUser.getLocalId(), localCardId, DBStatus.LOCAL_EDITED);
            }
        }
        List<Label> labels = card.getLabels();
        if (labels != null) {
            for (Label label : labels) {
                dataBaseAdapter.createJoinCardWithLabel(label.getLocalId(), localCardId, DBStatus.LOCAL_EDITED);
            }
        }
        if (card.getAttachments() != null) {
            for (Attachment attachment : card.getAttachments()) {
                if (attachment.getLocalId() == null) {
                    attachment.setCardId(localCardId);
                    dataBaseAdapter.createAttachment(accountId, attachment);
                }
            }
        }
        if (serverAdapter.hasInternetConnection()) {
            syncHelperFactory.create(serverAdapter, dataBaseAdapter, null).setResponseCallback(new ResponseCallback<>(account) {

                @Override
                public void onResponse(Boolean response) {
                    callback.onResponse(card);
                }

                @SuppressLint("MissingSuperCall")
                @Override
                public void onError(Throwable throwable) {
                    if (throwable.getClass() == DeckException.class && ((DeckException) throwable).getHint().equals(DeckException.Hint.DEPENDENCY_NOT_SYNCED_YET)) {
                        callback.onResponse(card);
                    } else {
                        callback.onError(throwable);
                    }
                }
            }).doUpSyncFor(new CardDataProvider(null, board, stack));
        } else {
            callback.onResponse(card);
        }
    });
}
Also used : Account(it.niedermann.nextcloud.deck.model.Account) JoinCardWithUser(it.niedermann.nextcloud.deck.model.JoinCardWithUser) User(it.niedermann.nextcloud.deck.model.User) CardDataProvider(it.niedermann.nextcloud.deck.persistence.sync.helpers.providers.CardDataProvider) Label(it.niedermann.nextcloud.deck.model.Label) Attachment(it.niedermann.nextcloud.deck.model.Attachment) FullStack(it.niedermann.nextcloud.deck.model.full.FullStack) DeckException(it.niedermann.nextcloud.deck.exceptions.DeckException) FullBoard(it.niedermann.nextcloud.deck.model.full.FullBoard) Board(it.niedermann.nextcloud.deck.model.Board) IResponseCallback(it.niedermann.nextcloud.deck.api.IResponseCallback) ResponseCallback(it.niedermann.nextcloud.deck.api.ResponseCallback) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AnyThread(androidx.annotation.AnyThread)

Example 2 with IResponseCallback

use of it.niedermann.nextcloud.deck.api.IResponseCallback in project nextcloud-deck by stefan-niedermann.

the class SyncManager method createAccessControl.

@AnyThread
public void createAccessControl(long accountId, @NonNull AccessControl entity, @NonNull IResponseCallback<AccessControl> callback) {
    executor.submit(() -> {
        Account account = dataBaseAdapter.getAccountByIdDirectly(accountId);
        FullBoard board = dataBaseAdapter.getFullBoardByLocalIdDirectly(accountId, entity.getBoardId());
        new DataPropagationHelper(serverAdapter, dataBaseAdapter, executor).createEntity(new AccessControlDataProvider(null, board, Collections.singletonList(entity)), entity, ResponseCallback.from(account, callback), ((entity1, response) -> {
            response.setBoardId(entity.getBoardId());
            response.setUserId(entity.getUser().getLocalId());
        }));
    });
}
Also used : GsonConfig(it.niedermann.nextcloud.deck.api.GsonConfig) NonNull(androidx.annotation.NonNull) Card(it.niedermann.nextcloud.deck.model.Card) Capabilities(it.niedermann.nextcloud.deck.model.ocs.Capabilities) ColorInt(androidx.annotation.ColorInt) Attachment(it.niedermann.nextcloud.deck.model.Attachment) HTTP_NOT_MODIFIED(java.net.HttpURLConnection.HTTP_NOT_MODIFIED) Future(java.util.concurrent.Future) DBStatus(it.niedermann.nextcloud.deck.model.enums.DBStatus) Matcher(java.util.regex.Matcher) Map(java.util.Map) IResponseCallback(it.niedermann.nextcloud.deck.api.IResponseCallback) FilterWidget(it.niedermann.nextcloud.deck.model.widget.filter.FilterWidget) UiThread(androidx.annotation.UiThread) Stack(it.niedermann.nextcloud.deck.model.Stack) StackWidgetModel(it.niedermann.nextcloud.deck.model.appwidgets.StackWidgetModel) StackDataProvider(it.niedermann.nextcloud.deck.persistence.sync.helpers.providers.StackDataProvider) Label(it.niedermann.nextcloud.deck.model.Label) FullStack(it.niedermann.nextcloud.deck.model.full.FullStack) DeckLog(it.niedermann.nextcloud.deck.DeckLog) BoardDataProvider(it.niedermann.nextcloud.deck.persistence.sync.helpers.providers.BoardDataProvider) FullDeckComment(it.niedermann.nextcloud.deck.model.ocs.comment.full.FullDeckComment) HTTP_UNAVAILABLE(java.net.HttpURLConnection.HTTP_UNAVAILABLE) FullCardWithProjects(it.niedermann.nextcloud.deck.model.full.FullCardWithProjects) FullCard(it.niedermann.nextcloud.deck.model.full.FullCard) Instant(java.time.Instant) DataPropagationHelper(it.niedermann.nextcloud.deck.persistence.sync.helpers.DataPropagationHelper) Executors(java.util.concurrent.Executors) FilterInformation(it.niedermann.nextcloud.deck.model.internal.FilterInformation) JoinCardWithUser(it.niedermann.nextcloud.deck.model.JoinCardWithUser) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Nullable(androidx.annotation.Nullable) ParsedResponse(com.nextcloud.android.sso.api.ParsedResponse) DeckComment(it.niedermann.nextcloud.deck.model.ocs.comment.DeckComment) SQLiteConstraintException(android.database.sqlite.SQLiteConstraintException) Optional(java.util.Optional) ResponseCallback(it.niedermann.nextcloud.deck.api.ResponseCallback) Pattern(java.util.regex.Pattern) ActivityDataProvider(it.niedermann.nextcloud.deck.persistence.sync.helpers.providers.ActivityDataProvider) MediatorLiveData(androidx.lifecycle.MediatorLiveData) MutableLiveData(androidx.lifecycle.MutableLiveData) Context(android.content.Context) OfflineException(it.niedermann.nextcloud.deck.exceptions.OfflineException) FullBoard(it.niedermann.nextcloud.deck.model.full.FullBoard) AbstractSyncDataProvider(it.niedermann.nextcloud.deck.persistence.sync.helpers.providers.AbstractSyncDataProvider) User(it.niedermann.nextcloud.deck.model.User) Pair(android.util.Pair) WorkerThread(androidx.annotation.WorkerThread) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DeckException(it.niedermann.nextcloud.deck.exceptions.DeckException) MainThread(androidx.annotation.MainThread) HashMap(java.util.HashMap) AccessControl(it.niedermann.nextcloud.deck.model.AccessControl) DataBaseAdapter(it.niedermann.nextcloud.deck.persistence.sync.adapters.db.DataBaseAdapter) ArrayList(java.util.ArrayList) OcsProjectResource(it.niedermann.nextcloud.deck.model.ocs.projects.OcsProjectResource) SuppressLint(android.annotation.SuppressLint) AccessControlDataProvider(it.niedermann.nextcloud.deck.persistence.sync.helpers.providers.AccessControlDataProvider) LabelDataProvider(it.niedermann.nextcloud.deck.persistence.sync.helpers.providers.LabelDataProvider) ServerAdapter(it.niedermann.nextcloud.deck.persistence.sync.adapters.ServerAdapter) LastSyncUtil(it.niedermann.nextcloud.deck.api.LastSyncUtil) NoSuchElementException(java.util.NoSuchElementException) ExecutorService(java.util.concurrent.ExecutorService) CardPropagationDataProvider(it.niedermann.nextcloud.deck.persistence.sync.helpers.providers.CardPropagationDataProvider) LiveData(androidx.lifecycle.LiveData) FullSingleCardWidgetModel(it.niedermann.nextcloud.deck.model.full.FullSingleCardWidgetModel) WrappedLiveData(it.niedermann.nextcloud.deck.persistence.sync.adapters.db.util.WrappedLiveData) AttachmentDataProvider(it.niedermann.nextcloud.deck.persistence.sync.helpers.providers.AttachmentDataProvider) UserSearchLiveData(it.niedermann.nextcloud.deck.persistence.sync.adapters.db.util.extrawurst.UserSearchLiveData) DeckCommentsDataProvider(it.niedermann.nextcloud.deck.persistence.sync.helpers.providers.DeckCommentsDataProvider) CardDataProvider(it.niedermann.nextcloud.deck.persistence.sync.helpers.providers.CardDataProvider) BoardWithStacksAndLabelsUpSyncDataProvider(it.niedermann.nextcloud.deck.persistence.sync.helpers.providers.partial.BoardWithStacksAndLabelsUpSyncDataProvider) NextcloudHttpRequestFailedException(com.nextcloud.android.sso.exceptions.NextcloudHttpRequestFailedException) BoardWithAclDownSyncDataProvider(it.niedermann.nextcloud.deck.persistence.sync.helpers.providers.partial.BoardWithAclDownSyncDataProvider) UpcomingCardsAdapterItem(it.niedermann.nextcloud.deck.ui.upcomingcards.UpcomingCardsAdapterItem) Board(it.niedermann.nextcloud.deck.model.Board) AnyThread(androidx.annotation.AnyThread) Account(it.niedermann.nextcloud.deck.model.Account) File(java.io.File) FilterWidgetCard(it.niedermann.nextcloud.deck.model.widget.filter.dto.FilterWidgetCard) SyncHelper(it.niedermann.nextcloud.deck.persistence.sync.helpers.SyncHelper) OcsComment(it.niedermann.nextcloud.deck.model.ocs.comment.OcsComment) Collections(java.util.Collections) Account(it.niedermann.nextcloud.deck.model.Account) DataPropagationHelper(it.niedermann.nextcloud.deck.persistence.sync.helpers.DataPropagationHelper) FullBoard(it.niedermann.nextcloud.deck.model.full.FullBoard) AccessControlDataProvider(it.niedermann.nextcloud.deck.persistence.sync.helpers.providers.AccessControlDataProvider) AnyThread(androidx.annotation.AnyThread)

Example 3 with IResponseCallback

use of it.niedermann.nextcloud.deck.api.IResponseCallback in project nextcloud-deck by stefan-niedermann.

the class SyncManager method cloneBoard.

/**
 * Creates a new {@link Board} and adds the same {@link Label} and {@link Stack} as in the origin {@link Board}.
 * Owner of the target {@link Board} will be the {@link User} with the {@link Account} of {@param targetAccountId}.
 *
 * @param cloneCards determines whether or not the cards in this {@link Board} shall be cloned or not
 *                   Does <strong>not</strong> clone any {@link Card} or {@link AccessControl} from the origin {@link Board}.
 */
@AnyThread
public void cloneBoard(long originAccountId, long originBoardLocalId, long targetAccountId, @ColorInt int targetBoardColor, boolean cloneCards, @NonNull IResponseCallback<FullBoard> callback) {
    executor.submit(() -> {
        Account originAccount = dataBaseAdapter.getAccountByIdDirectly(originAccountId);
        User newOwner = dataBaseAdapter.getUserByUidDirectly(originAccountId, originAccount.getUserName());
        if (newOwner == null) {
            callback.onError(new DeckException(DeckException.Hint.UNKNOWN_ACCOUNT_USER_ID, "User with Account-UID \"" + originAccount.getUserName() + "\" not found."));
            return;
        }
        FullBoard originalBoard = dataBaseAdapter.getFullBoardByLocalIdDirectly(originAccountId, originBoardLocalId);
        String newBoardTitleBaseName = originalBoard.getBoard().getTitle().trim();
        int newBoardTitleCopyIndex = 0;
        // already a copy?
        String regex = " \\(copy [0-9]+\\)$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(originalBoard.getBoard().getTitle());
        if (matcher.find()) {
            String found = matcher.group();
            newBoardTitleBaseName = newBoardTitleBaseName.substring(0, newBoardTitleBaseName.length() - found.length());
            Matcher indexMatcher = Pattern.compile("[0-9]+").matcher(found);
            // noinspection ResultOfMethodCallIgnored
            indexMatcher.find();
            String oldIndexString = indexMatcher.group();
            newBoardTitleCopyIndex = Integer.parseInt(oldIndexString);
        }
        String newBoardTitle;
        do {
            newBoardTitleCopyIndex++;
            newBoardTitle = newBoardTitleBaseName + " (copy " + newBoardTitleCopyIndex + ")";
        } while (dataBaseAdapter.getBoardForAccountByNameDirectly(targetAccountId, newBoardTitle) != null);
        originalBoard.setAccountId(targetAccountId);
        originalBoard.setId(null);
        originalBoard.setLocalId(null);
        originalBoard.getBoard().setTitle(newBoardTitle);
        originalBoard.getBoard().setColor(String.format("%06X", 0xFFFFFF & targetBoardColor));
        originalBoard.getBoard().setOwnerId(newOwner.getLocalId());
        originalBoard.setStatusEnum(DBStatus.LOCAL_EDITED);
        originalBoard.setOwner(newOwner);
        long newBoardId = dataBaseAdapter.createBoardDirectly(originAccountId, originalBoard.getBoard());
        originalBoard.setLocalId(newBoardId);
        boolean isSameAccount = targetAccountId == originAccountId;
        if (isSameAccount) {
            List<AccessControl> aclList = originalBoard.getParticipants();
            for (AccessControl acl : aclList) {
                acl.setLocalId(null);
                acl.setId(null);
                acl.setBoardId(newBoardId);
                dataBaseAdapter.createAccessControl(targetAccountId, acl);
            }
        }
        Map<Long, Long> oldToNewLabelIdsDictionary = new HashMap<>();
        for (Label label : originalBoard.getLabels()) {
            Long oldLocalId = label.getLocalId();
            label.setLocalId(null);
            label.setId(null);
            label.setAccountId(targetAccountId);
            label.setStatusEnum(DBStatus.LOCAL_EDITED);
            label.setBoardId(newBoardId);
            long newLocalId = dataBaseAdapter.createLabelDirectly(targetAccountId, label);
            oldToNewLabelIdsDictionary.put(oldLocalId, newLocalId);
        }
        List<Stack> oldStacks = originalBoard.getStacks();
        for (Stack stack : oldStacks) {
            Long oldStackId = stack.getLocalId();
            stack.setLocalId(null);
            stack.setId(null);
            stack.setStatusEnum(DBStatus.LOCAL_EDITED);
            stack.setAccountId(targetAccountId);
            stack.setBoardId(newBoardId);
            long createdStackId = dataBaseAdapter.createStack(targetAccountId, stack);
            if (cloneCards) {
                List<FullCard> oldCards = dataBaseAdapter.getFullCardsForStackDirectly(originAccountId, oldStackId, null);
                for (FullCard oldCard : oldCards) {
                    Card newCard = oldCard.getCard();
                    newCard.setId(null);
                    newCard.setUserId(newOwner.getLocalId());
                    newCard.setLocalId(null);
                    newCard.setStackId(createdStackId);
                    newCard.setAccountId(targetAccountId);
                    newCard.setStatusEnum(DBStatus.LOCAL_EDITED);
                    long createdCardId = dataBaseAdapter.createCardDirectly(targetAccountId, newCard);
                    if (oldCard.getLabels() != null) {
                        for (Label oldLabel : oldCard.getLabels()) {
                            Long newLabelId = oldToNewLabelIdsDictionary.get(oldLabel.getLocalId());
                            if (newLabelId != null) {
                                dataBaseAdapter.createJoinCardWithLabel(newLabelId, createdCardId, DBStatus.LOCAL_EDITED);
                            } else
                                DeckLog.error("ID of created Label is null! Skipping assignment of ", oldLabel.getTitle(), "…");
                        }
                    }
                    if (isSameAccount && oldCard.getAssignedUsers() != null) {
                        for (User assignedUser : oldCard.getAssignedUsers()) {
                            dataBaseAdapter.createJoinCardWithUser(assignedUser.getLocalId(), createdCardId, DBStatus.LOCAL_EDITED);
                        }
                    }
                }
            }
        }
        if (serverAdapter.hasInternetConnection()) {
            Account targetAccount = dataBaseAdapter.getAccountByIdDirectly(targetAccountId);
            ServerAdapter serverAdapterToUse = this.serverAdapter;
            if (originAccountId != targetAccountId) {
                serverAdapterToUse = new ServerAdapter(appContext, targetAccount.getName());
            }
            syncHelperFactory.create(serverAdapterToUse, dataBaseAdapter, null).setResponseCallback(new ResponseCallback<>(targetAccount) {

                @Override
                public void onResponse(Boolean response) {
                    callback.onResponse(dataBaseAdapter.getFullBoardByLocalIdDirectly(targetAccountId, newBoardId));
                }

                @SuppressLint("MissingSuperCall")
                @Override
                public void onError(Throwable throwable) {
                    callback.onError(throwable);
                }
            }).doUpSyncFor(new BoardWithStacksAndLabelsUpSyncDataProvider(dataBaseAdapter.getFullBoardByLocalIdDirectly(targetAccountId, newBoardId)));
        } else {
            callback.onResponse(dataBaseAdapter.getFullBoardByLocalIdDirectly(targetAccountId, newBoardId));
        }
    });
}
Also used : Account(it.niedermann.nextcloud.deck.model.Account) ServerAdapter(it.niedermann.nextcloud.deck.persistence.sync.adapters.ServerAdapter) JoinCardWithUser(it.niedermann.nextcloud.deck.model.JoinCardWithUser) User(it.niedermann.nextcloud.deck.model.User) Matcher(java.util.regex.Matcher) HashMap(java.util.HashMap) Label(it.niedermann.nextcloud.deck.model.Label) AccessControl(it.niedermann.nextcloud.deck.model.AccessControl) DeckException(it.niedermann.nextcloud.deck.exceptions.DeckException) FullBoard(it.niedermann.nextcloud.deck.model.full.FullBoard) IResponseCallback(it.niedermann.nextcloud.deck.api.IResponseCallback) ResponseCallback(it.niedermann.nextcloud.deck.api.ResponseCallback) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Pattern(java.util.regex.Pattern) BoardWithStacksAndLabelsUpSyncDataProvider(it.niedermann.nextcloud.deck.persistence.sync.helpers.providers.partial.BoardWithStacksAndLabelsUpSyncDataProvider) SuppressLint(android.annotation.SuppressLint) Stack(it.niedermann.nextcloud.deck.model.Stack) FullStack(it.niedermann.nextcloud.deck.model.full.FullStack) Card(it.niedermann.nextcloud.deck.model.Card) FullCard(it.niedermann.nextcloud.deck.model.full.FullCard) FilterWidgetCard(it.niedermann.nextcloud.deck.model.widget.filter.dto.FilterWidgetCard) FullCard(it.niedermann.nextcloud.deck.model.full.FullCard) AnyThread(androidx.annotation.AnyThread)

Example 4 with IResponseCallback

use of it.niedermann.nextcloud.deck.api.IResponseCallback in project nextcloud-deck by stefan-niedermann.

the class ImportAccountActivity method onActivityResult.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_AUTH_TOKEN_SSO && resultCode == RESULT_CANCELED) {
        binding.addButton.setEnabled(true);
    } else {
        try {
            AccountImporter.onActivityResult(requestCode, resultCode, data, ImportAccountActivity.this, new AccountImporter.IAccountAccessGranted() {

                @SuppressLint("ApplySharedPref")
                @Override
                public void accountAccessGranted(SingleSignOnAccount account) {
                    runOnUiThread(() -> {
                        binding.status.setText(null);
                        binding.status.setVisibility(View.GONE);
                        binding.progressCircular.setVisibility(View.VISIBLE);
                        binding.progressText.setVisibility(View.VISIBLE);
                        binding.progressCircular.setIndeterminate(true);
                        binding.progressText.setText(R.string.progress_import_indeterminate);
                    });
                    SingleAccountHelper.setCurrentAccount(getApplicationContext(), account.name);
                    final var syncManager = new SyncManager(ImportAccountActivity.this);
                    final var accountToCreate = new Account(account.name, account.userId, account.url);
                    syncManager.createAccount(accountToCreate, new IResponseCallback<>() {

                        @Override
                        public void onResponse(Account createdAccount) {
                            // Remember last account - THIS HAS TO BE DONE SYNCHRONOUSLY
                            DeckLog.log("--- Write: shared_preference_last_account | ", createdAccount.getId());
                            sharedPreferences.edit().putLong(sharedPreferenceLastAccount, createdAccount.getId()).commit();
                            syncManager.refreshCapabilities(new ResponseCallback<>(createdAccount) {

                                @Override
                                public void onResponse(Capabilities response) {
                                    if (!response.isMaintenanceEnabled()) {
                                        if (response.getDeckVersion().isSupported()) {
                                            var progress$ = syncManager.synchronize(new ResponseCallback<>(account) {

                                                @Override
                                                public void onResponse(Boolean response) {
                                                    restoreWifiPref();
                                                    SyncWorker.update(getApplicationContext());
                                                    setResult(RESULT_OK);
                                                    finish();
                                                }

                                                @Override
                                                public void onError(Throwable throwable) {
                                                    super.onError(throwable);
                                                    setStatusText(throwable.getMessage());
                                                    runOnUiThread(() -> ExceptionDialogFragment.newInstance(throwable, createdAccount).show(getSupportFragmentManager(), ExceptionDialogFragment.class.getSimpleName()));
                                                    rollbackAccountCreation(syncManager, createdAccount.getId());
                                                }
                                            });
                                            runOnUiThread(() -> progress$.observe(ImportAccountActivity.this, (progress) -> {
                                                DeckLog.log("New progress value", progress.first, progress.second);
                                                if (progress.first > 0) {
                                                    binding.progressCircular.setIndeterminate(false);
                                                }
                                                binding.progressText.setText(getString(R.string.progress_import, progress.first + 1, progress.second));
                                                binding.progressCircular.setProgress(progress.first);
                                                binding.progressCircular.setMax(progress.second);
                                            }));
                                        } else {
                                            setStatusText(getString(R.string.deck_outdated_please_update, response.getDeckVersion().getOriginalVersion()));
                                            runOnUiThread(() -> {
                                                binding.updateDeckButton.setOnClickListener((v) -> startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse(createdAccount.getUrl() + urlFragmentUpdateDeck))));
                                                binding.updateDeckButton.setVisibility(View.VISIBLE);
                                            });
                                            rollbackAccountCreation(syncManager, createdAccount.getId());
                                        }
                                    } else {
                                        setStatusText(R.string.maintenance_mode);
                                        rollbackAccountCreation(syncManager, createdAccount.getId());
                                    }
                                }

                                @Override
                                public void onError(Throwable throwable) {
                                    super.onError(throwable);
                                    if (throwable instanceof OfflineException) {
                                        setStatusText(R.string.you_have_to_be_connected_to_the_internet_in_order_to_add_an_account);
                                    } else {
                                        setStatusText(throwable.getMessage());
                                        runOnUiThread(() -> ExceptionDialogFragment.newInstance(throwable, createdAccount).show(getSupportFragmentManager(), ExceptionDialogFragment.class.getSimpleName()));
                                    }
                                    rollbackAccountCreation(syncManager, createdAccount.getId());
                                }
                            });
                        }

                        @Override
                        public void onError(Throwable error) {
                            IResponseCallback.super.onError(error);
                            if (error instanceof SQLiteConstraintException) {
                                DeckLog.error("Account has already been added, this should not be the case");
                            }
                            setStatusText(error.getMessage());
                            runOnUiThread(() -> ExceptionDialogFragment.newInstance(error, accountToCreate).show(getSupportFragmentManager(), ExceptionDialogFragment.class.getSimpleName()));
                            restoreWifiPref();
                        }
                    });
                }
            });
        } catch (AccountImportCancelledException e) {
            runOnUiThread(() -> binding.addButton.setEnabled(true));
            restoreWifiPref();
            DeckLog.info("Account import has been canceled.");
        }
    }
}
Also used : Context(android.content.Context) OfflineException(it.niedermann.nextcloud.deck.exceptions.OfflineException) ExceptionHandler(it.niedermann.nextcloud.deck.ui.exception.ExceptionHandler) R(it.niedermann.nextcloud.deck.R) Bundle(android.os.Bundle) REQUEST_AUTH_TOKEN_SSO(com.nextcloud.android.sso.AccountImporter.REQUEST_AUTH_TOKEN_SSO) AndroidGetAccountsPermissionNotGranted(com.nextcloud.android.sso.exceptions.AndroidGetAccountsPermissionNotGranted) NonNull(androidx.annotation.NonNull) Uri(android.net.Uri) Intent(android.content.Intent) Capabilities(it.niedermann.nextcloud.deck.model.ocs.Capabilities) AppCompatActivity(androidx.appcompat.app.AppCompatActivity) SuppressLint(android.annotation.SuppressLint) IResponseCallback(it.niedermann.nextcloud.deck.api.IResponseCallback) View(android.view.View) SyncManager(it.niedermann.nextcloud.deck.persistence.sync.SyncManager) SyncWorker(it.niedermann.nextcloud.deck.persistence.sync.SyncWorker) DeckLog(it.niedermann.nextcloud.deck.DeckLog) UiExceptionManager(com.nextcloud.android.sso.ui.UiExceptionManager) AccountImportCancelledException(com.nextcloud.android.sso.exceptions.AccountImportCancelledException) Account(it.niedermann.nextcloud.deck.model.Account) ExceptionDialogFragment(it.niedermann.nextcloud.deck.ui.exception.ExceptionDialogFragment) AccountImporter(com.nextcloud.android.sso.AccountImporter) StringRes(androidx.annotation.StringRes) ActivityImportAccountBinding(it.niedermann.nextcloud.deck.databinding.ActivityImportAccountBinding) SingleAccountHelper(com.nextcloud.android.sso.helper.SingleAccountHelper) SharedPreferences(android.content.SharedPreferences) SingleSignOnAccount(com.nextcloud.android.sso.model.SingleSignOnAccount) SQLiteConstraintException(android.database.sqlite.SQLiteConstraintException) PreferenceManager(androidx.preference.PreferenceManager) ResponseCallback(it.niedermann.nextcloud.deck.api.ResponseCallback) NextcloudFilesAppNotInstalledException(com.nextcloud.android.sso.exceptions.NextcloudFilesAppNotInstalledException) SingleSignOnAccount(com.nextcloud.android.sso.model.SingleSignOnAccount) Account(it.niedermann.nextcloud.deck.model.Account) SingleSignOnAccount(com.nextcloud.android.sso.model.SingleSignOnAccount) IResponseCallback(it.niedermann.nextcloud.deck.api.IResponseCallback) SyncManager(it.niedermann.nextcloud.deck.persistence.sync.SyncManager) Intent(android.content.Intent) AccountImportCancelledException(com.nextcloud.android.sso.exceptions.AccountImportCancelledException) AccountImporter(com.nextcloud.android.sso.AccountImporter) ExceptionDialogFragment(it.niedermann.nextcloud.deck.ui.exception.ExceptionDialogFragment) Capabilities(it.niedermann.nextcloud.deck.model.ocs.Capabilities) IResponseCallback(it.niedermann.nextcloud.deck.api.IResponseCallback) ResponseCallback(it.niedermann.nextcloud.deck.api.ResponseCallback) SuppressLint(android.annotation.SuppressLint) SQLiteConstraintException(android.database.sqlite.SQLiteConstraintException) OfflineException(it.niedermann.nextcloud.deck.exceptions.OfflineException)

Example 5 with IResponseCallback

use of it.niedermann.nextcloud.deck.api.IResponseCallback in project nextcloud-deck by stefan-niedermann.

the class SyncManagerTest method testSynchronize.

@Test
public void testSynchronize() {
    final var syncManagerSpy = spy(syncManager);
    LastSyncUtil.init(ApplicationProvider.getApplicationContext());
    final var account = new Account(1337L, "Test", "Peter", "example.com");
    final var capabilities = new Capabilities();
    capabilities.setDeckVersion(Version.minimumSupported());
    // Act as if refreshing capabilities is always successful
    doAnswer((invocation -> {
        // noinspection unchecked
        ((IResponseCallback<Capabilities>) invocation.getArgument(0)).onResponse(capabilities);
        return null;
    })).when(syncManagerSpy).refreshCapabilities(any());
    // Actual method invocation
    final var finalCallback = spy(new ResponseCallback<Boolean>(account) {

        @Override
        public void onResponse(Boolean response) {
        }
    });
    // Happy path
    final var syncHelper_positive = new SyncHelperMock(true);
    when(syncHelperFactory.create(any(), any(), any())).thenReturn(syncHelper_positive);
    syncManagerSpy.synchronize(finalCallback);
    verify(finalCallback, times(1)).onResponse(any());
    // Bad paths
    assertThrows(IllegalArgumentException.class, () -> syncManagerSpy.synchronize(new ResponseCallback<>(new Account(null)) {

        @Override
        public void onResponse(Boolean response) {
        }
    }));
    // noinspection ConstantConditions
    assertThrows(IllegalArgumentException.class, () -> syncManagerSpy.synchronize(new ResponseCallback<>(null) {

        @Override
        public void onResponse(Boolean response) {
        }
    }));
    final var syncHelper_negative = new SyncHelperMock(false);
    when(syncHelperFactory.create(any(), any(), any())).thenReturn(syncHelper_negative);
    syncManagerSpy.synchronize(finalCallback);
    verify(finalCallback, times(1)).onError(any());
}
Also used : TestUtil(it.niedermann.nextcloud.deck.TestUtil) Version(it.niedermann.nextcloud.deck.model.ocs.Version) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) NonNull(androidx.annotation.NonNull) Card(it.niedermann.nextcloud.deck.model.Card) Capabilities(it.niedermann.nextcloud.deck.model.ocs.Capabilities) ApplicationProvider(androidx.test.core.app.ApplicationProvider) Mockito.doThrow(org.mockito.Mockito.doThrow) InstantTaskExecutorRule(androidx.arch.core.executor.testing.InstantTaskExecutorRule) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Map(java.util.Map) IResponseCallback(it.niedermann.nextcloud.deck.api.IResponseCallback) Stack(it.niedermann.nextcloud.deck.model.Stack) StackDataProvider(it.niedermann.nextcloud.deck.persistence.sync.helpers.providers.StackDataProvider) Assert.fail(org.junit.Assert.fail) FullStack(it.niedermann.nextcloud.deck.model.full.FullStack) IRemoteEntity(it.niedermann.nextcloud.deck.model.interfaces.IRemoteEntity) Mockito.doNothing(org.mockito.Mockito.doNothing) Instant(java.time.Instant) InvocationTargetException(java.lang.reflect.InvocationTargetException) RobolectricTestRunner(org.robolectric.RobolectricTestRunner) NetworkErrorException(android.accounts.NetworkErrorException) List(java.util.List) ParsedResponse(com.nextcloud.android.sso.api.ParsedResponse) Assert.assertFalse(org.junit.Assert.assertFalse) ResponseCallback(it.niedermann.nextcloud.deck.api.ResponseCallback) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) MutableLiveData(androidx.lifecycle.MutableLiveData) Context(android.content.Context) OfflineException(it.niedermann.nextcloud.deck.exceptions.OfflineException) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) FullBoard(it.niedermann.nextcloud.deck.model.full.FullBoard) AbstractSyncDataProvider(it.niedermann.nextcloud.deck.persistence.sync.helpers.providers.AbstractSyncDataProvider) Assert.assertThrows(org.junit.Assert.assertThrows) RunWith(org.junit.runner.RunWith) Mockito.spy(org.mockito.Mockito.spy) DataBaseAdapter(it.niedermann.nextcloud.deck.persistence.sync.adapters.db.DataBaseAdapter) ServerAdapter(it.niedermann.nextcloud.deck.persistence.sync.adapters.ServerAdapter) LastSyncUtil(it.niedermann.nextcloud.deck.api.LastSyncUtil) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) WrappedLiveData(it.niedermann.nextcloud.deck.persistence.sync.adapters.db.util.WrappedLiveData) CardDataProvider(it.niedermann.nextcloud.deck.persistence.sync.helpers.providers.CardDataProvider) NextcloudHttpRequestFailedException(com.nextcloud.android.sso.exceptions.NextcloudHttpRequestFailedException) Assert.assertTrue(org.junit.Assert.assertTrue) Board(it.niedermann.nextcloud.deck.model.Board) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Account(it.niedermann.nextcloud.deck.model.Account) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) Assert.assertNull(org.junit.Assert.assertNull) Rule(org.junit.Rule) SyncHelper(it.niedermann.nextcloud.deck.persistence.sync.helpers.SyncHelper) Mockito.reset(org.mockito.Mockito.reset) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Account(it.niedermann.nextcloud.deck.model.Account) Capabilities(it.niedermann.nextcloud.deck.model.ocs.Capabilities) IResponseCallback(it.niedermann.nextcloud.deck.api.IResponseCallback) ResponseCallback(it.niedermann.nextcloud.deck.api.ResponseCallback) Test(org.junit.Test)

Aggregations

IResponseCallback (it.niedermann.nextcloud.deck.api.IResponseCallback)13 Account (it.niedermann.nextcloud.deck.model.Account)9 ResponseCallback (it.niedermann.nextcloud.deck.api.ResponseCallback)8 FullCard (it.niedermann.nextcloud.deck.model.full.FullCard)8 NonNull (androidx.annotation.NonNull)7 Board (it.niedermann.nextcloud.deck.model.Board)7 FullBoard (it.niedermann.nextcloud.deck.model.full.FullBoard)7 FullStack (it.niedermann.nextcloud.deck.model.full.FullStack)7 AnyThread (androidx.annotation.AnyThread)6 ViewModelProvider (androidx.lifecycle.ViewModelProvider)6 ExceptionDialogFragment (it.niedermann.nextcloud.deck.ui.exception.ExceptionDialogFragment)6 SuppressLint (android.annotation.SuppressLint)5 Context (android.content.Context)5 Bundle (android.os.Bundle)5 View (android.view.View)5 ColorInt (androidx.annotation.ColorInt)5 Nullable (androidx.annotation.Nullable)5 DeckLog (it.niedermann.nextcloud.deck.DeckLog)5 Card (it.niedermann.nextcloud.deck.model.Card)5 Stack (it.niedermann.nextcloud.deck.model.Stack)5