1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-05 10:49:24 +02:00

code simplification

This commit is contained in:
Nikolay Pultsin 2011-07-14 11:15:15 +01:00
parent 06522921a2
commit 63e99307bd
4 changed files with 27 additions and 31 deletions

View file

@ -127,11 +127,11 @@ abstract class BaseActivity extends ListActivity {
final FBTreeInfo info = myInfoMap.get(tree); final FBTreeInfo info = myInfoMap.get(tree);
if (info != null && info.CoverResourceId != -1) { if (info != null && info.CoverResourceId != -1) {
return info.CoverResourceId; return info.CoverResourceId;
} else if (((LibraryTree)tree).getBook() != null) {
return R.drawable.ic_list_library_book;
} else if (tree instanceof FileTree) { } else if (tree instanceof FileTree) {
final FileTree fileTree = (FileTree)tree; final FileTree fileTree = (FileTree)tree;
if (fileTree.getBook() != null) { if (fileTree.getFile().isDirectory()) {
return R.drawable.ic_list_library_book;
} else if (fileTree.getFile().isDirectory()) {
if (fileTree.getFile().isReadable()) { if (fileTree.getFile().isReadable()) {
return R.drawable.ic_list_library_folder; return R.drawable.ic_list_library_folder;
} else { } else {
@ -146,8 +146,6 @@ abstract class BaseActivity extends ListActivity {
return R.drawable.ic_list_library_author; return R.drawable.ic_list_library_author;
} else if (tree instanceof TagTree) { } else if (tree instanceof TagTree) {
return R.drawable.ic_list_library_tag; return R.drawable.ic_list_library_tag;
} else if (tree instanceof BookTree) {
return R.drawable.ic_list_library_book;
} else { } else {
return R.drawable.ic_list_library_books; return R.drawable.ic_list_library_books;
} }
@ -155,14 +153,13 @@ abstract class BaseActivity extends ListActivity {
@Override @Override
protected void onListItemClick(ListView listView, View view, int position, long rowId) { protected void onListItemClick(ListView listView, View view, int position, long rowId) {
final FBTree tree = getListAdapter().getItem(position); final LibraryTree tree = (LibraryTree)getListAdapter().getItem(position);
if (tree instanceof FileTree) { final Book book = tree.getBook();
final FileTree fileTree = (FileTree)tree;
final ZLFile file = fileTree.getFile();
final Book book = fileTree.getBook();
if (book != null) { if (book != null) {
showBookInfo(book); showBookInfo(book);
} else if (!file.isReadable()) { } else if (tree instanceof FileTree) {
final ZLFile file = ((FileTree)tree).getFile();
if (!file.isReadable()) {
UIUtil.showErrorMessage(this, "permissionDenied"); UIUtil.showErrorMessage(this, "permissionDenied");
} else if (file.isDirectory() || file.isArchive()) { } else if (file.isDirectory() || file.isArchive()) {
startActivityForResult( startActivityForResult(
@ -172,8 +169,6 @@ abstract class BaseActivity extends ListActivity {
CHILD_LIST_REQUEST CHILD_LIST_REQUEST
); );
} }
} else if (tree instanceof BookTree) {
showBookInfo(((BookTree)tree).Book);
} else { } else {
final FBTreeInfo info = myInfoMap.get(tree); final FBTreeInfo info = myInfoMap.get(tree);
if (info != null && info.Action != null) { if (info != null && info.Action != null) {
@ -189,8 +184,9 @@ abstract class BaseActivity extends ListActivity {
return false; return false;
} }
if (tree instanceof BookTree) { final Book book = ((LibraryTree)tree).getBook();
return mySelectedBook.equals(((BookTree)tree).Book); if (book != null) {
return mySelectedBook.equals(book);
} else if (tree instanceof AuthorTree) { } else if (tree instanceof AuthorTree) {
return mySelectedBook.authors().contains(((AuthorTree)tree).Author); return mySelectedBook.authors().contains(((AuthorTree)tree).Author);
} else if (tree instanceof TitleTree) { } else if (tree instanceof TitleTree) {
@ -215,12 +211,7 @@ abstract class BaseActivity extends ListActivity {
return false; return false;
} }
final ZLFile file = fileTree.getFile(); final ZLFile file = fileTree.getFile();
final String path = file.getPath(); String prefix = file.getPath();
if (mySelectedBookPath.equals(path)) {
return true;
}
String prefix = path;
if (file.isDirectory()) { if (file.isDirectory()) {
if (!prefix.endsWith("/")) { if (!prefix.endsWith("/")) {
prefix += '/'; prefix += '/';
@ -303,15 +294,10 @@ abstract class BaseActivity extends ListActivity {
@Override @Override
public boolean onContextItemSelected(MenuItem item) { public boolean onContextItemSelected(MenuItem item) {
final int position = ((AdapterView.AdapterContextMenuInfo)item.getMenuInfo()).position; final int position = ((AdapterView.AdapterContextMenuInfo)item.getMenuInfo()).position;
final FBTree tree = getListAdapter().getItem(position); final Book book = ((LibraryTree)getListAdapter().getItem(position)).getBook();
if (tree instanceof BookTree) {
return onContextItemSelected(item.getItemId(), ((BookTree)tree).Book);
} else if (tree instanceof FileTree) {
final Book book = ((FileTree)tree).getBook();
if (book != null) { if (book != null) {
return onContextItemSelected(item.getItemId(), book); return onContextItemSelected(item.getItemId(), book);
} }
}
return super.onContextItemSelected(item); return super.onContextItemSelected(item);
} }

View file

@ -36,6 +36,11 @@ public class BookTree extends LibraryTree {
return Book.getTitle(); return Book.getTitle();
} }
@Override
public Book getBook() {
return Book;
}
@Override @Override
protected String getStringId() { protected String getStringId() {
return getName(); return getName();

View file

@ -104,6 +104,7 @@ public class FileTree extends LibraryTree {
return myFile; return myFile;
} }
@Override
public Book getBook() { public Book getBook() {
return Book.getByFile(myFile); return Book.getByFile(myFile);
} }

View file

@ -36,6 +36,10 @@ public abstract class LibraryTree extends FBTree {
return getName(); return getName();
} }
public Book getBook() {
return null;
}
TagTree createTagSubTree(Tag tag) { TagTree createTagSubTree(Tag tag) {
return new TagTree(this, tag); return new TagTree(this, tag);
} }