Browse Source

Better flow and menu item selection before opening

Signed-off-by: alperozturk <alper_ozturk@proton.me>
alperozturk 11 months ago
parent
commit
1543685b5c

+ 7 - 0
app/src/main/java/com/nextcloud/utils/extensions/ContextExtensions.kt

@@ -16,8 +16,10 @@ import android.os.Build
 import android.os.Handler
 import android.os.Looper
 import android.widget.Toast
+import androidx.localbroadcastmanager.content.LocalBroadcastManager
 import com.google.common.io.Resources
 import com.owncloud.android.datamodel.ReceiverFlag
+import com.owncloud.android.ui.activity.DrawerActivity
 
 @SuppressLint("UnspecifiedRegisterReceiverFlag")
 fun Context.registerBroadcastReceiver(receiver: BroadcastReceiver?, filter: IntentFilter, flag: ReceiverFlag): Intent? {
@@ -53,3 +55,8 @@ fun Context.showToast(message: String) {
 }
 
 fun Context.showToast(messageId: Int) = showToast(getString(messageId))
+
+fun Context.sendOpenDrawerEvent() {
+    val intent = Intent(DrawerActivity.OPEN_DRAWER_MENU)
+    LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
+}

+ 37 - 5
app/src/main/java/com/owncloud/android/ui/activity/DrawerActivity.java

@@ -15,8 +15,10 @@ package com.owncloud.android.ui.activity;
 import android.accounts.AuthenticatorException;
 import android.accounts.OperationCanceledException;
 import android.app.Activity;
+import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
+import android.content.IntentFilter;
 import android.content.res.ColorStateList;
 import android.content.res.Configuration;
 import android.graphics.Bitmap;
@@ -122,6 +124,7 @@ import androidx.core.content.res.ResourcesCompat;
 import androidx.core.view.GravityCompat;
 import androidx.drawerlayout.widget.DrawerLayout;
 import androidx.fragment.app.Fragment;
+import androidx.localbroadcastmanager.content.LocalBroadcastManager;
 import hct.Hct;
 
 /**
@@ -138,6 +141,7 @@ public abstract class DrawerActivity extends ToolbarActivity
     private static final int MENU_ITEM_EXTERNAL_LINK = 111;
     private static final int MAX_LOGO_SIZE_PX = 1000;
     private static final int RELATIVE_THRESHOLD_WARNING = 80;
+    public static final String OPEN_DRAWER_MENU = "OPEN_DRAWER_MENU";
 
     /**
      * Reference to the drawer layout.
@@ -230,6 +234,14 @@ public abstract class DrawerActivity extends ToolbarActivity
     private void setupDrawerToggle() {
         mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {
 
+            @Override
+            public void onDrawerSlide(View drawerView, float slideOffset) {
+                super.onDrawerSlide(drawerView, slideOffset);
+                if (slideOffset > 0) {
+                    setDrawerMenuItemChecked();
+                }
+            }
+
             /** Called when a drawer has settled in a completely closed state. */
             public void onDrawerClosed(View view) {
                 super.onDrawerClosed(view);
@@ -249,7 +261,6 @@ public abstract class DrawerActivity extends ToolbarActivity
                 super.onDrawerOpened(drawerView);
                 mDrawerToggle.setDrawerIndicatorEnabled(true);
                 supportInvalidateOptionsMenu();
-                setDrawerMenuItemChecked();
             }
         };
 
@@ -866,6 +877,30 @@ public abstract class DrawerActivity extends ToolbarActivity
         }
     }
 
+    @Override
+    protected void onDestroy() {
+        super.onDestroy();
+        try {
+            LocalBroadcastManager.getInstance(this).unregisterReceiver(openDrawerReceiver);
+        } catch (IllegalArgumentException e) {
+            Log_OC.d(TAG, "drawerMenuUpdateReceiver not registered");
+        }
+    }
+
+    private void registerOpenDrawerReceiver() {
+        IntentFilter filter = new IntentFilter(OPEN_DRAWER_MENU);
+        LocalBroadcastManager.getInstance(this).registerReceiver(openDrawerReceiver, filter);
+    }
+
+    private final BroadcastReceiver openDrawerReceiver = new BroadcastReceiver() {
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            if (!isDrawerOpen()) {
+                openDrawer();
+            }
+        }
+    };
+
     /**
      * checks/highlights the provided menu item if the drawer has been initialized and the menu item exists.
      *
@@ -882,10 +917,6 @@ public abstract class DrawerActivity extends ToolbarActivity
             return;
         }
 
-        if (menuItem.isChecked()) {
-            return;
-        }
-
         viewThemeUtils.platform.colorNavigationView(mNavigationView);
         menuItem.setChecked(true);
     }
@@ -1011,6 +1042,7 @@ public abstract class DrawerActivity extends ToolbarActivity
 
         externalLinksProvider = new ExternalLinksProvider(getContentResolver());
         arbitraryDataProvider = new ArbitraryDataProviderImpl(this);
+        registerOpenDrawerReceiver();
     }
 
     @Override

+ 23 - 13
app/src/main/java/com/owncloud/android/ui/activity/SettingsActivity.java

@@ -21,6 +21,7 @@ import android.content.SharedPreferences;
 import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager.NameNotFoundException;
 import android.content.res.Configuration;
+import android.graphics.drawable.Drawable;
 import android.net.Uri;
 import android.os.Bundle;
 import android.preference.ListPreference;
@@ -49,6 +50,7 @@ import com.nextcloud.client.network.ConnectivityService;
 import com.nextcloud.client.preferences.AppPreferences;
 import com.nextcloud.client.preferences.AppPreferencesImpl;
 import com.nextcloud.client.preferences.DarkMode;
+import com.nextcloud.utils.extensions.ContextExtensionsKt;
 import com.owncloud.android.BuildConfig;
 import com.owncloud.android.MainApp;
 import com.owncloud.android.R;
@@ -876,20 +878,28 @@ public class SettingsActivity extends PreferenceActivity
 
     private void setupActionBar() {
         ActionBar actionBar = getDelegate().getSupportActionBar();
+        if (actionBar == null) return;
+
+        viewThemeUtils.platform.themeStatusBar(this);
+        actionBar.setDisplayHomeAsUpEnabled(true);
+        actionBar.setDisplayShowTitleEnabled(true);
+
+        if (getResources() == null) return;
+        Drawable menuIcon = ResourcesCompat.getDrawable(getResources(),
+                                                        R.drawable.ic_menu,
+                                                        null);
+
+        if (menuIcon == null) return;
+        viewThemeUtils.androidx.themeActionBar(this,
+                                               actionBar,
+                                               getString(R.string.actionbar_settings),
+                                               menuIcon);
+    }
 
-        if (actionBar != null) {
-            viewThemeUtils.platform.themeStatusBar(this);
-            actionBar.setDisplayHomeAsUpEnabled(true);
-            actionBar.setDisplayShowTitleEnabled(true);
-            if (this.getResources() != null) {
-                viewThemeUtils.androidx.themeActionBar(this,
-                                                       actionBar,
-                                                       getString(R.string.actionbar_settings),
-                                                       ResourcesCompat.getDrawable(this.getResources(),
-                                                                                   R.drawable.ic_arrow_back,
-                                                                                   null));
-            }
-        }
+    @Override
+    public void onBackPressed() {
+        ContextExtensionsKt.sendOpenDrawerEvent(this);
+        // super.onBackPressed();
     }
 
     private void launchDavDroidLogin() {