Search in sources :

Example 1 with NoteItem

use of forpdateam.ru.forpda.data.models.notes.NoteItem 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());
}
Also used : JSONObject(org.json.JSONObject) NoteItem(forpdateam.ru.forpda.data.models.notes.NoteItem) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) NoteItemBd(forpdateam.ru.forpda.data.realm.notes.NoteItemBd) JSONException(org.json.JSONException)

Example 2 with NoteItem

use of forpdateam.ru.forpda.data.models.notes.NoteItem 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);
}
Also used : JSONObject(org.json.JSONObject) NoteItem(forpdateam.ru.forpda.data.models.notes.NoteItem) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) NoteItemBd(forpdateam.ru.forpda.data.realm.notes.NoteItemBd)

Example 3 with NoteItem

use of forpdateam.ru.forpda.data.models.notes.NoteItem 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);
}
Also used : FunnyContent(forpdateam.ru.forpda.ui.views.FunnyContent) NoteItem(forpdateam.ru.forpda.data.models.notes.NoteItem) ArrayList(java.util.ArrayList) NoteItemBd(forpdateam.ru.forpda.data.realm.notes.NoteItemBd)

Aggregations

NoteItem (forpdateam.ru.forpda.data.models.notes.NoteItem)3 NoteItemBd (forpdateam.ru.forpda.data.realm.notes.NoteItemBd)3 ArrayList (java.util.ArrayList)3 JSONArray (org.json.JSONArray)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 FunnyContent (forpdateam.ru.forpda.ui.views.FunnyContent)1