Yuito-app-android/app/src/main/java/com/keylesspalace/tusky/MainActivity.java

642 lines
28 KiB
Java
Raw Normal View History

2017-05-03 20:29:33 +02:00
/* Copyright 2017 Andrew Dawson
*
* This file is a part of Tusky.
*
* 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.
*
* Tusky 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 Tusky; if not,
* see <http://www.gnu.org/licenses>. */
package com.keylesspalace.tusky;
import androidx.lifecycle.Lifecycle;
2017-05-03 20:29:33 +02:00
import android.content.Intent;
import android.graphics.Color;
2017-05-03 20:29:33 +02:00
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import androidx.annotation.Nullable;
import com.bumptech.glide.Glide;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.tabs.TabLayout;
import androidx.emoji.text.EmojiCompat;
import androidx.fragment.app.Fragment;
import androidx.core.content.ContextCompat;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AlertDialog;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.ImageButton;
2017-05-03 20:29:33 +02:00
import android.widget.ImageView;
import com.keylesspalace.tusky.appstore.CacheUpdater;
import com.keylesspalace.tusky.appstore.EventHub;
import com.keylesspalace.tusky.appstore.MainTabsChangedEvent;
import com.keylesspalace.tusky.appstore.ProfileEditedEvent;
import com.keylesspalace.tusky.components.conversation.ConversationsRepository;
import com.keylesspalace.tusky.components.search.SearchActivity;
import com.keylesspalace.tusky.db.AccountEntity;
2017-05-03 20:29:33 +02:00
import com.keylesspalace.tusky.entity.Account;
import com.keylesspalace.tusky.fragment.SFragment;
import com.keylesspalace.tusky.interfaces.ActionButtonActivity;
import com.keylesspalace.tusky.interfaces.ReselectableFragment;
import com.keylesspalace.tusky.pager.MainPagerAdapter;
import com.keylesspalace.tusky.util.CustomEmojiHelper;
import com.keylesspalace.tusky.util.NotificationHelper;
2017-05-05 00:55:34 +02:00
import com.keylesspalace.tusky.util.ThemeUtils;
2017-05-03 20:29:33 +02:00
import com.mikepenz.google_material_typeface_library.GoogleMaterial;
import com.mikepenz.materialdrawer.AccountHeader;
import com.mikepenz.materialdrawer.AccountHeaderBuilder;
import com.mikepenz.materialdrawer.Drawer;
import com.mikepenz.materialdrawer.DrawerBuilder;
import com.mikepenz.materialdrawer.model.DividerDrawerItem;
import com.mikepenz.materialdrawer.model.PrimaryDrawerItem;
import com.mikepenz.materialdrawer.model.ProfileDrawerItem;
import com.mikepenz.materialdrawer.model.ProfileSettingDrawerItem;
2017-05-03 20:29:33 +02:00
import com.mikepenz.materialdrawer.model.SecondaryDrawerItem;
import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem;
import com.mikepenz.materialdrawer.model.interfaces.IProfile;
import com.mikepenz.materialdrawer.util.AbstractDrawerImageLoader;
import com.mikepenz.materialdrawer.util.DrawerImageLoader;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import dagger.android.AndroidInjector;
import dagger.android.DispatchingAndroidInjector;
import dagger.android.HasAndroidInjector;
import io.reactivex.android.schedulers.AndroidSchedulers;
2017-05-03 20:29:33 +02:00
import static com.keylesspalace.tusky.util.MediaUtilsKt.deleteStaleCachedMedia;
import static com.uber.autodispose.AutoDispose.autoDisposable;
import static com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider.from;
public final class MainActivity extends BottomSheetActivity implements ActionButtonActivity,
HasAndroidInjector {
2017-05-03 20:29:33 +02:00
private static final String TAG = "MainActivity"; // logging tag
private static final long DRAWER_ITEM_ADD_ACCOUNT = -13;
private static final long DRAWER_ITEM_EDIT_PROFILE = 0;
private static final long DRAWER_ITEM_FAVOURITES = 1;
private static final long DRAWER_ITEM_LISTS = 2;
private static final long DRAWER_ITEM_SEARCH = 3;
private static final long DRAWER_ITEM_SAVED_TOOT = 4;
private static final long DRAWER_ITEM_ACCOUNT_SETTINGS = 5;
private static final long DRAWER_ITEM_SETTINGS = 6;
private static final long DRAWER_ITEM_ABOUT = 7;
private static final long DRAWER_ITEM_LOG_OUT = 8;
private static final long DRAWER_ITEM_FOLLOW_REQUESTS = 9;
public static final String STATUS_URL = "statusUrl";
2017-05-03 20:29:33 +02:00
@Inject
public DispatchingAndroidInjector<Object> androidInjector;
@Inject
public EventHub eventHub;
@Inject
public CacheUpdater cacheUpdater;
@Inject
ConversationsRepository conversationRepository;
private FloatingActionButton composeButton;
2017-05-03 20:29:33 +02:00
private AccountHeader headerResult;
private Drawer drawer;
private TabLayout tabLayout;
2017-06-22 20:59:12 +02:00
private ViewPager viewPager;
2017-05-03 20:29:33 +02:00
private int notificationTabPosition;
private MainPagerAdapter adapter;
private final EmojiCompat.InitCallback emojiInitCallback = new EmojiCompat.InitCallback() {
@Override
public void onInitialized() {
if(!isDestroyed()) {
updateProfiles();
}
}
};
2017-05-03 20:29:33 +02:00
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (accountManager.getActiveAccount() == null) {
// will be redirected to LoginActivity by BaseActivity
return;
}
Intent intent = getIntent();
boolean showNotificationTab = false;
if (intent != null) {
long accountId = intent.getLongExtra(NotificationHelper.ACCOUNT_ID, -1);
boolean accountRequested = (accountId != -1);
if (accountRequested) {
AccountEntity account = accountManager.getActiveAccount();
if (account == null || accountId != account.getId()) {
accountManager.setActiveAccount(accountId);
}
}
if (ComposeActivity.canHandleMimeType(intent.getType())) {
// Sharing to Tusky from an external app
if (accountRequested) {
// The correct account is already active
forwardShare(intent);
} else {
// No account was provided, show the chooser
showAccountChooserDialog(getString(R.string.action_share_as), true, account -> {
long requestedId = account.getId();
AccountEntity activeAccount = accountManager.getActiveAccount();
if (activeAccount != null && requestedId == activeAccount.getId()) {
// The correct account is already active
forwardShare(intent);
} else {
// A different account was requested, restart the activity
intent.putExtra(NotificationHelper.ACCOUNT_ID, requestedId);
changeAccount(requestedId, intent);
}
});
}
} else if (accountRequested) {
// user clicked a notification, show notification tab and switch user if necessary
showNotificationTab = true;
}
}
2017-05-03 20:29:33 +02:00
setContentView(R.layout.activity_main);
2018-07-31 21:25:25 +02:00
composeButton = findViewById(R.id.floating_btn);
ImageButton drawerToggle = findViewById(R.id.drawer_toggle);
tabLayout = findViewById(R.id.tab_layout);
viewPager = findViewById(R.id.pager);
2017-05-03 20:29:33 +02:00
2018-07-31 21:25:25 +02:00
composeButton.setOnClickListener(v -> {
Intent composeIntent = new Intent(getApplicationContext(), ComposeActivity.class);
startActivity(composeIntent);
2017-05-03 20:29:33 +02:00
});
setupDrawer();
// Setup the navigation drawer toggle button.
ThemeUtils.setDrawableTint(this, drawerToggle.getDrawable(), R.attr.toolbar_icon_tint);
drawerToggle.setOnClickListener(v -> drawer.openDrawer());
2017-05-03 20:29:33 +02:00
/* Fetch user info while we're doing other things. This has to be done after setting up the
2017-05-03 20:29:33 +02:00
* drawer, though, because its callback touches the header in the drawer. */
fetchUserInfo();
setupTabs(showNotificationTab);
2017-05-03 20:29:33 +02:00
int pageMargin = getResources().getDimensionPixelSize(R.dimen.tab_page_margin);
viewPager.setPageMargin(pageMargin);
Drawable pageMarginDrawable = ThemeUtils.getDrawable(this, R.attr.tab_page_margin_drawable,
R.drawable.tab_page_margin_dark);
viewPager.setPageMarginDrawable(pageMarginDrawable);
2017-05-03 20:29:33 +02:00
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
if (tab.getPosition() == notificationTabPosition) {
2018-07-23 21:59:10 +02:00
NotificationHelper.clearNotificationsForActiveAccount(MainActivity.this, accountManager);
2017-11-15 21:18:35 +01:00
}
2017-05-03 20:29:33 +02:00
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
if (adapter != null) {
Fragment fragment = adapter.getFragment(tab.getPosition());
if (fragment instanceof ReselectableFragment) {
((ReselectableFragment) fragment).onReselect();
}
}
}
2017-05-03 20:29:33 +02:00
});
// Setup push notifications
2018-07-23 21:59:10 +02:00
if (NotificationHelper.areNotificationsEnabled(this, accountManager)) {
NotificationHelper.enablePullNotifications();
2017-05-03 20:29:33 +02:00
} else {
NotificationHelper.disablePullNotifications();
2017-05-03 20:29:33 +02:00
}
eventHub.getEvents()
.observeOn(AndroidSchedulers.mainThread())
.as(autoDisposable(from(this, Lifecycle.Event.ON_DESTROY)))
.subscribe(event -> {
if (event instanceof ProfileEditedEvent) {
onFetchUserInfoSuccess(((ProfileEditedEvent) event).getNewProfileData());
}
if (event instanceof MainTabsChangedEvent) {
setupTabs(false);
}
});
// Flush old media that was cached for sharing
deleteStaleCachedMedia(getApplicationContext().getExternalFilesDir("Tusky"));
2017-05-03 20:29:33 +02:00
}
@Override
protected void onResume() {
super.onResume();
2018-07-23 21:59:10 +02:00
NotificationHelper.clearNotificationsForActiveAccount(this, accountManager);
2017-05-03 20:29:33 +02:00
}
@Override
public void onBackPressed() {
if (drawer != null && drawer.isDrawerOpen()) {
drawer.closeDrawer();
} else if (viewPager.getCurrentItem() != 0) {
viewPager.setCurrentItem(0);
} else {
super.onBackPressed();
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_MENU: {
if (drawer.isDrawerOpen()) {
drawer.closeDrawer();
} else {
drawer.openDrawer();
}
return true;
}
case KeyEvent.KEYCODE_SEARCH: {
startActivityWithSlideInAnimation(SearchActivity.getIntent(this));
return true;
}
}
if (event.isCtrlPressed() || event.isShiftPressed()) {
// FIXME: blackberry keyONE raises SHIFT key event even CTRL IS PRESSED
switch (keyCode) {
case KeyEvent.KEYCODE_N: {
// open compose activity by pressing SHIFT + N (or CTRL + N)
Intent composeIntent = new Intent(getApplicationContext(), ComposeActivity.class);
startActivity(composeIntent);
return true;
}
}
}
return super.onKeyDown(keyCode, event);
}
@Override
public void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
Intent intent = getIntent();
if (intent != null) {
String statusUrl = intent.getStringExtra(STATUS_URL);
if (statusUrl != null) {
viewUrl(statusUrl);
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
EmojiCompat.get().unregisterInitCallback(emojiInitCallback);
}
private void forwardShare(Intent intent) {
Intent composeIntent = new Intent(this, ComposeActivity.class);
composeIntent.setAction(intent.getAction());
composeIntent.setType(intent.getType());
composeIntent.putExtras(intent);
composeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(composeIntent);
finish();
2017-05-03 20:29:33 +02:00
}
private void setupDrawer() {
headerResult = new AccountHeaderBuilder()
.withActivity(this)
.withDividerBelowHeader(false)
.withHeaderBackgroundScaleType(ImageView.ScaleType.CENTER_CROP)
.withCurrentProfileHiddenInList(true)
.withOnAccountHeaderListener((view, profile, current) -> handleProfileClick(profile, current))
.addProfiles(
new ProfileSettingDrawerItem()
.withIdentifier(DRAWER_ITEM_ADD_ACCOUNT)
.withName(R.string.add_account_name)
.withDescription(R.string.add_account_description)
.withIcon(GoogleMaterial.Icon.gmd_add))
2017-05-03 20:29:33 +02:00
.build();
2017-05-03 20:29:33 +02:00
headerResult.getView()
.findViewById(R.id.material_drawer_account_header_current)
.setContentDescription(getString(R.string.action_view_profile));
ImageView background = headerResult.getHeaderBackgroundView();
background.setColorFilter(ContextCompat.getColor(this, R.color.header_background_filter));
background.setBackgroundColor(ContextCompat.getColor(this, R.color.window_background_dark));
final boolean animateAvatars = PreferenceManager.getDefaultSharedPreferences(this)
.getBoolean("animateGifAvatars", false);
2017-05-03 20:29:33 +02:00
DrawerImageLoader.init(new AbstractDrawerImageLoader() {
@Override
public void set(ImageView imageView, Uri uri, Drawable placeholder, String tag) {
if(animateAvatars) {
Glide.with(MainActivity.this)
.load(uri)
.placeholder(placeholder)
.into(imageView);
} else {
Glide.with(MainActivity.this)
.asBitmap()
.load(uri)
.placeholder(placeholder)
.into(imageView);
}
2017-05-03 20:29:33 +02:00
}
@Override
public void cancel(ImageView imageView) {
Glide.with(MainActivity.this).clear(imageView);
2017-05-03 20:29:33 +02:00
}
});
List<IDrawerItem> listItems = new ArrayList<>(10);
listItems.add(new PrimaryDrawerItem().withIdentifier(DRAWER_ITEM_EDIT_PROFILE).withName(R.string.action_edit_profile).withSelectable(false).withIcon(GoogleMaterial.Icon.gmd_person));
listItems.add(new PrimaryDrawerItem().withIdentifier(DRAWER_ITEM_FAVOURITES).withName(R.string.action_view_favourites).withSelectable(false).withIcon(GoogleMaterial.Icon.gmd_star));
listItems.add(new PrimaryDrawerItem().withIdentifier(DRAWER_ITEM_LISTS).withName(R.string.action_lists).withSelectable(false).withIcon(GoogleMaterial.Icon.gmd_list));
listItems.add(new PrimaryDrawerItem().withIdentifier(DRAWER_ITEM_SEARCH).withName(R.string.action_search).withSelectable(false).withIcon(GoogleMaterial.Icon.gmd_search));
listItems.add(new PrimaryDrawerItem().withIdentifier(DRAWER_ITEM_SAVED_TOOT).withName(R.string.action_access_saved_toot).withSelectable(false).withIcon(R.drawable.ic_notebook).withIconTintingEnabled(true));
listItems.add(new DividerDrawerItem());
listItems.add(new SecondaryDrawerItem().withIdentifier(DRAWER_ITEM_ACCOUNT_SETTINGS).withName(R.string.action_view_account_preferences).withSelectable(false).withIcon(R.drawable.ic_account_settings).withIconTintingEnabled(true));
listItems.add(new SecondaryDrawerItem().withIdentifier(DRAWER_ITEM_SETTINGS).withName(R.string.action_view_preferences).withSelectable(false).withIcon(GoogleMaterial.Icon.gmd_settings));
listItems.add(new SecondaryDrawerItem().withIdentifier(DRAWER_ITEM_ABOUT).withName(R.string.about_title_activity).withSelectable(false).withIcon(GoogleMaterial.Icon.gmd_info));
listItems.add(new SecondaryDrawerItem().withIdentifier(DRAWER_ITEM_LOG_OUT).withName(R.string.action_logout).withSelectable(false).withIcon(R.drawable.ic_logout).withIconTintingEnabled(true));
2017-05-03 20:29:33 +02:00
drawer = new DrawerBuilder()
.withActivity(this)
.withAccountHeader(headerResult)
.withHasStableIds(true)
.withSelectedItem(-1)
.withDrawerItems(listItems)
.withOnDrawerItemClickListener((view, position, drawerItem) -> {
if (drawerItem != null) {
long drawerItemIdentifier = drawerItem.getIdentifier();
2017-05-03 20:29:33 +02:00
if (drawerItemIdentifier == DRAWER_ITEM_EDIT_PROFILE) {
Intent intent = new Intent(MainActivity.this, EditProfileActivity.class);
2018-07-31 21:25:25 +02:00
startActivityWithSlideInAnimation(intent);
} else if (drawerItemIdentifier == DRAWER_ITEM_FAVOURITES) {
Intent intent = new Intent(MainActivity.this, FavouritesActivity.class);
2018-07-31 21:25:25 +02:00
startActivityWithSlideInAnimation(intent);
} else if (drawerItemIdentifier == DRAWER_ITEM_SEARCH) {
startActivityWithSlideInAnimation(SearchActivity.getIntent(this));
} else if (drawerItemIdentifier == DRAWER_ITEM_ACCOUNT_SETTINGS) {
Intent intent = PreferencesActivity.newIntent(MainActivity.this, PreferencesActivity.ACCOUNT_PREFERENCES);
startActivityWithSlideInAnimation(intent);
} else if (drawerItemIdentifier == DRAWER_ITEM_SETTINGS) {
Intent intent = PreferencesActivity.newIntent(MainActivity.this, PreferencesActivity.GENERAL_PREFERENCES);
2018-07-31 21:25:25 +02:00
startActivityWithSlideInAnimation(intent);
} else if (drawerItemIdentifier == DRAWER_ITEM_ABOUT) {
Intent intent = new Intent(MainActivity.this, AboutActivity.class);
2018-07-31 21:25:25 +02:00
startActivityWithSlideInAnimation(intent);
} else if (drawerItemIdentifier == DRAWER_ITEM_LOG_OUT) {
logout();
} else if (drawerItemIdentifier == DRAWER_ITEM_FOLLOW_REQUESTS) {
Intent intent = new Intent(MainActivity.this, AccountListActivity.class);
intent.putExtra("type", AccountListActivity.Type.FOLLOW_REQUESTS);
2018-07-31 21:25:25 +02:00
startActivityWithSlideInAnimation(intent);
} else if (drawerItemIdentifier == DRAWER_ITEM_SAVED_TOOT) {
Intent intent = new Intent(MainActivity.this, SavedTootActivity.class);
2018-07-31 21:25:25 +02:00
startActivityWithSlideInAnimation(intent);
2018-01-06 19:01:37 +01:00
} else if (drawerItemIdentifier == DRAWER_ITEM_LISTS) {
2018-07-31 21:25:25 +02:00
startActivityWithSlideInAnimation(ListsActivity.newIntent(this));
}
2017-05-03 20:29:33 +02:00
}
return false;
2017-05-03 20:29:33 +02:00
})
.build();
if (BuildConfig.DEBUG) {
IDrawerItem debugItem = new SecondaryDrawerItem()
.withIdentifier(1337)
.withName("debug")
.withDisabledTextColor(Color.GREEN)
.withSelectable(false)
.withEnabled(false);
drawer.addItem(debugItem);
}
EmojiCompat.get().registerInitCallback(emojiInitCallback);
2017-05-03 20:29:33 +02:00
}
private void setupTabs(boolean selectNotificationTab) {
List<TabData> tabs = accountManager.getActiveAccount().getTabPreferences();
adapter = new MainPagerAdapter(tabs, getSupportFragmentManager());
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
tabLayout.removeAllTabs();
for (int i = 0; i < tabs.size(); i++) {
TabLayout.Tab tab = tabLayout.newTab()
.setIcon(tabs.get(i).getIcon())
.setContentDescription(tabs.get(i).getText());
tabLayout.addTab(tab);
if (tabs.get(i).getId().equals(TabDataKt.NOTIFICATIONS)) {
notificationTabPosition = i;
if (selectNotificationTab) {
tab.select();
}
}
}
}
private boolean handleProfileClick(IProfile profile, boolean current) {
AccountEntity activeAccount = accountManager.getActiveAccount();
//open profile when active image was clicked
if (current && activeAccount != null) {
Account activity redesign (#662) * Refactor-all-the-things version of the fix for issue #573 * Migrate SpanUtils to kotlin because why not * Minimal fix for issue #573 * Add tests for compose spanning * Clean up code suggestions * Make FakeSpannable.getSpans implementation less awkward * Add secondary validation pass for urls * Address code review feedback * Fixup type filtering in FakeSpannable again * Make all mentions in compose activity use the default link color * new layout for AccountActivity * fix the light theme * convert AccountActivity to Kotlin * introduce AccountViewModel * Merge branch 'master' into account-activity-redesign # Conflicts: # app/src/main/java/com/keylesspalace/tusky/AccountActivity.java * add Bot badge to profile * parse custom emojis in usernames * add possibility to cancel follow request * add third tab on profiles * add account fields to profile * add support for moved accounts * set click listener on account moved view * fix tests * use 24dp as statusbar size * add ability to hide reblogs from followed accounts * add button to edit own account to AccountActivity * set toolbar top margin programmatically * fix crash * add shadow behind statusbar * introduce ViewExtensions to clean up code * move code out of offsetChangedListener for perf reasons * clean up stuff * add error handling * improve type safety * fix ConstraintLayout warning * remove unneeded ressources * fix event dispatching * fix crash in event handling * set correct emoji on title * improve some things * wrap follower/foillowing/status views
2018-06-18 13:26:18 +02:00
Intent intent = AccountActivity.getIntent(this, activeAccount.getAccountId());
2018-07-31 21:25:25 +02:00
startActivityWithSlideInAnimation(intent);
new Handler().postDelayed(() -> drawer.closeDrawer(), 100);
return true;
}
//open LoginActivity to add new account
if (profile.getIdentifier() == DRAWER_ITEM_ADD_ACCOUNT) {
2018-07-31 21:25:25 +02:00
startActivityWithSlideInAnimation(LoginActivity.getIntent(this, true));
new Handler().postDelayed(() -> drawer.closeDrawer(), 100);
return true;
}
//change Account
changeAccount(profile.getIdentifier(), null);
return false;
2017-05-03 20:29:33 +02:00
}
private void changeAccount(long newSelectedId, @Nullable Intent forward) {
cacheUpdater.stop();
SFragment.flushFilters();
accountManager.setActiveAccount(newSelectedId);
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
if (forward != null) {
intent.setType(forward.getType());
intent.setAction(forward.getAction());
intent.putExtras(forward);
}
startActivity(intent);
finishWithoutSlideOutAnimation();
overridePendingTransition(R.anim.explode, R.anim.explode);
}
private void logout() {
AccountEntity activeAccount = accountManager.getActiveAccount();
if (activeAccount != null) {
new AlertDialog.Builder(this)
.setTitle(R.string.action_logout)
.setMessage(getString(R.string.action_logout_confirm, activeAccount.getFullName()))
.setPositiveButton(android.R.string.yes, (dialog, which) -> {
NotificationHelper.deleteNotificationChannelsForAccount(accountManager.getActiveAccount(), MainActivity.this);
cacheUpdater.clearForUser(activeAccount.getId());
conversationRepository.deleteCacheForAccount(activeAccount.getId());
AccountEntity newAccount = accountManager.logActiveAccountOut();
if (!NotificationHelper.areNotificationsEnabled(MainActivity.this, accountManager)) {
NotificationHelper.disablePullNotifications();
}
Intent intent;
if (newAccount == null) {
intent = LoginActivity.getIntent(MainActivity.this, false);
} else {
intent = new Intent(MainActivity.this, MainActivity.class);
}
startActivity(intent);
finishWithoutSlideOutAnimation();
})
.setNegativeButton(android.R.string.no, null)
.show();
2017-05-03 20:29:33 +02:00
}
}
private void fetchUserInfo() {
mastodonApi.accountVerifyCredentials()
.observeOn(AndroidSchedulers.mainThread())
.as(autoDisposable(from(this, Lifecycle.Event.ON_DESTROY)))
.subscribe(this::onFetchUserInfoSuccess, MainActivity::onFetchUserInfoFailure);
2017-05-03 20:29:33 +02:00
}
private void onFetchUserInfoSuccess(Account me) {
2017-05-03 20:29:33 +02:00
// Add the header image and avatar from the account, into the navigation drawer header.
2017-05-03 20:29:33 +02:00
ImageView background = headerResult.getHeaderBackgroundView();
Glide.with(MainActivity.this)
2019-05-08 21:42:22 +02:00
.asBitmap()
.load(me.getHeader())
2017-05-03 20:29:33 +02:00
.into(background);
accountManager.updateActiveAccount(me);
NotificationHelper.createNotificationChannelsForAccount(accountManager.getActiveAccount(), this);
// Show follow requests in the menu, if this is a locked account.
if (me.getLocked() && drawer.getDrawerItem(DRAWER_ITEM_FOLLOW_REQUESTS) == null) {
PrimaryDrawerItem followRequestsItem = new PrimaryDrawerItem()
.withIdentifier(DRAWER_ITEM_FOLLOW_REQUESTS)
.withName(R.string.action_view_follow_requests)
.withSelectable(false)
.withIcon(GoogleMaterial.Icon.gmd_person_add);
drawer.addItemAtPosition(followRequestsItem, 3);
} else if (!me.getLocked()) {
2018-03-27 20:46:53 +02:00
drawer.removeItem(DRAWER_ITEM_FOLLOW_REQUESTS);
}
updateProfiles();
}
private void updateProfiles() {
List<AccountEntity> allAccounts = accountManager.getAllAccountsOrderedByActive();
List<IProfile> profiles = new ArrayList<>(allAccounts.size() + 1);
for (AccountEntity acc : allAccounts) {
CharSequence emojifiedName = CustomEmojiHelper.emojifyString(acc.getDisplayName(), acc.getEmojis(), headerResult.getView());
emojifiedName = EmojiCompat.get().process(emojifiedName);
profiles.add(
new ProfileDrawerItem()
.withSetSelected(acc.isActive())
.withName(emojifiedName)
.withIcon(acc.getProfilePictureUrl())
.withNameShown(true)
.withIdentifier(acc.getId())
.withEmail(acc.getFullName()));
2017-05-03 20:29:33 +02:00
}
// reuse the already existing "add account" item
for (IProfile profile : headerResult.getProfiles()) {
if (profile.getIdentifier() == DRAWER_ITEM_ADD_ACCOUNT) {
profiles.add(profile);
break;
}
}
headerResult.clear();
headerResult.setProfiles(profiles);
headerResult.setActiveProfile(accountManager.getActiveAccount().getId());
2017-05-03 20:29:33 +02:00
}
private static void onFetchUserInfoFailure(Throwable throwable) {
Log.e(TAG, "Failed to fetch user info. " + throwable.getMessage());
2017-05-03 20:29:33 +02:00
}
@Nullable
@Override
public FloatingActionButton getActionButton() {
return composeButton;
}
@Override
public AndroidInjector<Object> androidInjector() {
return androidInjector;
}
2017-03-11 02:50:49 +01:00
}