Search in sources :

Example 6 with StarDataSource

use of de.geeksfactory.opacclient.storage.StarDataSource in project opacclient by opacapp.

the class StarredFragment method getEncodedStarredObjects.

private JSONObject getEncodedStarredObjects() {
    JSONObject starred = new JSONObject();
    try {
        starred.put(JSON_LIBRARY_NAME, app.getLibrary().getIdent());
        JSONArray items = new JSONArray();
        StarDataSource data = new StarDataSource(getActivity());
        List<Starred> libItems = data.getAllItems(app.getLibrary().getIdent());
        for (Starred libItem : libItems) {
            JSONObject item = new JSONObject();
            item.put(JSON_ITEM_MNR, libItem.getMNr());
            item.put(JSON_ITEM_TITLE, libItem.getTitle());
            item.put(JSON_ITEM_MEDIATYPE, libItem.getMediaType());
            items.put(item);
        }
        starred.put(JSON_STARRED_LIST, items);
    } catch (JSONException e) {
        showExportError();
    }
    return starred;
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Starred(de.geeksfactory.opacclient.objects.Starred) StarDataSource(de.geeksfactory.opacclient.storage.StarDataSource)

Example 7 with StarDataSource

use of de.geeksfactory.opacclient.storage.StarDataSource in project opacclient by opacapp.

the class StarredFragment method onActivityResult.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE_EXPORT && resultCode == Activity.RESULT_OK) {
        Log.i("StarredFragment", data.toString());
        Uri uri = data.getData();
        try {
            OutputStream os = getActivity().getContentResolver().openOutputStream(uri);
            if (os != null) {
                JSONObject starred = getEncodedStarredObjects();
                PrintWriter pw = new PrintWriter(os, true);
                pw.write(starred.toString());
                pw.close();
                os.close();
            } else {
                showExportError();
            }
        } catch (FileNotFoundException e) {
            showExportError();
        } catch (IOException e) {
            showExportError();
        }
    } else if (requestCode == REQUEST_CODE_IMPORT && resultCode == Activity.RESULT_OK) {
        Uri uri = data.getData();
        try {
            StarDataSource dataSource = new StarDataSource(getActivity());
            InputStream is = getActivity().getContentResolver().openInputStream(uri);
            if (is != null) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                StringBuilder builder = new StringBuilder();
                String line = "";
                while ((line = reader.readLine()) != null) {
                    builder.append(line);
                }
                String list = builder.toString();
                JSONObject savedList = new JSONObject(list);
                String bib = savedList.getString(JSON_LIBRARY_NAME);
                // disallow import if from different library than current library
                if (bib != null && !bib.equals(app.getLibrary().getIdent())) {
                    Snackbar.make(getView(), R.string.info_different_library, Snackbar.LENGTH_SHORT).show();
                    return;
                }
                JSONArray items = savedList.getJSONArray(JSON_STARRED_LIST);
                for (int i = 0; i < items.length(); i++) {
                    JSONObject entry = items.getJSONObject(i);
                    if (entry.has(JSON_ITEM_MNR) && !dataSource.isStarred(bib, entry.getString(JSON_ITEM_MNR)) || !entry.has(JSON_ITEM_MNR) && !dataSource.isStarredTitle(bib, entry.getString(JSON_ITEM_TITLE))) {
                        // disallow dupes
                        dataSource.star(entry.optString(JSON_ITEM_MNR), entry.getString(JSON_ITEM_TITLE), bib, SearchResult.MediaType.valueOf(entry.getString(JSON_ITEM_MEDIATYPE)));
                    }
                }
                adapter.notifyDataSetChanged();
                Snackbar.make(getView(), R.string.info_starred_updated, Snackbar.LENGTH_SHORT).show();
            } else {
                showImportError();
            }
        } catch (JSONException | IOException e) {
            showImportError();
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileNotFoundException(java.io.FileNotFoundException) JSONArray(org.json.JSONArray) IOException(java.io.IOException) Uri(android.net.Uri) StarDataSource(de.geeksfactory.opacclient.storage.StarDataSource) JSONObject(org.json.JSONObject) BufferedReader(java.io.BufferedReader) PrintWriter(java.io.PrintWriter)

Aggregations

StarDataSource (de.geeksfactory.opacclient.storage.StarDataSource)7 OpacClient (de.geeksfactory.opacclient.OpacClient)3 Intent (android.content.Intent)2 OnClickListener (android.view.View.OnClickListener)2 OpacApi (de.geeksfactory.opacclient.apis.OpacApi)2 Starred (de.geeksfactory.opacclient.objects.Starred)2 JSONArray (org.json.JSONArray)2 JSONObject (org.json.JSONObject)2 DialogInterface (android.content.DialogInterface)1 Uri (android.net.Uri)1 Snackbar (android.support.design.widget.Snackbar)1 AlertDialog (android.support.v7.app.AlertDialog)1 View (android.view.View)1 AbsListView (android.widget.AbsListView)1 AdapterView (android.widget.AdapterView)1 ImageView (android.widget.ImageView)1 ListView (android.widget.ListView)1 TextView (android.widget.TextView)1 Toast (android.widget.Toast)1 EbookServiceApi (de.geeksfactory.opacclient.apis.EbookServiceApi)1