Skip to content

Commit

Permalink
Fix first add custom issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Jun Tang committed Jun 30, 2018
1 parent 6ad0749 commit fe9c417
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
4 changes: 4 additions & 0 deletions Mixin/Database/AlbumDAO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
35 changes: 22 additions & 13 deletions Mixin/Database/StickerDAO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit fe9c417

Please sign in to comment.