Commit 1d350f46 authored by Thomas's avatar Thomas
Browse files

Fix issue #550 - Quick account switch

parent 167c3e62
Loading
Loading
Loading
Loading
+51 −0
Original line number Diff line number Diff line
@@ -758,6 +758,57 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt
                }
            }).start();
        }
        //Fetch recent used accounts
        new Thread(() -> {
            try {
                List<BaseAccount> accounts = new Account(BaseMainActivity.this).getLastUsedAccounts();
                Handler mainHandler = new Handler(Looper.getMainLooper());
                Runnable myRunnable = () -> {
                    if (accounts != null && accounts.size() > 0) {
                        Helper.loadPP(this, headerMainBinding.otherAccount1, accounts.get(0));
                        headerMainBinding.otherAccount1.setVisibility(View.VISIBLE);
                        headerMainBinding.otherAccount1.setOnClickListener(v -> {
                            headerMenuOpen = false;
                            Toasty.info(BaseMainActivity.this, getString(R.string.toast_account_changed, "@" + accounts.get(0).mastodon_account.acct + "@" + accounts.get(0).instance), Toasty.LENGTH_LONG).show();
                            BaseMainActivity.currentToken = accounts.get(0).token;
                            BaseMainActivity.currentUserID = accounts.get(0).user_id;
                            api = accounts.get(0).api;
                            SharedPreferences.Editor editor = sharedpreferences.edit();
                            editor.putString(PREF_USER_TOKEN, accounts.get(0).token);
                            editor.commit();
                            //The user is now aut
                            //The user is now authenticated, it will be redirected to MainActivity
                            Intent mainActivity = new Intent(this, MainActivity.class);
                            startActivity(mainActivity);
                            finish();
                        });
                        if (accounts.size() > 1) {
                            Helper.loadPP(this, headerMainBinding.otherAccount2, accounts.get(1));
                            headerMainBinding.otherAccount2.setVisibility(View.VISIBLE);
                            headerMainBinding.otherAccount2.setOnClickListener(v -> {
                                headerMenuOpen = false;
                                Toasty.info(BaseMainActivity.this, getString(R.string.toast_account_changed, "@" + accounts.get(1).mastodon_account.acct + "@" + accounts.get(1).instance), Toasty.LENGTH_LONG).show();
                                BaseMainActivity.currentToken = accounts.get(1).token;
                                BaseMainActivity.currentUserID = accounts.get(1).user_id;
                                api = accounts.get(1).api;
                                SharedPreferences.Editor editor = sharedpreferences.edit();
                                editor.putString(PREF_USER_TOKEN, accounts.get(1).token);
                                editor.commit();
                                //The user is now aut
                                //The user is now authenticated, it will be redirected to MainActivity
                                Intent mainActivity = new Intent(this, MainActivity.class);
                                startActivity(mainActivity);
                                finish();
                            });
                        }
                    }
                };
                mainHandler.post(myRunnable);

            } catch (DBException e) {
                e.printStackTrace();
            }
        }).start();
    }

    protected abstract void rateThisApp();
+17 −0
Original line number Diff line number Diff line
@@ -318,6 +318,23 @@ public class Account extends BaseAccount implements Serializable {
        }
    }

    /**
     * Returns last used account
     *
     * @return BaseAccount  {@link BaseAccount}
     */
    public List<BaseAccount> getLastUsedAccounts() throws DBException {
        if (db == null) {
            throw new DBException("db is null. Wrong initialization.");
        }
        try {
            Cursor c = db.query(Sqlite.TABLE_USER_ACCOUNT, null, null, null, null, null, Sqlite.COL_UPDATED_AT + " DESC", null);
            return cursorToListUser(c);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * Remove an account from db
     *
+43 −10
Original line number Diff line number Diff line
@@ -37,6 +37,11 @@
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingBottom="5dp">

        <androidx.appcompat.widget.LinearLayoutCompat
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/account_profile_picture"
                android:layout_width="80dp"
@@ -47,6 +52,34 @@
                android:scaleType="fitCenter"
                tools:src="@tools:sample/avatars" />

            <androidx.appcompat.widget.LinearLayoutCompat
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

            <androidx.appcompat.widget.AppCompatImageView
                android:id="@+id/other_account1"
                android:layout_width="36dp"
                android:layout_height="36dp"
                android:layout_gravity="bottom|end"
                android:scaleType="fitCenter"
                android:visibility="gone"
                tools:src="@tools:sample/avatars"
                tools:visibility="visible" />

            <androidx.appcompat.widget.AppCompatImageView
                android:id="@+id/other_account2"
                android:layout_width="36dp"
                android:layout_height="36dp"
                android:layout_gravity="bottom|end"
                android:layout_marginStart="20dp"
                android:scaleType="fitCenter"
                android:visibility="gone"
                tools:src="@tools:sample/avatars"
                tools:visibility="visible" />
        </androidx.appcompat.widget.LinearLayoutCompat>


        <androidx.appcompat.widget.LinearLayoutCompat
            android:id="@+id/change_account"
            android:layout_width="match_parent"
@@ -55,7 +88,7 @@
            android:orientation="horizontal">

            <androidx.appcompat.widget.AppCompatTextView
                style="@style/TextAppearance.Material3.TitleMedium"
                style="@style/TextAppearance.Material3.TitleSmall"
                android:id="@+id/account_name"
                android:layout_width="0dp"
                android:layout_height="wrap_content"