Skip to content
This repository was archived by the owner on Oct 27, 2024. It is now read-only.

Commit 5cb3b1c

Browse files
committed
Sorting behaves like a passive view
1 parent 6245d92 commit 5cb3b1c

File tree

9 files changed

+108
-61
lines changed

9 files changed

+108
-61
lines changed

app/build.gradle

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ android {
77

88
defaultConfig {
99
applicationId "com.esoxjem.movieguide"
10-
minSdkVersion 15
10+
minSdkVersion 16
1111
targetSdkVersion 24
1212
versionCode 1
1313
versionName "1.0"
@@ -48,16 +48,12 @@ dependencies {
4848
compile 'com.github.bumptech.glide:glide:3.6.1'
4949
compile 'com.github.bumptech.glide:okhttp-integration:1.3.1'
5050

51-
// Dagger
52-
compile 'com.google.dagger:dagger:2.5'
53-
apt 'com.google.dagger:dagger-compiler:2.5'
54-
5551
// Other
5652
compile 'net.jcip:jcip-annotations:1.0'
5753
compile 'com.squareup.picasso:picasso:2.5.2'
5854
compile 'com.squareup.moshi:moshi:1.1.0'
5955

60-
// dependency injection
56+
// Dependency Injection
6157
apt 'com.google.dagger:dagger-compiler:2.5'
6258
compile 'com.google.dagger:dagger:2.5'
6359
provided 'javax.annotation:jsr250-api:1.0'

app/src/main/java/com/esoxjem/movieguide/details/MovieDetailsFragment.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -251,20 +251,11 @@ public void onClick(View view)
251251
switch (view.getId())
252252
{
253253
case R.id.video_thumb:
254-
String videoUrl = (String) view.getTag();
255-
Intent playVideoIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(videoUrl));
256-
startActivity(playVideoIntent);
254+
onThumbnailClick(view);
257255
break;
258256

259257
case R.id.review_content:
260-
TextView review = (TextView) view;
261-
if (review.getMaxLines() == 5)
262-
{
263-
review.setMaxLines(500);
264-
} else
265-
{
266-
review.setMaxLines(5);
267-
}
258+
onReviewClick((TextView) view);
268259
break;
269260

270261
case R.id.favorite:
@@ -276,6 +267,24 @@ public void onClick(View view)
276267
}
277268
}
278269

270+
private void onReviewClick(TextView view)
271+
{
272+
if (view.getMaxLines() == 5)
273+
{
274+
view.setMaxLines(500);
275+
} else
276+
{
277+
view.setMaxLines(5);
278+
}
279+
}
280+
281+
private void onThumbnailClick(View view)
282+
{
283+
String videoUrl = (String) view.getTag();
284+
Intent playVideoIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(videoUrl));
285+
startActivity(playVideoIntent);
286+
}
287+
279288
private void onFavoriteClick()
280289
{
281290
movieDetailsPresenter.onFavoriteClick(movie);

app/src/main/java/com/esoxjem/movieguide/details/MovieDetailsPresenter.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import com.esoxjem.movieguide.favorites.IFavoritesInteractor;
77
import com.esoxjem.movieguide.util.RxUtils;
88

9-
import java.lang.ref.WeakReference;
109
import java.util.List;
1110

1211
import rx.Subscriber;
@@ -19,7 +18,7 @@
1918
*/
2019
public class MovieDetailsPresenter implements IMovieDetailsPresenter
2120
{
22-
private WeakReference<IMovieDetailsView> movieDetailsView;
21+
private IMovieDetailsView view;
2322
private IMovieDetailsInteractor movieDetailsInteractor;
2423
private IFavoritesInteractor favoritesInteractor;
2524
private Subscription trailersSubscription;
@@ -34,12 +33,13 @@ public MovieDetailsPresenter(IMovieDetailsInteractor movieDetailsInteractor, IFa
3433
@Override
3534
public void setView(IMovieDetailsView view)
3635
{
37-
movieDetailsView = new WeakReference<>(view);
36+
this.view = view;
3837
}
3938

4039
@Override
4140
public void destroy()
4241
{
42+
view = null;
4343
RxUtils.unsubscribe(trailersSubscription, reviewSubscription);
4444
}
4545

@@ -48,13 +48,13 @@ public void showDetails(Movie movie)
4848
{
4949
if (isViewAttached())
5050
{
51-
movieDetailsView.get().showDetails(movie);
51+
view.showDetails(movie);
5252
}
5353
}
5454

5555
private boolean isViewAttached()
5656
{
57-
return movieDetailsView.get() != null;
57+
return view != null;
5858
}
5959

6060
@Override
@@ -81,7 +81,7 @@ public void onNext(List<Video> videos)
8181
{
8282
if (isViewAttached())
8383
{
84-
movieDetailsView.get().showTrailers(videos);
84+
view.showTrailers(videos);
8585
}
8686
}
8787
});
@@ -111,7 +111,7 @@ public void onNext(List<Review> reviews)
111111
{
112112
if (isViewAttached())
113113
{
114-
movieDetailsView.get().showReviews(reviews);
114+
view.showReviews(reviews);
115115
}
116116
}
117117
});
@@ -125,10 +125,10 @@ public void showFavoriteButton(Movie movie)
125125
{
126126
if (isFavorite)
127127
{
128-
movieDetailsView.get().showFavorited();
128+
view.showFavorited();
129129
} else
130130
{
131-
movieDetailsView.get().showUnFavorited();
131+
view.showUnFavorited();
132132
}
133133
}
134134
}
@@ -142,11 +142,11 @@ public void onFavoriteClick(Movie movie)
142142
if (isFavorite)
143143
{
144144
favoritesInteractor.unFavorite(movie.getId());
145-
movieDetailsView.get().showUnFavorited();
145+
view.showUnFavorited();
146146
} else
147147
{
148148
favoritesInteractor.setFavorite(movie);
149-
movieDetailsView.get().showFavorited();
149+
view.showFavorited();
150150
}
151151
}
152152
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package com.esoxjem.movieguide.listing;
22

3-
import rx.Subscription;
4-
53
/**
64
* @author arun
75
*/
86
public interface IMoviesListingPresenter
97
{
10-
Subscription displayMovies();
8+
void displayMovies();
9+
1110
void setView(IMoviesListingView view);
11+
12+
void destroy();
1213
}

app/src/main/java/com/esoxjem/movieguide/listing/MoviesListingFragment.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,13 @@
2424

2525
import javax.inject.Inject;
2626

27-
import rx.Subscription;
28-
2927
public class MoviesListingFragment extends Fragment implements IMoviesListingView
3028
{
3129
@Inject
3230
IMoviesListingPresenter moviesPresenter;
3331

3432
private RecyclerView.Adapter adapter;
3533
private List<Movie> movies = new ArrayList<>(20);
36-
private Subscription moviesSubscription;
3734
private Callback callback;
3835

3936
public MoviesListingFragment()
@@ -70,7 +67,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
7067
public void onViewCreated(View view, Bundle savedInstanceState)
7168
{
7269
super.onViewCreated(view, savedInstanceState);
73-
moviesSubscription = moviesPresenter.displayMovies();
70+
moviesPresenter.displayMovies();
7471
}
7572

7673
@Override
@@ -141,12 +138,8 @@ public void onMovieClicked(Movie movie)
141138
@Override
142139
public void onDestroyView()
143140
{
144-
if (moviesSubscription != null && !moviesSubscription.isUnsubscribed())
145-
{
146-
moviesSubscription.unsubscribe();
147-
}
148-
149141
super.onDestroyView();
142+
moviesPresenter.destroy();
150143
}
151144

152145
@Override

app/src/main/java/com/esoxjem/movieguide/listing/MoviesListingPresenter.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.esoxjem.movieguide.listing;
22

33
import com.esoxjem.movieguide.Movie;
4+
import com.esoxjem.movieguide.util.RxUtils;
45

56
import java.util.List;
67

@@ -17,6 +18,7 @@ public class MoviesListingPresenter implements IMoviesListingPresenter
1718
{
1819
private IMoviesListingView view;
1920
private IMoviesListingInteractor moviesInteractor;
21+
private Subscription fetchSubscription;
2022

2123
public MoviesListingPresenter(IMoviesListingInteractor interactor)
2224
{
@@ -30,16 +32,26 @@ public void setView(IMoviesListingView view)
3032
}
3133

3234
@Override
33-
public Subscription displayMovies()
35+
public void destroy()
3436
{
35-
return moviesInteractor.fetchMovies().subscribeOn(Schedulers.io())
37+
view = null;
38+
RxUtils.unsubscribe(fetchSubscription);
39+
}
40+
41+
@Override
42+
public void displayMovies()
43+
{
44+
fetchSubscription = moviesInteractor.fetchMovies().subscribeOn(Schedulers.io())
3645
.observeOn(AndroidSchedulers.mainThread())
3746
.doOnSubscribe(new Action0()
3847
{
3948
@Override
4049
public void call()
4150
{
42-
view.loadingStarted();
51+
if (isViewAttached())
52+
{
53+
view.loadingStarted();
54+
}
4355
}
4456
})
4557
.subscribe(new Subscriber<List<Movie>>()
@@ -63,4 +75,9 @@ public void onNext(List<Movie> movies)
6375
}
6476
});
6577
}
78+
79+
private boolean isViewAttached()
80+
{
81+
return view != null;
82+
}
6683
}

app/src/main/java/com/esoxjem/movieguide/sorting/ISortingDialogPresenter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ public interface ISortingDialogPresenter
1414
void onFavoritesSelected();
1515

1616
void setView(ISortingDialogView view);
17+
18+
void destroy();
1719
}

app/src/main/java/com/esoxjem/movieguide/sorting/SortingDialogFragment.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,10 @@ public void dismissDialog()
114114
dismiss();
115115
}
116116

117+
@Override
118+
public void onDestroyView()
119+
{
120+
super.onDestroyView();
121+
sortingDialogPresenter.destroy();
122+
}
117123
}

app/src/main/java/com/esoxjem/movieguide/sorting/SortingDialogPresenter.java

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
public class SortingDialogPresenter implements ISortingDialogPresenter
77
{
8-
private ISortingDialogView sortingDialogView;
8+
private ISortingDialogView view;
99
private ISortingDialogInteractor sortingDialogInteractor;
1010

1111
public SortingDialogPresenter(ISortingDialogInteractor interactor)
@@ -16,44 +16,67 @@ public SortingDialogPresenter(ISortingDialogInteractor interactor)
1616
@Override
1717
public void setView(ISortingDialogView view)
1818
{
19-
sortingDialogView = view;
19+
this.view = view;
2020
}
2121

2222
@Override
23-
public void setLastSavedOption()
23+
public void destroy()
2424
{
25-
int selectedOption = sortingDialogInteractor.getSelectedSortingOption();
25+
view = null;
26+
}
2627

27-
if (selectedOption == SortType.MOST_POPULAR.getValue())
28-
{
29-
sortingDialogView.setPopularChecked();
30-
} else if (selectedOption == SortType.HIGHEST_RATED.getValue())
31-
{
32-
sortingDialogView.setHighestRatedChecked();
33-
} else
28+
@Override
29+
public void setLastSavedOption()
30+
{
31+
if (isViewAttached())
3432
{
35-
sortingDialogView.setFavoritesChecked();
33+
int selectedOption = sortingDialogInteractor.getSelectedSortingOption();
34+
35+
if (selectedOption == SortType.MOST_POPULAR.getValue())
36+
{
37+
view.setPopularChecked();
38+
} else if (selectedOption == SortType.HIGHEST_RATED.getValue())
39+
{
40+
view.setHighestRatedChecked();
41+
} else
42+
{
43+
view.setFavoritesChecked();
44+
}
3645
}
3746
}
3847

48+
private boolean isViewAttached()
49+
{
50+
return view != null;
51+
}
52+
3953
@Override
4054
public void onPopularMoviesSelected()
4155
{
42-
sortingDialogInteractor.setSortingOption(SortType.MOST_POPULAR);
43-
sortingDialogView.dismissDialog();
56+
if (isViewAttached())
57+
{
58+
sortingDialogInteractor.setSortingOption(SortType.MOST_POPULAR);
59+
view.dismissDialog();
60+
}
4461
}
4562

4663
@Override
4764
public void onHighestRatedMoviesSelected()
4865
{
49-
sortingDialogInteractor.setSortingOption(SortType.HIGHEST_RATED);
50-
sortingDialogView.dismissDialog();
66+
if (isViewAttached())
67+
{
68+
sortingDialogInteractor.setSortingOption(SortType.HIGHEST_RATED);
69+
view.dismissDialog();
70+
}
5171
}
5272

5373
@Override
5474
public void onFavoritesSelected()
5575
{
56-
sortingDialogInteractor.setSortingOption(SortType.FAVORITES);
57-
sortingDialogView.dismissDialog();
76+
if (isViewAttached())
77+
{
78+
sortingDialogInteractor.setSortingOption(SortType.FAVORITES);
79+
view.dismissDialog();
80+
}
5881
}
5982
}

0 commit comments

Comments
 (0)