Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ManageAccountsViewModel.java « manageaccounts « notes « owncloud « niedermann « it « java « main « src « app - github.com/stefan-niedermann/nextcloud-notes.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6cbc199d8ef6ef5cb8d88ece50c28a586be99641 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package it.niedermann.owncloud.notes.manageaccounts;

import android.app.Application;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;

import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException;
import com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException;
import com.nextcloud.android.sso.helper.SingleAccountHelper;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import it.niedermann.owncloud.notes.persistence.NotesRepository;
import it.niedermann.owncloud.notes.persistence.entity.Account;
import it.niedermann.owncloud.notes.shared.model.IResponseCallback;

import static androidx.lifecycle.Transformations.distinctUntilChanged;

public class ManageAccountsViewModel extends AndroidViewModel {

    private final ExecutorService executor = Executors.newCachedThreadPool();

    @NonNull
    private final NotesRepository repo;

    public ManageAccountsViewModel(@NonNull Application application) {
        super(application);
        this.repo = NotesRepository.getInstance(application);
    }

    public void getCurrentAccount(@NonNull Context context, @NonNull IResponseCallback<Account> callback) {
        try {
            callback.onSuccess(repo.getAccountByName((SingleAccountHelper.getCurrentSingleSignOnAccount(context).name)));
        } catch (NextcloudFilesAppAccountNotFoundException | NoCurrentAccountSelectedException e) {
            callback.onError(e);
        }
    }

    public LiveData<List<Account>> getAccounts$() {
        return distinctUntilChanged(repo.getAccounts$());
    }

    public void deleteAccount(@NonNull Account account, @NonNull Context context) {
        executor.submit(() -> {
            final var accounts = repo.getAccounts();
            for (int i = 0; i < accounts.size(); i++) {
                if (accounts.get(i).getId() == account.getId()) {
                    if (i > 0) {
                        selectAccount(accounts.get(i - 1), context);
                    } else if (accounts.size() > 1) {
                        selectAccount(accounts.get(i + 1), context);
                    } else {
                        selectAccount(null, context);
                    }
                    repo.deleteAccount(accounts.get(i));
                    break;
                }
            }
        });
    }

    public void selectAccount(@Nullable Account account, @NonNull Context context) {
        SingleAccountHelper.commitCurrentAccount(context, (account == null) ? null : account.getAccountName());
    }

    public void countUnsynchronizedNotes(long accountId, @NonNull IResponseCallback<Long> callback) {
        executor.submit(() -> callback.onSuccess(repo.countUnsynchronizedNotes(accountId)));
    }
}