diff options
author | Thomas <tschneider.ac@gmail.com> | 2023-03-15 15:23:12 +0100 |
---|---|---|
committer | Thomas <tschneider.ac@gmail.com> | 2023-03-15 15:23:12 +0100 |
commit | ffe7dbf1dd60e236b1e0537b62114af0c49d31f2 (patch) | |
tree | 8857308433bed8eca15d4187237afa7882a7f33e | |
parent | dddef9706d6da428b18d9a399c8eb80a64c260df (diff) |
Some changes
9 files changed, 300 insertions, 186 deletions
diff --git a/app/src/main/java/app/fedilab/android/BaseMainActivity.java b/app/src/main/java/app/fedilab/android/BaseMainActivity.java index 6e2854fa4..71740d273 100644 --- a/app/src/main/java/app/fedilab/android/BaseMainActivity.java +++ b/app/src/main/java/app/fedilab/android/BaseMainActivity.java @@ -24,6 +24,7 @@ import static app.fedilab.android.mastodon.helper.Helper.PREF_USER_TOKEN; import static app.fedilab.android.mastodon.helper.Helper.displayReleaseNotesIfNeeded; import static app.fedilab.android.mastodon.helper.ThemeHelper.fetchAccentColor; import static app.fedilab.android.mastodon.ui.drawer.StatusAdapter.sendAction; +import static app.fedilab.android.peertube.activities.PeertubeMainActivity.typeOfConnection; import android.Manifest; import android.annotation.SuppressLint; @@ -374,6 +375,7 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt } else if (accounts.get(0).peertube_account != null) { account = "@" + accounts.get(0).peertube_account.getAcct() + "@" + accounts.get(0).instance; } + typeOfConnection = PeertubeMainActivity.TypeOfConnection.NORMAL; Toasty.info(activity, activity.getString(R.string.toast_account_changed, account), Toasty.LENGTH_LONG).show(); BaseMainActivity.currentToken = accounts.get(0).token; BaseMainActivity.currentUserID = accounts.get(0).user_id; @@ -546,6 +548,7 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt } else if (account.peertube_account != null) { acctForAccount = "@" + account.peertube_account.getUsername() + "@" + account.instance; } + typeOfConnection = PeertubeMainActivity.TypeOfConnection.NORMAL; Toasty.info(activity, activity.getString(R.string.toast_account_changed, acctForAccount), Toasty.LENGTH_LONG).show(); BaseMainActivity.currentToken = account.token; BaseMainActivity.currentUserID = account.user_id; @@ -559,6 +562,7 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt editor.commit(); //The user is now aut //The user is now authenticated, it will be redirected to MainActivity + Intent mainActivity = new Intent(activity, MainActivity.class); activity.startActivity(mainActivity); activity.finish(); @@ -711,6 +715,7 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt } else if (account.peertube_account != null) { acct = "@" + account.peertube_account.getUsername() + "@" + account.instance; } + typeOfConnection = PeertubeMainActivity.TypeOfConnection.NORMAL; Toasty.info(activity, activity.getString(R.string.toast_account_changed, acct), Toasty.LENGTH_LONG).show(); BaseMainActivity.currentToken = account.token; BaseMainActivity.currentUserID = account.user_id; @@ -1358,6 +1363,7 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt bundle.putBoolean(Helper.ARG_PEERTUBE_NAV_REMOTE, true); intent.putExtras(bundle); startActivity(intent); + finish(); } else if (id == R.id.nav_about_instance) { (new InstanceActivity()).show(getSupportFragmentManager(), null); } diff --git a/app/src/main/java/app/fedilab/android/mastodon/helper/Helper.java b/app/src/main/java/app/fedilab/android/mastodon/helper/Helper.java index ad5e36304..7b5e26077 100644 --- a/app/src/main/java/app/fedilab/android/mastodon/helper/Helper.java +++ b/app/src/main/java/app/fedilab/android/mastodon/helper/Helper.java @@ -302,6 +302,9 @@ public class Helper { public static final String PREF_USER_TOKEN = "PREF_USER_TOKEN"; public static final String PREF_USER_ID = "PREF_USER_ID"; public static final String PREF_USER_INSTANCE = "PREF_USER_INSTANCE"; + public static final String PREF_USER_INSTANCE_PEERTUBE_BROWSING = "PREF_USER_INSTANCE_PEERTUBE_BROWSING"; + + public static final String PREF_USER_SOFTWARE = "PREF_USER_SOFTWARE"; public static final String PREF_IS_MODERATOR = "PREF_IS_MODERATOR"; public static final String PREF_IS_ADMINISTRATOR = "PREF_IS_ADMINISTRATOR"; diff --git a/app/src/main/java/app/fedilab/android/peertube/activities/ManageInstancesActivity.java b/app/src/main/java/app/fedilab/android/peertube/activities/ManageInstancesActivity.java index 6c07c16fb..d4a063c71 100644 --- a/app/src/main/java/app/fedilab/android/peertube/activities/ManageInstancesActivity.java +++ b/app/src/main/java/app/fedilab/android/peertube/activities/ManageInstancesActivity.java @@ -14,7 +14,7 @@ package app.fedilab.android.peertube.activities; * You should have received a copy of the GNU General Public License along with Fedilab; if not, * see <http://www.gnu.org/licenses>. */ -import static app.fedilab.android.mastodon.helper.Helper.PREF_USER_INSTANCE; +import static app.fedilab.android.mastodon.helper.Helper.PREF_USER_INSTANCE_PEERTUBE_BROWSING; import static app.fedilab.android.mastodon.helper.Helper.addFragment; import static app.fedilab.android.peertube.activities.PeertubeMainActivity.INSTANCE_ADDRESS; import static app.fedilab.android.peertube.activities.PeertubeMainActivity.PICK_INSTANCE; @@ -32,6 +32,8 @@ import android.widget.LinearLayout; import android.widget.Toast; import androidx.appcompat.app.AlertDialog; +import androidx.fragment.app.FragmentManager; +import androidx.lifecycle.LifecycleOwner; import androidx.lifecycle.ViewModelProvider; import androidx.preference.PreferenceManager; import androidx.recyclerview.widget.LinearLayoutManager; @@ -65,33 +67,8 @@ public class ManageInstancesActivity extends BaseBarActivity implements AboutIns private AboutInstanceAdapter aboutInstanceAdapter; - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - binding = ActivityManageInstancesPeertubeBinding.inflate(getLayoutInflater()); - View view = binding.getRoot(); - setContentView(view); - if (getSupportActionBar() != null) { - getSupportActionBar().setDisplayHomeAsUpEnabled(true); - getSupportActionBar().setDisplayShowHomeEnabled(true); - } - binding.loader.setVisibility(View.VISIBLE); - binding.noAction.setVisibility(View.GONE); - binding.lvInstances.setVisibility(View.GONE); - binding.actionButton.setOnClickListener(v -> showRadioButtonDialogFullInstances(ManageInstancesActivity.this)); - aboutInstances = new ArrayList<>(); - aboutInstanceAdapter = new AboutInstanceAdapter(this.aboutInstances); - aboutInstanceAdapter.allInstancesRemoved = this; - binding.lvInstances.setAdapter(aboutInstanceAdapter); - LinearLayoutManager layoutManager - = new LinearLayoutManager(ManageInstancesActivity.this); - binding.lvInstances.setLayoutManager(layoutManager); - InfoInstanceVM viewModelInfoInstance = new ViewModelProvider(ManageInstancesActivity.this).get(InfoInstanceVM.class); - viewModelInfoInstance.getInstances().observe(ManageInstancesActivity.this, this::manageVIewInfoInstance); - } - @SuppressLint("ApplySharedPref") - private void showRadioButtonDialogFullInstances(Activity activity) { + public static void showRadioButtonDialogFullInstances(Activity activity, FragmentManager fragmentManager) { SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(activity); AlertDialog.Builder alt_bld = new MaterialAlertDialogBuilder(activity); alt_bld.setTitle(R.string.instance_choice); @@ -116,7 +93,7 @@ public class ManageInstancesActivity extends BaseBarActivity implements AboutIns WellKnownNodeinfo.NodeInfo instanceNodeInfo = new RetrofitPeertubeAPI(activity, newInstance, null).getNodeInfo(); if (instanceNodeInfo.getSoftware() != null && instanceNodeInfo.getSoftware().getName().trim().toLowerCase().compareTo("peertube") == 0) { SharedPreferences.Editor editor = sharedpreferences.edit(); - editor.putString(PREF_USER_INSTANCE, newInstance); + editor.putString(PREF_USER_INSTANCE_PEERTUBE_BROWSING, newInstance); editor.commit(); newInstance = newInstance.trim().toLowerCase(); InstanceData.AboutInstance aboutInstance = new RetrofitPeertubeAPI(activity, newInstance, null).getAboutInstance(); @@ -136,24 +113,49 @@ public class ManageInstancesActivity extends BaseBarActivity implements AboutIns }).start()); alt_bld.setNegativeButton(R.string.cancel, (dialog, which) -> dialog.dismiss()); alt_bld.setNeutralButton(R.string.help, (dialog, which) -> { - getSupportFragmentManager().setFragmentResultListener(PICK_INSTANCE, this, (requestKey, result) -> { + fragmentManager.setFragmentResultListener(PICK_INSTANCE, (LifecycleOwner) activity, (requestKey, result) -> { new Thread(() -> { String newInstance = result.getString(INSTANCE_ADDRESS); - InstanceData.AboutInstance aboutInstance = new RetrofitPeertubeAPI(ManageInstancesActivity.this, newInstance, null).getAboutInstance(); - SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); - new StoredInstanceDAO(ManageInstancesActivity.this, db).insertInstance(aboutInstance, newInstance); - runOnUiThread(() -> new Handler().post(() -> Helper.logoutNoRemoval(ManageInstancesActivity.this))); + InstanceData.AboutInstance aboutInstance = new RetrofitPeertubeAPI(activity, newInstance, null).getAboutInstance(); + SQLiteDatabase db = Sqlite.getInstance(activity, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); + new StoredInstanceDAO(activity, db).insertInstance(aboutInstance, newInstance); + activity.runOnUiThread(() -> new Handler().post(() -> Helper.logoutNoRemoval(activity))); }).start(); - getSupportFragmentManager().clearFragmentResultListener(PICK_INSTANCE); + fragmentManager.clearFragmentResultListener(PICK_INSTANCE); }); addFragment( - getSupportFragmentManager(), android.R.id.content, new FragmentLoginPickInstancePeertube(), + fragmentManager, android.R.id.content, new FragmentLoginPickInstancePeertube(), null, null, FragmentLoginPickInstancePeertube.class.getName()); }); AlertDialog alert = alt_bld.create(); alert.show(); } + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + binding = ActivityManageInstancesPeertubeBinding.inflate(getLayoutInflater()); + View view = binding.getRoot(); + setContentView(view); + if (getSupportActionBar() != null) { + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + getSupportActionBar().setDisplayShowHomeEnabled(true); + } + binding.loader.setVisibility(View.VISIBLE); + binding.noAction.setVisibility(View.GONE); + binding.lvInstances.setVisibility(View.GONE); + binding.actionButton.setOnClickListener(v -> showRadioButtonDialogFullInstances(ManageInstancesActivity.this, getSupportFragmentManager())); + aboutInstances = new ArrayList<>(); + aboutInstanceAdapter = new AboutInstanceAdapter(this.aboutInstances); + aboutInstanceAdapter.allInstancesRemoved = this; + binding.lvInstances.setAdapter(aboutInstanceAdapter); + LinearLayoutManager layoutManager + = new LinearLayoutManager(ManageInstancesActivity.this); + binding.lvInstances.setLayoutManager(layoutManager); + InfoInstanceVM viewModelInfoInstance = new ViewModelProvider(ManageInstancesActivity.this).get(InfoInstanceVM.class); + viewModelInfoInstance.getInstances().observe(ManageInstancesActivity.this, this::manageVIewInfoInstance); + } + private void manageVIewInfoInstance(List<InstanceData.AboutInstance> aboutInstances) { binding.loader.setVisibility(View.GONE); if (aboutInstances == null || aboutInstances.size() == 0) { diff --git a/app/src/main/java/app/fedilab/android/peertube/activities/PeertubeActivity.java b/app/src/main/java/app/fedilab/android/peertube/activities/PeertubeActivity.java index 3e29b1a0c..db886f890 100644 --- a/app/src/main/java/app/fedilab/android/peertube/activities/PeertubeActivity.java +++ b/app/src/main/java/app/fedilab/android/peertube/activities/PeertubeActivity.java @@ -127,7 +127,6 @@ import java.util.Objects; import java.util.regex.Matcher; import java.util.regex.Pattern; -import app.fedilab.android.BaseMainActivity; import app.fedilab.android.R; import app.fedilab.android.activities.BasePeertubeActivity; import app.fedilab.android.databinding.ActivityPeertubeBinding; @@ -591,7 +590,7 @@ public class PeertubeActivity extends BasePeertubeActivity implements CommentLis private void playVideo() { if (status == null && typeOfConnection == PeertubeMainActivity.TypeOfConnection.REMOTE_ACCOUNT) { app.fedilab.android.mastodon.viewmodel.mastodon.SearchVM searchVM = new ViewModelProvider(PeertubeActivity.this).get(app.fedilab.android.mastodon.viewmodel.mastodon.SearchVM.class); - searchVM.search(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, videoUuid, null, "statuses", false, true, false, 0, null, null, 1) + searchVM.search(HelperInstance.getLiveInstance(PeertubeActivity.this), HelperInstance.getToken(), videoUuid, null, "statuses", false, true, false, 0, null, null, 1) .observe(PeertubeActivity.this, results -> { if (results != null && results.statuses != null && results.statuses.size() > 0) { status = results.statuses.get(0); @@ -1041,7 +1040,7 @@ public class PeertubeActivity extends BasePeertubeActivity implements CommentLis } alt_bld.setPositiveButton(R.string.yes, (dialog, id) -> { if (status.reblogged) { - statusesVM.unReblog(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, status.id) + statusesVM.unReblog(HelperInstance.getLiveInstance(PeertubeActivity.this), HelperInstance.getToken(), status.id) .observe(PeertubeActivity.this, _status -> { if (_status != null) { status = _status; @@ -1050,7 +1049,7 @@ public class PeertubeActivity extends BasePeertubeActivity implements CommentLis }); } else { ((SparkButton) v).playAnimation(); - statusesVM.reblog(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, status.id, null) + statusesVM.reblog(HelperInstance.getLiveInstance(PeertubeActivity.this), HelperInstance.getToken(), status.id, null) .observe(PeertubeActivity.this, _status -> { if (_status != null) { status = _status; @@ -1065,7 +1064,7 @@ public class PeertubeActivity extends BasePeertubeActivity implements CommentLis alert.show(); } else { if (status.reblogged) { - statusesVM.unReblog(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, status.id) + statusesVM.unReblog(HelperInstance.getLiveInstance(PeertubeActivity.this), HelperInstance.getToken(), status.id) .observe(PeertubeActivity.this, _status -> { if (_status != null) { status = _status; @@ -1074,7 +1073,7 @@ public class PeertubeActivity extends BasePeertubeActivity implements CommentLis }); } else { ((SparkButton) v).playAnimation(); - statusesVM.reblog(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, status.id, null) + statusesVM.reblog(HelperInstance.getLiveInstance(PeertubeActivity.this), HelperInstance.getToken(), status.id, null) .observe(PeertubeActivity.this, _status -> { if (_status != null) { status = _status; @@ -1099,7 +1098,7 @@ public class PeertubeActivity extends BasePeertubeActivity implements CommentLis } alt_bld.setPositiveButton(R.string.yes, (dialog, id) -> { if (status.favourited) { - statusesVM.unFavourite(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, status.id) + statusesVM.unFavourite(HelperInstance.getLiveInstance(PeertubeActivity.this), HelperInstance.getToken(), status.id) .observe(PeertubeActivity.this, _status -> { if (_status != null) { status = _status; @@ -1108,7 +1107,7 @@ public class PeertubeActivity extends BasePeertubeActivity implements CommentLis }); } else { ((SparkButton) v).playAnimation(); - statusesVM.favourite(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, status.id) + statusesVM.favourite(HelperInstance.getLiveInstance(PeertubeActivity.this), HelperInstance.getToken(), status.id) .observe(PeertubeActivity.this, _status -> { if (_status != null) { status = _status; @@ -1123,7 +1122,7 @@ public class PeertubeActivity extends BasePeertubeActivity implements CommentLis alert.show(); } else { if (status.favourited) { - statusesVM.unFavourite(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, status.id) + statusesVM.unFavourite(HelperInstance.getLiveInstance(PeertubeActivity.this), HelperInstance.getToken(), status.id) .observe(PeertubeActivity.this, _status -> { if (_status != null) { status = _status; @@ -1132,7 +1131,7 @@ public class PeertubeActivity extends BasePeertubeActivity implements CommentLis }); } else { ((SparkButton) v).playAnimation(); - statusesVM.favourite(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, status.id) + statusesVM.favourite(HelperInstance.getLiveInstance(PeertubeActivity.this), HelperInstance.getToken(), status.id) .observe(PeertubeActivity.this, _status -> { if (_status != null) { status = _status; @@ -1148,7 +1147,7 @@ public class PeertubeActivity extends BasePeertubeActivity implements CommentLis if (status != null) { StatusesVM statusesVM = new ViewModelProvider(PeertubeActivity.this).get(StatusesVM.class); if (status.bookmarked) { - statusesVM.unBookmark(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, status.id) + statusesVM.unBookmark(HelperInstance.getLiveInstance(PeertubeActivity.this), HelperInstance.getToken(), status.id) .observe(PeertubeActivity.this, _status -> { if (_status != null) { status = _status; @@ -1157,7 +1156,7 @@ public class PeertubeActivity extends BasePeertubeActivity implements CommentLis }); } else { ((SparkButton) v).playAnimation(); - statusesVM.bookmark(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, status.id) + statusesVM.bookmark(HelperInstance.getLiveInstance(PeertubeActivity.this), HelperInstance.getToken(), status.id) .observe(PeertubeActivity.this, _status -> { if (_status != null) { status = _status; diff --git a/app/src/main/java/app/fedilab/android/peertube/activities/PeertubeMainActivity.java b/app/src/main/java/app/fedilab/android/peertube/activities/PeertubeMainActivity.java index 61e4c5afe..545b72f56 100644 --- a/app/src/main/java/app/fedilab/android/peertube/activities/PeertubeMainActivity.java +++ b/app/src/main/java/app/fedilab/android/peertube/activities/PeertubeMainActivity.java @@ -25,6 +25,7 @@ import static app.fedilab.android.BaseMainActivity.mamageNewIntent; import static app.fedilab.android.BaseMainActivity.manageDrawerMenu; import static app.fedilab.android.mastodon.helper.Helper.PREF_USER_ID; import static app.fedilab.android.mastodon.helper.Helper.PREF_USER_INSTANCE; +import static app.fedilab.android.mastodon.helper.Helper.PREF_USER_INSTANCE_PEERTUBE_BROWSING; import static app.fedilab.android.mastodon.helper.Helper.PREF_USER_SOFTWARE; import static app.fedilab.android.mastodon.helper.Helper.PREF_USER_TOKEN; import static app.fedilab.android.mastodon.helper.Helper.addFragment; @@ -34,11 +35,13 @@ import static app.fedilab.android.peertube.helper.SwitchAccountHelper.switchDial import android.annotation.SuppressLint; import android.content.Intent; import android.content.SharedPreferences; +import android.database.sqlite.SQLiteDatabase; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.Looper; +import android.util.Log; import android.util.TypedValue; import android.view.Menu; import android.view.MenuItem; @@ -96,7 +99,9 @@ import app.fedilab.android.peertube.fragment.FragmentLoginPickInstancePeertube; import app.fedilab.android.peertube.helper.Helper; import app.fedilab.android.peertube.helper.HelperInstance; import app.fedilab.android.peertube.services.RetrieveInfoService; +import app.fedilab.android.peertube.sqlite.StoredInstanceDAO; import app.fedilab.android.peertube.viewmodel.TimelineVM; +import app.fedilab.android.sqlite.Sqlite; public class PeertubeMainActivity extends PeertubeBaseMainActivity { @@ -165,13 +170,35 @@ public class PeertubeMainActivity extends PeertubeBaseMainActivity { if (extras != null && extras.containsKey(app.fedilab.android.mastodon.helper.Helper.ARG_PEERTUBE_NAV_REMOTE)) { if (extras.getBoolean(app.fedilab.android.mastodon.helper.Helper.ARG_PEERTUBE_NAV_REMOTE)) { typeOfConnection = PeertubeMainActivity.TypeOfConnection.REMOTE_ACCOUNT; + intentActvity.removeExtra(app.fedilab.android.mastodon.helper.Helper.ARG_PEERTUBE_NAV_REMOTE); } } } + SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(PeertubeMainActivity.this); + + if (typeOfConnection == TypeOfConnection.REMOTE_ACCOUNT) { + String defaultInstance = sharedpreferences.getString(PREF_USER_INSTANCE_PEERTUBE_BROWSING, null); + Log.v(app.fedilab.android.mastodon.helper.Helper.TAG, "defaultInstance-->: " + defaultInstance); + if (defaultInstance == null) { + getSupportFragmentManager().setFragmentResultListener(PICK_INSTANCE, PeertubeMainActivity.this, (requestKey, result) -> { + new Thread(() -> { + String newInstance = result.getString(INSTANCE_ADDRESS); + InstanceData.AboutInstance aboutInstance = new RetrofitPeertubeAPI(PeertubeMainActivity.this, newInstance, null).getAboutInstance(); + SQLiteDatabase db = Sqlite.getInstance(PeertubeMainActivity.this, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); + new StoredInstanceDAO(PeertubeMainActivity.this, db).insertInstance(aboutInstance, newInstance); + }).start(); + getSupportFragmentManager().clearFragmentResultListener(PICK_INSTANCE); + }); + addFragment( + getSupportFragmentManager(), android.R.id.content, new FragmentLoginPickInstancePeertube(), + null, null, FragmentLoginPickInstancePeertube.class.getName()); + } + return; + } + Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); - SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(PeertubeMainActivity.this); badgeCount = 0; headerMenuOpen = false; @@ -207,148 +234,153 @@ public class PeertubeMainActivity extends PeertubeBaseMainActivity { NavHeaderMainBinding headerMainBinding = NavHeaderMainBinding.inflate(getLayoutInflater()); currentAccount = null; - new Thread(() -> { - try { - if (currentToken == null) { - currentToken = sharedpreferences.getString(app.fedilab.android.mastodon.helper.Helper.PREF_USER_TOKEN, null); - } - currentAccount = new Account(PeertubeMainActivity.this).getConnectedAccount(); - if (currentAccount == null) { - if (currentUserID == null) { - currentUserID = sharedpreferences.getString(PREF_USER_ID, null); + if (Helper.isLoggedIn()) { + + new Thread(() -> { + try { + if (currentToken == null) { + currentToken = sharedpreferences.getString(app.fedilab.android.mastodon.helper.Helper.PREF_USER_TOKEN, null); } - if (currentInstance == null) { - currentInstance = sharedpreferences.getString(PREF_USER_INSTANCE, null); + currentAccount = new Account(PeertubeMainActivity.this).getConnectedAccount(); + if (currentAccount == null) { + if (currentUserID == null) { + currentUserID = sharedpreferences.getString(PREF_USER_ID, null); + } + if (currentInstance == null) { + currentInstance = sharedpreferences.getString(PREF_USER_INSTANCE, null); + } + currentAccount = new Account(PeertubeMainActivity.this).getUniqAccount(currentUserID, currentInstance); } - currentAccount = new Account(PeertubeMainActivity.this).getUniqAccount(currentUserID, currentInstance); - } - } catch (DBException e) { - e.printStackTrace(); - } - if (currentAccount != null && currentAccount.mastodon_account != null && typeOfConnection != TypeOfConnection.REMOTE_ACCOUNT) { - //It is a Mastodon User - Intent myIntent = new Intent(PeertubeMainActivity.this, MainActivity.class); - startActivity(myIntent); - finish(); - return; - } - //If the attached account is null, the app will fetch remote instance to get up-to-date values - if (currentAccount != null && currentAccount.peertube_account == null) { - try { - userMe = new RetrofitPeertubeAPI(PeertubeMainActivity.this, currentInstance, currentToken).verifyCredentials(); - currentAccount.peertube_account = userMe.getAccount(); - } catch (Error e) { + } catch (DBException e) { e.printStackTrace(); } - } - Handler mainHandler = new Handler(Looper.getMainLooper()); - Runnable myRunnable = () -> { - if (typeOfConnection == TypeOfConnection.REMOTE_ACCOUNT) { - headerMainBinding.accountAcc.setText(String.format("%s@%s", currentAccount.mastodon_account.username, currentAccount.instance)); - if (currentAccount.mastodon_account.display_name == null || currentAccount.mastodon_account.display_name.isEmpty()) { - currentAccount.mastodon_account.display_name = currentAccount.mastodon_account.acct; - } - headerMainBinding.accountName.setText(currentAccount.mastodon_account.display_name); - float scale = sharedpreferences.getFloat(getString(R.string.SET_FONT_SCALE), 1.1f); - headerMainBinding.accountName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18 * 1.1f / scale); - headerMainBinding.accountAcc.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18 * 1.1f / scale); - app.fedilab.android.mastodon.helper.Helper.loadPP(PeertubeMainActivity.this, headerMainBinding.accountProfilePicture, currentAccount, false); - headerMainBinding.backgroundImage.setAlpha(0.5f); - headerMainBinding.accountAcc.setOnClickListener(v -> headerMainBinding.changeAccount.callOnClick()); - headerMainBinding.changeAccount.setOnClickListener(v -> { - - headerMenuOpen = !headerMenuOpen; - manageDrawerMenu(PeertubeMainActivity.this, binding.drawerNavView, headerMainBinding); - }); - } else { - headerMainBinding.accountAcc.setText(String.format("%s@%s", currentAccount.peertube_account.getUsername(), currentAccount.instance)); - if (currentAccount.peertube_account.getDisplayName() == null || currentAccount.peertube_account.getDisplayName().isEmpty()) { - currentAccount.peertube_account.setDisplayName(currentAccount.peertube_account.getAcct()); + if (currentAccount != null && currentAccount.mastodon_account != null && typeOfConnection != TypeOfConnection.REMOTE_ACCOUNT) { + //It is a Mastodon User + Intent myIntent = new Intent(PeertubeMainActivity.this, MainActivity.class); + startActivity(myIntent); + finish(); + return; + } + //If the attached account is null, the app will fetch remote instance to get up-to-date values + if (currentAccount != null && currentAccount.peertube_account == null) { + try { + userMe = new RetrofitPeertubeAPI(PeertubeMainActivity.this, currentInstance, currentToken).verifyCredentials(); + currentAccount.peertube_account = userMe.getAccount(); + } catch (Error e) { + e.printStackTrace(); } - headerMainBinding.accountName.setText(currentAccount.peertube_account.getDisplayName()); - float scale = sharedpreferences.getFloat(getString(R.string.SET_FONT_SCALE), 1.1f); - headerMainBinding.accountName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18 * 1.1f / scale); - headerMainBinding.accountAcc.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18 * 1.1f / scale); - app.fedilab.android.mastodon.helper.Helper.loadPP(PeertubeMainActivity.this, headerMainBinding.accountProfilePicture, currentAccount, false); - headerMainBinding.backgroundImage.setAlpha(0.5f); - headerMainBinding.accountAcc.setOnClickListener(v -> headerMainBinding.changeAccount.callOnClick()); - headerMainBinding.changeAccount.setOnClickListener(v -> { - - headerMenuOpen = !headerMenuOpen; - manageDrawerMenu(PeertubeMainActivity.this, binding.drawerNavView, headerMainBinding); - }); } - - if (Helper.isLoggedIn()) { + Handler mainHandler = new Handler(Looper.getMainLooper()); + Runnable myRunnable = () -> { + if (typeOfConnection == TypeOfConnection.REMOTE_ACCOUNT) { + headerMainBinding.accountAcc.setText(String.format("%s@%s", currentAccount.mastodon_account.username, currentAccount.instance)); + if (currentAccount.mastodon_account.display_name == null || currentAccount.mastodon_account.display_name.isEmpty()) { + currentAccount.mastodon_account.display_name = currentAccount.mastodon_account.acct; + } + headerMainBinding.accountName.setText(currentAccount.mastodon_account.display_name); + float scale = sharedpreferences.getFloat(getString(R.string.SET_FONT_SCALE), 1.1f); + headerMainBinding.accountName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18 * 1.1f / scale); + headerMainBinding.accountAcc.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18 * 1.1f / scale); + app.fedilab.android.mastodon.helper.Helper.loadPP(PeertubeMainActivity.this, headerMainBinding.accountProfilePicture, currentAccount, false); + headerMainBinding.backgroundImage.setAlpha(0.5f); + headerMainBinding.accountAcc.setOnClickListener(v -> headerMainBinding.changeAccount.callOnClick()); + headerMainBinding.changeAccount.setOnClickListener(v -> { + + headerMenuOpen = !headerMenuOpen; + manageDrawerMenu(PeertubeMainActivity.this, binding.drawerNavView, headerMainBinding); + }); + } else { + headerMainBinding.accountAcc.setText(String.format("%s@%s", currentAccount.peertube_account.getUsername(), currentAccount.instance)); + if (currentAccount.peertube_account.getDisplayName() == null || currentAccount.peertube_account.getDisplayName().isEmpty()) { + currentAccount.peertube_account.setDisplayName(currentAccount.peertube_account.getAcct()); + } + headerMainBinding.accountName.setText(currentAccount.peertube_account.getDisplayName()); + float scale = sharedpreferences.getFloat(getString(R.string.SET_FONT_SCALE), 1.1f); + headerMainBinding.accountName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18 * 1.1f / scale); + headerMainBinding.accountAcc.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18 * 1.1f / scale); + app.fedilab.android.mastodon.helper.Helper.loadPP(PeertubeMainActivity.this, headerMainBinding.accountProfilePicture, currentAccount, false); + headerMainBinding.backgroundImage.setAlpha(0.5f); + headerMainBinding.accountAcc.setOnClickListener(v -> headerMainBinding.changeAccount.callOnClick()); + headerMainBinding.changeAccount.setOnClickListener(v -> { + + headerMenuOpen = !headerMenuOpen; + manageDrawerMenu(PeertubeMainActivity.this, binding.drawerNavView, headerMainBinding); + }); + } binding.navView.inflateMenu(R.menu.bottom_nav_menu_connected_peertube); refreshToken(); - } else { - binding.navView.inflateMenu(R.menu.bottom_nav_menu); - } - }; - mainHandler.post(myRunnable); - }).start(); + }; + mainHandler.post(myRunnable); + }).start(); + } else { + binding.navView.inflateMenu(R.menu.bottom_nav_menu_peertube); + } + headerMainBinding.instanceInfo.setVisibility(View.GONE); headerMainBinding.headerOptionInfo.setOnClickListener(v -> headerOptionInfoClick(PeertubeMainActivity.this, headerMainBinding, getSupportFragmentManager())); - binding.drawerNavView.addHeaderView(headerMainBinding.getRoot()); - - binding.drawerNavView.setNavigationItemSelectedListener(item -> { - if (item.getItemId() == R.id.action_settings) { - Intent intent = new Intent(PeertubeMainActivity.this, SettingsActivity.class); - startActivity(intent); - } else if (item.getItemId() == R.id.action_account) { - Intent intent; - if (typeOfConnection == TypeOfConnection.SURFING) { - switchDialog(PeertubeMainActivity.this, false); - } else { - if (Helper.canMakeAction()) { - intent = new Intent(PeertubeMainActivity.this, AccountActivity.class); - startActivity(intent); - overridePendingTransition(R.anim.slide_in_up, R.anim.slide_out_up); + if (Helper.isLoggedIn()) { + binding.drawerNavView.addHeaderView(headerMainBinding.getRoot()); + binding.drawerNavView.setNavigationItemSelectedListener(item -> { + if (item.getItemId() == R.id.action_settings) { + Intent intent = new Intent(PeertubeMainActivity.this, SettingsActivity.class); + startActivity(intent); + } else if (item.getItemId() == R.id.action_account) { + Intent intent; + if (typeOfConnection == TypeOfConnection.SURFING || typeOfConnection == TypeOfConnection.REMOTE_ACCOUNT) { + switchDialog(PeertubeMainActivity.this, false); } else { - intent = new Intent(PeertubeMainActivity.this, LoginActivity.class); - startActivity(intent); - } + if (Helper.canMakeAction()) { + intent = new Intent(PeertubeMainActivity.this, AccountActivity.class); + startActivity(intent); + overridePendingTransition(R.anim.slide_in_up, R.anim.slide_out_up); + } else { + |