mirror of
				https://github.com/mihonapp/mihon.git
				synced 2025-11-04 16:18:55 +01:00 
			
		
		
		
	Listen for downloaded pages, but it's not updating the UI yet
This commit is contained in:
		@@ -4,6 +4,7 @@ import android.os.Bundle;
 | 
			
		||||
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
import java.util.concurrent.TimeUnit;
 | 
			
		||||
import java.util.concurrent.atomic.AtomicInteger;
 | 
			
		||||
 | 
			
		||||
import javax.inject.Inject;
 | 
			
		||||
 | 
			
		||||
@@ -16,6 +17,7 @@ import rx.Observable;
 | 
			
		||||
import rx.Subscription;
 | 
			
		||||
import rx.android.schedulers.AndroidSchedulers;
 | 
			
		||||
import rx.schedulers.Schedulers;
 | 
			
		||||
import rx.subjects.PublishSubject;
 | 
			
		||||
import timber.log.Timber;
 | 
			
		||||
 | 
			
		||||
public class DownloadQueuePresenter extends BasePresenter<DownloadQueueFragment> {
 | 
			
		||||
@@ -25,6 +27,7 @@ public class DownloadQueuePresenter extends BasePresenter<DownloadQueueFragment>
 | 
			
		||||
    private DownloadQueue downloadQueue;
 | 
			
		||||
    private Subscription statusSubscription;
 | 
			
		||||
    private HashMap<Download, Subscription> progressSubscriptions;
 | 
			
		||||
    private HashMap<Download, Subscription> pageStatusSubscriptions;
 | 
			
		||||
 | 
			
		||||
    public final static int GET_DOWNLOAD_QUEUE = 1;
 | 
			
		||||
 | 
			
		||||
@@ -34,6 +37,7 @@ public class DownloadQueuePresenter extends BasePresenter<DownloadQueueFragment>
 | 
			
		||||
 | 
			
		||||
        downloadQueue = downloadManager.getQueue();
 | 
			
		||||
        progressSubscriptions = new HashMap<>();
 | 
			
		||||
        pageStatusSubscriptions = new HashMap<>();
 | 
			
		||||
 | 
			
		||||
        restartableLatestCache(GET_DOWNLOAD_QUEUE,
 | 
			
		||||
                () -> Observable.just(downloadQueue.get()),
 | 
			
		||||
@@ -48,12 +52,12 @@ public class DownloadQueuePresenter extends BasePresenter<DownloadQueueFragment>
 | 
			
		||||
    protected void onTakeView(DownloadQueueFragment view) {
 | 
			
		||||
        super.onTakeView(view);
 | 
			
		||||
 | 
			
		||||
        statusSubscription = downloadQueue.getStatusObservable()
 | 
			
		||||
        add(statusSubscription = downloadQueue.getStatusObservable()
 | 
			
		||||
                .subscribeOn(Schedulers.io())
 | 
			
		||||
                .observeOn(AndroidSchedulers.mainThread())
 | 
			
		||||
                .subscribe(download -> {
 | 
			
		||||
                    processStatus(download, view);
 | 
			
		||||
                });
 | 
			
		||||
                }));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
@@ -66,9 +70,11 @@ public class DownloadQueuePresenter extends BasePresenter<DownloadQueueFragment>
 | 
			
		||||
        switch (download.getStatus()) {
 | 
			
		||||
            case Download.DOWNLOADING:
 | 
			
		||||
                observeProgress(download, view);
 | 
			
		||||
                observePagesStatus(download, view);
 | 
			
		||||
                break;
 | 
			
		||||
            case Download.DOWNLOADED:
 | 
			
		||||
                unsubscribeProgress(download);
 | 
			
		||||
                unsubscribePagesStatus(download);
 | 
			
		||||
                download.totalProgress = download.pages.size() * 100;
 | 
			
		||||
                view.updateProgress(download);
 | 
			
		||||
                break;
 | 
			
		||||
@@ -89,13 +95,52 @@ public class DownloadQueuePresenter extends BasePresenter<DownloadQueueFragment>
 | 
			
		||||
        progressSubscriptions.put(download, subscription);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void observePagesStatus(Download download, DownloadQueueFragment view) {
 | 
			
		||||
        PublishSubject<Integer> pageStatusSubject = PublishSubject.create();
 | 
			
		||||
        for (Page page : download.pages)
 | 
			
		||||
            page.setStatusSubject(pageStatusSubject);
 | 
			
		||||
 | 
			
		||||
        final AtomicInteger downloadedPages = new AtomicInteger(0);
 | 
			
		||||
 | 
			
		||||
        Subscription subscription = pageStatusSubject
 | 
			
		||||
                .startWith(Observable.from(download.pages)
 | 
			
		||||
                        .filter(page -> page.getStatus() == Page.READY)
 | 
			
		||||
                        .map(page -> Page.READY))
 | 
			
		||||
                .filter(status -> status == Page.READY)
 | 
			
		||||
                .map(status -> downloadedPages.incrementAndGet())
 | 
			
		||||
                .subscribe(count -> {
 | 
			
		||||
                    // TODO
 | 
			
		||||
                });
 | 
			
		||||
 | 
			
		||||
        pageStatusSubscriptions.put(download, subscription);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void unsubscribeProgress(Download download) {
 | 
			
		||||
        Subscription subscription = progressSubscriptions.remove(download);
 | 
			
		||||
        if (subscription != null)
 | 
			
		||||
            subscription.unsubscribe();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void unsubscribePagesStatus(Download download) {
 | 
			
		||||
        for (Page page : download.pages)
 | 
			
		||||
            page.setStatusSubject(null);
 | 
			
		||||
 | 
			
		||||
        Subscription subscription = pageStatusSubscriptions.remove(download);
 | 
			
		||||
        if (subscription != null)
 | 
			
		||||
            subscription.unsubscribe();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void destroySubscriptions() {
 | 
			
		||||
        for (Download download : pageStatusSubscriptions.keySet()) {
 | 
			
		||||
            for (Page page : download.pages)
 | 
			
		||||
                page.setStatusSubject(null);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        for (Subscription subscription : pageStatusSubscriptions.values()) {
 | 
			
		||||
            subscription.unsubscribe();
 | 
			
		||||
        }
 | 
			
		||||
        pageStatusSubscriptions.clear();
 | 
			
		||||
 | 
			
		||||
        for (Subscription subscription : progressSubscriptions.values()) {
 | 
			
		||||
            subscription.unsubscribe();
 | 
			
		||||
        }
 | 
			
		||||
 
 | 
			
		||||
@@ -16,6 +16,7 @@ public class DownloadHolder extends ItemViewHolder<Download> {
 | 
			
		||||
 | 
			
		||||
    @ViewId(R.id.download_title) TextView downloadTitle;
 | 
			
		||||
    @ViewId(R.id.download_progress) ProgressBar downloadProgress;
 | 
			
		||||
    @ViewId(R.id.download_progress_text) TextView downloadProgressText;
 | 
			
		||||
 | 
			
		||||
    public DownloadHolder(View view) {
 | 
			
		||||
        super(view);
 | 
			
		||||
 
 | 
			
		||||
@@ -5,28 +5,34 @@
 | 
			
		||||
    android:layout_marginLeft="15dp"
 | 
			
		||||
    android:layout_marginRight="15dp">
 | 
			
		||||
 | 
			
		||||
    <TextView
 | 
			
		||||
    <LinearLayout
 | 
			
		||||
        android:layout_width="match_parent"
 | 
			
		||||
        android:layout_height="wrap_content"
 | 
			
		||||
        android:layout_marginTop="10dp"
 | 
			
		||||
        android:id="@+id/download_title"/>
 | 
			
		||||
        android:orientation="horizontal">
 | 
			
		||||
 | 
			
		||||
    <FrameLayout
 | 
			
		||||
        android:layout_width="match_parent"
 | 
			
		||||
        android:layout_height="wrap_content">
 | 
			
		||||
 | 
			
		||||
        <ProgressBar
 | 
			
		||||
            style="?android:attr/progressBarStyleHorizontal"
 | 
			
		||||
            android:layout_width="match_parent"
 | 
			
		||||
        <TextView
 | 
			
		||||
            android:layout_width="wrap_content"
 | 
			
		||||
            android:layout_height="wrap_content"
 | 
			
		||||
            android:id="@+id/download_progress" />
 | 
			
		||||
            android:id="@+id/download_title"/>
 | 
			
		||||
 | 
			
		||||
        <View
 | 
			
		||||
            android:layout_width="0dp"
 | 
			
		||||
            android:layout_height="0dp"
 | 
			
		||||
            android:layout_weight="1" />
 | 
			
		||||
 | 
			
		||||
        <TextView
 | 
			
		||||
            android:layout_width="wrap_content"
 | 
			
		||||
            android:layout_height="wrap_content"
 | 
			
		||||
            android:id="@+id/download_progress_text"
 | 
			
		||||
            android:layout_gravity="center_horizontal" />
 | 
			
		||||
            android:layout_gravity="left" />
 | 
			
		||||
 | 
			
		||||
    </FrameLayout>
 | 
			
		||||
    </LinearLayout>
 | 
			
		||||
 | 
			
		||||
    <ProgressBar
 | 
			
		||||
        style="?android:attr/progressBarStyleHorizontal"
 | 
			
		||||
        android:layout_width="match_parent"
 | 
			
		||||
        android:layout_height="wrap_content"
 | 
			
		||||
        android:id="@+id/download_progress" />
 | 
			
		||||
 | 
			
		||||
</LinearLayout>
 | 
			
		||||
		Reference in New Issue
	
	Block a user