diff --git a/app/src/main/java/app/fedilab/android/BaseMainActivity.java b/app/src/main/java/app/fedilab/android/BaseMainActivity.java index a86f778e..f622faf6 100644 --- a/app/src/main/java/app/fedilab/android/BaseMainActivity.java +++ b/app/src/main/java/app/fedilab/android/BaseMainActivity.java @@ -26,6 +26,7 @@ import static app.fedilab.android.mastodon.ui.drawer.StatusAdapter.sendAction; import android.Manifest; import android.annotation.SuppressLint; +import android.app.Activity; import android.app.SearchManager; import android.content.BroadcastReceiver; import android.content.Context; @@ -87,6 +88,7 @@ import com.bumptech.glide.request.target.CustomTarget; import com.bumptech.glide.request.target.Target; import com.bumptech.glide.request.transition.Transition; import com.google.android.material.dialog.MaterialAlertDialogBuilder; +import com.google.android.material.navigation.NavigationView; import com.google.android.material.snackbar.Snackbar; import com.google.android.material.tabs.TabLayout; @@ -314,7 +316,251 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt } }; private NetworkStateReceiver networkStateReceiver; - private boolean headerMenuOpen; + private static boolean headerMenuOpen; + + public static void fetchRecentAccounts(Activity activity, NavHeaderMainBinding headerMainBinding) { + SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(activity); + //Fetch some db values to initialize data + new Thread(() -> { + try { + if (currentAccount == null) { + if (currentToken == null || currentToken.trim().isEmpty()) { + currentToken = sharedpreferences.getString(Helper.PREF_USER_TOKEN, null); + } + try { + currentAccount = new Account(activity).getConnectedAccount(); + } catch (DBException e) { + e.printStackTrace(); + } + } + if (currentAccount != null) { + MutedAccounts mutedAccounts = new MutedAccounts(activity).getMutedAccount(currentAccount); + if (mutedAccounts != null && mutedAccounts.accounts != null) { + filteredAccounts = mutedAccounts.accounts; + } + } + //Delete cache older than 7 days + new StatusCache(activity).deleteForAllAccountAfter7Days(); + } catch (DBException e) { + e.printStackTrace(); + } + }).start(); + + //Fetch recent used accounts + new Thread(() -> { + try { + List accounts = new Account(activity).getLastUsedAccounts(); + Handler mainHandler = new Handler(Looper.getMainLooper()); + Runnable myRunnable = () -> { + if (accounts != null && accounts.size() > 0) { + Helper.loadPP(activity, headerMainBinding.otherAccount1, accounts.get(0)); + headerMainBinding.otherAccount1.setVisibility(View.VISIBLE); + headerMainBinding.otherAccount1.setOnClickListener(v -> { + headerMenuOpen = false; + String account = ""; + if (accounts.get(0).mastodon_account != null) { + account = "@" + accounts.get(0).mastodon_account.acct + "@" + accounts.get(0).instance; + } else if (accounts.get(0).peertube_account != null) { + account = "@" + accounts.get(0).peertube_account.getAcct() + "@" + accounts.get(0).instance; + } + 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; + BaseMainActivity.currentInstance = accounts.get(0).instance; + api = accounts.get(0).api; + SharedPreferences.Editor editor = sharedpreferences.edit(); + editor.putString(PREF_USER_ID, accounts.get(0).user_id); + editor.putString(PREF_USER_TOKEN, accounts.get(0).token); + editor.putString(PREF_USER_INSTANCE, accounts.get(0).instance); + editor.putString(PREF_USER_SOFTWARE, accounts.get(0).software); + 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(); + }); + if (accounts.size() > 1) { + Helper.loadPP(activity, headerMainBinding.otherAccount2, accounts.get(1)); + headerMainBinding.otherAccount2.setVisibility(View.VISIBLE); + headerMainBinding.otherAccount2.setOnClickListener(v -> { + headerMenuOpen = false; + String account = ""; + if (accounts.get(1).mastodon_account != null) { + account = "@" + accounts.get(1).mastodon_account.acct + "@" + accounts.get(1).instance; + } else if (accounts.get(1).peertube_account != null) { + account = "@" + accounts.get(1).peertube_account.getAcct() + "@" + accounts.get(1).instance; + } + Toasty.info(activity, activity.getString(R.string.toast_account_changed, account), Toasty.LENGTH_LONG).show(); + BaseMainActivity.currentToken = accounts.get(1).token; + BaseMainActivity.currentUserID = accounts.get(1).user_id; + BaseMainActivity.currentInstance = accounts.get(1).instance; + api = accounts.get(1).api; + SharedPreferences.Editor editor = sharedpreferences.edit(); + editor.putString(PREF_USER_ID, accounts.get(1).user_id); + editor.putString(PREF_USER_TOKEN, accounts.get(1).token); + editor.putString(PREF_USER_SOFTWARE, accounts.get(1).software); + editor.putString(PREF_USER_INSTANCE, accounts.get(1).instance); + 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(); + }); + } + } + }; + mainHandler.post(myRunnable); + + } catch (DBException e) { + e.printStackTrace(); + } + }).start(); + } + + public static void manageDrawerMenu(Activity activity, NavigationView navigationView, NavHeaderMainBinding headerMainBinding) { + SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(activity); + if (headerMenuOpen) { + headerMainBinding.ownerAccounts.setImageResource(R.drawable.ic_baseline_arrow_drop_up_24); + new Thread(() -> { + try { + List accounts = new Account(activity).getCrossAccounts(); + Handler mainHandler = new Handler(Looper.getMainLooper()); + Runnable myRunnable = () -> { + navigationView.getMenu().clear(); + navigationView.inflateMenu(R.menu.menu_accounts); + headerMenuOpen = true; + + Menu mainMenu = navigationView.getMenu(); + SubMenu currentSubmenu = null; + String lastInstance = ""; + if (accounts != null) { + for (final BaseAccount account : accounts) { + if (!currentToken.equalsIgnoreCase(account.token)) { + if (!lastInstance.trim().equalsIgnoreCase(account.instance.trim())) { + lastInstance = account.instance.toUpperCase(); + currentSubmenu = mainMenu.addSubMenu(account.instance.toUpperCase()); + } + if (currentSubmenu == null) { + continue; + } + String acct = ""; + String url = ""; + boolean disableGif = sharedpreferences.getBoolean(activity.getString(R.string.SET_DISABLE_GIF), false); + if (account.mastodon_account != null) { + acct = account.mastodon_account.acct; + url = !disableGif ? account.mastodon_account.avatar : account.mastodon_account.avatar_static; + if (url != null && url.startsWith("/")) { + url = "https://" + account.instance + account.mastodon_account.avatar; + } + } else if (account.peertube_account != null) { + acct = account.peertube_account.getAcct(); + url = account.peertube_account.getAvatar().getPath(); + if (url != null && url.startsWith("/")) { + url = "https://" + account.instance + account.peertube_account.getAvatar().getPath(); + } + } + + final MenuItem item = currentSubmenu.add("@" + acct); + item.setIcon(R.drawable.ic_person); + if (!activity.isDestroyed() && !activity.isFinishing() && url != null) { + if (url.contains(".gif")) { + Glide.with(activity) + .asGif() + .load(url) + .into(new CustomTarget() { + @Override + public void onResourceReady(@NonNull GifDrawable resource, Transition transition) { + item.setIcon(resource); + item.getIcon().setColorFilter(0xFFFFFFFF, PorterDuff.Mode.MULTIPLY); + } + + @Override + public void onLoadCleared(@Nullable Drawable placeholder) { + + } + }); + } else { + Glide.with(activity) + .asDrawable() + .load(url) + .into(new CustomTarget() { + @Override + public void onResourceReady(@NonNull Drawable resource, Transition transition) { + item.setIcon(resource); + item.getIcon().setColorFilter(0xFFFFFFFF, PorterDuff.Mode.MULTIPLY); + } + + @Override + public void onLoadCleared(@Nullable Drawable placeholder) { + + } + }); + } + + } + item.setOnMenuItemClickListener(item1 -> { + if (!activity.isFinishing()) { + headerMenuOpen = false; + String acctForAccount = ""; + if (account.mastodon_account != null) { + acctForAccount = "@" + account.mastodon_account.username + "@" + account.instance; + } else if (account.peertube_account != null) { + acctForAccount = "@" + account.peertube_account.getUsername() + "@" + account.instance; + } + Toasty.info(activity, activity.getString(R.string.toast_account_changed, acctForAccount), Toasty.LENGTH_LONG).show(); + BaseMainActivity.currentToken = account.token; + BaseMainActivity.currentUserID = account.user_id; + BaseMainActivity.currentInstance = account.instance; + api = account.api; + SharedPreferences.Editor editor = sharedpreferences.edit(); + editor.putString(PREF_USER_TOKEN, account.token); + editor.putString(PREF_USER_SOFTWARE, account.software); + editor.putString(PREF_USER_INSTANCE, account.instance); + editor.putString(PREF_USER_ID, account.user_id); + 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(); + headerMainBinding.ownerAccounts.setImageResource(R.drawable.ic_baseline_arrow_drop_down_24); + return true; + } + return false; + }); + + } + } + + } + currentSubmenu = mainMenu.addSubMenu(""); + MenuItem addItem = currentSubmenu.add(R.string.add_account); + addItem.setIcon(R.drawable.ic_baseline_person_add_24); + addItem.setOnMenuItemClickListener(item -> { + Intent intent = new Intent(activity, LoginActivity.class); + activity.startActivity(intent); + return true; + }); + + }; + mainHandler.post(myRunnable); + } catch (DBException e) { + e.printStackTrace(); + } + }).start(); + } else { + navigationView.getMenu().clear(); + if (MainActivity.currentAccount.mastodon_account != null) { + navigationView.inflateMenu(R.menu.activity_main_drawer); + } else if (MainActivity.currentAccount.peertube_account != null) { + navigationView.inflateMenu(R.menu.activity_main_drawer_peertube); + } + headerMainBinding.ownerAccounts.setImageResource(R.drawable.ic_baseline_arrow_drop_down_24); + headerMenuOpen = false; + } + } @Override protected void onCreate(Bundle savedInstanceState) { @@ -642,141 +888,7 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt headerMainBinding.accountAcc.setOnClickListener(v -> headerMainBinding.changeAccount.callOnClick()); headerMainBinding.changeAccount.setOnClickListener(v -> { headerMenuOpen = !headerMenuOpen; - if (headerMenuOpen) { - headerMainBinding.ownerAccounts.setImageResource(R.drawable.ic_baseline_arrow_drop_up_24); - new Thread(() -> { - try { - List accounts = new Account(BaseMainActivity.this).getCrossAccounts(); - Handler mainHandler = new Handler(Looper.getMainLooper()); - Runnable myRunnable = () -> { - binding.navView.getMenu().clear(); - binding.navView.inflateMenu(R.menu.menu_accounts); - headerMenuOpen = true; - - Menu mainMenu = binding.navView.getMenu(); - SubMenu currentSubmenu = null; - String lastInstance = ""; - if (accounts != null) { - for (final BaseAccount account : accounts) { - if (!currentToken.equalsIgnoreCase(account.token)) { - if (!lastInstance.trim().equalsIgnoreCase(account.instance.trim())) { - lastInstance = account.instance.toUpperCase(); - currentSubmenu = mainMenu.addSubMenu(account.instance.toUpperCase()); - } - if (currentSubmenu == null) { - continue; - } - String acct = ""; - String url = ""; - boolean disableGif = sharedpreferences.getBoolean(getString(R.string.SET_DISABLE_GIF), false); - if (account.mastodon_account != null) { - acct = account.mastodon_account.acct; - url = !disableGif ? account.mastodon_account.avatar : account.mastodon_account.avatar_static; - if (url != null && url.startsWith("/")) { - url = "https://" + account.instance + account.mastodon_account.avatar; - } - } else if (account.peertube_account != null) { - acct = account.peertube_account.getAcct(); - url = account.peertube_account.getAvatar().getPath(); - if (url != null && url.startsWith("/")) { - url = "https://" + account.instance + account.peertube_account.getAvatar().getPath(); - } - } - - final MenuItem item = currentSubmenu.add("@" + acct); - item.setIcon(R.drawable.ic_person); - if (!this.isDestroyed() && !this.isFinishing() && url != null) { - if (url.contains(".gif")) { - Glide.with(BaseMainActivity.this) - .asGif() - .load(url) - .into(new CustomTarget() { - @Override - public void onResourceReady(@NonNull GifDrawable resource, Transition transition) { - item.setIcon(resource); - item.getIcon().setColorFilter(0xFFFFFFFF, PorterDuff.Mode.MULTIPLY); - } - - @Override - public void onLoadCleared(@Nullable Drawable placeholder) { - - } - }); - } else { - Glide.with(BaseMainActivity.this) - .asDrawable() - .load(url) - .into(new CustomTarget() { - @Override - public void onResourceReady(@NonNull Drawable resource, Transition transition) { - item.setIcon(resource); - item.getIcon().setColorFilter(0xFFFFFFFF, PorterDuff.Mode.MULTIPLY); - } - - @Override - public void onLoadCleared(@Nullable Drawable placeholder) { - - } - }); - } - - } - item.setOnMenuItemClickListener(item1 -> { - if (!this.isFinishing()) { - headerMenuOpen = false; - String acctForAccount = ""; - if (account.mastodon_account != null) { - acctForAccount = "@" + account.mastodon_account.username + "@" + account.instance; - } else if (account.peertube_account != null) { - acctForAccount = "@" + account.peertube_account.getUsername() + "@" + account.instance; - } - Toasty.info(BaseMainActivity.this, getString(R.string.toast_account_changed, acctForAccount), Toasty.LENGTH_LONG).show(); - BaseMainActivity.currentToken = account.token; - BaseMainActivity.currentUserID = account.user_id; - BaseMainActivity.currentInstance = account.instance; - api = account.api; - SharedPreferences.Editor editor = sharedpreferences.edit(); - editor.putString(PREF_USER_TOKEN, account.token); - editor.putString(PREF_USER_SOFTWARE, account.software); - editor.putString(PREF_USER_INSTANCE, account.instance); - editor.putString(PREF_USER_ID, account.user_id); - 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(); - headerMainBinding.ownerAccounts.setImageResource(R.drawable.ic_baseline_arrow_drop_down_24); - return true; - } - return false; - }); - - } - } - - } - currentSubmenu = mainMenu.addSubMenu(""); - MenuItem addItem = currentSubmenu.add(R.string.add_account); - addItem.setIcon(R.drawable.ic_baseline_person_add_24); - addItem.setOnMenuItemClickListener(item -> { - Intent intent = new Intent(BaseMainActivity.this, LoginActivity.class); - startActivity(intent); - return true; - }); - - }; - mainHandler.post(myRunnable); - } catch (DBException e) { - e.printStackTrace(); - } - }).start(); - } else { - binding.navView.getMenu().clear(); - binding.navView.inflateMenu(R.menu.activity_main_drawer); - headerMainBinding.ownerAccounts.setImageResource(R.drawable.ic_baseline_arrow_drop_down_24); - headerMenuOpen = false; - } + manageDrawerMenu(BaseMainActivity.this, binding.navView, headerMainBinding); }); headerMainBinding.headerOptionInfo.setOnClickListener(v -> { @@ -929,103 +1041,7 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt } }).start(); } - //Fetch some db values to initialize data - new Thread(() -> { - try { - if (currentAccount == null) { - if (currentToken == null || currentToken.trim().isEmpty()) { - currentToken = sharedpreferences.getString(Helper.PREF_USER_TOKEN, null); - } - try { - currentAccount = new Account(BaseMainActivity.this).getConnectedAccount(); - } catch (DBException e) { - e.printStackTrace(); - } - } - if (currentAccount != null) { - MutedAccounts mutedAccounts = new MutedAccounts(BaseMainActivity.this).getMutedAccount(currentAccount); - if (mutedAccounts != null && mutedAccounts.accounts != null) { - filteredAccounts = mutedAccounts.accounts; - } - } - //Delete cache older than 7 days - new StatusCache(BaseMainActivity.this).deleteForAllAccountAfter7Days(); - } catch (DBException e) { - e.printStackTrace(); - } - }).start(); - - //Fetch recent used accounts - new Thread(() -> { - try { - List 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; - String account = ""; - if (accounts.get(0).mastodon_account != null) { - account = "@" + accounts.get(0).mastodon_account.acct + "@" + accounts.get(0).instance; - } else if (accounts.get(0).peertube_account != null) { - account = "@" + accounts.get(0).peertube_account.getAcct() + "@" + accounts.get(0).instance; - } - Toasty.info(BaseMainActivity.this, getString(R.string.toast_account_changed, account), Toasty.LENGTH_LONG).show(); - BaseMainActivity.currentToken = accounts.get(0).token; - BaseMainActivity.currentUserID = accounts.get(0).user_id; - BaseMainActivity.currentInstance = accounts.get(0).instance; - api = accounts.get(0).api; - SharedPreferences.Editor editor = sharedpreferences.edit(); - editor.putString(PREF_USER_ID, accounts.get(0).user_id); - editor.putString(PREF_USER_TOKEN, accounts.get(0).token); - editor.putString(PREF_USER_INSTANCE, accounts.get(0).instance); - editor.putString(PREF_USER_SOFTWARE, accounts.get(0).software); - 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; - String account = ""; - if (accounts.get(1).mastodon_account != null) { - account = "@" + accounts.get(1).mastodon_account.acct + "@" + accounts.get(1).instance; - } else if (accounts.get(1).peertube_account != null) { - account = "@" + accounts.get(1).peertube_account.getAcct() + "@" + accounts.get(1).instance; - } - Toasty.info(BaseMainActivity.this, getString(R.string.toast_account_changed, account), Toasty.LENGTH_LONG).show(); - BaseMainActivity.currentToken = accounts.get(1).token; - BaseMainActivity.currentUserID = accounts.get(1).user_id; - BaseMainActivity.currentInstance = accounts.get(1).instance; - api = accounts.get(1).api; - SharedPreferences.Editor editor = sharedpreferences.edit(); - editor.putString(PREF_USER_ID, accounts.get(1).user_id); - editor.putString(PREF_USER_TOKEN, accounts.get(1).token); - editor.putString(PREF_USER_SOFTWARE, accounts.get(1).software); - editor.putString(PREF_USER_INSTANCE, accounts.get(1).instance); - 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(); + fetchRecentAccounts(BaseMainActivity.this, headerMainBinding); } protected abstract void rateThisApp(); 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 318dda55..f0ad7c08 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 @@ -14,6 +14,11 @@ package app.fedilab.android.peertube.activities; * You should have received a copy of the GNU General Public License along with Fedilab; if not, * see . */ +import static app.fedilab.android.BaseMainActivity.currentAccount; +import static app.fedilab.android.BaseMainActivity.currentInstance; +import static app.fedilab.android.BaseMainActivity.currentToken; +import static app.fedilab.android.BaseMainActivity.fetchRecentAccounts; +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_SOFTWARE; @@ -30,6 +35,8 @@ import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.os.Handler; +import android.os.Looper; +import android.util.TypedValue; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -43,6 +50,7 @@ import androidx.appcompat.app.AlertDialog; import androidx.appcompat.widget.SearchView; import androidx.appcompat.widget.Toolbar; import androidx.appcompat.widget.TooltipCompat; +import androidx.core.view.GravityCompat; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentStatePagerAdapter; @@ -68,6 +76,7 @@ import app.fedilab.android.R; import app.fedilab.android.activities.AboutActivity; import app.fedilab.android.activities.PeertubeBaseMainActivity; import app.fedilab.android.databinding.ActivityMainPeertubeBinding; +import app.fedilab.android.databinding.NavHeaderMainBinding; 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; @@ -84,7 +93,6 @@ import app.fedilab.android.peertube.client.entities.WellKnownNodeinfo; import app.fedilab.android.peertube.fragment.DisplayOverviewFragment; import app.fedilab.android.peertube.fragment.DisplayVideosFragment; import app.fedilab.android.peertube.helper.Helper; -import app.fedilab.android.peertube.helper.HelperAcadInstance; import app.fedilab.android.peertube.helper.HelperInstance; import app.fedilab.android.peertube.services.RetrieveInfoService; import app.fedilab.android.peertube.sqlite.StoredInstanceDAO; @@ -93,6 +101,7 @@ import app.fedilab.android.sqlite.Sqlite; import es.dmoral.toasty.Toasty; + public class PeertubeMainActivity extends PeertubeBaseMainActivity { @@ -105,6 +114,7 @@ public class PeertubeMainActivity extends PeertubeBaseMainActivity { private DisplayVideosFragment recentFragment, locaFragment, trendingFragment, subscriptionFragment, mostLikedFragment; private DisplayOverviewFragment overviewFragment; private ActivityMainPeertubeBinding binding; + private static boolean headerMenuOpen; private final BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = item -> { @@ -218,9 +228,10 @@ public class PeertubeMainActivity extends PeertubeBaseMainActivity { Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); + SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(PeertubeMainActivity.this); badgeCount = 0; - + headerMenuOpen = false; binding.navView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener); if (getSupportActionBar() != null) { @@ -251,6 +262,106 @@ public class PeertubeMainActivity extends PeertubeBaseMainActivity { bundle.putSerializable(Helper.TIMELINE_TYPE, TimelineVM.TimelineType.MOST_LIKED); mostLikedFragment.setArguments(bundle); + 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(); + } catch (DBException e) { + e.printStackTrace(); + } + + //If the attached account is null, the app will fetch remote instance to get up-to-date values + if (currentAccount != null && currentAccount.mastodon_account == null && currentAccount.peertube_account == null) { + try { + userMe = new RetrofitPeertubeAPI(PeertubeMainActivity.this, currentInstance, currentToken).verifyCredentials(); + currentAccount.peertube_account = userMe.getAccount(); + } catch (Error e) { + e.printStackTrace(); + } + } + Handler mainHandler = new Handler(Looper.getMainLooper()); + Runnable myRunnable = () -> { + 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); + + }; + mainHandler.post(myRunnable); + }).start(); + headerMainBinding.instanceInfo.setVisibility(View.GONE); + headerMainBinding.headerOptionInfo.setVisibility(View.GONE); + binding.drawerNavView.addHeaderView(headerMainBinding.getRoot()); + headerMainBinding.accountAcc.setOnClickListener(v -> headerMainBinding.changeAccount.callOnClick()); + headerMainBinding.changeAccount.setOnClickListener(v -> { + headerMenuOpen = !headerMenuOpen; + manageDrawerMenu(PeertubeMainActivity.this, binding.drawerNavView, headerMainBinding); + }); + 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.isLoggedIn()) { + intent = new Intent(PeertubeMainActivity.this, AccountActivity.class); + startActivity(intent); + overridePendingTransition(R.anim.slide_in_up, R.anim.slide_out_up); + } else { + intent = new Intent(PeertubeMainActivity.this, LoginActivity.class); + startActivity(intent); + } + + } + } else if (item.getItemId() == R.id.action_upload) { + Intent intent = new Intent(PeertubeMainActivity.this, PeertubeUploadActivity.class); + startActivity(intent); + } else if (item.getItemId() == R.id.action_myvideos) { + Intent intent = new Intent(PeertubeMainActivity.this, VideosTimelineActivity.class); + Bundle bundledrawer = new Bundle(); + bundledrawer.putSerializable("type", TimelineVM.TimelineType.MY_VIDEOS); + intent.putExtras(bundledrawer); + startActivity(intent); + } else if (item.getItemId() == R.id.action_history) { + Intent intent = new Intent(PeertubeMainActivity.this, VideosTimelineActivity.class); + Bundle bundledrawer = new Bundle(); + bundledrawer.putSerializable("type", TimelineVM.TimelineType.HISTORY); + intent.putExtras(bundledrawer); + startActivity(intent); + } else if (item.getItemId() == R.id.action_most_liked) { + Intent intent = new Intent(PeertubeMainActivity.this, VideosTimelineActivity.class); + Bundle bundledrawer = new Bundle(); + bundledrawer.putSerializable("type", TimelineVM.TimelineType.MOST_LIKED); + intent.putExtras(bundledrawer); + startActivity(intent); + } else if (item.getItemId() == R.id.action_playlist) { + Intent intent; + intent = new Intent(PeertubeMainActivity.this, AllPlaylistsActivity.class); + startActivity(intent); + } else if (item.getItemId() == R.id.action_sepia_search) { + Intent intent = new Intent(PeertubeMainActivity.this, SepiaSearchActivity.class); + startActivity(intent); + } else if (item.getItemId() == R.id.action_about) { + Intent intent = new Intent(PeertubeMainActivity.this, AboutActivity.class); + startActivity(intent); + } + binding.drawerLayout.close(); + return false; + }); + overviewFragment = new DisplayOverviewFragment(); if (!Helper.isLoggedIn()) { PagerAdapter mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager()); @@ -298,7 +409,7 @@ public class PeertubeMainActivity extends PeertubeBaseMainActivity { } }); - setTitleCustom(R.string.title_discover); + setTitleCustom(R.string.title_home); if (Helper.isLoggedIn()) { binding.navView.inflateMenu(R.menu.bottom_nav_menu_connected_peertube); @@ -322,8 +433,6 @@ public class PeertubeMainActivity extends PeertubeBaseMainActivity { RateThisApp.showRateDialogIfNeeded(this); } - - final SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(PeertubeMainActivity.this); boolean search_cast = sharedpreferences.getBoolean(getString(R.string.set_cast_choice), false); if (search_cast) { super.discoverCast(); @@ -334,6 +443,8 @@ public class PeertubeMainActivity extends PeertubeBaseMainActivity { Intent intent = new Intent(PeertubeMainActivity.this, InstancePickerActivity.class); startActivityForResult(intent, PICK_INSTANCE); } + + fetchRecentAccounts(PeertubeMainActivity.this, headerMainBinding); } public DisplayVideosFragment getSubscriptionFragment() { @@ -421,7 +532,28 @@ public class PeertubeMainActivity extends PeertubeBaseMainActivity { BaseAccount finalAccount1 = account; runOnUiThread(() -> { app.fedilab.android.mastodon.helper.Helper.loadPP(this, binding.profilePicture, finalAccount1); - binding.profilePicture.setOnClickListener(v -> switchDialog(PeertubeMainActivity.this, false)); + MenuItem accountItem = binding.drawerNavView.getMenu().findItem(R.id.action_account); + FrameLayout rootView = (FrameLayout) accountItem.getActionView(); + FrameLayout redCircle = rootView.findViewById(R.id.view_alert_red_circle); + TextView countTextView = rootView.findViewById(R.id.view_alert_count_textview); + //change counter for notifications + if (badgeCount > 0) { + countTextView.setText(String.valueOf(badgeCount)); + redCircle.setVisibility(View.VISIBLE); + } else { + redCircle.setVisibility(View.GONE); + } + TooltipCompat.setTooltipText(accountItem.getActionView(), getText(R.string.account)); + + switch (typeOfConnection) { + case NORMAL: + accountItem.setVisible(true); + break; + case SURFING: + accountItem.setVisible(false); + break; + } + binding.profilePicture.setOnClickListener(v -> binding.drawerLayout.openDrawer(GravityCompat.START)); }); editor.putString(PREF_USER_ID, account.user_id); editor.putBoolean(getString(R.string.set_autoplay_choice), userMe.isAutoPlayVideo()); @@ -469,7 +601,26 @@ public class PeertubeMainActivity extends PeertubeBaseMainActivity { public boolean onCreateOptionsMenu(@NotNull Menu menu) { getMenuInflater().inflate(R.menu.main_menu_peertube, menu); + + MenuItem incognitoItem = menu.findItem(R.id.action_incognito); + switch (typeOfConnection) { + case NORMAL: + if (Helper.isLoggedIn()) { + incognitoItem.setVisible(true); + final SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(PeertubeMainActivity.this); + boolean checked = sharedpreferences.getBoolean(getString(R.string.set_store_in_history), true); + incognitoItem.setChecked(checked); + } else { + incognitoItem.setVisible(false); + } + break; + case SURFING: + incognitoItem.setVisible(false); + break; + } + MenuItem myActionMenuItem = menu.findItem(R.id.action_search); + SearchView searchView = (SearchView) myActionMenuItem.getActionView(); searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override @@ -501,138 +652,21 @@ public class PeertubeMainActivity extends PeertubeBaseMainActivity { return false; } }); - - MenuItem uploadItem = menu.findItem(R.id.action_upload); - MenuItem myVideosItem = menu.findItem(R.id.action_myvideos); - MenuItem playslistItem = menu.findItem(R.id.action_playlist); - MenuItem historyItem = menu.findItem(R.id.action_history); - MenuItem mostLikedItem = menu.findItem(R.id.action_most_liked); - MenuItem settingsItem = menu.findItem(R.id.action_settings); - MenuItem sepiaSearchItem = menu.findItem(R.id.action_sepia_search); - MenuItem incognitoItem = menu.findItem(R.id.action_incognito); - MenuItem accountItem = menu.findItem(R.id.action_account); - MenuItem changeInstanceItem = menu.findItem(R.id.action_change_instance); - - FrameLayout rootView = (FrameLayout) accountItem.getActionView(); - - FrameLayout redCircle = rootView.findViewById(R.id.view_alert_red_circle); - TextView countTextView = rootView.findViewById(R.id.view_alert_count_textview); - //change counter for notifications - if (badgeCount > 0) { - countTextView.setText(String.valueOf(badgeCount)); - redCircle.setVisibility(View.VISIBLE); - } else { - redCircle.setVisibility(View.GONE); - } - TooltipCompat.setTooltipText(accountItem.getActionView(), getText(R.string.account)); - - switch (typeOfConnection) { - case NORMAL: - accountItem.setVisible(true); - if (Helper.isLoggedIn()) { - uploadItem.setVisible(true); - myVideosItem.setVisible(true); - playslistItem.setVisible(true); - historyItem.setVisible(true); - settingsItem.setVisible(false); - mostLikedItem.setVisible(true); - incognitoItem.setVisible(true); - final SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(PeertubeMainActivity.this); - boolean checked = sharedpreferences.getBoolean(getString(R.string.set_store_in_history), true); - incognitoItem.setChecked(checked); - } else { - uploadItem.setVisible(false); - myVideosItem.setVisible(false); - playslistItem.setVisible(false); - historyItem.setVisible(false); - settingsItem.setVisible(true); - mostLikedItem.setVisible(true); - incognitoItem.setVisible(false); - } - break; - case SURFING: - accountItem.setVisible(true); - uploadItem.setVisible(false); - myVideosItem.setVisible(false); - playslistItem.setVisible(false); - historyItem.setVisible(false); - settingsItem.setVisible(false); - mostLikedItem.setVisible(false); - incognitoItem.setVisible(false); - break; - } - return true; } @Override public boolean onPrepareOptionsMenu(Menu menu) { - final MenuItem accountItem = menu.findItem(R.id.action_account); - FrameLayout rootView = (FrameLayout) accountItem.getActionView(); - rootView.setOnClickListener(v -> onOptionsItemSelected(accountItem)); return super.onPrepareOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { - String type = null; - String action = "TIMELINE"; if (item.getItemId() == R.id.action_change_instance) { Intent intent = new Intent(PeertubeMainActivity.this, ManageInstancesActivity.class); startActivity(intent); overridePendingTransition(R.anim.slide_in_up, R.anim.slide_out_up); - action = "CHANGE_INSTANCE"; - type = ""; - } else 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.isLoggedIn()) { - intent = new Intent(PeertubeMainActivity.this, AccountActivity.class); - startActivity(intent); - overridePendingTransition(R.anim.slide_in_up, R.anim.slide_out_up); - } else { - intent = new Intent(PeertubeMainActivity.this, LoginActivity.class); - startActivity(intent); - } - - } - } else if (item.getItemId() == R.id.action_upload) { - Intent intent = new Intent(PeertubeMainActivity.this, PeertubeUploadActivity.class); - startActivity(intent); - } else if (item.getItemId() == R.id.action_myvideos) { - Intent intent = new Intent(PeertubeMainActivity.this, VideosTimelineActivity.class); - Bundle bundle = new Bundle(); - bundle.putSerializable("type", TimelineVM.TimelineType.MY_VIDEOS); - intent.putExtras(bundle); - startActivity(intent); - type = HelperAcadInstance.MYVIDEOS; - } else if (item.getItemId() == R.id.action_history) { - Intent intent = new Intent(PeertubeMainActivity.this, VideosTimelineActivity.class); - Bundle bundle = new Bundle(); - bundle.putSerializable("type", TimelineVM.TimelineType.HISTORY); - intent.putExtras(bundle); - startActivity(intent); - type = HelperAcadInstance.HISTORY; - } else if (item.getItemId() == R.id.action_most_liked) { - Intent intent = new Intent(PeertubeMainActivity.this, VideosTimelineActivity.class); - Bundle bundle = new Bundle(); - bundle.putSerializable("type", TimelineVM.TimelineType.MOST_LIKED); - intent.putExtras(bundle); - startActivity(intent); - type = HelperAcadInstance.MOSTLIKED; - } else if (item.getItemId() == R.id.action_playlist) { - Intent intent; - intent = new Intent(PeertubeMainActivity.this, AllPlaylistsActivity.class); - startActivity(intent); - } else if (item.getItemId() == R.id.action_sepia_search) { - Intent intent = new Intent(PeertubeMainActivity.this, SepiaSearchActivity.class); - startActivity(intent); } else if (item.getItemId() == R.id.action_about) { Intent intent = new Intent(PeertubeMainActivity.this, AboutActivity.class); startActivity(intent); @@ -706,6 +740,17 @@ public class PeertubeMainActivity extends PeertubeBaseMainActivity { alert.show(); } + + @Override + public void onBackPressed() { + if (binding.drawerLayout.isDrawerOpen(GravityCompat.START)) { + binding.drawerLayout.closeDrawer(GravityCompat.START); + } else { + super.onBackPressed(); + } + } + + @SuppressLint("ApplySharedPref") @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { diff --git a/app/src/main/res/layouts/peertube/layout/activity_main_peertube.xml b/app/src/main/res/layouts/peertube/layout/activity_main_peertube.xml index eaaf1108..971a87fc 100644 --- a/app/src/main/res/layouts/peertube/layout/activity_main_peertube.xml +++ b/app/src/main/res/layouts/peertube/layout/activity_main_peertube.xml @@ -54,6 +54,7 @@ android:contentDescription="@string/select_accounts" /> + \ No newline at end of file diff --git a/app/src/main/res/layouts/peertube/layout/counter_account_icon_peertube.xml b/app/src/main/res/layouts/peertube/layout/counter_account_icon_peertube.xml index f2d16927..741ec31b 100644 --- a/app/src/main/res/layouts/peertube/layout/counter_account_icon_peertube.xml +++ b/app/src/main/res/layouts/peertube/layout/counter_account_icon_peertube.xml @@ -1,35 +1,19 @@ + android:id="@+id/view_alert_red_circle" + android:layout_width="14dp" + android:layout_height="14dp" + android:layout_gravity="center_vertical|end" + android:background="@drawable/circle_red" + android:visibility="gone" + tools:visibility="visible"> - - - - - - - - + android:textColor="@color/white" + tools:text="3" /> \ No newline at end of file diff --git a/app/src/main/res/menus/peertube/menu/activity_main_drawer_peertube.xml b/app/src/main/res/menus/peertube/menu/activity_main_drawer_peertube.xml new file mode 100644 index 00000000..9f7d9f85 --- /dev/null +++ b/app/src/main/res/menus/peertube/menu/activity_main_drawer_peertube.xml @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/menus/peertube/menu/main_menu_peertube.xml b/app/src/main/res/menus/peertube/menu/main_menu_peertube.xml index 2bb07aac..8ff6abed 100644 --- a/app/src/main/res/menus/peertube/menu/main_menu_peertube.xml +++ b/app/src/main/res/menus/peertube/menu/main_menu_peertube.xml @@ -1,69 +1,28 @@ - - - - - - - - - - + android:visible="false" + app:showAsAction="never" /> + app:showAsAction="never" /> + app:showAsAction="never" />