@@ -878,10 +878,10 @@ public final class StarsContext {
878878private final class StarsTransactionsContextImpl {
879879 private let account : Account
880880 private weak var starsContext : StarsContext ?
881- private let peerId : EnginePeer . Id
881+ fileprivate let peerId : EnginePeer . Id
882882 private let mode : StarsTransactionsContext . Mode
883883
884- private var _state : StarsTransactionsContext . State
884+ fileprivate var _state : StarsTransactionsContext . State
885885 private let _statePromise = Promise < StarsTransactionsContext . State > ( )
886886 var state : Signal < StarsTransactionsContext . State , NoError > {
887887 return self . _statePromise. get ( )
@@ -894,17 +894,24 @@ private final class StarsTransactionsContextImpl {
894894 init ( account: Account , subject: StarsTransactionsContext . Subject , mode: StarsTransactionsContext . Mode ) {
895895 assert ( Queue . mainQueue ( ) . isCurrent ( ) )
896896
897+
898+ let currentTransactions : [ StarsContext . State . Transaction ]
899+
897900 self . account = account
898901 switch subject {
902+ case let . starsTransactionsContext( transactionsContext) :
903+ self . peerId = transactionsContext. peerId
904+ currentTransactions = transactionsContext. currentState? . transactions ?? [ ]
899905 case let . starsContext( starsContext) :
900906 self . starsContext = starsContext
901907 self . peerId = starsContext. peerId
908+ currentTransactions = starsContext. currentState? . transactions ?? [ ]
902909 case let . peer( peerId) :
903910 self . peerId = peerId
911+ currentTransactions = [ ]
904912 }
905913 self . mode = mode
906914
907- let currentTransactions = self . starsContext? . currentState? . transactions ?? [ ]
908915 let initialTransactions : [ StarsContext . State . Transaction ]
909916 switch mode {
910917 case . all:
@@ -918,7 +925,33 @@ private final class StarsTransactionsContextImpl {
918925 self . _state = StarsTransactionsContext . State ( transactions: initialTransactions, canLoadMore: true , isLoading: false )
919926 self . _statePromise. set ( . single( self . _state) )
920927
921- if let starsContext = self . starsContext {
928+ if case let . starsTransactionsContext( transactionsContext) = subject {
929+ self . stateDisposable = ( transactionsContext. state
930+ |> deliverOnMainQueue) . start ( next: { [ weak self] state in
931+ guard let self else {
932+ return
933+ }
934+ let currentTransactions = state. transactions
935+ let filteredTransactions : [ StarsContext . State . Transaction ]
936+ switch mode {
937+ case . all:
938+ filteredTransactions = currentTransactions
939+ case . incoming:
940+ filteredTransactions = currentTransactions. filter { $0. count > 0 }
941+ case . outgoing:
942+ filteredTransactions = currentTransactions. filter { $0. count < 0 }
943+ }
944+
945+ if !filteredTransactions. isEmpty && self . _state. transactions. isEmpty && filteredTransactions != initialTransactions {
946+ var updatedState = self . _state
947+ updatedState. transactions. removeAll ( where: { $0. flags. contains ( . isLocal) } )
948+ for transaction in filteredTransactions. reversed ( ) {
949+ updatedState. transactions. insert ( transaction, at: 0 )
950+ }
951+ self . updateState ( updatedState)
952+ }
953+ } )
954+ } else if case let . starsContext( starsContext) = subject {
922955 self . stateDisposable = ( starsContext. state
923956 |> deliverOnMainQueue) . start ( next: { [ weak self] state in
924957 guard let self, let state else {
@@ -1021,6 +1054,7 @@ public final class StarsTransactionsContext {
10211054 fileprivate let impl : QueueLocalObject < StarsTransactionsContextImpl >
10221055
10231056 public enum Subject {
1057+ case starsTransactionsContext( StarsTransactionsContext )
10241058 case starsContext( StarsContext )
10251059 case peer( EnginePeer . Id )
10261060 }
@@ -1043,6 +1077,14 @@ public final class StarsTransactionsContext {
10431077 }
10441078 }
10451079
1080+ public var currentState : StarsTransactionsContext . State ? {
1081+ var state : StarsTransactionsContext . State ?
1082+ self . impl. syncWith { impl in
1083+ state = impl. _state
1084+ }
1085+ return state
1086+ }
1087+
10461088 public func reload( ) {
10471089 self . impl. with {
10481090 $0. loadMore ( reload: true )
@@ -1060,6 +1102,14 @@ public final class StarsTransactionsContext {
10601102 return StarsTransactionsContextImpl ( account: account, subject: subject, mode: mode)
10611103 } )
10621104 }
1105+
1106+ var peerId : EnginePeer . Id {
1107+ var peerId : EnginePeer . Id ?
1108+ self . impl. syncWith { impl in
1109+ peerId = impl. peerId
1110+ }
1111+ return peerId!
1112+ }
10631113}
10641114
10651115private final class StarsSubscriptionsContextImpl {
0 commit comments