finished account selection in dashboard, but will redesign lately

This commit is contained in:
Mariotaku Lee 2015-03-18 16:33:04 +08:00
parent 5fc7b03a7c
commit 2be77fd05d
5 changed files with 83 additions and 36 deletions

View File

@ -40,6 +40,7 @@ import android.support.v4.util.Pair;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.internal.view.SupportMenuInflater;
import android.support.v7.widget.ActionMenuView;
import android.support.v7.widget.ActionMenuView.OnMenuItemClickListener;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.Adapter;
@ -47,6 +48,7 @@ import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
@ -55,13 +57,13 @@ import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.TextView;
import com.commonsware.cwac.merge.MergeAdapter;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import org.apache.commons.lang3.ArrayUtils;
import org.mariotaku.twidere.R;
import org.mariotaku.twidere.activity.FiltersActivity;
import org.mariotaku.twidere.activity.SettingsActivity;
@ -86,6 +88,8 @@ import org.mariotaku.twidere.util.content.SupportFragmentReloadCursorObserver;
import org.mariotaku.twidere.view.ShapedImageView;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import static org.mariotaku.twidere.util.Utils.openUserFavorites;
import static org.mariotaku.twidere.util.Utils.openUserLists;
@ -117,6 +121,7 @@ public class AccountsDashboardFragment extends BaseSupportListFragment implement
private Context mThemedContext;
private ImageLoaderWrapper mImageLoader;
private SupportAccountActionProvider mAccountActionProvider;
@Override
public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
@ -173,8 +178,10 @@ public class AccountsDashboardFragment extends BaseSupportListFragment implement
@Override
public void onLoadFinished(final Loader<Cursor> loader, final Cursor data) {
final Menu menu = mAccountsToggleMenu.getMenu();
final SupportAccountActionProvider provider = (SupportAccountActionProvider) MenuItemCompat.getActionProvider(menu.findItem(MENU_SELECT_ACCOUNT));
mAccountActionProvider = (SupportAccountActionProvider) MenuItemCompat.getActionProvider(menu.findItem(MENU_SELECT_ACCOUNT));
mAccountActionProvider.setExclusive(false);
final ArrayList<ParcelableAccount> accounts = new ArrayList<>();
final Set<Long> activatedIds = new HashSet<>();
if (data != null) {
data.moveToFirst();
final Indices indices = new Indices(data);
@ -182,8 +189,11 @@ public class AccountsDashboardFragment extends BaseSupportListFragment implement
while (!data.isAfterLast()) {
final ParcelableAccount account = new ParcelableAccount(data, indices);
accounts.add(account);
if (defaultId < 0 && account.is_activated) {
defaultId = account.account_id;
if (account.is_activated) {
if (defaultId < 0) {
defaultId = account.account_id;
}
activatedIds.add(account.account_id);
}
data.moveToNext();
}
@ -191,7 +201,9 @@ public class AccountsDashboardFragment extends BaseSupportListFragment implement
mAccountsAdapter.setSelectedAccountId(mPreferences.getLong(KEY_DEFAULT_ACCOUNT_ID, defaultId));
}
}
provider.setAccounts(accounts.toArray(new ParcelableAccount[accounts.size()]));
mAccountActionProvider.setAccounts(accounts.toArray(new ParcelableAccount[accounts.size()]));
mAccountActionProvider.setSelectedAccountIds(ArrayUtils.toPrimitive(activatedIds.toArray(new Long[activatedIds.size()])));
mAccountsAdapter.changeCursor(data);
updateAccountOptionsSeparatorLabel();
updateDefaultAccountState();
@ -212,12 +224,6 @@ public class AccountsDashboardFragment extends BaseSupportListFragment implement
final OptionItem option = (OptionItem) item;
switch (option.id) {
case MENU_SEARCH: {
// final FragmentActivity a = getActivity();
// if (a instanceof HomeActivity) {
// ((HomeActivity) a).openSearchView(account);
// } else {
// getActivity().onSearchRequested();
// }
final Intent intent = new Intent(getActivity(), QuickSearchBarActivity.class);
intent.putExtra(EXTRA_ACCOUNT_ID, account.account_id);
startActivity(intent);
@ -347,6 +353,19 @@ public class AccountsDashboardFragment extends BaseSupportListFragment implement
mAccountsToggleMenu = (ActionMenuView) mAccountSelectorView.findViewById(R.id.toggle_menu);
final SupportMenuInflater menuInflater = new SupportMenuInflater(context);
menuInflater.inflate(R.menu.action_dashboard_timeline_toggle, mAccountsToggleMenu.getMenu());
mAccountsToggleMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
if (item.getGroupId() != SupportAccountActionProvider.MENU_GROUP) return false;
final ParcelableAccount[] accounts = mAccountActionProvider.getAccounts();
final ParcelableAccount account = accounts[item.getOrder()];
final ContentValues values = new ContentValues();
values.put(Accounts.IS_ACTIVATED, !account.is_activated);
final String where = Accounts.ACCOUNT_ID + " = " + account.account_id;
mResolver.update(Accounts.CONTENT_URI, values, where, null);
return true;
}
});
mAccountProfileContainer.setOnClickListener(this);

View File

@ -8,6 +8,7 @@ import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import org.apache.commons.lang3.ArrayUtils;
import org.mariotaku.twidere.TwidereConstants;
import org.mariotaku.twidere.model.ParcelableAccount;
@ -17,7 +18,8 @@ public class AccountActionProvider extends ActionProvider implements TwidereCons
private ParcelableAccount[] mAccounts;
private long mAccountId;
private long[] mAccountIds;
private boolean mExclusive;
public AccountActionProvider(final Context context, final ParcelableAccount[] accounts) {
super(context);
@ -45,27 +47,38 @@ public class AccountActionProvider extends ActionProvider implements TwidereCons
@Override
public void onPrepareSubMenu(final SubMenu subMenu) {
if (mAccounts == null) return;
subMenu.removeGroup(MENU_GROUP);
for (final ParcelableAccount account : mAccounts) {
final MenuItem item = subMenu.add(MENU_GROUP, Menu.NONE, 0, account.name);
if (mAccounts == null) return;
for (int i = 0, j = mAccounts.length; i < j; i++) {
final ParcelableAccount account = mAccounts[i];
final MenuItem item = subMenu.add(MENU_GROUP, Menu.NONE, i, account.name);
final Intent intent = new Intent();
intent.putExtra(EXTRA_ACCOUNT, account);
item.setIntent(intent);
}
subMenu.setGroupCheckable(MENU_GROUP, true, true);
subMenu.setGroupCheckable(MENU_GROUP, true, mExclusive);
if (mAccountIds == null) return;
for (int i = 0, j = subMenu.size(); i < j; i++) {
final MenuItem item = subMenu.getItem(i);
final Intent intent = item.getIntent();
final ParcelableAccount account = intent.getParcelableExtra(EXTRA_ACCOUNT);
if (account.account_id == mAccountId) {
if (ArrayUtils.contains(mAccountIds, account.account_id)) {
item.setChecked(true);
}
}
}
public void setAccountId(final long accountId) {
mAccountId = accountId;
public boolean isExclusive() {
return mExclusive;
}
public void setExclusive(boolean exclusive) {
mExclusive = exclusive;
}
public void setSelectedAccountIds(final long... accountIds) {
mAccountIds = accountIds;
}
}

View File

@ -27,6 +27,7 @@ import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import org.apache.commons.lang3.ArrayUtils;
import org.mariotaku.twidere.TwidereConstants;
import org.mariotaku.twidere.model.ParcelableAccount;
@ -36,18 +37,32 @@ public class SupportAccountActionProvider extends ActionProvider implements Twid
private ParcelableAccount[] mAccounts;
private long mAccountId;
private long[] mAccountIds;
private boolean mExclusive;
public SupportAccountActionProvider(final Context context, final ParcelableAccount[] accounts) {
super(context);
mAccounts = accounts;
setAccounts(accounts);
}
public SupportAccountActionProvider(final Context context) {
this(context, ParcelableAccount.getAccounts(context, false, false));
}
public ParcelableAccount[] getAccounts() {
return mAccounts;
}
@Override
public boolean onPerformDefaultAction() {
return true;
}
public boolean isExclusive() {
return mExclusive;
}
@Override
public boolean hasSubMenu() {
return true;
@ -62,33 +77,33 @@ public class SupportAccountActionProvider extends ActionProvider implements Twid
mAccounts = accounts;
}
public void setExclusive(boolean exclusive) {
mExclusive = exclusive;
}
@Override
public void onPrepareSubMenu(final SubMenu subMenu) {
if (mAccounts == null) return;
subMenu.removeGroup(MENU_GROUP);
for (final ParcelableAccount account : mAccounts) {
final MenuItem item = subMenu.add(MENU_GROUP, Menu.NONE, 0, account.name);
if (mAccounts == null) return;
for (int i = 0, j = mAccounts.length; i < j; i++) {
final ParcelableAccount account = mAccounts[i];
final MenuItem item = subMenu.add(MENU_GROUP, Menu.NONE, i, account.name);
final Intent intent = new Intent();
intent.putExtra(EXTRA_ACCOUNT, account);
item.setIntent(intent);
}
subMenu.setGroupCheckable(MENU_GROUP, true, mExclusive);
if (mAccountIds == null) return;
for (int i = 0, j = subMenu.size(); i < j; i++) {
final MenuItem item = subMenu.getItem(i);
final Intent intent = item.getIntent();
final ParcelableAccount account = intent.getParcelableExtra(EXTRA_ACCOUNT);
if (account.account_id == mAccountId) {
if (ArrayUtils.contains(mAccountIds, mAccounts[i].account_id)) {
item.setChecked(true);
}
}
}
public void setAccountId(final long accountId) {
mAccountId = accountId;
public void setExclusive(boolean exclusive) {
mExclusive = exclusive;
}
public void setSelectedAccountIds(final long... accountIds) {
mAccountIds = accountIds;
}
}

View File

@ -182,7 +182,7 @@ public class MultiSelectEventHandler implements Constants, ActionMode.Callback,
final ParcelableAccount account = intent.getParcelableExtra(EXTRA_ACCOUNT);
mMultiSelectManager.setAccountId(account.account_id);
if (mAccountActionProvider != null) {
mAccountActionProvider.setAccountId(account.account_id);
mAccountActionProvider.setSelectedAccountIds(account.account_id);
}
mode.invalidate();
}
@ -193,7 +193,7 @@ public class MultiSelectEventHandler implements Constants, ActionMode.Callback,
public boolean onCreateActionMode(final ActionMode mode, final Menu menu) {
mode.getMenuInflater().inflate(R.menu.action_multi_select_contents, menu);
mAccountActionProvider = (AccountActionProvider) menu.findItem(MENU_SELECT_ACCOUNT).getActionProvider();
mAccountActionProvider.setAccountId(mMultiSelectManager.getFirstSelectAccountId());
mAccountActionProvider.setSelectedAccountIds(mMultiSelectManager.getFirstSelectAccountId());
return true;
}

View File

@ -23,7 +23,7 @@
<item
android:id="@id/select_account"
android:icon="@drawable/ic_action_user"
android:icon="@drawable/ic_action_more_vertical"
android:title="@string/select_account"
app:actionProviderClass="org.mariotaku.twidere.menu.SupportAccountActionProvider"
app:showAsAction="always"/>