#1502 Navigate to correct tab and fix double insertions

This commit is contained in:
Sean Mac Gillicuddy 2019-09-23 11:47:18 +01:00
parent bf044e9ddb
commit 35e2da81e4
3 changed files with 42 additions and 8 deletions

View File

@ -39,15 +39,30 @@ class NewBookDao @Inject constructor(private val box: Box<BookOnDiskEntity>) {
fun insert(booksOnDisk: List<BookOnDisk>) {
box.store.callInTx {
box
.query {
inValues(BookOnDiskEntity_.bookId, booksOnDisk.map { it.book.id }.toTypedArray())
}
.remove()
box.put(booksOnDisk.distinctBy { it.book.id }.map(::BookOnDiskEntity))
val uniqueBooks = uniqueBooksByFile(booksOnDisk)
removeEntriesWithMatchingIds(uniqueBooks)
box.put(uniqueBooks.distinctBy { it.book.id }.map(::BookOnDiskEntity))
}
}
private fun uniqueBooksByFile(booksOnDisk: List<BookOnDisk>): List<BookOnDisk> {
val booksThatPointToSameLocation = box
.query {
inValues(BookOnDiskEntity_.file, booksOnDisk.map { it.file.path }.toTypedArray())
}.find().map(::BookOnDisk)
return booksOnDisk.filter { bookOnDisk: BookOnDisk ->
booksThatPointToSameLocation.find { it.file.path == bookOnDisk.file.path } == null
}
}
private fun removeEntriesWithMatchingIds(newBooks: List<BookOnDisk>) {
box
.query {
inValues(BookOnDiskEntity_.bookId, newBooks.map { it.book.id }.toTypedArray())
}
.remove()
}
fun delete(databaseId: Long) {
box.remove(databaseId)
}

View File

@ -255,6 +255,7 @@ public class MainActivity extends BaseActivity implements WebViewCallback,
private TextView tabSwitcherIcon;
private TableDrawerAdapter tableDrawerAdapter;
private RecyclerView tableDrawerRight;
private boolean hasLocalBooks;
private ItemTouchHelper.Callback tabCallback = new ItemTouchHelper.Callback() {
@Override
public int getMovementFlags(@NonNull RecyclerView recyclerView,
@ -895,7 +896,7 @@ public class MainActivity extends BaseActivity implements WebViewCallback,
return true;
case R.id.menu_openfile:
manageZimFiles(1);
manageZimFiles(hasLocalBooks ? 0 : 1);
break;
case R.id.menu_settings:
@ -1182,7 +1183,10 @@ public class MainActivity extends BaseActivity implements WebViewCallback,
storageObserver.getBooksOnFileSystem()
.take(1)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(presenter::saveBooks, Throwable::printStackTrace);
.subscribe(books -> {
presenter.saveBooks(books);
hasLocalBooks = !books.isEmpty();
}, Throwable::printStackTrace);
}
// Workaround for popup bottom menu on older devices

View File

@ -74,6 +74,18 @@ class ZimFileReader(
val description: String get() = jniKiwixReader.description
val favicon: String get() = jniKiwixReader.favicon
val language: String get() = jniKiwixReader.language
private val mediaCount: Int?
get() = try {
jniKiwixReader.mediaCount
} catch (ignore: UnsatisfiedLinkError) {
null
}
private val articleCount: Int?
get() = try {
jniKiwixReader.articleCount
} catch (ignore: UnsatisfiedLinkError) {
null
}
fun searchSuggestions(prefix: String, count: Int) =
jniKiwixReader.searchSuggestions(prefix, count)
@ -195,6 +207,9 @@ class ZimFileReader(
date = this@ZimFileReader.date
description = this@ZimFileReader.description
language = this@ZimFileReader.language
articleCount = this@ZimFileReader.articleCount.toString()
mediaCount = this@ZimFileReader.mediaCount.toString()
bookName = name
}
companion object {