This repository has been archived by the owner on May 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
136 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 77 additions & 16 deletions
93
mobile/src/main/java/com/github/ayltai/newspaper/app/view/FeaturedPresenter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,105 @@ | ||
package com.github.ayltai.newspaper.app.view; | ||
|
||
import java.io.File; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import android.app.Activity; | ||
import android.arch.lifecycle.Lifecycle; | ||
import android.arch.lifecycle.LifecycleObserver; | ||
import android.arch.lifecycle.OnLifecycleEvent; | ||
import android.net.Uri; | ||
import android.support.annotation.NonNull; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.util.Log; | ||
|
||
import com.github.ayltai.newspaper.Constants; | ||
import com.github.ayltai.newspaper.app.data.model.FeaturedItem; | ||
import com.github.ayltai.newspaper.app.widget.FeaturedView; | ||
import com.github.ayltai.newspaper.media.FrescoImageLoader; | ||
import com.github.ayltai.newspaper.media.DaggerImageComponent; | ||
import com.github.ayltai.newspaper.media.ImageModule; | ||
import com.github.ayltai.newspaper.util.RxUtils; | ||
import com.github.ayltai.newspaper.util.TestUtils; | ||
import com.github.ayltai.newspaper.util.ViewUtils; | ||
import com.github.piasy.biv.loader.ImageLoader; | ||
|
||
import io.reactivex.Observable; | ||
import io.reactivex.disposables.Disposable; | ||
|
||
public class FeaturedPresenter extends ItemPresenter<FeaturedView> { | ||
@Override | ||
public void onViewAttached(@NonNull final FeaturedView view, final boolean isFirstTimeAttachment) { | ||
this.manageDisposable(Observable.interval(Constants.FEATURED_IMAGE_ROTATION, TimeUnit.SECONDS) | ||
public class FeaturedPresenter extends ItemPresenter<FeaturedView> implements LifecycleObserver { | ||
private Disposable disposable; | ||
|
||
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME) | ||
protected void onResume() { | ||
this.disposable = Observable.interval(Constants.FEATURED_IMAGE_ROTATION, TimeUnit.SECONDS) | ||
.compose(RxUtils.applyObservableBackgroundToMainSchedulers()) | ||
.subscribe(time -> { | ||
if (this.getModel() instanceof FeaturedItem) { | ||
if (this.getModel() instanceof FeaturedItem && this.getView() != null) { | ||
((FeaturedItem)this.getModel()).next(); | ||
|
||
if (!this.getModel().getImages().isEmpty()) FrescoImageLoader.loadImage(this.getModel().getImages().get(0).getUrl()) | ||
.compose(RxUtils.applyMaybeBackgroundToMainSchedulers()) | ||
.subscribe( | ||
bitmap -> { | ||
view.setImages(this.getModel().getImages()); | ||
view.setTitle(this.getModel().getTitle()); | ||
}, | ||
error -> { | ||
DaggerImageComponent.builder() | ||
.imageModule(new ImageModule(this.getView().getContext())) | ||
.build() | ||
.imageLoader() | ||
.loadImage(Uri.parse(this.getModel().getImages().get(0).getUrl()), new ImageLoader.Callback() { | ||
@Override | ||
public void onCacheHit(final File image) { | ||
} | ||
|
||
@Override | ||
public void onCacheMiss(final File image) { | ||
} | ||
|
||
@Override | ||
public void onStart() { | ||
} | ||
|
||
@Override | ||
public void onProgress(final int progress) { | ||
} | ||
|
||
@Override | ||
public void onFinish() { | ||
} | ||
|
||
@Override | ||
public void onSuccess(final File image) { | ||
if (FeaturedPresenter.this.getView() != null) { | ||
FeaturedPresenter.this.getView().setImages(FeaturedPresenter.this.getModel().getImages()); | ||
FeaturedPresenter.this.getView().setTitle(FeaturedPresenter.this.getModel().getTitle()); | ||
} | ||
} | ||
|
||
@Override | ||
public void onFail(final Exception error) { | ||
if (TestUtils.isLoggable()) Log.e(this.getClass().getSimpleName(), error.getMessage(), error); | ||
} | ||
); | ||
}); | ||
|
||
this.bindModel(this.getModel()); | ||
} | ||
})); | ||
}); | ||
} | ||
|
||
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) | ||
protected void onPause() { | ||
if (this.disposable != null && !this.disposable.isDisposed()) this.disposable.dispose(); | ||
} | ||
|
||
@Override | ||
public void onViewAttached(@NonNull final FeaturedView view, final boolean isFirstTimeAttachment) { | ||
super.onViewAttached(view, isFirstTimeAttachment); | ||
|
||
final Activity activity = ViewUtils.getActivity(view); | ||
if (activity instanceof AppCompatActivity) ((AppCompatActivity)activity).getLifecycle().addObserver(this); | ||
} | ||
|
||
@Override | ||
public void onViewDetached() { | ||
super.onViewDetached(); | ||
|
||
if (this.getView() != null) { | ||
final Activity activity = ViewUtils.getActivity(this.getView()); | ||
if (activity instanceof AppCompatActivity) ((AppCompatActivity)activity).getLifecycle().removeObserver(this); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.