2021-02-05 08:58:48 +01:00
//
2021-02-07 07:42:50 +01:00
// H o m e T i m e l i n e V i e w C o n t r o l l e r + D e b u g A c t i o n . s w i f t
2021-02-05 08:58:48 +01:00
// M a s t o d o n
//
// C r e a t e d b y M a i n a s u K C i r n o o n 2 0 2 1 - 2 - 5 .
//
import os . log
import UIKit
#if DEBUG
2021-02-07 07:42:50 +01:00
extension HomeTimelineViewController {
2021-02-05 08:58:48 +01:00
var debugMenu : UIMenu {
let menu = UIMenu (
title : " Debug Tools " ,
image : nil ,
identifier : nil ,
options : . displayInline ,
children : [
UIAction ( title : " Sign Out " , image : UIImage ( systemName : " escape " ) , attributes : . destructive ) { [ weak self ] action in
guard let self = self else { return }
self . signOutAction ( action )
}
]
)
return menu
}
}
2021-02-07 07:42:50 +01:00
extension HomeTimelineViewController {
2021-02-05 08:58:48 +01:00
@objc private func signOutAction ( _ sender : UIAction ) {
guard let activeMastodonAuthenticationBox = context . authenticationService . activeMastodonAuthenticationBox . value else {
return
}
let currentAccountCount = context . authenticationService . mastodonAuthentications . value . count
let isAuthenticationExistWhenSignOut = currentAccountCount - 1 > 0
// p r e p a r e a d v a n c e
let authenticationViewModel = AuthenticationViewModel ( context : context , coordinator : coordinator , isAuthenticationExist : isAuthenticationExistWhenSignOut )
context . authenticationService . signOutMastodonUser (
domain : activeMastodonAuthenticationBox . domain ,
userID : activeMastodonAuthenticationBox . userID
)
. receive ( on : DispatchQueue . main )
. sink { [ weak self ] result in
guard let self = self else { return }
switch result {
case . failure ( let error ) :
assertionFailure ( error . localizedDescription )
case . success ( let isSignOut ) :
os_log ( . info , log : . debug , " %{public}s[%{public}ld], %{public}s: sign out %s " , ( ( #file as NSString ) . lastPathComponent ) , #line , #function , isSignOut ? " success " : " fail " )
guard isSignOut else { return }
if ! isAuthenticationExistWhenSignOut {
self . coordinator . present ( scene : . authentication ( viewModel : authenticationViewModel ) , from : nil , transition : . modal ( animated : true , completion : nil ) )
}
}
}
. store ( in : & disposeBag )
}
}
#endif