Browse Source

Add home search appbar

Signed-off-by: Joris Bodin <joris.bodin@infomaniak.com>
Joris Bodin 5 years ago
parent
commit
a0ff4e4cc1

+ 25 - 45
src/main/java/com/owncloud/android/datamodel/ThumbnailsCacheManager.java

@@ -45,6 +45,7 @@ import android.view.MenuItem;
 import android.view.WindowManager;
 import android.widget.ImageView;
 
+import com.google.android.material.button.MaterialButton;
 import com.nextcloud.client.network.ConnectivityService;
 import com.owncloud.android.MainApp;
 import com.owncloud.android.R;
@@ -1040,51 +1041,28 @@ public final class ThumbnailsCacheManager {
     }
 
     public static boolean cancelPotentialAvatarWork(Object file, Object callContext) {
-        if (callContext instanceof ImageView) {
-            return cancelPotentialAvatarWork(file, (ImageView) callContext);
-        } else if (callContext instanceof MenuItem) {
-            return cancelPotentialAvatarWork(file, (MenuItem)callContext);
-        }
-
-        return false;
-    }
-
-    public static boolean cancelPotentialAvatarWork(Object file, ImageView imageView) {
-        final AvatarGenerationTask avatarWorkerTask = getAvatarWorkerTask(imageView);
-
-        if (avatarWorkerTask != null) {
-            final Object usernameData = avatarWorkerTask.mUserId;
-            // If usernameData is not yet set or it differs from the new data
-            if (usernameData == null || !usernameData.equals(file)) {
-                // Cancel previous task
-                avatarWorkerTask.cancel(true);
-                Log_OC.v(TAG, "Cancelled generation of avatar for a reused imageView");
-            } else {
-                // The same work is already in progress
-                return false;
-            }
-        }
-        // No task associated with the ImageView, or an existing task was cancelled
-        return true;
-    }
-
-    public static boolean cancelPotentialAvatarWork(Object file, MenuItem menuItem) {
-        final AvatarGenerationTask avatarWorkerTask = getAvatarWorkerTask(menuItem);
-
-        if (avatarWorkerTask != null) {
-            final Object usernameData = avatarWorkerTask.mUserId;
-            // If usernameData is not yet set or it differs from the new data
-            if (usernameData == null || !usernameData.equals(file)) {
-                // Cancel previous task
-                avatarWorkerTask.cancel(true);
-                Log_OC.v(TAG, "Cancelled generation of avatar for a reused imageView");
-            } else {
-                // The same work is already in progress
-                return false;
+        if (callContext instanceof ImageView ||
+            callContext instanceof MenuItem ||
+            callContext instanceof MaterialButton) {
+
+            AvatarGenerationTask avatarWorkerTask = getAvatarWorkerTask(callContext);
+            if (avatarWorkerTask != null) {
+                final Object usernameData = avatarWorkerTask.mUserId;
+                // If usernameData is not yet set or it differs from the new data
+                if (usernameData == null || !usernameData.equals(file)) {
+                    // Cancel previous task
+                    avatarWorkerTask.cancel(true);
+                    Log_OC.v(TAG, "Cancelled generation of avatar for a reused imageView");
+                } else {
+                    // The same work is already in progress
+                    return false;
+                }
             }
+            // No task associated with the ImageView, or an existing task was cancelled
+            return true;
+        } else {
+            return false;
         }
-        // No task associated with the ImageView, or an existing task was cancelled
-        return true;
     }
 
     public static ThumbnailGenerationTask getBitmapWorkerTask(ImageView imageView) {
@@ -1155,9 +1133,11 @@ public final class ThumbnailsCacheManager {
 
     public static AvatarGenerationTask getAvatarWorkerTask(Object callContext) {
         if (callContext instanceof ImageView) {
-            return getAvatarWorkerTask(((ImageView)callContext).getDrawable());
+            return getAvatarWorkerTask(((ImageView) callContext).getDrawable());
         } else if (callContext instanceof MenuItem) {
-            return getAvatarWorkerTask(((MenuItem)callContext).getIcon());
+            return getAvatarWorkerTask(((MenuItem) callContext).getIcon());
+        } else if (callContext instanceof MaterialButton) {
+            return getAvatarWorkerTask(((MaterialButton) callContext).getIcon());
         }
 
         return null;

+ 21 - 10
src/main/java/com/owncloud/android/ui/activity/DrawerActivity.java

@@ -53,6 +53,7 @@ import android.widget.TextView;
 import com.bumptech.glide.Glide;
 import com.bumptech.glide.request.animation.GlideAnimation;
 import com.bumptech.glide.request.target.SimpleTarget;
+import com.google.android.material.button.MaterialButton;
 import com.google.android.material.navigation.NavigationView;
 import com.nextcloud.client.account.User;
 import com.nextcloud.client.di.Injectable;
@@ -515,8 +516,7 @@ public abstract class DrawerActivity extends ToolbarActivity
                 break;
 
             case R.id.drawer_menu_account_manage:
-                Intent manageAccountsIntent = new Intent(getApplicationContext(), ManageAccountsActivity.class);
-                startActivityForResult(manageAccountsIntent, ACTION_MANAGE_ACCOUNTS);
+                openManageAccounts();
                 break;
 
             default:
@@ -525,6 +525,11 @@ public abstract class DrawerActivity extends ToolbarActivity
         }
     }
 
+    public void openManageAccounts() {
+        Intent manageAccountsIntent = new Intent(getApplicationContext(), ManageAccountsActivity.class);
+        startActivityForResult(manageAccountsIntent, ACTION_MANAGE_ACCOUNTS);
+    }
+
     private void startPhotoSearch(MenuItem menuItem) {
         SearchEvent searchEvent = new SearchEvent("image/%", SearchRemoteOperation.SearchType.PHOTO_SEARCH);
 
@@ -1367,22 +1372,28 @@ public abstract class DrawerActivity extends ToolbarActivity
     @Override
     public void avatarGenerated(Drawable avatarDrawable, Object callContext) {
         if (callContext instanceof MenuItem) {
-            MenuItem mi = (MenuItem) callContext;
-            mi.setIcon(avatarDrawable);
+            MenuItem menuItem = (MenuItem) callContext;
+            menuItem.setIcon(avatarDrawable);
         } else if (callContext instanceof ImageView) {
-            ImageView iv = (ImageView) callContext;
-            iv.setImageDrawable(avatarDrawable);
+            ImageView imageView = (ImageView) callContext;
+            imageView.setImageDrawable(avatarDrawable);
+        } else if (callContext instanceof MaterialButton) {
+            MaterialButton materialButton = (MaterialButton) callContext;
+            materialButton.setIcon(avatarDrawable);
         }
     }
 
     @Override
     public boolean shouldCallGeneratedCallback(String tag, Object callContext) {
         if (callContext instanceof MenuItem) {
-            MenuItem mi = (MenuItem) callContext;
-            return String.valueOf(mi.getTitle()).equals(tag);
+            MenuItem menuItem = (MenuItem) callContext;
+            return String.valueOf(menuItem.getTitle()).equals(tag);
         } else if (callContext instanceof ImageView) {
-            ImageView iv = (ImageView) callContext;
-            return String.valueOf(iv.getTag()).equals(tag);
+            ImageView imageView = (ImageView) callContext;
+            return String.valueOf(imageView.getTag()).equals(tag);
+        } else if (callContext instanceof MaterialButton) {
+            MaterialButton materialButton = (MaterialButton) callContext;
+            return String.valueOf(materialButton.getTag()).equals(tag);
         }
         return false;
     }

+ 13 - 1
src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java

@@ -260,6 +260,14 @@ public class FileDisplayActivity extends FileActivity
         // setup toolbar
         setupToolbar();
 
+        mMenuButton.setOnClickListener(v -> {
+            openDrawer();
+        });
+
+        mSwitchAccountButton.setOnClickListener(v -> {
+            openManageAccounts();
+        });
+
         // setup drawer
         if (MainApp.isOnlyOnDevice()) {
             setupDrawer(R.id.nav_on_device);
@@ -697,7 +705,6 @@ public class FileDisplayActivity extends FileActivity
         OCFileListFragment fileListFragment = getListOfFilesFragment();
         if (fileListFragment != null) {
             fileListFragment.listDirectory(MainApp.isOnlyOnDevice(), fromSearch);
-            setupToolbar();
         }
     }
 
@@ -2582,6 +2589,11 @@ public class FileDisplayActivity extends FileActivity
             setAccountInDrawer(user);
             setupDrawer();
 
+            mSwitchAccountButton.setTag(user.getAccountName());
+            DisplayUtils.setAvatar(user, this, getResources()
+                                       .getDimension(R.dimen.nav_drawer_menu_avatar_radius), getResources(),
+                                   mSwitchAccountButton, this);
+
             final String lastDisplayedAccountName = mLastDisplayedAccount != null ? mLastDisplayedAccount.name : null;
             final boolean accountChanged = !user.getAccountName().equals(lastDisplayedAccountName);
             if (accountChanged) {

+ 38 - 12
src/main/java/com/owncloud/android/ui/activity/ToolbarActivity.java

@@ -32,6 +32,10 @@ import android.widget.ImageView;
 import android.widget.LinearLayout;
 import android.widget.TextView;
 
+import com.google.android.material.appbar.AppBarLayout;
+import com.google.android.material.button.MaterialButton;
+import com.google.android.material.card.MaterialCardView;
+import com.google.android.material.textview.MaterialTextView;
 import com.owncloud.android.R;
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.OCFile;
@@ -45,6 +49,12 @@ import androidx.appcompat.widget.Toolbar;
  * Base class providing toolbar registration functionality, see {@link #setupToolbar()}.
  */
 public abstract class ToolbarActivity extends BaseActivity {
+    private AppBarLayout mAppBar;
+    private RelativeLayout mDefaultToolbar;
+    private MaterialCardView mHomeToolbar;
+    protected MaterialButton mMenuButton;
+    private MaterialTextView mSearchText;
+    protected MaterialButton mSwitchAccountButton;
     private ImageView mPreviewImage;
     private FrameLayout mPreviewImageContainer;
     private LinearLayout mInfoBox;
@@ -56,8 +66,8 @@ public abstract class ToolbarActivity extends BaseActivity {
     }
 
     /**
-     * Toolbar setup that must be called in implementer's {@link #onCreate} after {@link #setContentView} if they
-     * want to use the toolbar.
+     * Toolbar setup that must be called in implementer's {@link #onCreate} after {@link #setContentView} if they want
+     * to use the toolbar.
      */
     protected void setupToolbar(boolean useBackgroundImage) {
         int primaryColor = ThemeUtils.primaryAppbarColor(this);
@@ -66,6 +76,17 @@ public abstract class ToolbarActivity extends BaseActivity {
         Toolbar toolbar = findViewById(R.id.toolbar);
         setSupportActionBar(toolbar);
 
+        mAppBar = findViewById(R.id.appbar);
+        mDefaultToolbar = findViewById(R.id.default_toolbar);
+        mHomeToolbar = findViewById(R.id.home_toolbar);
+        mMenuButton = findViewById(R.id.menu_button);
+        mSearchText = findViewById(R.id.search_text);
+        mSwitchAccountButton = findViewById(R.id.switch_account_button);
+
+        if (mAppBar != null) {
+            mAppBar.setElevation(getResources().getDimension(R.dimen.default_appbar_elevation));
+        }
+
         mInfoBox = findViewById(R.id.info_box);
         mInfoBoxMessage = findViewById(R.id.info_box_message);
 
@@ -95,17 +116,22 @@ public abstract class ToolbarActivity extends BaseActivity {
      * Updates title bar and home buttons (state and icon).
      */
     protected void updateActionBarTitleAndHomeButton(OCFile chosenFile) {
-        String title = ThemeUtils.getDefaultDisplayNameForRootFolder(this);    // default
-        boolean inRoot;
-
-        // choose the appropriate title
-        inRoot =  chosenFile == null ||
-                        (chosenFile.isFolder() && chosenFile.getParentId() == FileDataStorageManager.ROOT_PARENT_ID);
-        if (!inRoot) {
-            title = chosenFile.getFileName();
-        }
+        String title;
+        boolean isRoot = isRoot(chosenFile);
 
+        title = isRoot ? ThemeUtils.getDefaultDisplayNameForRootFolder(this) : chosenFile.getFileName();
         updateActionBarTitleAndHomeButtonByString(title);
+
+        if (isRoot) {
+            mAppBar.setElevation(0);
+            mDefaultToolbar.setVisibility(View.GONE);
+            mHomeToolbar.setVisibility(View.VISIBLE);
+            mSearchText.setText(String.format("Search in %s", title));
+        } else {
+            mAppBar.setElevation(getResources().getDimension(R.dimen.default_appbar_elevation));
+            mDefaultToolbar.setVisibility(View.VISIBLE);
+            mHomeToolbar.setVisibility(View.GONE);
+        }
     }
 
     /**
@@ -192,6 +218,6 @@ public abstract class ToolbarActivity extends BaseActivity {
      * get the toolbar's preview image view.
      */
     public ImageView getPreviewImageView() {
-            return mPreviewImage;
+        return mPreviewImage;
     }
 }

+ 9 - 0
src/main/res/drawable/ic_menu.xml

@@ -0,0 +1,9 @@
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+        android:width="24dp"
+        android:height="24dp"
+        android:viewportWidth="24.0"
+        android:viewportHeight="24.0">
+    <path
+        android:fillColor="#FF000000"
+        android:pathData="M3,18h18v-2L3,16v2zM3,13h18v-2L3,11v2zM3,6v2h18L21,6L3,6z"/>
+</vector>

+ 61 - 0
src/main/res/layout/toolbar_standard.xml

@@ -24,10 +24,12 @@
     android:id="@+id/appbar"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
+    android:background="@color/appbar"
     android:theme="@style/ThemeOverlay.MaterialComponents.Dark.ActionBar"
     tools:viewBindingIgnore="true">
 
     <RelativeLayout
+        android:id="@+id/default_toolbar"
         android:layout_width="match_parent"
         android:layout_height="wrap_content">
 
@@ -58,6 +60,65 @@
 
     </RelativeLayout>
 
+    <com.google.android.material.card.MaterialCardView
+        android:id="@+id/home_toolbar"
+        android:layout_width="match_parent"
+        android:layout_height="45dp"
+        android:layout_marginStart="16dp"
+        android:layout_marginTop="6dp"
+        android:layout_marginEnd="16dp"
+        android:visibility="gone"
+        app:strokeColor="#E0E0E0"
+        app:strokeWidth="1dp">
+
+        <androidx.constraintlayout.widget.ConstraintLayout
+            android:layout_width="match_parent"
+            android:layout_height="match_parent">
+
+            <com.google.android.material.button.MaterialButton
+                android:id="@+id/menu_button"
+                style="@style/Widget.AppTheme.Button.IconButton"
+                android:layout_width="38dp"
+                android:layout_height="38dp"
+                android:layout_marginStart="8dp"
+                android:contentDescription="@string/action_switch_grid_view"
+                app:cornerRadius="@dimen/button_corner_radius"
+                app:icon="@drawable/ic_menu"
+                app:iconTint="@color/fontAppbar"
+                app:layout_constraintBottom_toBottomOf="parent"
+                app:layout_constraintStart_toStartOf="parent"
+                app:layout_constraintTop_toTopOf="parent" />
+
+            <com.google.android.material.textview.MaterialTextView
+                android:id="@+id/search_text"
+                android:layout_width="0dp"
+                android:layout_height="wrap_content"
+                android:layout_marginStart="18dp"
+                android:layout_marginEnd="18dp"
+                android:gravity="start"
+                android:textColor="@color/fontSecondaryAppbar"
+                android:textSize="16sp"
+                app:layout_constraintBottom_toBottomOf="parent"
+                app:layout_constraintLeft_toRightOf="@id/menu_button"
+                app:layout_constraintRight_toLeftOf="@id/switch_account_button"
+                app:layout_constraintTop_toTopOf="parent" />
+
+            <com.google.android.material.button.MaterialButton
+                android:id="@+id/switch_account_button"
+                style="@style/Widget.AppTheme.Button.IconButton"
+                android:layout_width="38dp"
+                android:layout_height="38dp"
+                android:layout_marginEnd="8dp"
+                app:cornerRadius="@dimen/button_corner_radius"
+                app:iconSize="28dp"
+                app:layout_constraintBottom_toBottomOf="parent"
+                app:layout_constraintEnd_toEndOf="parent"
+                app:layout_constraintTop_toTopOf="parent" />
+
+        </androidx.constraintlayout.widget.ConstraintLayout>
+
+    </com.google.android.material.card.MaterialCardView>
+
     <include layout="@layout/info_box" />
 
 </com.google.android.material.appbar.AppBarLayout>

+ 1 - 0
src/main/res/values/dims.xml

@@ -145,4 +145,5 @@
     <dimen name="button_corner_radius">24dp</dimen>
     <integer name="media_grid_width">4</integer>
     <dimen name="copy_internal_link_padding">10dp</dimen>
+    <dimen name="default_appbar_elevation">4dp</dimen>
 </resources>