Skip to content

Commit 4cd32d8

Browse files
committed
Add State to ChannelViewItem struct
1 parent 0fd1bf3 commit 4cd32d8

File tree

4 files changed

+61
-6
lines changed

4 files changed

+61
-6
lines changed

LightningWallet/Modules/Channels/ChannelCell.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class ChannelCell: UITableViewCell {
2121
}
2222

2323
func bind(viewItem: ChannelViewItem) {
24-
label.text = "Remote PubKey: \(viewItem.remotePubKey)\nLocalBalance: \(viewItem.localBalance)\nRemote Balance: \(viewItem.remoteBalance)"
24+
label.text = "State: \(viewItem.state)\nRemote PubKey: \(viewItem.remotePubKey)\nLocalBalance: \(viewItem.localBalance)\nRemote Balance: \(viewItem.remoteBalance)"
2525
}
2626

2727
}

LightningWallet/Modules/Channels/ChannelsInteractor.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,14 @@ extension ChannelsInteractor: IChannelsInteractor {
2626
.disposed(by: disposeBag)
2727
}
2828

29+
func fetchPendingChannels() {
30+
lightningKit.pendingChannelsSingle
31+
.subscribeOn(ConcurrentDispatchQueueScheduler(qos: .userInitiated))
32+
.observeOn(MainScheduler.instance)
33+
.subscribe(onSuccess: { [weak self] response in
34+
self?.delegate?.didUpdatePendingChannels(response: response)
35+
})
36+
.disposed(by: disposeBag)
37+
}
38+
2939
}

LightningWallet/Modules/Channels/ChannelsModule.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,25 @@ protocol IChannelsViewDelegate {
1818

1919
protocol IChannelsInteractor {
2020
func fetchOpenChannels()
21+
func fetchPendingChannels()
2122
}
2223

2324
protocol IChannelsInteractorDelegate: AnyObject {
2425
func didUpdate(openChannels: [Lnrpc_Channel])
26+
func didUpdatePendingChannels(response: Lnrpc_PendingChannelsResponse)
2527
}
2628

2729
struct ChannelViewItem {
30+
let state: State
2831
let remotePubKey: String
2932
let localBalance: Int
3033
let remoteBalance: Int
34+
35+
enum State {
36+
case open
37+
case pendingOpen
38+
case pendingClosing
39+
case pendingForceClosing
40+
case waitingClose
41+
}
3142
}

LightningWallet/Modules/Channels/ChannelsPresenter.swift

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,36 @@ class ChannelsPresenter {
66
private let interactor: IChannelsInteractor
77
private let router: IChannelsRouter
88

9+
private var openChannels: [Lnrpc_Channel] = []
10+
private var pendingOpenChannels: [Lnrpc_PendingChannelsResponse.PendingOpenChannel] = []
11+
private var pendingClosingChannels: [Lnrpc_PendingChannelsResponse.ClosedChannel] = []
12+
private var pendingForceClosingChannels: [Lnrpc_PendingChannelsResponse.ForceClosedChannel] = []
13+
private var waitingCloseChannels: [Lnrpc_PendingChannelsResponse.WaitingCloseChannel] = []
14+
915
init(interactor: IChannelsInteractor, router: IChannelsRouter) {
1016
self.interactor = interactor
1117
self.router = router
1218
}
1319

20+
private func syncView() {
21+
let factory = ChannelsViewItemFactory()
22+
23+
let openChannelViewItems = openChannels.map { factory.viewItem(channel: $0) }
24+
let pendingOpenChannelViewItems = pendingOpenChannels.map { factory.viewItem(state: .pendingOpen, pendingChannel: $0.channel) }
25+
let pendingClosingChannelViewItems = pendingClosingChannels.map { factory.viewItem(state: .pendingClosing, pendingChannel: $0.channel) }
26+
let pendingForceClosingChannelViewItems = pendingForceClosingChannels.map { factory.viewItem(state: .pendingForceClosing, pendingChannel: $0.channel) }
27+
let waitingCloseChannelViewItems = waitingCloseChannels.map { factory.viewItem(state: .waitingClose, pendingChannel: $0.channel) }
28+
29+
view?.show(viewItems: openChannelViewItems + pendingOpenChannelViewItems + pendingClosingChannelViewItems + pendingForceClosingChannelViewItems + waitingCloseChannelViewItems)
30+
}
31+
1432
}
1533

1634
extension ChannelsPresenter: IChannelsViewDelegate {
1735

1836
func onLoad() {
1937
interactor.fetchOpenChannels()
38+
interactor.fetchPendingChannels()
2039
}
2140

2241
func onClose() {
@@ -40,11 +59,16 @@ extension ChannelsPresenter: IChannelsViewDelegate {
4059
extension ChannelsPresenter: IChannelsInteractorDelegate {
4160

4261
func didUpdate(openChannels: [Lnrpc_Channel]) {
43-
let factory = ChannelsViewItemFactory()
44-
let viewItems = openChannels.map {
45-
factory.viewItem(channel: $0)
46-
}
47-
view?.show(viewItems: viewItems)
62+
self.openChannels = openChannels
63+
syncView()
64+
}
65+
66+
func didUpdatePendingChannels(response: Lnrpc_PendingChannelsResponse) {
67+
self.pendingOpenChannels = response.pendingOpenChannels
68+
self.pendingClosingChannels = response.pendingClosingChannels
69+
self.pendingForceClosingChannels = response.pendingForceClosingChannels
70+
self.waitingCloseChannels = response.waitingCloseChannels
71+
syncView()
4872
}
4973

5074
}
@@ -53,10 +77,20 @@ class ChannelsViewItemFactory {
5377

5478
func viewItem(channel: Lnrpc_Channel) -> ChannelViewItem {
5579
ChannelViewItem(
80+
state: .open,
5681
remotePubKey: channel.remotePubkey,
5782
localBalance: Int(channel.localBalance),
5883
remoteBalance: Int(channel.remoteBalance)
5984
)
6085
}
6186

87+
func viewItem(state: ChannelViewItem.State, pendingChannel: Lnrpc_PendingChannelsResponse.PendingChannel) -> ChannelViewItem {
88+
ChannelViewItem(
89+
state: state,
90+
remotePubKey: pendingChannel.remoteNodePub,
91+
localBalance: Int(pendingChannel.localBalance),
92+
remoteBalance: Int(pendingChannel.remoteBalance)
93+
)
94+
}
95+
6296
}

0 commit comments

Comments
 (0)