moved multiple account dialog to recyclerview (#4381)

moved multiple account dialog to recyclerview
This commit is contained in:
Tobias Kaminsky 2019-08-23 07:45:38 +02:00 committed by GitHub
commit 88714c1c9f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 300 additions and 100 deletions

View file

@ -63,6 +63,7 @@ import com.owncloud.android.ui.activity.UploadListActivity;
import com.owncloud.android.ui.activity.UploadPathActivity;
import com.owncloud.android.ui.activity.UserInfoActivity;
import com.owncloud.android.ui.dialog.ChooseTemplateDialogFragment;
import com.owncloud.android.ui.dialog.MultipleAccountsDialog;
import com.owncloud.android.ui.fragment.ExtendedListFragment;
import com.owncloud.android.ui.fragment.FileDetailActivitiesFragment;
import com.owncloud.android.ui.fragment.FileDetailFragment;
@ -137,10 +138,9 @@ abstract class ComponentsModule {
@ContributesAndroidInjector abstract PreviewMediaFragment previewMediaFragment();
@ContributesAndroidInjector abstract PreviewTextFragment previewTextFragment();
@ContributesAndroidInjector
abstract PhotoFragment photoFragment();
@ContributesAndroidInjector abstract PhotoFragment photoFragment();
@ContributesAndroidInjector abstract ReceiveExternalFilesActivity.DialogMultipleAccount dialogMultipleAccount();
@ContributesAndroidInjector abstract MultipleAccountsDialog multipleAccountsDialog();
@ContributesAndroidInjector abstract ReceiveExternalFilesActivity.DialogInputUploadFilename dialogInputUploadFilename();
@ContributesAndroidInjector abstract FileUploader fileUploader();

View file

@ -47,6 +47,7 @@ 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;
@ -72,11 +73,16 @@ import androidx.core.graphics.drawable.DrawableCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import static com.owncloud.android.ui.adapter.AccountListAdapter.KEY_DISPLAY_NAME;
import static com.owncloud.android.ui.adapter.AccountListAdapter.KEY_USER_INFO_REQUEST_CODE;
/**
* An Activity that allows the user to manage accounts.
*/
public class ManageAccountsActivity extends FileActivity
implements AccountListAdapter.AccountListAdapterListener, AccountManagerCallback<Boolean>, ComponentsGetter {
public class ManageAccountsActivity extends FileActivity implements AccountListAdapter.AccountListAdapterListener,
AccountManagerCallback<Boolean>,
ComponentsGetter,
AccountListAdapter.ClickListener {
private static final String TAG = ManageAccountsActivity.class.getSimpleName();
public static final String KEY_ACCOUNT_LIST_CHANGED = "ACCOUNT_LIST_CHANGED";
@ -130,7 +136,12 @@ public class ManageAccountsActivity extends FileActivity
arbitraryDataProvider = new ArbitraryDataProvider(getContentResolver());
accountListAdapter = new AccountListAdapter(this, accountManager, getAccountListItems(), tintedCheck);
accountListAdapter = new AccountListAdapter(this,
accountManager,
getAccountListItems(),
tintedCheck,
this,
true);
recyclerView.setAdapter(accountListAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
@ -268,10 +279,12 @@ public class ManageAccountsActivity extends FileActivity
String name = result.getString(AccountManager.KEY_ACCOUNT_NAME);
accountManager.setCurrentOwnCloudAccount(name);
accountListAdapter = new AccountListAdapter(
this,
accountManager,
getAccountListItems(),
tintedCheck
this,
accountManager,
getAccountListItems(),
tintedCheck,
this,
true
);
recyclerView.setAdapter(accountListAdapter);
runOnUiThread(() -> accountListAdapter.notifyDataSetChanged());
@ -318,7 +331,12 @@ public class ManageAccountsActivity extends FileActivity
List<AccountListItem> accountListItemArray = getAccountListItems();
if (accountListItemArray.size() > SINGLE_ACCOUNT) {
accountListAdapter = new AccountListAdapter(this, accountManager, accountListItemArray, tintedCheck);
accountListAdapter = new AccountListAdapter(this,
accountManager,
accountListItemArray,
tintedCheck,
this,
true);
recyclerView.setAdapter(accountListAdapter);
} else {
onBackPressed();
@ -432,6 +450,19 @@ public class ManageAccountsActivity extends FileActivity
}
}
@Override
public void onClick(Account account) {
final Intent intent = new Intent(this, UserInfoActivity.class);
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);
}
/**
* Defines callbacks for service binding, passed to bindService()
*/

View file

@ -58,13 +58,11 @@ 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;
import android.widget.TextView;
import com.nextcloud.client.account.UserAccountManager;
import com.nextcloud.client.di.Injectable;
import com.nextcloud.client.preferences.AppPreferences;
import com.owncloud.android.MainApp;
@ -79,12 +77,11 @@ import com.owncloud.android.operations.CreateFolderOperation;
import com.owncloud.android.operations.RefreshFolderOperation;
import com.owncloud.android.operations.UploadFileOperation;
import com.owncloud.android.syncadapter.FileSyncAdapter;
import com.owncloud.android.ui.adapter.AccountListAdapter;
import com.owncloud.android.ui.adapter.AccountListItem;
import com.owncloud.android.ui.adapter.UploaderAdapter;
import com.owncloud.android.ui.asynctasks.CopyAndUploadContentUrisTask;
import com.owncloud.android.ui.dialog.ConfirmationDialogFragment;
import com.owncloud.android.ui.dialog.CreateFolderDialogFragment;
import com.owncloud.android.ui.dialog.MultipleAccountsDialog;
import com.owncloud.android.ui.dialog.SortingOrderDialogFragment;
import com.owncloud.android.ui.fragment.TaskRetainerFragment;
import com.owncloud.android.ui.helpers.UriUploader;
@ -120,8 +117,6 @@ import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AlertDialog.Builder;
import androidx.appcompat.widget.SearchView;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.core.view.MenuItemCompat;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;
@ -226,7 +221,7 @@ public class ReceiveExternalFilesActivity extends FileActivity
}
private void showAccountChooserDialog() {
DialogMultipleAccount dialog = new DialogMultipleAccount();
MultipleAccountsDialog dialog = new MultipleAccountsDialog();
dialog.show(getSupportFragmentManager(), null);
}
@ -234,6 +229,11 @@ public class ReceiveExternalFilesActivity extends FileActivity
return this;
}
public void changeAccount(Account account) {
setAccount(account, false);
onAccountSet(mAccountWasRestored);
}
@Override
protected void onAccountSet(boolean stateWasRecovered) {
super.onAccountSet(mAccountWasRestored);
@ -291,52 +291,6 @@ public class ReceiveExternalFilesActivity extends FileActivity
}
}
public static class DialogMultipleAccount extends DialogFragment implements Injectable {
private AccountListAdapter mAccountListAdapter;
private Drawable mTintedCheck;
@Inject UserAccountManager accountManager;
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final ReceiveExternalFilesActivity parent = (ReceiveExternalFilesActivity) getActivity();
AlertDialog.Builder builder = new Builder(parent);
mTintedCheck = DrawableCompat.wrap(ContextCompat.getDrawable(parent, R.drawable.account_circle_white));
int tint = ThemeUtils.primaryColor(getContext());
DrawableCompat.setTint(mTintedCheck, tint);
mAccountListAdapter = new AccountListAdapter(parent, accountManager, getAccountListItems(parent), mTintedCheck);
builder.setTitle(R.string.common_choose_account);
builder.setAdapter((ListAdapter) mAccountListAdapter, (dialog, which) -> {
final ReceiveExternalFilesActivity parentActivity = (ReceiveExternalFilesActivity) getActivity();
parentActivity.setAccount(parentActivity.mAccountManager.getAccountsByType(
MainApp.getAccountType(getActivity()))[which], false);
parentActivity.onAccountSet(parentActivity.mAccountWasRestored);
dialog.dismiss();
});
builder.setCancelable(true);
return builder.create();
}
/**
* creates the account list items list including the add-account action in case multiaccount_support is enabled.
*
* @return list of account list items
*/
private List<AccountListItem> getAccountListItems(ReceiveExternalFilesActivity activity) {
Account[] accountList = activity.mAccountManager.getAccountsByType(MainApp.getAccountType(getActivity()));
List<AccountListItem> adapterAccountList = new ArrayList<>(accountList.length);
for (Account account : accountList) {
adapterAccountList.add(new AccountListItem(account));
}
return adapterAccountList;
}
}
public static class DialogInputUploadFilename extends DialogFragment implements Injectable {
private static final String KEY_SUBJECT_TEXT = "SUBJECT_TEXT";
private static final String KEY_EXTRA_TEXT = "EXTRA_TEXT";
@ -729,11 +683,12 @@ public class ReceiveExternalFilesActivity extends FileActivity
}
private void setupActionBarSubtitle() {
ActionBar actionBar = getSupportActionBar();
if (isHaveMultipleAccount()) {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setSubtitle(getAccount().name);
}
ThemeUtils.setColoredSubtitle(actionBar, getAccount().name, this);
} else if (actionBar != null) {
actionBar.setSubtitle(null);
}
}

View file

@ -26,7 +26,6 @@
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;
@ -36,17 +35,13 @@ 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;
@ -67,10 +62,17 @@ public class AccountListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHo
private Drawable tintedCheck;
private UserAccountManager accountManager;
private static final String KEY_DISPLAY_NAME = "DISPLAY_NAME";
private static final int KEY_USER_INFO_REQUEST_CODE = 13;
public static final String KEY_DISPLAY_NAME = "DISPLAY_NAME";
public static final int KEY_USER_INFO_REQUEST_CODE = 13;
private ClickListener clickListener;
private boolean showAddAccount;
public AccountListAdapter(BaseActivity context, UserAccountManager accountManager, List<AccountListItem> values, Drawable tintedCheck) {
public AccountListAdapter(BaseActivity context,
UserAccountManager accountManager,
List<AccountListItem> values,
Drawable tintedCheck,
ClickListener clickListener,
boolean showAddAccount) {
this.context = context;
this.accountManager = accountManager;
this.values = values;
@ -79,11 +81,13 @@ public class AccountListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHo
}
this.accountAvatarRadiusDimension = context.getResources().getDimension(R.dimen.list_item_avatar_icon_radius);
this.tintedCheck = tintedCheck;
this.clickListener = clickListener;
this.showAddAccount = showAddAccount;
}
@Override
public int getItemViewType(int position) {
if (position == values.size() - 1) {
if (position == values.size() - 1 && showAddAccount) {
return AccountListItem.TYPE_ACTION_ADD;
}
return AccountListItem.TYPE_ACCOUNT;
@ -113,6 +117,7 @@ public class AccountListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHo
if (AccountListItem.TYPE_ACCOUNT == accountListItem.getType()) {
Account account = accountListItem.getAccount();
AccountViewHolderItem item = (AccountViewHolderItem)holder;
item.setData(account);
setAccount(item, account);
setUsername(item, account);
setAvatar(item, account);
@ -121,21 +126,6 @@ public class AccountListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHo
TextView usernameView = item.usernameViewItem;
TextView accountView = item.accountViewItem;
// OnClickListener for when the user selects an account
holder.itemView.setOnClickListener(view -> {
final Intent intent = new Intent(context, UserInfoActivity.class);
if (accountListItem.isEnabled()) {
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");
}
context.startActivityForResult(intent, KEY_USER_INFO_REQUEST_CODE);
}
});
if (!accountListItem.isEnabled()) {
usernameView.setPaintFlags(usernameView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
accountView.setPaintFlags(accountView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
@ -300,19 +290,33 @@ public class AccountListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHo
/**
* Account ViewHolderItem to get smooth scrolling.
*/
static class AccountViewHolderItem extends RecyclerView.ViewHolder {
class AccountViewHolderItem extends RecyclerView.ViewHolder implements View.OnClickListener {
private ImageView imageViewItem;
private ImageView checkViewItem;
private TextView usernameViewItem;
private TextView accountViewItem;
private Account account;
AccountViewHolderItem(@NonNull View view) {
super(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);
view.setOnClickListener(this);
}
public void setData(Account account) {
this.account = account;
}
@Override
public void onClick(View v) {
if (clickListener != null && v.isEnabled()) {
clickListener.onClick(account);
}
}
}
@ -326,4 +330,8 @@ public class AccountListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHo
this.usernameViewItem = view.findViewById(R.id.user_name);
}
}
public interface ClickListener {
void onClick(Account account);
}
}

View file

@ -0,0 +1,138 @@
/*
*
* Nextcloud Android client application
*
* @author Tobias Kaminsky
* Copyright (C) 2019 Tobias Kaminsky
* Copyright (C) 2019 Nextcloud GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
package com.owncloud.android.ui.dialog;
import android.accounts.Account;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import com.nextcloud.client.account.UserAccountManager;
import com.nextcloud.client.di.Injectable;
import com.owncloud.android.R;
import com.owncloud.android.ui.activity.ReceiveExternalFilesActivity;
import com.owncloud.android.ui.adapter.AccountListAdapter;
import com.owncloud.android.ui.adapter.AccountListItem;
import com.owncloud.android.utils.ThemeUtils;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
public class MultipleAccountsDialog extends DialogFragment implements Injectable, AccountListAdapter.ClickListener {
@BindView(R.id.list)
RecyclerView listView;
@Inject UserAccountManager accountManager;
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Activity activity = getActivity();
if (activity == null) {
throw new IllegalArgumentException("Activity may not be null");
}
int accentColor = ThemeUtils.primaryAccentColor(getContext());
// Inflate the layout for the dialog
LayoutInflater inflater = activity.getLayoutInflater();
@SuppressLint("InflateParams") View view = inflater.inflate(R.layout.multiple_accounts, null);
ButterKnife.bind(this, view);
final ReceiveExternalFilesActivity parent = (ReceiveExternalFilesActivity) getActivity();
AlertDialog.Builder builder = new AlertDialog.Builder(parent);
Drawable tintedCheck = DrawableCompat.wrap(ContextCompat.getDrawable(parent, R.drawable.account_circle_white));
int tint = ThemeUtils.primaryColor(getContext());
DrawableCompat.setTint(tintedCheck, tint);
AccountListAdapter adapter = new AccountListAdapter(parent,
accountManager,
getAccountListItems(),
tintedCheck,
this,
false);
listView.setHasFixedSize(true);
listView.setLayoutManager(new LinearLayoutManager(activity));
listView.setAdapter(adapter);
builder.setView(view)
.setTitle(ThemeUtils.getColoredTitle(getResources().getString(R.string.common_choose_account), accentColor));
Dialog dialog = builder.create();
Window window = dialog.getWindow();
if (window != null) {
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
}
return dialog;
}
/**
* creates the account list items list including the add-account action in case
* multiaccount_support is enabled.
*
* @return list of account list items
*/
private List<AccountListItem> getAccountListItems() {
Account[] accountList = accountManager.getAccounts();
List<AccountListItem> adapterAccountList = new ArrayList<>(accountList.length);
for (Account account : accountList) {
adapterAccountList.add(new AccountListItem(account));
}
return adapterAccountList;
}
@Override
public void onClick(Account account) {
final ReceiveExternalFilesActivity parentActivity = (ReceiveExternalFilesActivity) getActivity();
if (parentActivity != null) {
parentActivity.changeAccount(account);
}
dismiss();
}
}

View file

@ -232,6 +232,27 @@ public final class ThemeUtils {
}
}
/**
* Set color of subtitle to white/black depending on background color
*
* @param actionBar actionBar to be used
* @param title title to be shown
*/
public static void setColoredSubtitle(@Nullable ActionBar actionBar, String title, Context context) {
if (actionBar != null) {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT) {
actionBar.setSubtitle(title);
} else {
Spannable text = new SpannableString(title);
text.setSpan(new ForegroundColorSpan(fontColor(context)),
0,
text.length(),
Spannable.SPAN_INCLUSIVE_INCLUSIVE);
actionBar.setSubtitle(text);
}
}
}
/**
* For activities that do not use drawer, e.g. Settings, this can be used to correctly tint back button based on
* theme

View file

@ -30,7 +30,7 @@
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></androidx.recyclerview.widget.RecyclerView>
android:layout_weight="1" />
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/filename"

View file

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Nextcloud Android client application
@author Tobias Kaminsky
Copyright (C) 2019 Tobias Kaminsky
Copyright (C) 2019 Nextcloud GmbH
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="clip_horizontal"
android:orientation="vertical"
android:padding="@dimen/standard_padding">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

View file

@ -43,8 +43,8 @@ public class AccountListAdapterTest {
private ManageAccountsActivity manageAccountsActivity;
/**
* Setting up and mocking the manageAccountsActivity class, and then mocking the method calls in the construction of
* the object
* Setting up and mocking the manageAccountsActivity class, and then mocking the method calls in
* the construction of the object
*/
@Before
public void setup() {
@ -58,8 +58,12 @@ public class AccountListAdapterTest {
*/
@Test
public void test_getItemCountEmptyList() {
accountListAdapter = new AccountListAdapter(manageAccountsActivity, null,
new ArrayList<>(), null);
accountListAdapter = new AccountListAdapter(manageAccountsActivity,
null,
new ArrayList<>(),
null,
null,
true);
assertEquals(0, accountListAdapter.getItemCount());
}
@ -72,7 +76,12 @@ public class AccountListAdapterTest {
accounts.add(new AccountListItem());
accounts.add(new AccountListItem());
accountListAdapter = new AccountListAdapter(manageAccountsActivity, null, accounts, null);
accountListAdapter = new AccountListAdapter(manageAccountsActivity,
null,
accounts,
null,
null,
true);
assertEquals(2, accountListAdapter.getItemCount());
}
@ -87,7 +96,12 @@ public class AccountListAdapterTest {
.thenReturn(new Float(0.1));
List<AccountListItem> accounts = new ArrayList<>();
accountListAdapter = new AccountListAdapter(manageAccountsActivity, null, accounts, null);
accountListAdapter = new AccountListAdapter(manageAccountsActivity,
null,
accounts,
null,
null,
true);
AccountListItem accountListItem1 = new AccountListItem();
AccountListItem accountListItem2 = new AccountListItem();