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

NoteDetailsViewModel.java « details « edit « 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: 028ff0fdaacec5904fe012d70b222151ea31efaf (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
package it.niedermann.owncloud.notes.edit.details;

import android.app.Application;

import androidx.annotation.AnyThread;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.SavedStateHandle;

import java.time.Instant;
import java.util.Calendar;
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.persistence.entity.Note;
import it.niedermann.owncloud.notes.shared.model.IResponseCallback;

public class NoteDetailsViewModel extends AndroidViewModel {

    private final ExecutorService executor = Executors.newCachedThreadPool();

    @NonNull
    private final NotesRepository repo;

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

    public void getNoteById(long noteId, @NonNull IResponseCallback<Note> callback) {
        executor.submit(() -> callback.onSuccess(repo.getNoteById(noteId)));
    }

    public LiveData<String> getTitle$(long noteId) {
        return repo.getTitle$(noteId);
    }

    public LiveData<Calendar> getModified$(long noteId) {
        return repo.getModified$(noteId);
    }

    public LiveData<String> getCategory$(long noteId) {
        return repo.getCategory$(noteId);
    }

    public LiveData<Boolean> isFavorite$(long noteId) {
        return repo.isFavorite$(noteId);
    }

    @AnyThread
    public void toggleFavorite(@NonNull Account account, long noteId) {
        repo.toggleFavoriteAndSync(account, noteId);
    }

    public void commit(@NonNull Account account, long noteId, String title, String category) {
        executor.submit(() -> {
            final Note note = repo.getNoteById(noteId);
            note.setCategory(category);
            repo.updateNoteAndSync(account, note, note.getContent(), title, null);
        });
    }
}