1
1
package com .example .dell .moviestage1 ;
2
2
3
3
import android .content .Context ;
4
+ import android .content .Intent ;
5
+ import android .content .SharedPreferences ;
4
6
import android .net .Uri ;
7
+ import android .preference .PreferenceManager ;
5
8
import android .support .annotation .NonNull ;
6
9
import android .support .annotation .Nullable ;
7
10
import android .support .v4 .app .LoaderManager ;
8
11
import android .support .v4 .content .Loader ;
9
12
import android .support .v7 .app .AppCompatActivity ;
10
13
import android .os .Bundle ;
11
14
import android .support .v7 .widget .GridLayoutManager ;
12
- import android .support .v7 .widget .LinearLayoutManager ;
13
15
import android .support .v7 .widget .RecyclerView ;
16
+ import android .util .Log ;
14
17
import android .view .Menu ;
15
18
import android .view .MenuItem ;
16
19
import android .view .View ;
17
- import android .widget .AdapterView ;
18
- import android .widget .ArrayAdapter ;
19
20
import android .widget .ImageView ;
20
21
import android .widget .Spinner ;
21
- import android .widget .TextView ;
22
22
import android .support .v7 .widget .Toolbar ;
23
23
24
- import com .squareup .picasso .Picasso ;
25
24
26
25
import java .util .ArrayList ;
27
26
28
27
import butterknife .BindView ;
29
28
30
- public class MainActivity extends AppCompatActivity implements LoaderManager .LoaderCallbacks <ArrayList <Movie >> {
29
+ public class MainActivity extends AppCompatActivity implements LoaderManager .LoaderCallbacks <ArrayList <Movie >>, SharedPreferences . OnSharedPreferenceChangeListener {
31
30
32
- String api_key = "" ;
31
+ private static final String LOG_TAG = MainActivity . class . getName () ;
33
32
34
- String REQUEST_URL = "https://api.themoviedb.org/3/discover/movie?api_key=" +api_key +"&language=en-US&sort_by=popularity.desc&include_adult=false&include_video=false&page=1" ;
33
+ private static final String URL_SCHEME = "https" ;
34
+ private static final String BASE_URL = "api.tmdb.org" ;
35
+ public static final String PATH = "/3/movie/" ;
35
36
37
+ private String api_key = "" ;
36
38
37
- RecyclerView recyclerView ;
38
- MovieAdapter adapter ;
39
- Context context ;
40
- Toolbar toolbar ;
41
- Spinner spinner ;
39
+
40
+ private String sortBy ;
41
+
42
+ private RecyclerView recyclerView ;
43
+ private MovieAdapter adapter ;
44
+ private Context context ;
45
+ private Toolbar toolbar ;
42
46
43
47
private static final int SECTION_LOADER_ID = 1 ;
44
- private TextView mEmptyStateTextView ;
45
48
View loadingIndicator ;
46
- @ BindView (R .id .imageView ) ImageView poster ;
49
+ @ BindView (R .id .imageView )
50
+ ImageView poster ;
47
51
48
52
@ Override
49
53
protected void onCreate (Bundle savedInstanceState ) {
@@ -53,38 +57,59 @@ protected void onCreate(Bundle savedInstanceState) {
53
57
54
58
recyclerView = findViewById (R .id .recyclerView );
55
59
toolbar = findViewById (R .id .toolbarView );
60
+ loadingIndicator = findViewById (R .id .loading_indicator );
56
61
57
62
setSupportActionBar (toolbar );
58
63
59
-
60
-
61
64
LoaderManager loaderManager = LoaderManager .getInstance (this );
62
65
loaderManager .initLoader (SECTION_LOADER_ID , null , this );
66
+ }
63
67
68
+ private String setUpSharePreference () {
69
+ SharedPreferences sharedPreferences = PreferenceManager .getDefaultSharedPreferences (this );
70
+ sharedPreferences .registerOnSharedPreferenceChangeListener (this );
64
71
72
+ sortBy = sharedPreferences .getString (getString (R .string .settings_order_by_key ), getString (R .string .highest_rated_value ));
65
73
74
+ return sortBy ;
75
+ }
66
76
77
+ @ Override
78
+ protected void onDestroy () {
79
+ super .onDestroy ();
80
+ PreferenceManager .getDefaultSharedPreferences (this ).unregisterOnSharedPreferenceChangeListener (this );
67
81
}
68
82
69
83
@ NonNull
70
84
@ Override
71
85
public Loader <ArrayList <Movie >> onCreateLoader (int i , @ Nullable Bundle bundle ) {
72
86
73
- return new SectionLoader (context , REQUEST_URL );
87
+ setUpSharePreference ();
88
+ Uri .Builder builder = new Uri .Builder ();
89
+
90
+ builder .scheme (URL_SCHEME ).authority (BASE_URL ).path (PATH );
91
+ builder .appendPath (sortBy );
92
+ builder .appendQueryParameter ("api_key" , api_key );
93
+
94
+
95
+ Log .e (LOG_TAG , builder .toString ());
96
+
97
+ return new SectionLoader (context , builder .toString ());
74
98
}
75
99
76
100
@ Override
77
101
public void onLoadFinished (@ NonNull Loader <ArrayList <Movie >> loader , ArrayList <Movie > movieArrayList ) {
78
102
103
+ loadingIndicator = findViewById (R .id .loading_indicator );
104
+ loadingIndicator .setVisibility (View .GONE );
105
+
79
106
adapter = new MovieAdapter (context , movieArrayList );
80
107
recyclerView .setLayoutManager (new GridLayoutManager (context , 2 ));
81
108
recyclerView .setAdapter (adapter );
82
-
83
109
}
84
110
85
111
@ Override
86
112
public void onLoaderReset (@ NonNull Loader <ArrayList <Movie >> loader ) {
87
-
88
113
}
89
114
90
115
@ Override
@@ -95,25 +120,20 @@ public boolean onCreateOptionsMenu(Menu menu) {
95
120
96
121
@ Override
97
122
public boolean onOptionsItemSelected (MenuItem item ) {
98
- switch (item .getItemId ()){
99
- case R .id .popular :
100
- showPopular ();
101
- return true ;
102
- case R .id .rated :
103
- showRated ();
104
- return true ;
105
- default :
123
+ int id = item .getItemId ();
124
+ if (id == R .id .settings ) {
125
+ Intent intent = new Intent (context , SettingsActivity .class );
126
+ startActivity (intent );
127
+ return true ;
106
128
}
107
-
108
-
109
129
return super .onOptionsItemSelected (item );
110
130
}
111
131
112
- private void showRated () {
113
- }
114
-
115
- private void showPopular () {
116
-
117
-
132
+ @ Override
133
+ public void onSharedPreferenceChanged ( SharedPreferences sharedPreferences , String key ) {
134
+ if ( key . equals ( getString ( R . string . settings_order_by_key ))) {
135
+ setUpSharePreference ();
136
+ getSupportLoaderManager (). restartLoader ( SECTION_LOADER_ID , null , this );
137
+ }
118
138
}
119
139
}
0 commit comments