mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-12-23 14:45:21 +00:00
Refactoring
This commit is contained in:
@@ -19,15 +19,15 @@ import TelegramStringFormatting
|
||||
import ItemListPeerItem
|
||||
import ItemListPeerActionItem
|
||||
|
||||
private extension PeerMuteState {
|
||||
private extension EnginePeer.NotificationSettings.MuteState {
|
||||
var timeInterval: Int32? {
|
||||
switch self {
|
||||
case .default:
|
||||
return nil
|
||||
case .unmuted:
|
||||
return 0
|
||||
case let .muted(until):
|
||||
return until
|
||||
case .default:
|
||||
return nil
|
||||
case .unmuted:
|
||||
return 0
|
||||
case let .muted(until):
|
||||
return until
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -459,26 +459,30 @@ public func notificationsPeerCategoryController(context: AccountContext, categor
|
||||
updateNotificationsDisposable.set((context.account.postbox.combinedView(keys: [key])
|
||||
|> deliverOnMainQueue).start(next: { view in
|
||||
if let view = view.views[key] as? PeerNotificationSettingsView {
|
||||
_ = context.account.postbox.transaction { transaction in
|
||||
let _ = (context.engine.data.get(
|
||||
EngineDataMap(view.notificationSettings.keys.map(TelegramEngine.EngineData.Item.Peer.Peer.init)),
|
||||
EngineDataMap(view.notificationSettings.keys.map(TelegramEngine.EngineData.Item.Peer.NotificationSettings.init))
|
||||
)
|
||||
|> deliverOnMainQueue).start(next: { peerMap, notificationSettingsMap in
|
||||
updateState { current in
|
||||
var current = current
|
||||
for (key, value) in view.notificationSettings {
|
||||
if let value = value as? TelegramPeerNotificationSettings {
|
||||
if let local = current.mode.settings[key] {
|
||||
if !value.isEqual(to: local.settings), let peer = transaction.getPeer(key), let settings = transaction.getPeerNotificationSettings(key) as? TelegramPeerNotificationSettings, !settings.isEqual(to: local.settings) {
|
||||
current = current.withUpdatedPeerSound(peer, settings.messageSound).withUpdatedPeerMuteInterval(peer, settings.muteState.timeInterval).withUpdatedPeerDisplayPreviews(peer, settings.displayPreviews)
|
||||
if !value.isEqual(to: local.settings), let maybePeer = peerMap[key], let peer = maybePeer, let settings = notificationSettingsMap[key], !settings._asNotificationSettings().isEqual(to: local.settings) {
|
||||
current = current.withUpdatedPeerSound(peer._asPeer(), settings.messageSound._asMessageSound()).withUpdatedPeerMuteInterval(peer._asPeer(), settings.muteState.timeInterval).withUpdatedPeerDisplayPreviews(peer._asPeer(), settings.displayPreviews._asDisplayPreviews())
|
||||
}
|
||||
} else if let peer = transaction.getPeer(key) {
|
||||
} else if let maybePeer = peerMap[key], let peer = maybePeer {
|
||||
if case .default = value.messageSound, case .unmuted = value.muteState, case .default = value.displayPreviews {
|
||||
} else {
|
||||
current = current.withUpdatedPeerSound(peer, value.messageSound).withUpdatedPeerMuteInterval(peer, value.muteState.timeInterval).withUpdatedPeerDisplayPreviews(peer, value.displayPreviews)
|
||||
current = current.withUpdatedPeerSound(peer._asPeer(), value.messageSound).withUpdatedPeerMuteInterval(peer._asPeer(), EnginePeer.NotificationSettings.MuteState(value.muteState).timeInterval).withUpdatedPeerDisplayPreviews(peer._asPeer(), value.displayPreviews)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return current
|
||||
}
|
||||
}.start(completed: {
|
||||
|
||||
completion()
|
||||
})
|
||||
} else {
|
||||
@@ -492,9 +496,7 @@ public func notificationsPeerCategoryController(context: AccountContext, categor
|
||||
updateNotificationsView({})
|
||||
|
||||
let presentPeerSettings: (PeerId, @escaping () -> Void) -> Void = { peerId, completion in
|
||||
let _ = (context.account.postbox.transaction { transaction -> Peer? in
|
||||
return transaction.getPeer(peerId)
|
||||
}
|
||||
let _ = (context.engine.data.get(TelegramEngine.EngineData.Item.Peer.Peer(id: peerId))
|
||||
|> deliverOnMainQueue).start(next: { peer in
|
||||
completion()
|
||||
|
||||
@@ -503,43 +505,49 @@ public func notificationsPeerCategoryController(context: AccountContext, categor
|
||||
}
|
||||
|
||||
let mode = stateValue.with { $0.mode }
|
||||
pushControllerImpl?(notificationPeerExceptionController(context: context, peer: peer, mode: mode, updatePeerSound: { peerId, sound in
|
||||
pushControllerImpl?(notificationPeerExceptionController(context: context, peer: peer._asPeer(), mode: mode, updatePeerSound: { peerId, sound in
|
||||
_ = updatePeerSound(peer.id, sound).start(next: { _ in
|
||||
updateNotificationsDisposable.set(nil)
|
||||
_ = combineLatest(updatePeerSound(peer.id, sound), context.account.postbox.loadedPeerWithId(peerId) |> deliverOnMainQueue).start(next: { _, peer in
|
||||
updateState { value in
|
||||
return value.withUpdatedPeerSound(peer, sound)
|
||||
_ = combineLatest(updatePeerSound(peer.id, sound), context.engine.data.get(TelegramEngine.EngineData.Item.Peer.Peer(id: peerId)) |> deliverOnMainQueue).start(next: { _, peer in
|
||||
if let peer = peer {
|
||||
updateState { value in
|
||||
return value.withUpdatedPeerSound(peer._asPeer(), sound)
|
||||
}
|
||||
}
|
||||
updateNotificationsView({})
|
||||
})
|
||||
})
|
||||
}, updatePeerNotificationInterval: { peerId, muteInterval in
|
||||
updateNotificationsDisposable.set(nil)
|
||||
_ = combineLatest(updatePeerNotificationInterval(peerId, muteInterval), context.account.postbox.loadedPeerWithId(peerId) |> deliverOnMainQueue).start(next: { _, peer in
|
||||
updateState { value in
|
||||
return value.withUpdatedPeerMuteInterval(peer, muteInterval)
|
||||
_ = combineLatest(updatePeerNotificationInterval(peerId, muteInterval), context.engine.data.get(TelegramEngine.EngineData.Item.Peer.Peer(id: peerId)) |> deliverOnMainQueue).start(next: { _, peer in
|
||||
if let peer = peer {
|
||||
updateState { value in
|
||||
return value.withUpdatedPeerMuteInterval(peer._asPeer(), muteInterval)
|
||||
}
|
||||
}
|
||||
updateNotificationsView({})
|
||||
})
|
||||
}, updatePeerDisplayPreviews: { peerId, displayPreviews in
|
||||
updateNotificationsDisposable.set(nil)
|
||||
_ = combineLatest(updatePeerDisplayPreviews(peerId, displayPreviews), context.account.postbox.loadedPeerWithId(peerId) |> deliverOnMainQueue).start(next: { _, peer in
|
||||
updateState { value in
|
||||
return value.withUpdatedPeerDisplayPreviews(peer, displayPreviews)
|
||||
_ = combineLatest(updatePeerDisplayPreviews(peerId, displayPreviews), context.engine.data.get(TelegramEngine.EngineData.Item.Peer.Peer(id: peerId)) |> deliverOnMainQueue).start(next: { _, peer in
|
||||
if let peer = peer {
|
||||
updateState { value in
|
||||
return value.withUpdatedPeerDisplayPreviews(peer._asPeer(), displayPreviews)
|
||||
}
|
||||
}
|
||||
updateNotificationsView({})
|
||||
})
|
||||
}, removePeerFromExceptions: {
|
||||
let _ = (context.engine.peers.removeCustomNotificationSettings(peerIds: [peerId])
|
||||
|> map { _ -> Peer? in }
|
||||
|> then(context.account.postbox.transaction { transaction -> Peer? in
|
||||
return transaction.getPeer(peerId)
|
||||
})).start(next: { peer in
|
||||
let _ = (
|
||||
context.engine.peers.removeCustomNotificationSettings(peerIds: [peerId])
|
||||
|> map { _ -> EnginePeer? in }
|
||||
|> then(context.engine.data.get(TelegramEngine.EngineData.Item.Peer.Peer(id: peerId)))
|
||||
).start(next: { peer in
|
||||
guard let peer = peer else {
|
||||
return
|
||||
}
|
||||
updateState { value in
|
||||
return value.withUpdatedPeerDisplayPreviews(peer, .default).withUpdatedPeerSound(peer, .default).withUpdatedPeerMuteInterval(peer, nil)
|
||||
return value.withUpdatedPeerDisplayPreviews(peer._asPeer(), .default).withUpdatedPeerSound(peer._asPeer(), .default).withUpdatedPeerMuteInterval(peer._asPeer(), nil)
|
||||
}
|
||||
updateNotificationsView({})
|
||||
})
|
||||
@@ -624,15 +632,8 @@ public func notificationsPeerCategoryController(context: AccountContext, categor
|
||||
actionSheet?.dismissAnimated()
|
||||
|
||||
let values = stateValue.with { $0.mode.settings.values }
|
||||
let _ = (context.account.postbox.transaction { transaction -> Void in
|
||||
for value in values {
|
||||
if transaction.getPeer(value.peer.id) == nil {
|
||||
updatePeers(transaction: transaction, peers: [value.peer], update: { _, updated in
|
||||
updated
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let _ = (context.engine.peers.ensurePeersAreLocallyAvailable(peers: values.map { EnginePeer($0.peer) })
|
||||
|> deliverOnMainQueue).start(completed: {
|
||||
updateNotificationsDisposable.set(nil)
|
||||
updateState { state in
|
||||
@@ -659,11 +660,8 @@ public func notificationsPeerCategoryController(context: AccountContext, categor
|
||||
return current.withUpdatedRevealedPeerId(peerId)
|
||||
}
|
||||
}, removePeer: { peer in
|
||||
_ = (context.account.postbox.transaction { transaction in
|
||||
if transaction.getPeer(peer.id) == nil {
|
||||
updatePeers(transaction: transaction, peers: [peer], update: { _, updated in return updated})
|
||||
}
|
||||
} |> deliverOnMainQueue).start(completed: {
|
||||
let _ = (context.engine.peers.ensurePeersAreLocallyAvailable(peers: [EnginePeer(peer)])
|
||||
|> deliverOnMainQueue).start(completed: {
|
||||
updateNotificationsDisposable.set(nil)
|
||||
updateState { value in
|
||||
return value.withUpdatedPeerMuteInterval(peer, nil).withUpdatedPeerSound(peer, .default).withUpdatedPeerDisplayPreviews(peer, .default)
|
||||
|
||||
Reference in New Issue
Block a user