From 90db3e6a1af2eb28033610a083feee3cbeca3147 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Thu, 20 Nov 2014 23:19:06 -0500 Subject: [PATCH] Fix the download progress not updating Integer roundoff errors... --- .../src/test/kotlin/DLProgressEventTest.kt | 20 +++++++++++ .../activity/downloader/BookItemHolder.java | 36 +++++++------------ .../downloader/manager/BookManager.kt | 6 ++-- .../downloader/manager/DLProgressEvent.kt | 2 +- 4 files changed, 38 insertions(+), 26 deletions(-) create mode 100644 app-test/src/test/kotlin/DLProgressEventTest.kt diff --git a/app-test/src/test/kotlin/DLProgressEventTest.kt b/app-test/src/test/kotlin/DLProgressEventTest.kt new file mode 100644 index 0000000..b114d9a --- /dev/null +++ b/app-test/src/test/kotlin/DLProgressEventTest.kt @@ -0,0 +1,20 @@ +/** + * Created by bspeice on 11/20/14. + */ + +import org.junit.Test +import org.bspeice.minimalbible.activity.downloader.manager.DLProgressEvent +import org.crosswire.jsword.book.Book +import org.mockito.Mockito +import org.junit.Assert + +class DLProgressEventTest { + + val b = Mockito.mock(javaClass()) + + Test fun fiftyPercentIsOneEighty() { + + val e = DLProgressEvent(50, b) + Assert.assertEquals(180, e.toCircular()) + } +} diff --git a/app/src/main/java/org/bspeice/minimalbible/activity/downloader/BookItemHolder.java b/app/src/main/java/org/bspeice/minimalbible/activity/downloader/BookItemHolder.java index e1871fb..4f894a3 100644 --- a/app/src/main/java/org/bspeice/minimalbible/activity/downloader/BookItemHolder.java +++ b/app/src/main/java/org/bspeice/minimalbible/activity/downloader/BookItemHolder.java @@ -77,7 +77,7 @@ public class BookItemHolder { .subscribe(new Action1() { @Override public void call(DLProgressEvent event) { - BookItemHolder.this.displayProgress((int) event.toCircular()); + BookItemHolder.this.displayProgress(event.toCircular()); } }); } @@ -104,21 +104,15 @@ public class BookItemHolder { /** * Display the current progress of this download - * TODO: Clean up this logic if at all possible... * @param progress The progress out of 360 (degrees of a circle) */ private void displayProgress(int progress) { + int downloadView; if (progress == DLProgressEvent.PROGRESS_BEGINNING) { // Download starting - RelativeLayout.LayoutParams acronymParams = - (RelativeLayout.LayoutParams)acronym.getLayoutParams(); - acronymParams.addRule(RelativeLayout.LEFT_OF, downloadProgress.getId()); - - RelativeLayout.LayoutParams nameParams = - (RelativeLayout.LayoutParams)itemName.getLayoutParams(); - nameParams.addRule(RelativeLayout.LEFT_OF, downloadProgress.getId()); + downloadView = downloadProgress.getId(); isDownloaded.setVisibility(View.GONE); downloadProgress.setVisibility(View.VISIBLE); @@ -126,13 +120,7 @@ public class BookItemHolder { downloadProgress.spin(); } else if (progress < 360) { // Download in progress - RelativeLayout.LayoutParams acronymParams = - (RelativeLayout.LayoutParams)acronym.getLayoutParams(); - acronymParams.addRule(RelativeLayout.LEFT_OF, downloadProgress.getId()); - - RelativeLayout.LayoutParams nameParams = - (RelativeLayout.LayoutParams)itemName.getLayoutParams(); - nameParams.addRule(RelativeLayout.LEFT_OF, downloadProgress.getId()); + downloadView = downloadProgress.getId(); isDownloaded.setVisibility(View.GONE); downloadProgress.setVisibility(View.VISIBLE); @@ -142,18 +130,20 @@ public class BookItemHolder { } else { // Download complete subscription.unsubscribe(); - RelativeLayout.LayoutParams acronymParams = - (RelativeLayout.LayoutParams)acronym.getLayoutParams(); - acronymParams.addRule(RelativeLayout.LEFT_OF, isDownloaded.getId()); - - RelativeLayout.LayoutParams nameParams = - (RelativeLayout.LayoutParams)itemName.getLayoutParams(); - nameParams.addRule(RelativeLayout.LEFT_OF, isDownloaded.getId()); + downloadView = downloadProgress.getId(); isDownloaded.setVisibility(View.VISIBLE); downloadProgress.setVisibility(View.GONE); displayInstalled(); } + + RelativeLayout.LayoutParams acronymParams = + (RelativeLayout.LayoutParams) acronym.getLayoutParams(); + acronymParams.addRule(RelativeLayout.LEFT_OF, downloadView); + + RelativeLayout.LayoutParams nameParams = + (RelativeLayout.LayoutParams) itemName.getLayoutParams(); + nameParams.addRule(RelativeLayout.LEFT_OF, downloadView); } public void onScrollOffscreen() { diff --git a/app/src/main/kotlin/org/bspeice/minimalbible/activity/downloader/manager/BookManager.kt b/app/src/main/kotlin/org/bspeice/minimalbible/activity/downloader/manager/BookManager.kt index 1d07eeb..a970e1e 100644 --- a/app/src/main/kotlin/org/bspeice/minimalbible/activity/downloader/manager/BookManager.kt +++ b/app/src/main/kotlin/org/bspeice/minimalbible/activity/downloader/manager/BookManager.kt @@ -114,8 +114,10 @@ class BookManager(private val installedBooks: Books, val rM: RefreshManager) : val job = ev.getJob() bookMappings.filter { it.getKey() == job.getJobID() } .map { - val event = DLProgressEvent(job.getWorkDone() / job.getTotalWork() * 100, - it.getValue()) + // We multiply by 100 first to avoid integer truncation + // Also avoids roundoff error. Neat trick, but I'm spending just as much time + // documenting it as implementing the floating point would take + val event = DLProgressEvent(job.getWorkDone() * 100 / job.getTotalWork(), it.getValue()) downloadEvents onNext event if (job.getWorkDone() == job.getTotalWork()) { diff --git a/app/src/main/kotlin/org/bspeice/minimalbible/activity/downloader/manager/DLProgressEvent.kt b/app/src/main/kotlin/org/bspeice/minimalbible/activity/downloader/manager/DLProgressEvent.kt index 7f1ed9e..5bfd1dd 100644 --- a/app/src/main/kotlin/org/bspeice/minimalbible/activity/downloader/manager/DLProgressEvent.kt +++ b/app/src/main/kotlin/org/bspeice/minimalbible/activity/downloader/manager/DLProgressEvent.kt @@ -12,5 +12,5 @@ class DLProgressEvent(val progress: Int, val b: Book) { val PROGRESS_BEGINNING = 0 } - fun toCircular() = progress.toFloat() * 360 / 100 + fun toCircular() = (progress.toFloat() * 360 / 100).toInt() }