Эх сурвалжийг харах

Add first attemp at resolving #2953

Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
Nick Antoniou 6 жил өмнө
parent
commit
93b9b96d6f

+ 10 - 37
src/main/java/com/owncloud/android/ui/activity/ManageAccountsActivity.java

@@ -35,9 +35,6 @@ import android.os.Bundle;
 import android.os.Handler;
 import android.os.IBinder;
 import android.view.MenuItem;
-import android.view.View;
-import android.widget.AdapterView;
-import android.widget.ListView;
 
 import com.evernote.android.job.JobRequest;
 import com.evernote.android.job.util.support.PersistableBundleCompat;
@@ -50,7 +47,6 @@ import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.files.services.FileDownloader;
 import com.owncloud.android.files.services.FileUploader;
 import com.owncloud.android.jobs.AccountRemovalJob;
-import com.owncloud.android.lib.common.OwnCloudAccount;
 import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.services.OperationsService;
 import com.owncloud.android.ui.adapter.AccountListAdapter;
@@ -73,6 +69,8 @@ import javax.inject.Inject;
 
 import androidx.core.content.ContextCompat;
 import androidx.core.graphics.drawable.DrawableCompat;
+import androidx.recyclerview.widget.LinearLayoutManager;
+import androidx.recyclerview.widget.RecyclerView;
 
 /**
  * An Activity that allows the user to manage accounts.
@@ -85,14 +83,11 @@ public class ManageAccountsActivity extends FileActivity
     public static final String KEY_CURRENT_ACCOUNT_CHANGED = "CURRENT_ACCOUNT_CHANGED";
     public static final String PENDING_FOR_REMOVAL = UserAccountManager.PENDING_FOR_REMOVAL;
 
-    private static final String KEY_DISPLAY_NAME = "DISPLAY_NAME";
-
-    private static final int KEY_USER_INFO_REQUEST_CODE = 13;
     private static final int KEY_DELETE_CODE = 101;
     private static final int SINGLE_ACCOUNT = 1;
     private static final int MIN_MULTI_ACCOUNT_SIZE = 2;
 
-    private ListView mListView;
+    private RecyclerView mRecyclerView;
     private final Handler mHandler = new Handler();
     private String mAccountName;
     private AccountListAdapter mAccountListAdapter;
@@ -116,7 +111,7 @@ public class ManageAccountsActivity extends FileActivity
 
         setContentView(R.layout.accounts_layout);
 
-        mListView = findViewById(R.id.account_list);
+        mRecyclerView = findViewById(R.id.account_list);
 
         setupToolbar();
         updateActionBarTitleAndHomeButtonByString(getResources().getString(R.string.prefs_manage_accounts));
@@ -137,30 +132,8 @@ public class ManageAccountsActivity extends FileActivity
 
         mAccountListAdapter = new AccountListAdapter(this, getUserAccountManager(), getAccountListItems(), mTintedCheck);
 
-        mListView.setAdapter(mAccountListAdapter);
-
-        final Intent intent = new Intent(this, UserInfoActivity.class);
-
-        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
-            @Override
-            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
-                AccountListItem item = mAccountListAdapter.getItem(position);
-
-                if (item != null && item.isEnabled()) {
-                    Account account = item.getAccount();
-                    intent.putExtra(UserInfoActivity.KEY_ACCOUNT, Parcels.wrap(account));
-                    try {
-                        OwnCloudAccount oca = new OwnCloudAccount(account, MainApp.getAppContext());
-                        intent.putExtra(KEY_DISPLAY_NAME, oca.getDisplayName());
-                    } catch (com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException e) {
-                        Log_OC.d(TAG, "Failed to find NC account");
-                    }
-
-                    startActivityForResult(intent, KEY_USER_INFO_REQUEST_CODE);
-                }
-            }
-        });
-
+        mRecyclerView.setAdapter(mAccountListAdapter);
+        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
         initializeComponentGetters();
     }
 
@@ -308,7 +281,7 @@ public class ManageAccountsActivity extends FileActivity
                                         getAccountListItems(),
                                         mTintedCheck
                                 );
-                                mListView.setAdapter(mAccountListAdapter);
+                                mRecyclerView.setAdapter(mAccountListAdapter);
                                 runOnUiThread(new Runnable() {
                                     @Override
                                     public void run() {
@@ -360,7 +333,7 @@ public class ManageAccountsActivity extends FileActivity
             List<AccountListItem> accountListItemArray = getAccountListItems();
             if (accountListItemArray.size() > SINGLE_ACCOUNT) {
                 mAccountListAdapter = new AccountListAdapter(this, getUserAccountManager(), accountListItemArray, mTintedCheck);
-                mListView.setAdapter(mAccountListAdapter);
+                mRecyclerView.setAdapter(mAccountListAdapter);
             } else {
                 onBackPressed();
             }
@@ -408,8 +381,8 @@ public class ManageAccountsActivity extends FileActivity
     }
 
     private void performAccountRemoval(Account account) {
-        // disable account in list view
-        for (int i = 0; i < mAccountListAdapter.getCount(); i++) {
+        // disable account in recycler view
+        for (int i = 0; i < mAccountListAdapter.getItemCount(); i++) {
             AccountListItem item = mAccountListAdapter.getItem(i);
 
             if (item != null && item.getAccount().equals(account)) {

+ 2 - 1
src/main/java/com/owncloud/android/ui/activity/ReceiveExternalFilesActivity.java

@@ -58,6 +58,7 @@ import android.widget.Button;
 import android.widget.EditText;
 import android.widget.ImageView;
 import android.widget.LinearLayout;
+import android.widget.ListAdapter;
 import android.widget.ListView;
 import android.widget.ProgressBar;
 import android.widget.Spinner;
@@ -309,7 +310,7 @@ public class ReceiveExternalFilesActivity extends FileActivity
             mAccountListAdapter = new AccountListAdapter(parent, accountManager, getAccountListItems(parent), mTintedCheck);
 
             builder.setTitle(R.string.common_choose_account);
-            builder.setAdapter(mAccountListAdapter, (dialog, which) -> {
+            builder.setAdapter((ListAdapter) mAccountListAdapter, (dialog, which) -> {
                 final ReceiveExternalFilesActivity parentActivity = (ReceiveExternalFilesActivity) getActivity();
                 parentActivity.setAccount(parentActivity.mAccountManager.getAccountsByType(
                         MainApp.getAccountType(getActivity()))[which], false);

+ 119 - 38
src/main/java/com/owncloud/android/ui/adapter/AccountListAdapter.java

@@ -22,41 +22,52 @@
 package com.owncloud.android.ui.adapter;
 
 import android.accounts.Account;
+import android.content.Intent;
 import android.graphics.Paint;
 import android.graphics.drawable.Drawable;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
-import android.widget.ArrayAdapter;
 import android.widget.ImageView;
 import android.widget.TextView;
 
 import com.nextcloud.client.account.UserAccountManager;
+import com.owncloud.android.MainApp;
 import com.owncloud.android.R;
 import com.owncloud.android.lib.common.OwnCloudAccount;
 import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.ui.activity.BaseActivity;
+import com.owncloud.android.ui.activity.UserInfoActivity;
 import com.owncloud.android.utils.DisplayUtils;
 import com.owncloud.android.utils.ThemeUtils;
 
+import org.parceler.Parcels;
+
+import java.util.ArrayList;
 import java.util.List;
 
 import androidx.annotation.NonNull;
+import androidx.recyclerview.widget.RecyclerView;
 
 /**
- * This Adapter populates a ListView with all accounts within the app.
+ * This Adapter populates a RecyclerView with all accounts within the app.
  */
-public class AccountListAdapter extends ArrayAdapter<AccountListItem> implements DisplayUtils.AvatarGenerationListener {
+public class AccountListAdapter extends RecyclerView.Adapter<AccountListAdapter.AccountViewHolderItem>
+                                implements DisplayUtils.AvatarGenerationListener {
     private static final String TAG = AccountListAdapter.class.getSimpleName();
     private float mAccountAvatarRadiusDimension;
     private final BaseActivity mContext;
     private List<AccountListItem> mValues;
     private AccountListAdapterListener mListener;
     private Drawable mTintedCheck;
+    private RecyclerView mRecyclerView;
     private UserAccountManager accountManager;
 
-    public AccountListAdapter(BaseActivity context, UserAccountManager accountManager, List<AccountListItem> values, Drawable tintedCheck) {
-        super(context, -1, values);
+
+    private static final String KEY_DISPLAY_NAME = "DISPLAY_NAME";
+    private static final int KEY_USER_INFO_REQUEST_CODE = 13;
+
+    public AccountListAdapter(BaseActivity context, List<AccountListItem> values, Drawable tintedCheck) {
         this.mContext = context;
         this.accountManager = accountManager;
         this.mValues = values;
@@ -67,41 +78,37 @@ public class AccountListAdapter extends ArrayAdapter<AccountListItem> implements
         this.mTintedCheck = tintedCheck;
     }
 
+    @Override
+    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
+        super.onAttachedToRecyclerView(recyclerView);
+        mRecyclerView = recyclerView;
+    }
+
     @NonNull
     @Override
-    public View getView(final int position, View convertView, @NonNull ViewGroup parent) {
+    public AccountViewHolderItem onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
         AccountViewHolderItem viewHolder;
-        View view = convertView;
-
-        if (view == null) {
-            LayoutInflater inflater = mContext.getLayoutInflater();
-            view = inflater.inflate(R.layout.account_item, parent, false);
-
-            viewHolder = new AccountViewHolderItem();
-            viewHolder.imageViewItem = view.findViewById(R.id.user_icon);
-            viewHolder.checkViewItem = view.findViewById(R.id.ticker);
-            viewHolder.checkViewItem.setImageDrawable(mTintedCheck);
-            viewHolder.usernameViewItem = view.findViewById(R.id.user_name);
-            viewHolder.accountViewItem = view.findViewById(R.id.account);
-
-            view.setTag(viewHolder);
-        } else {
-            viewHolder = (AccountViewHolderItem) view.getTag();
-        }
+        View view = LayoutInflater.from(mContext).inflate(R.layout.account_item, parent, false);
+        viewHolder = new AccountViewHolderItem(view);
+        viewHolder.checkViewItem.setImageDrawable(mTintedCheck);
+        return viewHolder;
+    }
 
+    @Override
+    public void onBindViewHolder(@NonNull AccountViewHolderItem holder, int position) {
         AccountListItem accountListItem = mValues.get(position);
 
         if (accountListItem != null) {
             // create account item
             if (AccountListItem.TYPE_ACCOUNT == accountListItem.getType()) {
                 Account account = accountListItem.getAccount();
-                setAccount(viewHolder, account);
-                setUsername(viewHolder, account);
-                setAvatar(viewHolder, account);
-                setCurrentlyActiveState(viewHolder, account);
+                setAccount(holder, account);
+                setUsername(holder, account);
+                setAvatar(holder, account);
+                setCurrentlyActiveState(holder, account);
 
-                TextView usernameView = viewHolder.usernameViewItem;
-                TextView accountView = viewHolder.accountViewItem;
+                TextView usernameView = (holder).usernameViewItem;
+                TextView accountView = (holder).accountViewItem;
 
                 if (!accountListItem.isEnabled()) {
                     usernameView.setPaintFlags(usernameView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
@@ -113,21 +120,44 @@ public class AccountListAdapter extends ArrayAdapter<AccountListItem> implements
 
             } // create add account action item
             else if (AccountListItem.TYPE_ACTION_ADD == accountListItem.getType() && mListener != null) {
-                return setupAddAccountListItem(parent);
+                setupAddAccountListItem(holder);
             }
         }
 
-        return view;
+        // OnClickListener for when the user selects an account
+        holder.itemView.setOnClickListener(new View.OnClickListener() {
+            @Override
+            public void onClick(View view) {
+                final Intent intent = new Intent(mContext, UserInfoActivity.class);
+                if (accountListItem.isEnabled()) {
+                    Account account = accountListItem.getAccount();
+                    intent.putExtra(UserInfoActivity.KEY_ACCOUNT, Parcels.wrap(account));
+                    try {
+                        OwnCloudAccount oca = new OwnCloudAccount(account, MainApp.getAppContext());
+                        intent.putExtra(KEY_DISPLAY_NAME, oca.getDisplayName());
+                    } catch (com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException e) {
+                        Log_OC.d(TAG, "Failed to find NC account");
+                    }
+                    mContext.startActivityForResult(intent, KEY_USER_INFO_REQUEST_CODE);
+                }
+            }
+        });
+
     }
 
+    /**
+     * Sets up a View to be used for adding a new account
+     * @param holder, the holder which contains the View to be used for the Add Account action.
+     */
     @NonNull
-    private View setupAddAccountListItem(ViewGroup parent) {
-        LayoutInflater inflater = mContext.getLayoutInflater();
-        View actionView = inflater.inflate(R.layout.account_action, parent, false);
+    private void setupAddAccountListItem(AccountViewHolderItem holder) {
+        View actionView = holder.itemView;
 
+        holder.accountViewItem.setVisibility(View.INVISIBLE);
+        holder.checkViewItem.setVisibility(View.INVISIBLE);
         TextView userName = actionView.findViewById(R.id.user_name);
         userName.setText(R.string.prefs_add_account);
-        userName.setTextColor(ThemeUtils.primaryColor(getContext(), true));
+        userName.setTextColor(ThemeUtils.primaryColor(mContext, true));
 
         ((ImageView) actionView.findViewById(R.id.user_icon)).setImageResource(R.drawable.ic_account_plus);
 
@@ -140,8 +170,6 @@ public class AccountListAdapter extends ArrayAdapter<AccountListItem> implements
         } else {
             actionView.setOnClickListener(v -> mListener.createAccount());
         }
-
-        return actionView;
     }
 
     private void setAccount(AccountViewHolderItem viewHolder, Account account) {
@@ -192,6 +220,46 @@ public class AccountListAdapter extends ArrayAdapter<AccountListItem> implements
         return String.valueOf(((ImageView)callContext).getTag()).equals(tag);
     }
 
+    /**
+     * Returns the total number of items in the data set held by the adapter.
+     *
+     * @return The total number of items in this adapter.
+     */
+    @Override
+    public int getItemCount() {
+        return this.mValues.size();
+    }
+
+    /**
+     * Returns an AccountListItem from the specified position in the mValues list
+     * @param position of the object to be returned
+     * @return An AccountListItem of the specified position
+     */
+    public AccountListItem getItem(int position) {
+        return mValues.get(position);
+    }
+
+    /**
+     * Deletes the elements in the mValues list and notifies the Adapter
+     */
+    public void clear() {
+        final int size = mValues.size();
+        mValues.clear();
+        notifyItemRangeRemoved(0, size);
+    }
+
+    /**
+     * Adds all of the items to the data set.
+     * @param items The item list to be added.
+     */
+    public void addAll(List<AccountListItem> items){
+        if(mValues == null){
+            mValues = new ArrayList<>();
+        }
+        mValues.addAll(items);
+        notifyDataSetChanged();
+    }
+
     /**
      * Listener interface for Activities using the {@link AccountListAdapter}
      */
@@ -205,11 +273,24 @@ public class AccountListAdapter extends ArrayAdapter<AccountListItem> implements
     /**
      * Account ViewHolderItem to get smooth scrolling.
      */
-    private static class AccountViewHolderItem {
+    static class AccountViewHolderItem extends RecyclerView.ViewHolder {
         private ImageView imageViewItem;
         private ImageView checkViewItem;
 
         private TextView usernameViewItem;
         private TextView accountViewItem;
+
+        private View parentView;
+
+        AccountViewHolderItem(@NonNull View view) {
+            super(view);
+            this.parentView = view;
+            this.imageViewItem = view.findViewById(R.id.user_icon);
+            this.checkViewItem = view.findViewById(R.id.ticker);
+            this.usernameViewItem = view.findViewById(R.id.user_name);
+            this.accountViewItem = view.findViewById(R.id.account);
+        }
+
+
     }
 }

+ 1 - 1
src/main/res/layout/accounts_layout.xml

@@ -29,7 +29,7 @@
 	<include
 		layout="@layout/toolbar_standard"/>
 
-	<ListView
+	<androidx.recyclerview.widget.RecyclerView
 		android:id="@+id/account_list"
 		android:layout_width="fill_parent"
 		android:layout_height="fill_parent"