Skip to content

Commit

Permalink
onClick listeners for export/import textviews, geometer#294
Browse files Browse the repository at this point in the history
  • Loading branch information
liquiddandruff authored and redmanmale committed Mar 29, 2018
1 parent 4b444ab commit 8618c22
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
Expand Up @@ -361,8 +361,30 @@ public View getView(int position, View convertView, ViewGroup parent) {
bookTitleView.setVisibility(View.GONE);
exportView.setText("Export");
exportView.setVisibility(View.VISIBLE);
exportView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String out = "FBReader." + myBook.getTitle() + ".xml";
if(myCollection.exportBookmarks(out, new BookmarkQuery(myBook, 20)))
Toast.makeText(getBaseContext(), "Bookmarks of current book exported to " + out, Toast.LENGTH_LONG).show();
else
Toast.makeText(getBaseContext(), "Failed to export bookmarks to " + out, Toast.LENGTH_LONG).show();
return;
}
});
importView.setText("Import");
importView.setVisibility(View.VISIBLE);
importView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String in = "FBReader." + myBook.getTitle() + ".xml";
if(myCollection.importBookmarks(in))
Toast.makeText(getBaseContext(), "Bookmarks of current book imported from " + in, Toast.LENGTH_LONG).show();
else
Toast.makeText(getBaseContext(), "Failed to import bookmarks from " + in, Toast.LENGTH_LONG).show();
return;
}
});
} else {
exportView.setVisibility(View.GONE);
importView.setVisibility(View.GONE);
Expand Down Expand Up @@ -407,15 +429,6 @@ public final int getCount() {
}

public final void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// use listener
if (view.findViewById(R.id.bookmark_export).getVisibility() == View.VISIBLE) {
String out = "FBReader." + myBook.getTitle() + ".xml";
if(myCollection.exportBookmarks(out, new BookmarkQuery(myBook, 20)))
Toast.makeText(getBaseContext(), "Bookmarks of current book exported to " + out, Toast.LENGTH_LONG).show();
else
Toast.makeText(getBaseContext(), "Failed to export bookmarks", Toast.LENGTH_LONG).show();
return;
}
final Bookmark bookmark = getItem(position);
if (bookmark != null) {
gotoBookmark(bookmark);
Expand Down
Expand Up @@ -57,7 +57,7 @@ final class SQLiteBooksDatabase extends BooksDatabase {
myDatabase = context.openOrCreateDatabase("books.db", Context.MODE_PRIVATE, null);
migrate();
//exportBookmarks("FBReader.bookmarks.xml");
importBookmarks("FBReader.bookmarks.xml");
//importBookmarks("FBReader.bookmarks.xml");
}

@Override
Expand Down Expand Up @@ -971,12 +971,12 @@ protected void deleteBookmark(Bookmark bookmark) {
}

@Override
protected boolean exportBookmarks(String dir) {
protected boolean exportBookmarks(String dir, BookmarkQuery query) {
File sdDir = new File(Environment.getExternalStorageDirectory(), dir);
try {
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(sdDir),"utf-8"));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(sdDir), "UTF-8"));
StringBuilder sb = new StringBuilder();
for (BookmarkQuery query = new BookmarkQuery(20); ; query = query.next()) {
for (;; query = query.next()) {
final List<Bookmark> bookmarks = loadBookmarks(query);
if (bookmarks.isEmpty()) {
break;
Expand All @@ -997,16 +997,17 @@ protected boolean exportBookmarks(String dir) {
protected boolean importBookmarks(String dir) {
File sdDir = new File(Environment.getExternalStorageDirectory(), dir);
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(sdDir),"utf-8"));
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(sdDir), "UTF-8"));
StringBuilder sb = new StringBuilder();
String line;
while((line = reader.readLine()) != null) {
sb.append(line);
}
String token = "<?xml version='1.1' encoding='UTF-8'?>";
List<String> serializedList = Arrays.asList(sb.toString().split(Pattern.quote(token)));
String[] serializedList = sb.toString().split(Pattern.quote(token));
for(String xml : serializedList) {
if(xml.length() <= 10) continue;
// returns -1 on failure
saveBookmark(SerializerUtil.deserializeBookmark(token + xml), true);
}
reader.close();
Expand Down

0 comments on commit 8618c22

Please sign in to comment.