Added support of new wallpaper API methods

This commit is contained in:
Ilya Laktyushin 2019-01-10 23:17:19 +04:00
parent 515e530d4b
commit a24964036d
4 changed files with 105 additions and 26 deletions

View File

@ -112,6 +112,7 @@ private var declaredEncodables: Void = {
declareEncodable(LoggingSettings.self, f: { LoggingSettings(decoder: $0) })
declareEncodable(CachedLocalizationInfos.self, f: { CachedLocalizationInfos(decoder: $0) })
declareEncodable(CachedSecureIdConfiguration.self, f: { CachedSecureIdConfiguration(decoder: $0) })
declareEncodable(CachedWallpapersConfiguration.self, f: { CachedWallpapersConfiguration(decoder: $0) })
declareEncodable(SynchronizeGroupedPeersOperation.self, f: { SynchronizeGroupedPeersOperation(decoder: $0) })
declareEncodable(ContentPrivacySettings.self, f: { ContentPrivacySettings(decoder: $0) })
declareEncodable(TelegramDeviceContactImportedData.self, f: { TelegramDeviceContactImportedData(decoder: $0) })

View File

@ -64,6 +64,7 @@ public struct Namespaces {
public static let cachedSentMediaReferences: Int8 = 4
public static let cachedStickerQueryResults: Int8 = 5
public static let cachedSecureIdConfiguration: Int8 = 6
public static let cachedWallpapersConfiguration: Int8 = 7
}
struct UnorderedItemList {

View File

@ -67,6 +67,35 @@ public enum TelegramWallpaper: OrderedItemListEntryContents, Equatable {
}
}
}
public static func ==(lhs: TelegramWallpaper, rhs: TelegramWallpaper) -> Bool {
switch lhs {
case .builtin:
if case .builtin = rhs {
return true
} else {
return false
}
case let .color(color):
if case .color(color) = rhs {
return true
} else {
return false
}
case let .image(representations):
if case .image(representations) = rhs {
return true
} else {
return false
}
case let .file(lhsId, _, lhsIsCreator, lhsTitle, lhsSlug, _, lhsColor):
if case let .file(rhsId, _, rhsIsCreator, rhsTitle, rhsSlug, _, rhsColor) = rhs, lhsId == rhsId, lhsIsCreator == rhsIsCreator, lhsTitle == rhsTitle, lhsSlug == rhsSlug, lhsColor == rhsColor {
return true
} else {
return false
}
}
}
}
extension TelegramWallpaper {

View File

@ -7,41 +7,68 @@ import Postbox
import SwiftSignalKit
#endif
final class CachedWallpapersConfiguration: PostboxCoding {
let hash: Int32
init(hash: Int32) {
self.hash = hash
}
init(decoder: PostboxDecoder) {
self.hash = decoder.decodeInt32ForKey("hash", orElse: 0)
}
func encode(_ encoder: PostboxEncoder) {
encoder.encodeInt32(self.hash, forKey: "hash")
}
}
public func telegramWallpapers(postbox: Postbox, network: Network) -> Signal<[TelegramWallpaper], NoError> {
return postbox.transaction { transaction -> [TelegramWallpaper] in
return postbox.transaction { transaction -> ([TelegramWallpaper], Int32?) in
let configuration = transaction.retrieveItemCacheEntry(id: ItemCacheEntryId(collectionId: Namespaces.CachedItemCollection.cachedWallpapersConfiguration, key: ValueBoxKey(length: 0))) as? CachedWallpapersConfiguration
let items = transaction.getOrderedListItems(collectionId: Namespaces.OrderedItemList.CloudWallpapers)
if items.count == 0 {
return [.builtin]
return ([.builtin], 0)
} else {
return items.map { $0.contents as! TelegramWallpaper }
return (items.map { $0.contents as! TelegramWallpaper }, configuration?.hash)
}
} |> mapToSignal { list -> Signal<[TelegramWallpaper], NoError> in
let remote = network.request(Api.functions.account.getWallPapers(hash: 0))
}
|> mapToSignal { list, hash -> Signal<[TelegramWallpaper], NoError> in
let remote = network.request(Api.functions.account.getWallPapers(hash: hash ?? 0))
|> retryRequest
|> mapToSignal { result -> Signal<[TelegramWallpaper], NoError> in
return .never()
/*var items: [TelegramWallpaper] = []
for wallpaper in result {
items.append(TelegramWallpaper(apiWallpaper: wallpaper))
}
items.removeFirst()
items.insert(.builtin, at: 0)
if items == list {
return .complete()
} else {
return postbox.transaction { transaction -> [TelegramWallpaper] in
var entries: [OrderedItemListEntry] = []
for item in items {
var intValue = Int32(entries.count)
let id = MemoryBuffer(data: Data(bytes: &intValue, count: 4))
entries.append(OrderedItemListEntry(id: id, contents: item))
switch result {
case let .wallPapers(hash, wallpapers):
var items: [TelegramWallpaper] = []
var addedBuiltin = false
for apiWallpaper in wallpapers {
let wallpaper = TelegramWallpaper(apiWallpaper: apiWallpaper)
if case .file = wallpaper {
} else if !addedBuiltin {
addedBuiltin = true
items.append(.builtin)
}
items.append(wallpaper)
}
transaction.replaceOrderedItemListItems(collectionId: Namespaces.OrderedItemList.CloudWallpapers, items: entries)
return items
}
}*/
if items == list {
return .complete()
} else {
return postbox.transaction { transaction -> [TelegramWallpaper] in
var entries: [OrderedItemListEntry] = []
for item in items {
var intValue = Int32(entries.count)
let id = MemoryBuffer(data: Data(bytes: &intValue, count: 4))
entries.append(OrderedItemListEntry(id: id, contents: item))
}
transaction.replaceOrderedItemListItems(collectionId: Namespaces.OrderedItemList.CloudWallpapers, items: entries)
transaction.putItemCacheEntry(id: ItemCacheEntryId(collectionId: Namespaces.CachedItemCollection.cachedWallpapersConfiguration, key: ValueBoxKey(length: 0)), entry: CachedWallpapersConfiguration(hash: hash), collectionSpec: ItemCacheCollectionSpec(lowWaterItemCount: 1, highWaterItemCount: 1))
return items
}
}
case .wallPapersNotModified:
return .complete()
}
}
return .single(list)
|> then(remote)
@ -123,3 +150,24 @@ public func getWallpaper(account: Account, slug: String) -> Signal<TelegramWallp
return TelegramWallpaper(apiWallpaper: wallpaper)
}
}
public func saveWallpaper(account: Account, wallpaper: TelegramWallpaper) -> Signal<Void, NoError> {
return saveUnsaveWallpaper(account: account, wallpaper: wallpaper, unsave: false)
}
public func deleteWallpaper(account: Account, wallpaper: TelegramWallpaper) -> Signal<Void, NoError> {
return saveUnsaveWallpaper(account: account, wallpaper: wallpaper, unsave: true)
}
private func saveUnsaveWallpaper(account: Account, wallpaper: TelegramWallpaper, unsave: Bool) -> Signal<Void, NoError> {
guard case let .file(_, _, _, _, wallpaperSlug, _, _) = wallpaper, let slug = wallpaperSlug else {
return .complete()
}
return account.network.request(Api.functions.account.saveWallPaper(wallpaper: Api.InputWallPaper.inputWallPaperSlug(slug: slug), unsave: unsave ? Api.Bool.boolTrue : Api.Bool.boolFalse))
|> `catch` { _ -> Signal<Api.Bool, NoError> in
return .complete()
}
|> mapToSignal { _ -> Signal<Void, NoError> in
return .complete()
}
}