Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8bf727a8 authored by Stefan Niedermann's avatar Stefan Niedermann
Browse files

#1170 Migrate NotesServerSyncHelper to NotesRepository

parent 5018bdc9
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -102,6 +102,7 @@ dependencies {

    // Testing
    testImplementation 'junit:junit:4.13.2'
    testImplementation 'org.mockito:mockito-core:3.9.0'
    testImplementation 'org.robolectric:robolectric:4.5.1'
    testImplementation 'androidx.test:core:1.3.0'
    testImplementation 'androidx.test.ext:junit:1.1.2'
+5 −5
Original line number Diff line number Diff line
@@ -21,7 +21,7 @@ import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.branding.BrandedDialogFragment;
import it.niedermann.owncloud.notes.databinding.DialogAccountSwitcherBinding;
import it.niedermann.owncloud.notes.manageaccounts.ManageAccountsActivity;
import it.niedermann.owncloud.notes.persistence.NotesDatabase;
import it.niedermann.owncloud.notes.persistence.NotesRepository;
import it.niedermann.owncloud.notes.persistence.entity.Account;

import static it.niedermann.owncloud.notes.branding.BrandingUtil.applyBrandToLayerDrawable;
@@ -33,7 +33,7 @@ public class AccountSwitcherDialog extends BrandedDialogFragment {

    private static final String KEY_CURRENT_ACCOUNT_ID = "current_account_id";

    private NotesDatabase db;
    private NotesRepository repo;
    private DialogAccountSwitcherBinding binding;
    private AccountSwitcherListener accountSwitcherListener;
    private long currentAccountId;
@@ -55,7 +55,7 @@ public class AccountSwitcherDialog extends BrandedDialogFragment {
            this.currentAccountId = args.getLong(KEY_CURRENT_ACCOUNT_ID);
        }

        db = NotesDatabase.getInstance(requireActivity());
        repo = NotesRepository.getInstance(requireContext());
    }

    @NonNull
@@ -63,7 +63,7 @@ public class AccountSwitcherDialog extends BrandedDialogFragment {
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        binding = DialogAccountSwitcherBinding.inflate(requireActivity().getLayoutInflater());

        final LiveData<Account> account$ = db.getAccountDao().getAccountById$(currentAccountId);
        final LiveData<Account> account$ = repo.getAccountById$(currentAccountId);
        account$.observe(requireActivity(), (currentLocalAccount) -> {
            account$.removeObservers(requireActivity());

@@ -81,7 +81,7 @@ public class AccountSwitcherDialog extends BrandedDialogFragment {
                dismiss();
            }));
            binding.accountsList.setAdapter(adapter);
            final LiveData<List<Account>> localAccounts$ = db.getAccountDao().getAccounts$();
            final LiveData<List<Account>> localAccounts$ = repo.getAccounts$();
            localAccounts$.observe(requireActivity(), (localAccounts) -> {
                localAccounts$.removeObservers(requireActivity());
                for (Account localAccount : localAccounts) {
+17 −17
Original line number Diff line number Diff line
@@ -41,7 +41,7 @@ import it.niedermann.owncloud.notes.edit.category.CategoryDialogFragment;
import it.niedermann.owncloud.notes.edit.category.CategoryDialogFragment.CategoryDialogListener;
import it.niedermann.owncloud.notes.edit.title.EditTitleDialogFragment;
import it.niedermann.owncloud.notes.edit.title.EditTitleDialogFragment.EditTitleListener;
import it.niedermann.owncloud.notes.persistence.NotesDatabase;
import it.niedermann.owncloud.notes.persistence.NotesRepository;
import it.niedermann.owncloud.notes.persistence.entity.Account;
import it.niedermann.owncloud.notes.persistence.entity.Note;
import it.niedermann.owncloud.notes.shared.model.ApiVersion;
@@ -76,7 +76,7 @@ public abstract class BaseNoteFragment extends BrandedFragment implements Catego
    @Nullable
    private Note originalNote;
    private int originalScrollY;
    protected NotesDatabase db;
    protected NotesRepository repo;
    private NoteFragmentListener listener;
    private boolean titleModified = false;

@@ -90,7 +90,7 @@ public abstract class BaseNoteFragment extends BrandedFragment implements Catego
        } catch (ClassCastException e) {
            throw new ClassCastException(context.getClass() + " must implement " + NoteFragmentListener.class);
        }
        db = NotesDatabase.getInstance(context);
        repo = NotesRepository.getInstance(context);
    }

    @Override
@@ -99,7 +99,7 @@ public abstract class BaseNoteFragment extends BrandedFragment implements Catego
        new Thread(() -> {
            try {
                SingleSignOnAccount ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(requireContext().getApplicationContext());
                this.localAccount = db.getAccountDao().getAccountByName(ssoAccount.name);
                this.localAccount = repo.getAccountByName(ssoAccount.name);

                if (savedInstanceState == null) {
                    long id = requireArguments().getLong(PARAM_NOTE_ID);
@@ -107,11 +107,11 @@ public abstract class BaseNoteFragment extends BrandedFragment implements Catego
                        long accountId = requireArguments().getLong(PARAM_ACCOUNT_ID);
                        if (accountId > 0) {
                            /* Switch account if account id has been provided */
                            this.localAccount = db.getAccountDao().getAccountById(accountId);
                            this.localAccount = repo.getAccountById(accountId);
                            SingleAccountHelper.setCurrentAccount(requireContext().getApplicationContext(), localAccount.getAccountName());
                        }
                        isNew = false;
                        note = originalNote = db.getNoteDao().getNoteById(id);
                        note = originalNote = repo.getNoteById(id);
                        requireActivity().runOnUiThread(() -> onNoteLoaded(note));
                        requireActivity().invalidateOptionsMenu();
                    } else {
@@ -126,7 +126,7 @@ public abstract class BaseNoteFragment extends BrandedFragment implements Catego
                                requireActivity().invalidateOptionsMenu();
                            }
                        } else {
                            note = db.addNote(localAccount.getId(), cloudNote);
                            note = repo.addNote(localAccount.getId(), cloudNote);
                            originalNote = null;
                            requireActivity().runOnUiThread(() -> onNoteLoaded(note));
                            requireActivity().invalidateOptionsMenu();
@@ -213,19 +213,19 @@ public abstract class BaseNoteFragment extends BrandedFragment implements Catego
        if (itemId == R.id.menu_cancel) {
            new Thread(() -> {
                if (originalNote == null) {
                    db.deleteNoteAndSync(localAccount, note.getId());
                    repo.deleteNoteAndSync(localAccount, note.getId());
                } else {
                    db.updateNoteAndSync(localAccount, originalNote, null, null, null);
                    repo.updateNoteAndSync(localAccount, originalNote, null, null, null);
                }
            }).start();
            listener.close();
            return true;
        } else if (itemId == R.id.menu_delete) {
            db.deleteNoteAndSync(localAccount, note.getId());
            repo.deleteNoteAndSync(localAccount, note.getId());
            listener.close();
            return true;
        } else if (itemId == R.id.menu_favorite) {
            db.toggleFavoriteAndSync(localAccount, note.getId());
            repo.toggleFavoriteAndSync(localAccount, note.getId());
            listener.onNoteUpdated(note);
            prepareFavoriteOption(item);
            return true;
@@ -288,7 +288,7 @@ public abstract class BaseNoteFragment extends BrandedFragment implements Catego

    public void onCloseNote() {
        if (!titleModified && originalNote == null && getContent().isEmpty()) {
            db.deleteNoteAndSync(localAccount, note.getId());
            repo.deleteNoteAndSync(localAccount, note.getId());
        }
    }

@@ -304,13 +304,13 @@ public abstract class BaseNoteFragment extends BrandedFragment implements Catego
            if (note.getContent().equals(newContent)) {
                if (note.getScrollY() != originalScrollY) {
                    Log.v(TAG, "... only saving new scroll state, since content did not change");
                    db.getNoteDao().updateScrollY(note.getId(), note.getScrollY());
                    repo.updateScrollY(note.getId(), note.getScrollY());
                } else {
                    Log.v(TAG, "... not saving, since nothing has changed");
                }
            } else {
                // FIXME requires database queries on main thread!
                note = db.updateNoteAndSync(localAccount, note, newContent, null, callback);
                note = repo.updateNoteAndSync(localAccount, note, newContent, null, callback);
                listener.onNoteUpdated(note);
                requireActivity().invalidateOptionsMenu();
            }
@@ -354,7 +354,7 @@ public abstract class BaseNoteFragment extends BrandedFragment implements Catego

    @Override
    public void onCategoryChosen(String category) {
        db.setCategory(localAccount, note.getId(), category);
        repo.setCategory(localAccount, note.getId(), category);
        note.setCategory(category);
        listener.onNoteUpdated(note);
    }
@@ -364,13 +364,13 @@ public abstract class BaseNoteFragment extends BrandedFragment implements Catego
        titleModified = true;
        note.setTitle(newTitle);
        new Thread(() -> {
            note = db.updateNoteAndSync(localAccount, note, note.getContent(), newTitle, null);
            note = repo.updateNoteAndSync(localAccount, note, note.getContent(), newTitle, null);
            requireActivity().runOnUiThread(() -> listener.onNoteUpdated(note));
        }).start();
    }

    public void moveNote(Account account) {
        db.moveNoteToAnotherAccount(account, note);
        repo.moveNoteToAnotherAccount(account, note);
        listener.close();
    }

+6 −6
Original line number Diff line number Diff line
@@ -125,7 +125,7 @@ public class NotePreviewFragment extends SearchableBaseNoteFragment implements O
    protected void registerInternalNoteLinkHandler() {
        binding.singleNoteContent.registerOnLinkClickCallback((link) -> {
            try {
                final long noteLocalId = db.getNoteDao().getLocalIdByRemoteId(this.note.getAccountId(), Long.parseLong(link));
                final long noteLocalId = repo.getLocalIdByRemoteId(this.note.getAccountId(), Long.parseLong(link));
                Log.i(TAG, "Found note for remoteId \"" + link + "\" in account \"" + this.note.getAccountId() + "\" with localId + \"" + noteLocalId + "\". Attempt to open " + EditNoteActivity.class.getSimpleName() + " for this note.");
                startActivity(new Intent(requireActivity().getApplicationContext(), EditNoteActivity.class).putExtra(EditNoteActivity.PARAM_NOTE_ID, noteLocalId));
                return true;
@@ -153,20 +153,20 @@ public class NotePreviewFragment extends SearchableBaseNoteFragment implements O

    @Override
    public void onRefresh() {
        if (noteLoaded && db.getNoteServerSyncHelper().isSyncPossible() && SSOUtil.isConfigured(getContext())) {
        if (noteLoaded && repo.isSyncPossible() && SSOUtil.isConfigured(getContext())) {
            binding.swiperefreshlayout.setRefreshing(true);
            new Thread(() -> {
                try {
                    final Account account = db.getAccountDao().getAccountByName(SingleAccountHelper.getCurrentSingleSignOnAccount(requireContext()).name);
                    db.getNoteServerSyncHelper().addCallbackPull(account, () -> new Thread(() -> {
                        note = db.getNoteDao().getNoteById(note.getId());
                    final Account account = repo.getAccountByName(SingleAccountHelper.getCurrentSingleSignOnAccount(requireContext()).name);
                    repo.addCallbackPull(account, () -> new Thread(() -> {
                        note = repo.getNoteById(note.getId());
                        changedText = note.getContent();
                        requireActivity().runOnUiThread(() -> {
                            binding.singleNoteContent.setMarkdownString(note.getContent());
                            binding.swiperefreshlayout.setRefreshing(false);
                        });
                    }).start());
                    db.getNoteServerSyncHelper().scheduleSync(account, false);
                    repo.scheduleSync(account, false);
                } catch (NextcloudFilesAppAccountNotFoundException | NoCurrentAccountSelectedException e) {
                    e.printStackTrace();
                }
+4 −4
Original line number Diff line number Diff line
@@ -11,7 +11,7 @@ import androidx.lifecycle.MutableLiveData;
import java.util.List;

import it.niedermann.owncloud.notes.main.navigation.NavigationItem;
import it.niedermann.owncloud.notes.persistence.NotesDatabase;
import it.niedermann.owncloud.notes.persistence.NotesRepository;

import static androidx.lifecycle.Transformations.map;
import static androidx.lifecycle.Transformations.switchMap;
@@ -19,14 +19,14 @@ import static it.niedermann.owncloud.notes.shared.util.DisplayUtils.convertToCat

public class CategoryViewModel extends AndroidViewModel {

    private final NotesDatabase db;
    private final NotesRepository repo;

    @NonNull
    private final MutableLiveData<String> searchTerm = new MutableLiveData<>("");

    public CategoryViewModel(@NonNull Application application) {
        super(application);
        db = NotesDatabase.getInstance(application);
        repo = NotesRepository.getInstance(application);
    }

    public void postSearchTerm(@NonNull String searchTerm) {
@@ -36,7 +36,7 @@ public class CategoryViewModel extends AndroidViewModel {
    @NonNull
    public LiveData<List<NavigationItem.CategoryNavigationItem>> getCategories(long accountId) {
        return switchMap(this.searchTerm, searchTerm ->
                map(db.getNoteDao().searchCategories$(accountId, TextUtils.isEmpty(searchTerm) ? "%" : "%" + searchTerm + "%"),
                map(repo.searchCategories$(accountId, TextUtils.isEmpty(searchTerm) ? "%" : "%" + searchTerm + "%"),
                        categories -> convertToCategoryNavigationItem(getApplication(), categories)));
    }
}
Loading