diff --git a/Mixin/Database/AlbumDAO.swift b/Mixin/Database/AlbumDAO.swift index 7eb58b5793..9f07ed010a 100644 --- a/Mixin/Database/AlbumDAO.swift +++ b/Mixin/Database/AlbumDAO.swift @@ -19,6 +19,10 @@ class AlbumDAO { return MixinDatabase.shared.getCodable(condition: Album.Properties.category == AlbumCategory.PERSONAL.rawValue) } + func getSelfAlbumId() -> String? { + return getSelfAlbum()?.albumId + } + func getAlbums() -> [Album] { return MixinDatabase.shared.getCodables(condition: Album.Properties.category != AlbumCategory.PERSONAL.rawValue, orderBy: [Album.Properties.updatedAt.asOrder(by: .descending)], inTransaction: false) } diff --git a/Mixin/Database/StickerDAO.swift b/Mixin/Database/StickerDAO.swift index bd30f2375c..0cee3ef196 100644 --- a/Mixin/Database/StickerDAO.swift +++ b/Mixin/Database/StickerDAO.swift @@ -58,28 +58,37 @@ final class StickerDAO { } } - func insertOrUpdateStickers(stickers: [StickerResponse], albumId: String? = nil) { + func insertOrUpdateStickers(stickers: [StickerResponse], albumId: String) { let lastUserAtProperty = Sticker.Properties.lastUseAt.asProperty().name let propertyList = Sticker.Properties.all.filter { $0.name != lastUserAtProperty } MixinDatabase.shared.transaction { (database) in - var albumId = albumId - if albumId == nil { - let albumIdValue = try database.getValue(on: Album.Properties.albumId, fromTable: Album.tableName, where: Album.Properties.category == AlbumCategory.PERSONAL.rawValue, limit: 1) - if albumIdValue.type != .null && !albumIdValue.stringValue.isEmpty { - albumId = albumIdValue.stringValue - } - } - - if let albumId = albumId { - try database.insertOrReplace(objects: stickers.map { StickerRelationship(albumId: albumId, stickerId: $0.stickerId, createdAt: $0.createdAt) }, intoTable: StickerRelationship.tableName) - } - + try database.insertOrReplace(objects: stickers.map { StickerRelationship(albumId: albumId, stickerId: $0.stickerId, createdAt: $0.createdAt) }, intoTable: StickerRelationship.tableName) try database.insertOrReplace(objects: stickers.map { Sticker.createSticker(from: $0) }, on: propertyList, intoTable: Sticker.tableName) NotificationCenter.default.afterPostOnMain(name: .StickerDidChange) } } + func insertOrUpdateFavoriteSticker(sticker: StickerResponse) { + if let albumId = AlbumDAO.shared.getSelfAlbumId() { + insertOrUpdateStickers(stickers: [sticker], albumId: albumId) + } else { + switch StickerAPI.shared.albums() { + case let .success(albums): + for album in albums { + guard album.category == AlbumCategory.PERSONAL.rawValue else { + continue + } + AlbumDAO.shared.insertOrUpdateAblum(album: album) + insertOrUpdateStickers(stickers: [sticker], albumId: album.albumId) + break + } + case .failure: + ConcurrentJobQueue.shared.addJob(job: RefreshStickerJob()) + } + } + } + func updateUsedAt(stickerId: String, usedAt: String) { MixinDatabase.shared.update(maps: [(Sticker.Properties.lastUseAt, usedAt)], tableName: Sticker.tableName, condition: Sticker.Properties.stickerId == stickerId) } diff --git a/Mixin/UserInterface/Controllers/Chat/ConversationViewController.swift b/Mixin/UserInterface/Controllers/Chat/ConversationViewController.swift index 5ecb4ee793..f390898ef5 100644 --- a/Mixin/UserInterface/Controllers/Chat/ConversationViewController.swift +++ b/Mixin/UserInterface/Controllers/Chat/ConversationViewController.swift @@ -949,7 +949,7 @@ extension ConversationViewController: ConversationTableViewActionDelegate { switch result { case let .success(sticker): DispatchQueue.global().async { - StickerDAO.shared.insertOrUpdateStickers(stickers: [sticker]) + StickerDAO.shared.insertOrUpdateFavoriteSticker(sticker: sticker) NotificationCenter.default.postOnMain(name: .ToastMessageDidAppear, object: Localized.TOAST_ADDED) } case .failure: diff --git a/Mixin/UserInterface/Controllers/Chat/StickerAddViewController.swift b/Mixin/UserInterface/Controllers/Chat/StickerAddViewController.swift index 939c4b89cb..e3935b37b9 100644 --- a/Mixin/UserInterface/Controllers/Chat/StickerAddViewController.swift +++ b/Mixin/UserInterface/Controllers/Chat/StickerAddViewController.swift @@ -102,8 +102,7 @@ extension StickerAddViewController: ContainerViewControllerDelegate { case let .success(sticker): SDWebImageManager.shared().imageCache?.storeImageData(toDisk: Data(base64Encoded: stickerBase64), forKey: sticker.assetUrl) DispatchQueue.global().async { - StickerDAO.shared.insertOrUpdateStickers(stickers: [sticker]) - + StickerDAO.shared.insertOrUpdateFavoriteSticker(sticker: sticker) DispatchQueue.main.async { NotificationCenter.default.postOnMain(name: .ToastMessageDidAppear, object: Localized.TOAST_ADDED) self?.navigationController?.popViewController(animated: true)