2020-09-03 19:08:53 +02:00
|
|
|
package app.fedilab.fedilabtube;
|
|
|
|
/* Copyright 2020 Thomas Schneider
|
|
|
|
*
|
|
|
|
* This file is a part of TubeLab
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* TubeLab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with TubeLab; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.text.SpannableString;
|
|
|
|
import android.text.Spanned;
|
|
|
|
import android.text.style.ForegroundColorSpan;
|
|
|
|
import android.text.style.UnderlineSpan;
|
2020-09-18 18:10:04 +02:00
|
|
|
import android.view.Menu;
|
2020-09-03 19:08:53 +02:00
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import androidx.appcompat.app.AlertDialog;
|
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
|
|
import androidx.core.content.ContextCompat;
|
|
|
|
import androidx.fragment.app.Fragment;
|
|
|
|
import androidx.fragment.app.FragmentManager;
|
|
|
|
import androidx.fragment.app.FragmentStatePagerAdapter;
|
|
|
|
import androidx.viewpager.widget.PagerAdapter;
|
|
|
|
import androidx.viewpager.widget.ViewPager;
|
|
|
|
|
|
|
|
import com.google.android.material.tabs.TabLayout;
|
|
|
|
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
2020-09-25 18:58:04 +02:00
|
|
|
import app.fedilab.fedilabtube.client.RetrofitPeertubeAPI;
|
|
|
|
import app.fedilab.fedilabtube.client.data.AccountData.Account;
|
2020-09-03 19:08:53 +02:00
|
|
|
import app.fedilab.fedilabtube.fragment.DisplayAccountsFragment;
|
2020-09-29 17:42:15 +02:00
|
|
|
import app.fedilab.fedilabtube.fragment.DisplayChannelsFragment;
|
2020-09-03 19:08:53 +02:00
|
|
|
import app.fedilab.fedilabtube.fragment.DisplayNotificationsFragment;
|
|
|
|
import app.fedilab.fedilabtube.helper.Helper;
|
2020-11-10 18:44:48 +01:00
|
|
|
import app.fedilab.fedilabtube.helper.SwitchAccountHelper;
|
2020-09-03 19:08:53 +02:00
|
|
|
import app.fedilab.fedilabtube.sqlite.AccountDAO;
|
|
|
|
import app.fedilab.fedilabtube.sqlite.Sqlite;
|
|
|
|
|
|
|
|
|
|
|
|
public class AccountActivity extends AppCompatActivity {
|
|
|
|
|
|
|
|
|
|
|
|
private ViewPager mPager;
|
|
|
|
private TabLayout tabLayout;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
|
|
|
|
|
|
setContentView(R.layout.activity_account);
|
|
|
|
if (getSupportActionBar() != null)
|
|
|
|
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
|
|
|
|
|
|
|
|
|
|
|
SpannableString content_create = new SpannableString(getString(R.string.join_peertube));
|
|
|
|
content_create.setSpan(new UnderlineSpan(), 0, content_create.length(), 0);
|
2020-09-14 15:53:31 +02:00
|
|
|
content_create.setSpan(new ForegroundColorSpan(ContextCompat.getColor(AccountActivity.this, Helper.getColorAccent())), 0, content_create.length(),
|
2020-09-03 19:08:53 +02:00
|
|
|
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
|
|
|
|
|
|
|
|
|
|
|
|
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
|
|
|
|
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
2020-10-01 17:42:03 +02:00
|
|
|
String token = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null);
|
2020-09-03 19:08:53 +02:00
|
|
|
|
|
|
|
TextView instanceView = findViewById(R.id.instance);
|
2020-10-01 17:42:03 +02:00
|
|
|
Account account = new AccountDAO(AccountActivity.this, db).getAccountByToken(token);
|
2020-09-03 19:08:53 +02:00
|
|
|
|
|
|
|
if (account == null) {
|
|
|
|
Helper.logoutCurrentUser(AccountActivity.this, null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImageView profile_picture = findViewById(R.id.profile_picture);
|
|
|
|
TextView username = findViewById(R.id.username);
|
|
|
|
TextView displayname = findViewById(R.id.displayname);
|
|
|
|
|
2020-09-27 16:33:43 +02:00
|
|
|
setTitle(String.format("@%s", account.getUsername()));
|
2020-09-03 19:08:53 +02:00
|
|
|
|
2020-09-25 18:58:04 +02:00
|
|
|
Helper.loadGiF(AccountActivity.this, account.getAvatar().getPath(), profile_picture);
|
2020-09-27 16:33:43 +02:00
|
|
|
username.setText(String.format("@%s", account.getUsername()));
|
2020-09-25 18:58:04 +02:00
|
|
|
displayname.setText(account.getDisplayName());
|
2020-09-03 19:08:53 +02:00
|
|
|
|
2020-09-25 18:58:04 +02:00
|
|
|
instanceView.setText(account.getHost());
|
2020-09-03 19:08:53 +02:00
|
|
|
|
|
|
|
Button logout_button = findViewById(R.id.logout_button);
|
|
|
|
logout_button.setOnClickListener(v -> {
|
|
|
|
AlertDialog.Builder dialogBuilderLogoutAccount = new AlertDialog.Builder(AccountActivity.this);
|
2020-10-02 19:29:00 +02:00
|
|
|
dialogBuilderLogoutAccount.setMessage(getString(R.string.logout_account_confirmation, account.getUsername(), account.getHost()));
|
2020-09-03 19:08:53 +02:00
|
|
|
dialogBuilderLogoutAccount.setPositiveButton(R.string.action_logout, (dialog, id) -> {
|
2020-10-02 19:29:00 +02:00
|
|
|
Helper.logoutCurrentUser(AccountActivity.this, account);
|
2020-09-03 19:08:53 +02:00
|
|
|
dialog.dismiss();
|
|
|
|
});
|
|
|
|
dialogBuilderLogoutAccount.setNegativeButton(R.string.cancel, (dialog, id) -> dialog.dismiss());
|
|
|
|
AlertDialog alertDialogLogoutAccount = dialogBuilderLogoutAccount.create();
|
|
|
|
alertDialogLogoutAccount.show();
|
|
|
|
});
|
|
|
|
|
|
|
|
Button settings = findViewById(R.id.settings);
|
|
|
|
settings.setOnClickListener(v -> {
|
|
|
|
Intent intent = new Intent(AccountActivity.this, SettingsActivity.class);
|
|
|
|
startActivity(intent);
|
|
|
|
});
|
|
|
|
|
|
|
|
tabLayout = findViewById(R.id.account_tabLayout);
|
|
|
|
mPager = findViewById(R.id.account_viewpager);
|
|
|
|
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.title_notifications)));
|
|
|
|
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.title_muted)));
|
|
|
|
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.title_channel)));
|
|
|
|
|
|
|
|
|
|
|
|
mPager.setOffscreenPageLimit(3);
|
|
|
|
|
|
|
|
|
|
|
|
mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
|
|
|
|
@Override
|
|
|
|
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPageSelected(int position) {
|
|
|
|
TabLayout.Tab tab = tabLayout.getTabAt(position);
|
|
|
|
if (tab != null)
|
|
|
|
tab.select();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPageScrollStateChanged(int state) {
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
|
|
|
|
@Override
|
|
|
|
public void onTabSelected(TabLayout.Tab tab) {
|
|
|
|
mPager.setCurrentItem(tab.getPosition());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onTabUnselected(TabLayout.Tab tab) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onTabReselected(TabLayout.Tab tab) {
|
|
|
|
Fragment fragment = null;
|
|
|
|
if (mPager.getAdapter() != null)
|
|
|
|
fragment = (Fragment) mPager.getAdapter().instantiateItem(mPager, tab.getPosition());
|
|
|
|
switch (tab.getPosition()) {
|
|
|
|
case 0:
|
|
|
|
if (fragment != null) {
|
|
|
|
DisplayNotificationsFragment displayNotificationsFragment = ((DisplayNotificationsFragment) fragment);
|
|
|
|
displayNotificationsFragment.scrollToTop();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if (fragment != null) {
|
|
|
|
DisplayAccountsFragment displayAccountsFragment = ((DisplayAccountsFragment) fragment);
|
|
|
|
displayAccountsFragment.scrollToTop();
|
|
|
|
}
|
|
|
|
break;
|
2020-09-29 17:42:15 +02:00
|
|
|
case 2:
|
|
|
|
if (fragment != null) {
|
|
|
|
DisplayChannelsFragment displayChannelsFragment = ((DisplayChannelsFragment) fragment);
|
|
|
|
displayChannelsFragment.scrollToTop();
|
|
|
|
}
|
|
|
|
break;
|
2020-09-03 19:08:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
PagerAdapter mPagerAdapter = new AccountsPagerAdapter(getSupportFragmentManager());
|
|
|
|
mPager.setAdapter(mPagerAdapter);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
}
|
|
|
|
|
2020-09-18 18:10:04 +02:00
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(@NotNull Menu menu) {
|
|
|
|
getMenuInflater().inflate(R.menu.main_profile, menu);
|
|
|
|
return true;
|
|
|
|
}
|
2020-09-03 19:08:53 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
if (item.getItemId() == android.R.id.home) {
|
|
|
|
finish();
|
|
|
|
return true;
|
2020-09-18 18:10:04 +02:00
|
|
|
} else if (item.getItemId() == R.id.action_add_account) {
|
2020-11-10 18:44:48 +01:00
|
|
|
SwitchAccountHelper.switchDialog(AccountActivity.this, true);
|
2020-09-03 19:08:53 +02:00
|
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pager adapter for three tabs (notifications, muted, blocked)
|
|
|
|
*/
|
2020-10-02 19:29:00 +02:00
|
|
|
private static class AccountsPagerAdapter extends FragmentStatePagerAdapter {
|
2020-09-03 19:08:53 +02:00
|
|
|
|
|
|
|
AccountsPagerAdapter(FragmentManager fm) {
|
|
|
|
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
@NotNull
|
|
|
|
@Override
|
|
|
|
public Fragment getItem(int position) {
|
|
|
|
Bundle bundle = new Bundle();
|
|
|
|
switch (position) {
|
|
|
|
case 1:
|
|
|
|
DisplayAccountsFragment displayAccountsFragment = new DisplayAccountsFragment();
|
2020-09-29 17:42:15 +02:00
|
|
|
bundle.putSerializable("accountFetch", RetrofitPeertubeAPI.DataType.MUTED);
|
2020-09-03 19:08:53 +02:00
|
|
|
displayAccountsFragment.setArguments(bundle);
|
|
|
|
return displayAccountsFragment;
|
2020-09-29 17:42:15 +02:00
|
|
|
case 2:
|
|
|
|
return new DisplayChannelsFragment();
|
2020-09-03 19:08:53 +02:00
|
|
|
default:
|
|
|
|
return new DisplayNotificationsFragment();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getCount() {
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|