2021-03-03 09:12:48 +01:00
//
// S t a t u s P r o v i d e r + U I T a b l e V i e w D e l e g a t e . s w i f t
// 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 - 3 - 3 .
//
import Combine
import CoreDataStack
import MastodonSDK
2021-03-10 10:14:12 +01:00
import os . log
import UIKit
2021-03-03 09:12:48 +01:00
extension StatusTableViewCellDelegate where Self : StatusProvider {
// TODO:
// f u n c h a n d l e T a b l e V i e w ( _ t a b l e V i e w : U I T a b l e V i e w , e s t i m a t e d H e i g h t F o r R o w A t i n d e x P a t h : I n d e x P a t h ) - > C G F l o a t {
// }
func handleTableView ( _ tableView : UITableView , willDisplay cell : UITableViewCell , forRowAt indexPath : IndexPath ) {
let now = Date ( )
var pollID : Mastodon . Entity . Poll . ID ?
toot ( for : cell , indexPath : indexPath )
. compactMap { [ weak self ] toot -> AnyPublisher < Mastodon . Response . Content < Mastodon . Entity . Poll > , Error > ? in
guard let self = self else { return nil }
guard let authenticationBox = self . context . authenticationService . activeMastodonAuthenticationBox . value else { return nil }
guard let toot = ( toot ? . reblog ? ? toot ) else { return nil }
guard let poll = toot . poll else { return nil }
pollID = poll . id
// n o t e x p i r e d A N D l a s t u p d a t e > 6 0 s
guard ! poll . expired else {
2021-03-10 10:14:12 +01:00
os_log ( . info , log : . debug , " %{public}s[%{public}ld], %{public}s: poll %s expired. Skip for update " , ( #file as NSString ) . lastPathComponent , #line , #function , poll . id )
2021-03-03 09:12:48 +01:00
return nil
}
let timeIntervalSinceUpdate = now . timeIntervalSince ( poll . updatedAt )
2021-03-04 11:53:29 +01:00
#if DEBUG
2021-03-10 10:14:12 +01:00
let autoRefreshTimeInterval : TimeInterval = 3 // s p e e d u p t e s t i n g
2021-03-04 11:53:29 +01:00
#else
let autoRefreshTimeInterval : TimeInterval = 60
#endif
guard timeIntervalSinceUpdate > autoRefreshTimeInterval else {
2021-03-10 10:14:12 +01:00
os_log ( . info , log : . debug , " %{public}s[%{public}ld], %{public}s: poll %s updated in the %.2fs. Skip for update " , ( #file as NSString ) . lastPathComponent , #line , #function , poll . id , timeIntervalSinceUpdate )
2021-03-03 09:12:48 +01:00
return nil
}
2021-03-10 10:14:12 +01:00
os_log ( . info , log : . debug , " %{public}s[%{public}ld], %{public}s: poll %s info update… " , ( #file as NSString ) . lastPathComponent , #line , #function , poll . id )
2021-03-03 09:12:48 +01:00
return self . context . apiService . poll (
domain : toot . domain ,
pollID : poll . id ,
pollObjectID : poll . objectID ,
mastodonAuthenticationBox : authenticationBox
)
}
. setFailureType ( to : Error . self )
. switchToLatest ( )
. receive ( on : DispatchQueue . main )
. sink ( receiveCompletion : { completion in
switch completion {
case . failure ( let error ) :
2021-03-10 10:14:12 +01:00
os_log ( . info , log : . debug , " %{public}s[%{public}ld], %{public}s: poll %s info fail to update: %s " , ( #file as NSString ) . lastPathComponent , #line , #function , pollID ? ? " ? " , error . localizedDescription )
2021-03-03 09:12:48 +01:00
case . finished :
break
}
} , receiveValue : { response in
let poll = response . value
2021-03-10 10:14:12 +01:00
os_log ( . info , log : . debug , " %{public}s[%{public}ld], %{public}s: poll %s info updated " , ( #file as NSString ) . lastPathComponent , #line , #function , poll . id )
2021-03-03 09:12:48 +01:00
} )
. store ( in : & disposeBag )
2021-03-10 07:36:28 +01:00
toot ( for : cell , indexPath : indexPath )
. sink { [ weak self ] toot in
guard let self = self else { return }
guard let media = ( toot ? . mediaAttachments ? ? Set ( ) ) . first else { return }
guard let videoPlayerViewModel = self . context . videoPlaybackService . dequeueVideoPlayerViewModel ( for : media ) else { return }
DispatchQueue . main . async {
videoPlayerViewModel . willDisplay ( )
}
}
. store ( in : & disposeBag )
2021-03-03 09:12:48 +01:00
}
2021-03-10 10:14:12 +01:00
func handleTableView ( _ tableView : UITableView , didEndDisplaying cell : UITableViewCell , forRowAt indexPath : IndexPath ) {
// o s _ l o g ( " % { p u b l i c } s [ % { p u b l i c } l d ] , % { p u b l i c } s : i n d e x P a t h % s " , ( ( # f i l e a s N S S t r i n g ) . l a s t P a t h C o m p o n e n t ) , # l i n e , # f u n c t i o n , i n d e x P a t h . d e b u g D e s c r i p t i o n )
toot ( for : cell , indexPath : indexPath )
. sink { [ weak self ] toot in
guard let self = self else { return }
guard let media = ( toot ? . mediaAttachments ? ? Set ( ) ) . first else { return }
guard let videoPlayerViewModel = self . context . videoPlaybackService . dequeueVideoPlayerViewModel ( for : media ) else { return }
DispatchQueue . main . async {
videoPlayerViewModel . didEndDisplaying ( )
}
}
. store ( in : & disposeBag )
}
2021-03-03 09:12:48 +01:00
}
2021-03-10 10:14:12 +01:00
extension StatusTableViewCellDelegate where Self : StatusProvider { }