@ -176,6 +176,8 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
private val binding by viewBinding ( ActivityMainBinding :: inflate )
private lateinit var activeAccount : AccountEntity
private lateinit var header : AccountHeaderView
private var onTabSelectedListener : OnTabSelectedListener ? = null
@ -209,7 +211,7 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
super . onCreate ( savedInstanceState )
// will be redirected to LoginActivity by BaseActivity
val activeAccount = accountManager . activeAccount ?: return
activeAccount = accountManager . activeAccount ?: return
if ( explodeAnimationWasRequested ( ) ) {
overrideActivityTransitionCompat (
@ -224,7 +226,12 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
// check for savedInstanceState in order to not handle intent events more than once
if ( intent != null && savedInstanceState == null ) {
showNotificationTab = handleIntent ( intent , activeAccount )
if ( isFinishing ) {
// handleIntent() finished this activity and started a new one - no need to continue initialization
return
}
}
window . statusBarColor = Color . TRANSPARENT // don't draw a status bar, the DrawerLayout and the MaterialDrawerLayout have their own
setContentView ( binding . root )
@ -258,10 +265,10 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
setupDrawer (
savedInstanceState ,
addSearchButton = hideTopToolbar ,
addTrendingTagsButton = ! accountManager . ac tiveAccount !! . tabPreferences . hasTab (
addTrendingTagsButton = ! activeAccount . tabPreferences . hasTab (
TRENDING _TAGS
) ,
addTrendingStatusesButton = ! accountManager . ac tiveAccount !! . tabPreferences . hasTab (
addTrendingStatusesButton = ! activeAccount . tabPreferences . hasTab (
TRENDING _STATUSES
)
)
@ -352,7 +359,6 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
override fun onNewIntent ( intent : Intent ) {
super . onNewIntent ( intent )
val activeAccount = accountManager . activeAccount ?: return
val showNotificationTab = handleIntent ( intent , activeAccount )
if ( showNotificationTab ) {
val tabs = activeAccount . tabPreferences
@ -394,8 +400,7 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
}
val accountRequested = tuskyAccountId != - 1L
if ( accountRequested && tuskyAccountId != activeAccount . id ) {
accountManager . setActiveAccount ( tuskyAccountId )
changeAccount ( tuskyAccountId , intent , withAnimation = false )
changeAccount ( tuskyAccountId , intent )
return false
}
@ -451,11 +456,9 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
// TODO a bit cumbersome (also for resetting)
lifecycleScope . launch ( Dispatchers . IO ) {
accountManager . activeAccount ?. let {
if ( it . hasDirectMessageBadge != showBadge ) {
it . hasDirectMessageBadge = showBadge
accountManager . saveAccount ( it )
}
if ( activeAccount . hasDirectMessageBadge != showBadge ) {
activeAccount . hasDirectMessageBadge = showBadge
accountManager . saveAccount ( activeAccount )
}
}
}
@ -562,7 +565,6 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
private fun forwardToComposeActivity ( intent : Intent ) {
val composeOptions =
intent . getParcelableExtraCompat < ComposeActivity . ComposeOptions > ( COMPOSE _OPTIONS )
val composeIntent = if ( composeOptions != null ) {
ComposeActivity . startIntent ( this , composeOptions )
} else {
@ -570,7 +572,6 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
action = intent . action
type = intent . type
putExtras ( intent )
flags = Intent . FLAG _ACTIVITY _NEW _TASK or Intent . FLAG _ACTIVITY _CLEAR _TASK
}
}
startActivity ( composeIntent )
@ -831,11 +832,9 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
0 -> {
Log . d ( TAG , " Creating \" Load more \" gap " )
lifecycleScope . launch {
accountManager . activeAccount ?. let {
developerToolsUseCase . createLoadMoreGap (
it . id
)
}
developerToolsUseCase . createLoadMoreGap (
activeAccount . id
)
}
}
}
@ -864,7 +863,7 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
// Save the previous tab so it can be restored later
val previousTab = tabAdapter . tabs . getOrNull ( binding . viewPager . currentItem )
val tabs = accountManager . ac tiveAccount !! . tabPreferences
val tabs = activeAccount . tabPreferences
// Detach any existing mediator before changing tab contents and attaching a new mediator
tabLayoutMediator ?. detach ( )
@ -883,7 +882,7 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
}
if ( tabs [ position ] . id == DIRECT ) {
val badge = tab . orCreateBadge
badge . isVisible = accountManager . activeAccount ?. hasDirectMessageBadge ?: fals e
badge . isVisible = activeAccount . hasDirectMessageBadg e
badge . backgroundColor = MaterialColors . getColor ( binding . mainDrawer , materialR . attr . colorPrimary )
directMessageTab = tab
}
@ -921,11 +920,9 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
if ( tab == directMessageTab ) {
tab . badge ?. isVisible = false
accountManager . activeAccount ?. let {
if ( it . hasDirectMessageBadge ) {
it . hasDirectMessageBadge = false
accountManager . saveAccount ( it )
}
if ( activeAccount . hasDirectMessageBadge ) {
activeAccount . hasDirectMessageBadge = false
accountManager . saveAccount ( activeAccount )
}
}
}
@ -971,10 +968,8 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
}
private fun handleProfileClick ( profile : IProfile , current : Boolean ) : Boolean {
val activeAccount = accountManager . activeAccount
// open profile when active image was clicked
if ( current && activeAccount != null ) {
if ( current ) {
val intent = AccountActivity . getIntent ( this , activeAccount . accountId )
startActivityWithSlideInAnimation ( intent )
return false
@ -994,49 +989,43 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
private fun changeAccount (
newSelectedId : Long ,
forward : Intent ? ,
withAnimation : Boolean = true
) {
cacheUpdater . stop ( )
accountManager . setActiveAccount ( newSelectedId )
val intent = Intent ( this , MainActivity :: class . java )
if ( withAnimation ) {
intent . putExtra ( OPEN _WITH _EXPLODE _ANIMATION , true )
}
if ( forward != null ) {
intent . type = forward . type
intent . action = forward . action
intent . putExtras ( forward )
}
startActivity ( intent )
finish ( )
startActivity ( intent )
}
private fun logout ( ) {
accountManager . activeAccount ?. let { activeAccount ->
AlertDialog . Builder ( this )
. setTitle ( R . string . action _logout )
. setMessage ( getString ( R . string . action _logout _confirm , activeAccount . fullName ) )
. setPositiveButton ( android . R . string . ok ) { _ : DialogInterface ? , _ : Int ->
binding . appBar . hide ( )
binding . viewPager . hide ( )
binding . progressBar . show ( )
binding . bottomNav . hide ( )
binding . composeButton . hide ( )
lifecycleScope . launch {
val otherAccountAvailable = logoutUsecase . logout ( )
val intent = if ( otherAccountAvailable ) {
Intent ( this @MainActivity , MainActivity :: class . java )
} else {
LoginActivity . getIntent ( this @MainActivity , LoginActivity . MODE _DEFAULT )
}
startActivity ( intent )
finish ( )
AlertDialog . Builder ( this )
. setTitle ( R . string . action _logout )
. setMessage ( getString ( R . string . action _logout _confirm , activeAccount . fullName ) )
. setPositiveButton ( android . R . string . ok ) { _ : DialogInterface ? , _ : Int ->
binding . appBar . hide ( )
binding . viewPager . hide ( )
binding . progressBar . show ( )
binding . bottomNav . hide ( )
binding . composeButton . hide ( )
lifecycleScope . launch {
val otherAccountAvailable = logoutUsecase . logout ( activeAccount )
val intent = if ( otherAccountAvailable ) {
Intent ( this @MainActivity , MainActivity :: class . java )
} else {
LoginActivity . getIntent ( this @MainActivity , LoginActivity . MODE _DEFAULT )
}
startActivity ( intent )
finish ( )
}
. setNegativeButton ( android . R . string . cancel , null )
. show ( )
}
}
. setNegativeButton ( android . R . string . cancel , null )
. show ( )
}
private fun fetchUserInfo ( ) = lifecycleScope . launch {
@ -1058,11 +1047,8 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
loadDrawerAvatar ( me . avatar , false )
accountManager . updateActiveAccount ( me )
NotificationHelper . createNotificationChannelsForAccount (
accountManager . activeAccount !! ,
this
)
accountManager . updateAccount ( activeAccount , me )
NotificationHelper . createNotificationChannelsForAccount ( activeAccount , this )
// Setup push notifications
showMigrationNoticeIfNecessary (
@ -1217,9 +1203,9 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
}
header . clear ( )
header . profiles = profiles
header . setActiveProfile ( accountManager . ac tiveAccount !! . id )
header . setActiveProfile ( activeAccount . id )
binding . mainToolbar . subtitle = if ( accountManager . shouldDisplaySelfUsername ( ) ) {
accountManager . ac tiveAccount !! . fullName
activeAccount . fullName
} else {
null
}