|
2 | 2 |
|
3 | 3 | import android.graphics.Typeface;
|
4 | 4 | import android.support.annotation.NonNull;
|
| 5 | + |
5 | 6 | import com.jorge.boats.xkcd.data.P;
|
6 | 7 | import com.jorge.boats.xkcd.di.PerActivity;
|
7 | 8 | import com.jorge.boats.xkcd.domain.entity.DomainStripe;
|
|
12 | 13 | import com.jorge.boats.xkcd.util.RandomUtil;
|
13 | 14 | import com.jorge.boats.xkcd.view.stripe.StripeContentView;
|
14 | 15 | import com.jorge.boats.xkcd.view.widget.RetryLinearLayout;
|
| 16 | + |
15 | 17 | import javax.inject.Inject;
|
16 | 18 | import javax.inject.Named;
|
| 19 | + |
17 | 20 | import retrofit2.adapter.rxjava.HttpException;
|
18 | 21 | import rx.Subscriber;
|
19 | 22 |
|
20 |
| -@PerActivity public class StripePresenter implements Presenter<StripeContentView> { |
21 |
| - |
22 |
| - private final UseCase<Typeface> mTypefaceUseCase; |
23 |
| - private final UseCase<DomainStripe> mStripeUseCase; |
24 |
| - |
25 |
| - @Inject PresentationEntityMapper mEntityMapper; |
26 |
| - @Inject RetryLinearLayout mRetry; |
27 |
| - |
28 |
| - private StripeContentView mView; |
29 |
| - |
30 |
| - @Inject |
31 |
| - public StripePresenter(final @NonNull @Named("typeface") UseCase<Typeface> typefaceUseCase, |
32 |
| - final @NonNull @Named("stripe") UseCase<DomainStripe> stripeUseCase) { |
33 |
| - mTypefaceUseCase = typefaceUseCase; |
34 |
| - mStripeUseCase = stripeUseCase; |
35 |
| - } |
| 23 | +@PerActivity |
| 24 | +public class StripePresenter implements Presenter<StripeContentView> { |
36 | 25 |
|
37 |
| - public void setView(@NonNull StripeContentView view) { |
38 |
| - mView = view; |
39 |
| - } |
| 26 | + private final UseCase<Typeface> mTypefaceUseCase; |
| 27 | + private final UseCase<DomainStripe> mStripeUseCase; |
40 | 28 |
|
41 |
| - public void initialize() { |
42 |
| - requestTitleTypeface(); |
43 |
| - } |
| 29 | + @Inject |
| 30 | + PresentationEntityMapper mEntityMapper; |
| 31 | + @Inject |
| 32 | + RetryLinearLayout mRetry; |
44 | 33 |
|
45 |
| - private void requestTitleTypeface() { |
46 |
| - mTypefaceUseCase.execute(new TitleTypefaceSubscriber()); |
47 |
| - } |
| 34 | + private StripeContentView mView; |
48 | 35 |
|
49 |
| - private void requestStripe() { |
50 |
| - loadStripe(); |
51 |
| - } |
52 |
| - |
53 |
| - private void loadStripe() { |
54 |
| - mStripeUseCase.execute(new StripeSubscriber()); |
55 |
| - } |
56 |
| - |
57 |
| - public void switchToStripeNum(final long stripeNum) { |
58 |
| - updateUseCase(stripeNum); |
59 |
| - requestStripe(); |
60 |
| - } |
61 |
| - |
62 |
| - private void updateUseCase(final long stripeNum) { |
63 |
| - ((GetStripeUseCase) mStripeUseCase).setRequestedStripeNum(stripeNum); |
64 |
| - } |
| 36 | + @Inject |
| 37 | + public StripePresenter(final @NonNull @Named("typeface") UseCase<Typeface> typefaceUseCase, |
| 38 | + final @NonNull @Named("stripe") UseCase<DomainStripe> stripeUseCase) { |
| 39 | + mTypefaceUseCase = typefaceUseCase; |
| 40 | + mStripeUseCase = stripeUseCase; |
| 41 | + } |
65 | 42 |
|
66 |
| - @Override public void resume() { |
67 |
| - } |
| 43 | + public void setView(@NonNull StripeContentView view) { |
| 44 | + mView = view; |
| 45 | + } |
68 | 46 |
|
69 |
| - @Override public void pause() { |
| 47 | + public void initialize() { |
| 48 | + requestTitleTypeface(); |
| 49 | + } |
70 | 50 |
|
71 |
| - } |
| 51 | + private void requestTitleTypeface() { |
| 52 | + mTypefaceUseCase.execute(new TitleTypefaceSubscriber()); |
| 53 | + } |
72 | 54 |
|
73 |
| - @Override public void destroy() { |
74 |
| - mTypefaceUseCase.destroy(); |
75 |
| - mStripeUseCase.destroy(); |
76 |
| - } |
| 55 | + private void requestStripe() { |
| 56 | + loadStripe(); |
| 57 | + } |
77 | 58 |
|
78 |
| - @Override public void requestRetry() { |
79 |
| - requestStripe(); |
80 |
| - } |
| 59 | + private void loadStripe() { |
| 60 | + mStripeUseCase.execute(new StripeSubscriber()); |
| 61 | + } |
81 | 62 |
|
82 |
| - @Override public boolean isRetryViewShown() { |
83 |
| - return mRetry.isShown(); |
84 |
| - } |
| 63 | + public void switchToStripeNum(final long stripeNum) { |
| 64 | + updateUseCase(stripeNum); |
| 65 | + requestStripe(); |
| 66 | + } |
85 | 67 |
|
86 |
| - public void actionNext() { |
87 |
| - if (isRetryViewShown()) return; |
| 68 | + private void updateUseCase(final long stripeNum) { |
| 69 | + ((GetStripeUseCase) mStripeUseCase).setRequestedStripeNum(stripeNum); |
| 70 | + } |
88 | 71 |
|
89 |
| - switchToStripeNum(mView.getStripeNum() + 1); |
90 |
| - } |
| 72 | + @Override |
| 73 | + public void resume() { |
| 74 | + } |
91 | 75 |
|
92 |
| - public void actionRandom() { |
93 |
| - if (isRetryViewShown()) return; |
94 |
| - switchToStripeNum(RandomUtil.nextLong(1, (int) (mView.getStripeNum() - 1))); |
95 |
| - } |
| 76 | + @Override |
| 77 | + public void pause() { |
96 | 78 |
|
97 |
| - public void actionShare() { |
98 |
| - mView.share(); |
99 |
| - } |
| 79 | + } |
100 | 80 |
|
101 |
| - public void actionPrevious() { |
102 |
| - if (isRetryViewShown()) return; |
103 |
| - long targetContainer = mView.getStripeNum(); |
| 81 | + @Override |
| 82 | + public void destroy() { |
| 83 | + mTypefaceUseCase.destroy(); |
| 84 | + mStripeUseCase.destroy(); |
| 85 | + } |
104 | 86 |
|
105 |
| - if (targetContainer == DomainStripe.STRIPE_NUM_FIRST) { |
106 |
| - return; |
| 87 | + @Override |
| 88 | + public void requestRetry() { |
| 89 | + requestStripe(); |
107 | 90 | }
|
108 | 91 |
|
109 |
| - switchToStripeNum(targetContainer - 1); |
110 |
| - } |
| 92 | + @Override |
| 93 | + public boolean isRetryViewShown() { |
| 94 | + return mRetry.isShown(); |
| 95 | + } |
111 | 96 |
|
112 |
| - private final class TitleTypefaceSubscriber extends Subscriber<Typeface> { |
| 97 | + public void actionNext() { |
| 98 | + if (isRetryViewShown()) return; |
113 | 99 |
|
114 |
| - @Override public void onCompleted() { |
115 |
| - //Do nothing |
| 100 | + switchToStripeNum(mView.getStripeNum() + 1); |
116 | 101 | }
|
117 | 102 |
|
118 |
| - @Override public void onError(final @NonNull Throwable e) { |
119 |
| - ApplicationLogger.e(e, e.getClass().getName()); |
| 103 | + public void actionRandom() { |
| 104 | + if (isRetryViewShown()) return; |
| 105 | + switchToStripeNum(RandomUtil.nextLong(1, P.maxShownStripeNum.get())); |
120 | 106 | }
|
121 | 107 |
|
122 |
| - @Override public void onNext(final @NonNull Typeface typeface) { |
123 |
| - mView.setTitleTypeface(typeface); |
124 |
| - mRetry.setTextTypeface(typeface); |
| 108 | + public void actionShare() { |
| 109 | + mView.share(); |
125 | 110 | }
|
126 |
| - } |
127 | 111 |
|
128 |
| - private final class StripeSubscriber extends Subscriber<DomainStripe> { |
| 112 | + public void actionPrevious() { |
| 113 | + if (isRetryViewShown()) return; |
| 114 | + long targetContainer = mView.getStripeNum(); |
129 | 115 |
|
130 |
| - @Override public void onStart() { |
131 |
| - mView.showLoading(); |
132 |
| - mView.hideRetry(); |
133 |
| - } |
| 116 | + if (targetContainer == DomainStripe.STRIPE_NUM_FIRST) { |
| 117 | + return; |
| 118 | + } |
134 | 119 |
|
135 |
| - @Override public void onCompleted() { |
136 |
| - mView.showContent(); |
137 |
| - mView.hideLoading(); |
138 |
| - mView.hideRetry(); |
| 120 | + switchToStripeNum(targetContainer - 1); |
139 | 121 | }
|
140 | 122 |
|
141 |
| - @Override public void onError(final @NonNull Throwable e) { |
142 |
| - ApplicationLogger.e(e, e.getClass().getName()); |
143 |
| - mView.hideLoading(); |
144 |
| - //Upon attempt of loading a non-existing stripe, load the current stripe instead |
145 |
| - if ((e instanceof HttpException) && ((HttpException) e).code() == 404) { |
146 |
| - switchToStripeNum(DomainStripe.STRIPE_NUM_CURRENT); |
147 |
| - return; |
148 |
| - } |
149 |
| - mView.showRetry(e); |
150 |
| - mView.hideContent(); |
151 |
| - } |
| 123 | + private final class TitleTypefaceSubscriber extends Subscriber<Typeface> { |
152 | 124 |
|
153 |
| - @Override public void onNext(final @NonNull DomainStripe domainStripe) { |
154 |
| - final long num; |
| 125 | + @Override |
| 126 | + public void onCompleted() { |
| 127 | + //Do nothing |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public void onError(final @NonNull Throwable e) { |
| 132 | + ApplicationLogger.e(e, e.getClass().getName()); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public void onNext(final @NonNull Typeface typeface) { |
| 137 | + mView.setTitleTypeface(typeface); |
| 138 | + mRetry.setTextTypeface(typeface); |
| 139 | + } |
| 140 | + } |
155 | 141 |
|
156 |
| - mView.setStripeNum(num = domainStripe.getNum()); |
157 |
| - updateUseCase(num); |
158 |
| - P.lastShownStripeNum.put((int) num).apply(); |
159 |
| - mView.renderStripe(mEntityMapper.transform(domainStripe)); |
| 142 | + private final class StripeSubscriber extends Subscriber<DomainStripe> { |
| 143 | + |
| 144 | + @Override |
| 145 | + public void onStart() { |
| 146 | + mView.showLoading(); |
| 147 | + mView.hideRetry(); |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public void onCompleted() { |
| 152 | + mView.showContent(); |
| 153 | + mView.hideLoading(); |
| 154 | + mView.hideRetry(); |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public void onError(final @NonNull Throwable e) { |
| 159 | + ApplicationLogger.e(e, e.getClass().getName()); |
| 160 | + mView.hideLoading(); |
| 161 | + //Upon attempt of loading a non-existing stripe, load the current stripe instead |
| 162 | + if ((e instanceof HttpException) && ((HttpException) e).code() == 404) { |
| 163 | + switchToStripeNum(DomainStripe.STRIPE_NUM_CURRENT); |
| 164 | + return; |
| 165 | + } |
| 166 | + mView.showRetry(e); |
| 167 | + mView.hideContent(); |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public void onNext(final @NonNull DomainStripe domainStripe) { |
| 172 | + final long num; |
| 173 | + |
| 174 | + mView.setStripeNum(num = domainStripe.getNum()); |
| 175 | + updateUseCase(num); |
| 176 | + P.lastShownStripeNum.put((int) num).apply(); |
| 177 | + mView.renderStripe(mEntityMapper.transform(domainStripe)); |
| 178 | + } |
160 | 179 | }
|
161 |
| - } |
162 | 180 | }
|
0 commit comments