use of forpdateam.ru.forpda.data.realm.notes.NoteItemBd in project ForPDA by RadiationX.
the class NotesFragment method exportNotes.
private void exportNotes() {
if (realm.isClosed())
return;
RealmResults<NoteItemBd> results = realm.where(NoteItemBd.class).findAllSorted("id", Sort.DESCENDING);
ArrayList<NoteItem> noteItems = new ArrayList<>();
for (NoteItemBd item : results) {
noteItems.add(new NoteItem(item));
}
final JSONArray jsonBody = new JSONArray();
for (NoteItem item : noteItems) {
try {
JSONObject jsonItem = new JSONObject();
jsonItem.put("id", item.getId());
jsonItem.put("title", item.getTitle());
jsonItem.put("link", item.getLink());
jsonItem.put("content", item.getContent());
jsonBody.put(jsonItem);
} catch (JSONException e) {
e.printStackTrace();
}
}
saveImageToExternalStorage(jsonBody.toString());
}
use of forpdateam.ru.forpda.data.realm.notes.NoteItemBd in project ForPDA by RadiationX.
the class NotesFragment method onAddNote.
@Override
public void onAddNote(NoteItem item) {
if (realm.isClosed())
return;
realm.executeTransactionAsync(realm1 -> {
NoteItemBd itemBd = realm1.where(NoteItemBd.class).equalTo("id", item.getId()).findFirst();
if (itemBd != null) {
itemBd.setTitle(item.getTitle());
itemBd.setLink(item.getLink());
itemBd.setContent(item.getContent());
} else {
itemBd = new NoteItemBd(item);
}
realm1.insertOrUpdate(itemBd);
}, this::loadCacheData);
}
use of forpdateam.ru.forpda.data.realm.notes.NoteItemBd in project ForPDA by RadiationX.
the class NotesFragment method importNotes.
private void importNotes(String jsonSource) {
ArrayList<NoteItem> noteItems = new ArrayList<>();
try {
final JSONArray jsonBody = new JSONArray(jsonSource);
for (int i = 0; i < jsonBody.length(); i++) {
try {
JSONObject jsonItem = jsonBody.getJSONObject(i);
NoteItem item = new NoteItem();
item.setId(jsonItem.getLong("id"));
item.setTitle(jsonItem.getString("title"));
item.setLink(jsonItem.getString("link"));
item.setContent(jsonItem.getString("content"));
noteItems.add(item);
} catch (JSONException e) {
e.printStackTrace();
}
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getContext(), "Ошибка разбора файла: " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
Toast.makeText(getContext(), "Заметки успешно импортированы", Toast.LENGTH_SHORT).show();
if (realm.isClosed())
return;
realm.executeTransactionAsync(realm1 -> {
for (NoteItem item : noteItems) {
realm1.insertOrUpdate(new NoteItemBd(item));
}
}, this::loadCacheData);
}
use of forpdateam.ru.forpda.data.realm.notes.NoteItemBd in project ForPDA by RadiationX.
the class NotesFragment method loadCacheData.
@Override
public void loadCacheData() {
super.loadCacheData();
if (!realm.isClosed()) {
setRefreshing(true);
RealmResults<NoteItemBd> results = realm.where(NoteItemBd.class).findAllSorted("id", Sort.DESCENDING);
if (results.isEmpty()) {
if (!contentController.contains(ContentController.TAG_NO_DATA)) {
FunnyContent funnyContent = new FunnyContent(getContext()).setImage(R.drawable.ic_bookmark).setTitle(R.string.funny_notes_nodata_title);
contentController.addContent(funnyContent, ContentController.TAG_NO_DATA);
}
contentController.showContent(ContentController.TAG_NO_DATA);
} else {
contentController.hideContent(ContentController.TAG_NO_DATA);
}
ArrayList<NoteItem> nonBdResult = new ArrayList<>();
for (NoteItemBd item : results) {
nonBdResult.add(new NoteItem(item));
}
adapter.addAll(nonBdResult);
}
setRefreshing(false);
}
use of forpdateam.ru.forpda.data.realm.notes.NoteItemBd in project ForPDA by RadiationX.
the class NotesFragment method addNote.
public static void addNote(NoteItem item) {
Realm realm = Realm.getDefaultInstance();
realm.executeTransactionAsync(realm1 -> {
realm1.insertOrUpdate(new NoteItemBd(item));
}, () -> {
realm.close();
NotesFragment notesFragment = (NotesFragment) TabManager.get().getByClass(NotesFragment.class);
if (notesFragment == null) {
return;
}
notesFragment.loadCacheData();
});
}
Aggregations