Skip to content

Commit d540357

Browse files
enhancement(Drawer): update drawer to material theme (fixes #79) (#166)
Signed-off-by: Jonas <244199422+researchxxl@users.noreply.github.com> Co-authored-by: researchxxl <244199422+researchxxl@users.noreply.github.com>
1 parent 904bf3b commit d540357

File tree

11 files changed

+425
-435
lines changed

11 files changed

+425
-435
lines changed

app/src/main/java/com/nutomic/syncthingandroid/activities/MainActivity.java

Lines changed: 25 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,16 @@
1313
import android.os.Bundle;
1414
import android.os.Handler;
1515
import android.os.IBinder;
16-
import android.util.DisplayMetrics;
16+
import android.text.TextUtils;
1717
import android.util.Log;
18-
import android.util.TypedValue;
1918
import android.view.KeyEvent;
2019
import android.view.LayoutInflater;
2120
import android.view.MenuItem;
2221
import android.view.View;
23-
import android.view.ViewGroup;
2422
import android.widget.Button;
2523
import android.widget.ProgressBar;
2624
import android.widget.TextView;
25+
import android.widget.Toast;
2726

2827
import androidx.activity.OnBackPressedCallback;
2928
import androidx.appcompat.app.ActionBar;
@@ -66,8 +65,6 @@
6665

6766
import javax.inject.Inject;
6867

69-
import static java.lang.Math.min;
70-
7168
/**
7269
* Shows {@link FolderListFragment} and
7370
* {@link DeviceListFragment} in different tabs, and
@@ -80,8 +77,6 @@ public class MainActivity extends SyncthingActivity
8077

8178
private Boolean ENABLE_VERBOSE_LOG = false;
8279

83-
private static final String IS_SHOWING_RESTART_DIALOG = "RESTART_DIALOG_STATE";
84-
8580
private static final int FOLDER_FRAGMENT_ID = 0;
8681
private static final int DEVICE_FRAGMENT_ID = 1;
8782
private static final int STATUS_FRAGMENT_ID = 2;
@@ -121,7 +116,7 @@ public class MainActivity extends SyncthingActivity
121116
// Session flag to track if user chose "remind later" for important news
122117
private boolean mImportantNewsRemindLaterThisSession = false;
123118

124-
@Inject SharedPreferences mPreferences;
119+
@Inject public SharedPreferences mPreferences;
125120
private ConfigRouter mConfig;
126121
private final Handler mUIRefreshHandler = new Handler();
127122

@@ -258,17 +253,10 @@ public void onCreate(Bundle savedInstanceState) {
258253
mDrawerFragment = new DrawerFragment();
259254
}
260255

261-
if (savedInstanceState != null) {
262-
if (savedInstanceState.getBoolean(IS_SHOWING_RESTART_DIALOG)){
263-
showRestartDialog();
264-
}
265-
}
266-
267256
fm.beginTransaction().replace(R.id.drawer, mDrawerFragment).commitAllowingStateLoss();
268-
mDrawerToggle = new Toggle(this, mDrawerLayout);
257+
mDrawerToggle = new Toggle(this, mDrawerLayout, mDrawerFragment);
269258
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
270259
mDrawerLayout.addDrawerListener(mDrawerToggle);
271-
setOptimalDrawerWidth(findViewById(R.id.drawer));
272260

273261
/**
274262
* SyncthingService needs to be started from this activity as the user
@@ -442,7 +430,6 @@ protected void onSaveInstanceState(Bundle outState) {
442430
putFragment.accept(mDeviceListFragment);
443431
putFragment.accept(mStatusFragment);
444432

445-
outState.putBoolean(IS_SHOWING_RESTART_DIALOG, mRestartDialog != null && mRestartDialog.isShowing());
446433
Util.dismissDialogSafe(mRestartDialog, this);
447434
Util.dismissDialogSafe(mUsageReportingDialog, this);
448435
}
@@ -497,7 +484,12 @@ public void showRestartDialog(){
497484
mRestartDialog.show();
498485
}
499486

500-
public void showQrCodeDialog(String deviceId) {
487+
public void showQrCodeDialog() {
488+
String deviceId = mPreferences.getString(Constants.PREF_LOCAL_DEVICE_ID, "");
489+
if (TextUtils.isEmpty(deviceId)) {
490+
Toast.makeText(this, R.string.could_not_access_deviceid, Toast.LENGTH_SHORT).show();
491+
return;
492+
}
501493
RestApi restApi = getApi();
502494
List<Device> devices = mConfig.getDevices(restApi, true);
503495
String deviceName = "";
@@ -525,10 +517,20 @@ public boolean onOptionsItemSelected(MenuItem item) {
525517
/**
526518
* Handles drawer opened and closed events, toggling option menu state.
527519
*/
528-
private class Toggle extends ActionBarDrawerToggle {
529-
public Toggle(AppCompatActivity activity, DrawerLayout drawerLayout) {
520+
private static class Toggle extends ActionBarDrawerToggle {
521+
522+
private final DrawerFragment drawerFragment;
523+
524+
public Toggle(AppCompatActivity activity, DrawerLayout drawerLayout, DrawerFragment drawerFragment) {
530525
super(activity, drawerLayout, R.string.open_main_menu, R.string.close_main_menu);
531526
setDrawerIndicatorEnabled(false);
527+
this.drawerFragment = drawerFragment;
528+
}
529+
530+
@Override
531+
public void onDrawerOpened(View drawerView) {
532+
super.onDrawerOpened(drawerView);
533+
drawerFragment.drawerOpened();
532534
}
533535

534536
@Override
@@ -563,25 +565,6 @@ public boolean onKeyDown(int keyCode, KeyEvent e) {
563565
return super.onKeyDown(keyCode, e);
564566
}
565567

566-
/**
567-
* Calculating width based on
568-
* http://www.google.com/design/spec/patterns/navigation-drawer.html#navigation-drawer-specs.
569-
*/
570-
private void setOptimalDrawerWidth(View drawerContainer) {
571-
int actionBarSize = 0;
572-
TypedValue tv = new TypedValue();
573-
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
574-
actionBarSize = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
575-
}
576-
577-
ViewGroup.LayoutParams params = drawerContainer.getLayoutParams();
578-
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
579-
int minScreenWidth = min(displayMetrics.widthPixels, displayMetrics.heightPixels);
580-
581-
params.width = min(minScreenWidth - actionBarSize, 5 * actionBarSize);
582-
drawerContainer.requestLayout();
583-
}
584-
585568
/**
586569
* Displays dialog asking user to accept/deny usage reporting.
587570
*/
@@ -795,12 +778,13 @@ private void updateExitFabVisibility() {
795778
/**
796779
* Exits the application by stopping the service and finishing the activity.
797780
* This method is called directly from the FAB since it's only visible when safe to exit.
781+
* Also called from drawer's exit button
798782
*/
799-
private void doExit() {
783+
public void doExit() {
800784
if (isFinishing()) {
801785
return;
802786
}
803-
Log.i(TAG, "Exiting app on user request via FAB");
787+
Log.i(TAG, "Exiting app on user request");
804788
stopService(new Intent(this, SyncthingService.class));
805789
finishAndRemoveTask();
806790
}

app/src/main/java/com/nutomic/syncthingandroid/activities/SyncthingActivity.java

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@
44
import android.content.Context;
55
import android.content.Intent;
66
import android.content.ServiceConnection;
7-
import android.content.res.Configuration;
8-
import android.os.Build;
97
import android.os.Bundle;
108
import android.os.IBinder;
119

10+
import androidx.activity.EdgeToEdge;
1211
import androidx.appcompat.app.ActionBar;
1312
import androidx.appcompat.widget.Toolbar;
14-
import androidx.core.view.WindowCompat;
15-
import androidx.core.view.WindowInsetsCompat;
1613
import androidx.core.view.WindowInsetsControllerCompat;
1714

1815
import com.nutomic.syncthingandroid.R;
@@ -29,19 +26,6 @@ public abstract class SyncthingActivity extends ThemedAppCompatActivity implemen
2926

3027
private SyncthingService mSyncthingService;
3128

32-
@Override
33-
protected void onCreate(Bundle savedInstanceState) {
34-
super.onCreate(savedInstanceState);
35-
36-
// Opt-in to edge-to-edge
37-
WindowCompat.setDecorFitsSystemWindows(getWindow(), false);
38-
WindowInsetsControllerCompat insetsController = new WindowInsetsControllerCompat(getWindow(), getWindow().getDecorView());
39-
int currentNightMode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
40-
boolean isDarkMode = currentNightMode == Configuration.UI_MODE_NIGHT_YES;
41-
insetsController.setAppearanceLightStatusBars(!isDarkMode);
42-
insetsController.setAppearanceLightNavigationBars(!isDarkMode);
43-
}
44-
4529
/**
4630
* Look for a Toolbar in the layout and bind it as the activity's actionbar with reasonable
4731
* defaults.

app/src/main/java/com/nutomic/syncthingandroid/activities/ThemedAppCompatActivity.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
import android.content.SharedPreferences;
44
import android.os.Bundle;
5+
6+
import androidx.activity.EdgeToEdge;
57
import androidx.preference.PreferenceManager;
68
import androidx.appcompat.app.AppCompatActivity;
79
import androidx.appcompat.app.AppCompatDelegate;
8-
// import android.util.Log;
910

1011
import com.nutomic.syncthingandroid.service.Constants;
1112

@@ -14,14 +15,13 @@
1415
*/
1516
public abstract class ThemedAppCompatActivity extends AppCompatActivity {
1617

17-
// private static final String TAG = "ThemedAppCompatActivity";
18-
1918
@Override
2019
protected void onCreate(Bundle savedInstanceState) {
2120
// Load theme.
2221
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
2322
Integer prefAppTheme = Integer.parseInt(sharedPreferences.getString(Constants.PREF_APP_THEME, Constants.APP_THEME_FOLLOW_SYSTEM));
2423
AppCompatDelegate.setDefaultNightMode(prefAppTheme);
24+
EdgeToEdge.enable(this);
2525
super.onCreate(savedInstanceState);
2626
}
2727
}

0 commit comments

Comments
 (0)