77import android .view .MenuItem ;
88import android .widget .ImageView ;
99import android .widget .TextView ;
10- import android .widget .Toast ;
1110
12- import androidx .annotation .NonNull ;
13- import androidx .annotation .Nullable ;
1411import androidx .appcompat .app .ActionBarDrawerToggle ;
1512import androidx .appcompat .app .AppCompatActivity ;
1613import androidx .appcompat .widget .Toolbar ;
1714import androidx .core .view .GravityCompat ;
1815import androidx .drawerlayout .widget .DrawerLayout ;
16+ import androidx .fragment .app .FragmentTransaction ;
1917import androidx .lifecycle .ViewModelProvider ;
2018import androidx .navigation .NavController ;
21- import androidx .navigation .NavDestination ;
2219import androidx .navigation .fragment .NavHostFragment ;
2320import androidx .navigation .ui .AppBarConfiguration ;
2421import androidx .navigation .ui .NavigationUI ;
2522
2623import com .google .android .material .navigation .NavigationView ;
2724import com .squareup .picasso .Picasso ;
2825
29- import org .jetbrains .annotations .Contract ;
30-
3126import java .util .Objects ;
3227
28+ import cn .settile .fanboxviewer .Fragments .SettingsFragment ;
3329import cn .settile .fanboxviewer .ViewComponents .LogoutDialog ;
3430import cn .settile .fanboxviewer .ViewModels .MainViewModel ;
3531
@@ -43,22 +39,20 @@ public class MainActivity extends AppCompatActivity
4339 MainActivity ctx = null ;
4440
4541 NavController navController ;
42+ private NavHostFragment navHostFragment ;
4643
4744 @ Override
4845 protected void onCreate (Bundle savedInstanceState ) {
4946 super .onCreate (savedInstanceState );
5047 ctx = this ;
51-
5248 setContentView (R .layout .activity_main );
5349
54-
5550 prepareUIAndActions ();
5651
5752 setTitle (R .string .app_name );
5853 Toolbar toolbar = findViewById (R .id .toolbar );
5954 setSupportActionBar (toolbar );
6055
61-
6256 DrawerLayout drawer = findViewById (R .id .drawer_layout );
6357 NavigationView navigationView = findViewById (R .id .nav_view );
6458
@@ -74,7 +68,8 @@ protected void onCreate(Bundle savedInstanceState) {
7468 //navigationView.getMenu().getItem(1).setEnabled(true);
7569
7670 setResult (-1 );
77- NavHostFragment navHostFragment = (NavHostFragment ) getSupportFragmentManager ().findFragmentById (R .id .nav_host_fragment_main );
71+ navHostFragment = (NavHostFragment ) getSupportFragmentManager ().findFragmentById (R .id .nav_host_fragment_main );
72+ assert navHostFragment != null ;
7873 navController = navHostFragment .getNavController ();
7974 NavigationUI .setupWithNavController (navigationView , navController );
8075
@@ -84,24 +79,18 @@ protected void onCreate(Bundle savedInstanceState) {
8479 .build ();
8580
8681
87- navController .addOnDestinationChangedListener (new NavController .OnDestinationChangedListener () {
88- @ Override
89- public void onDestinationChanged (@ NonNull NavController controller ,
90- @ NonNull NavDestination destination , @ Nullable Bundle arguments ) {
91- //navHostFragment.getNavController().navigateUp();
92- Log .d (TAG , destination .getLabel ().toString ());
82+ navController .addOnDestinationChangedListener ((controller , destination , arguments ) -> {
83+ //navHostFragment.getNavController().navigateUp();
84+ Log .d (TAG , destination .getLabel ().toString ());
9385
94- }
9586 });
9687
9788 }
9889
9990
10091 void prepareUIAndActions () {
101-
10292 viewModel = new ViewModelProvider (this ).get (MainViewModel .class );
10393
104-
10594 viewModel .is_logged_in ().observe (this , (it ) -> {
10695 Log .i (TAG , it .toString ());
10796 NavigationView navV = (NavigationView ) ctx .findViewById (R .id .nav_view );
@@ -128,16 +117,15 @@ void prepareUIAndActions() {
128117 });
129118
130119 viewModel .getUser_icon_url ().observe (this , (it ) -> {
131- if (!Objects .equals (it , "" )) Picasso .get ()
120+ if (!Objects .equals (it , "" ))
121+ Picasso .get ()
132122 .load (it )
133123 .placeholder (R .drawable .ic_settings_system_daydream_black_24dp )
134124 .resize (180 , 180 )
135125 .into ((ImageView ) findViewById (R .id .userIcon ));
136126 });
137127 }
138128
139- ;
140-
141129
142130 @ Override
143131 public void onBackPressed () {
@@ -157,6 +145,7 @@ public boolean onCreateOptionsMenu(Menu menu) {
157145 return true ;
158146 }
159147
148+
160149 @ Override
161150 public boolean onOptionsItemSelected (MenuItem item ) {
162151 // Handle action bar item clicks here. The action bar will
@@ -166,6 +155,13 @@ public boolean onOptionsItemSelected(MenuItem item) {
166155
167156 //noinspection SimplifiableIfStatement
168157 if (id == R .id .action_settings ) {
158+ FragmentTransaction ft = getSupportFragmentManager ().beginTransaction ();
159+ SettingsFragment sf = new SettingsFragment ();
160+ ft .replace (R .id .mainTabFragment , sf );
161+ ft .commit ();
162+ if (getSupportActionBar () != null ){
163+ getSupportActionBar ().setTitle (R .string .action_settings );
164+ }
169165 return true ;
170166 } else if (id == R .id .action_refresh ) {
171167 return true ;
@@ -174,6 +170,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
174170 return super .onOptionsItemSelected (item );
175171 }
176172
173+
177174 @ Override
178175 public boolean onNavigationItemSelected (MenuItem item ) {
179176 // Handle navigation view item clicks here.
@@ -184,47 +181,58 @@ public boolean onNavigationItemSelected(MenuItem item) {
184181 //Toast.makeText(this, "HOME", Toast.LENGTH_SHORT).show();
185182 } else if (id == R .id .nav_supporting ) {
186183 //Toast.makeText(this, "Fan Cards", Toast.LENGTH_SHORT).show();
187-
188184 } else if (id == R .id .nav_search ) {
189- Toast .makeText (this , "Search" , Toast .LENGTH_SHORT ).show ();
185+ // Toast.makeText(this, "Search", Toast.LENGTH_SHORT).show();
190186 } else if (id == R .id .nav_settings ) {
191- //TODO (SettingsActivity)
192- //callSettings();
187+ FragmentTransaction ft = getSupportFragmentManager ().beginTransaction ();
188+ SettingsFragment sf = new SettingsFragment ();
189+ ft .replace (R .id .mainTabFragment , sf );
190+ ft .commit ();
191+ if (getSupportActionBar () != null ){
192+ getSupportActionBar ().setTitle (R .string .action_settings );
193+ }
193194 } else if (id == R .id .nav_logout ) {
194- //Toast.makeText(this, "Logout", Toast.LENGTH_SHORT).show();
195195 new LogoutDialog (this ).show ();
196196 } else if (id == R .id .nav_recommend ) {
197- Toast .makeText (this , "Recommended" , Toast .LENGTH_SHORT ).show ();
197+ // Toast.makeText(this, "Recommended", Toast.LENGTH_SHORT).show();
198198 }
199199
200200 drawer .closeDrawer (GravityCompat .START );
201201 return true ;
202202 }
203203
204+ private void closeDrawer (){
205+ DrawerLayout drawer = findViewById (R .id .drawer_layout );
206+ if (Objects .nonNull (drawer ))
207+ drawer .closeDrawer (GravityCompat .START );
208+ }
209+
204210 //UICalls
205211 public void toSupportingFragment (MenuItem v ) {
206212 if (navController .getCurrentDestination ().getId () != R .id .supportingFragment )
207213 navController .navigate (R .id .supportingFragment );
214+ closeDrawer ();
208215 }
209216
210217 public void toMainTabFragment (MenuItem v ) {
211218 if (navController .getCurrentDestination ().getId () != R .id .mainTabFragment )
212219 navController .navigate (R .id .mainTabFragment );
220+ closeDrawer ();
213221 }
214222
223+ public void toSettingsFragment (MenuItem v ) {
224+ if (navController .getCurrentDestination ().getId () != R .id .settingsFragment )
225+ navController .navigate (R .id .settingsFragment );
226+ closeDrawer ();
227+ }
215228
216- public void callLogout (MenuItem v ) {
229+ public void openLogoutDialog (MenuItem ignored ) {
217230 new LogoutDialog (this ).show ();
218231 }
219232
220- @ Contract ( "_->null" )
233+
221234 public void callLogin () {
222235 Intent intent = new Intent (this , LoginActivity .class );
223236 if (!viewModel .is_logged_in ().getValue ()) startActivityForResult (intent , -1 );
224237 }
225-
226- public void callSettings () {
227- Intent intent = new Intent (this , SettingsActivity .class );
228- startActivity (intent );
229- }
230238}
0 commit comments