fedilab-Android-App/app/src/main/java/app/fedilab/android/peertube/helper/SwitchAccountHelper.java

89 lines
3.8 KiB
Java
Raw Normal View History

2023-01-22 16:48:14 +01:00
package app.fedilab.android.peertube.helper;
2023-01-24 15:09:21 +01:00
/* Copyright 2023 Thomas Schneider
2023-01-22 16:48:14 +01:00
*
2023-01-24 15:09:21 +01:00
* This file is a part of Fedilab
2023-01-22 16:48:14 +01:00
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
2023-01-24 15:09:21 +01:00
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
2023-01-22 16:48:14 +01:00
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
2023-01-24 15:09:21 +01:00
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
2023-01-22 16:48:14 +01:00
* see <http://www.gnu.org/licenses>. */
2023-01-23 15:01:44 +01:00
import static app.fedilab.android.mastodon.helper.Helper.PREF_USER_ID;
2023-01-24 18:08:58 +01:00
import static app.fedilab.android.mastodon.helper.Helper.PREF_USER_INSTANCE;
2023-01-24 15:03:02 +01:00
import static app.fedilab.android.mastodon.helper.Helper.PREF_USER_SOFTWARE;
2023-01-23 15:01:44 +01:00
import static app.fedilab.android.mastodon.helper.Helper.PREF_USER_TOKEN;
2023-01-22 16:48:14 +01:00
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import androidx.appcompat.app.AlertDialog;
2023-01-24 15:03:02 +01:00
import androidx.preference.PreferenceManager;
2023-01-22 16:48:14 +01:00
2023-01-25 15:14:42 +01:00
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
2023-01-22 16:48:14 +01:00
import java.util.List;
2023-01-23 09:31:32 +01:00
import app.fedilab.android.R;
2023-01-24 18:08:58 +01:00
import app.fedilab.android.activities.MainActivity;
2023-01-23 12:12:22 +01:00
import app.fedilab.android.mastodon.client.entities.app.Account;
import app.fedilab.android.mastodon.client.entities.app.BaseAccount;
import app.fedilab.android.mastodon.exception.DBException;
2023-01-22 16:48:14 +01:00
import app.fedilab.android.peertube.activities.LoginActivity;
import app.fedilab.android.peertube.drawer.OwnAccountsAdapter;
public class SwitchAccountHelper {
public static void switchDialog(Activity activity, boolean withAddAccount) {
2023-01-23 12:12:22 +01:00
List<BaseAccount> accounts = null;
try {
accounts = new Account(activity).getAll();
} catch (DBException e) {
e.printStackTrace();
}
2023-01-22 16:48:14 +01:00
2023-01-30 18:15:12 +01:00
AlertDialog.Builder builderSingle = new MaterialAlertDialogBuilder(activity);
2023-01-22 16:48:14 +01:00
builderSingle.setTitle(activity.getString(R.string.list_of_accounts));
if (accounts != null) {
final OwnAccountsAdapter accountsListAdapter = new OwnAccountsAdapter(activity, accounts);
2023-01-23 12:12:22 +01:00
final BaseAccount[] accountArray = new BaseAccount[accounts.size()];
2023-01-22 16:48:14 +01:00
int i = 0;
2023-01-23 12:12:22 +01:00
for (BaseAccount account : accounts) {
2023-01-22 16:48:14 +01:00
accountArray[i] = account;
i++;
}
builderSingle.setAdapter(accountsListAdapter, (dialog, which) -> {
2023-01-23 12:12:22 +01:00
final BaseAccount account = accountArray[which];
2023-01-24 15:03:02 +01:00
SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(activity);
2023-01-22 16:48:14 +01:00
SharedPreferences.Editor editor = sharedpreferences.edit();
2023-01-23 15:01:44 +01:00
editor.putString(PREF_USER_TOKEN, account.token);
2023-01-24 15:03:02 +01:00
editor.putString(PREF_USER_SOFTWARE, account.software);
2023-01-24 18:08:58 +01:00
editor.putString(PREF_USER_INSTANCE, account.instance);
2023-01-23 15:01:44 +01:00
editor.putString(PREF_USER_ID, account.user_id);
2023-01-24 18:08:58 +01:00
editor.commit();
2023-01-22 16:48:14 +01:00
dialog.dismiss();
2023-01-24 18:08:58 +01:00
Intent intent = new Intent(activity, MainActivity.class);
2023-01-22 16:48:14 +01:00
activity.startActivity(intent);
activity.finish();
});
}
builderSingle.setNegativeButton(R.string.cancel, (dialog, which) -> dialog.dismiss());
if (withAddAccount) {
builderSingle.setPositiveButton(R.string.add_account, (dialog, which) -> {
Intent intent = new Intent(activity, LoginActivity.class);
activity.startActivity(intent);
activity.finish();
});
}
builderSingle.show();
}
}