Skip to content

Commit

Permalink
Fix 'eraseToEffect()' is deprecated
Browse files Browse the repository at this point in the history
  • Loading branch information
Jimmy-Prime committed Mar 16, 2024
1 parent 253056d commit 53c4574
Show file tree
Hide file tree
Showing 11 changed files with 169 additions and 133 deletions.
22 changes: 11 additions & 11 deletions EhPanda/App/Tools/Clients/AuthorizationClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ extension AuthorizationClient {
return !LAContext().canEvaluatePolicy(.deviceOwnerAuthentication, error: &error)
},
localAuthroize: { reason in
Future { promise in
let context = LAContext()
var error: NSError?
Effect.publisher {
Future { promise in
let context = LAContext()
var error: NSError?

if context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &error) {
context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason) { isSuccess, _ in
promise(.success(isSuccess))
if context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &error) {
context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason) { isSuccess, _ in
promise(.success(isSuccess))
}
} else {
promise(.success(false))
}
} else {
promise(.success(false))
}
.receive(on: DispatchQueue.main)
}
.eraseToAnyPublisher()
.receive(on: DispatchQueue.main)
.eraseToEffect()
}
)
}
Expand Down
115 changes: 58 additions & 57 deletions EhPanda/App/Tools/Clients/DatabaseClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,36 @@ struct DatabaseClient {
extension DatabaseClient {
static let live: Self = .init(
prepareDatabase: {
Future { promise in
PersistenceController.shared.prepare {
switch $0 {
case .success:
promise(.success(nil))
Effect.publisher {
Future { promise in
PersistenceController.shared.prepare {
switch $0 {
case .success:
promise(.success(nil))

case .failure(let appError):
promise(.success(appError))
case .failure(let appError):
promise(.success(appError))
}
}
}
.receive(on: DispatchQueue.main)
}
.eraseToAnyPublisher()
.receive(on: DispatchQueue.main)
.eraseToEffect()
},
dropDatabase: {
Future { promise in
PersistenceController.shared.rebuild {
switch $0 {
case .success:
promise(.success(nil))
Effect.publisher {
Future { promise in
PersistenceController.shared.rebuild {
switch $0 {
case .success:
promise(.success(nil))

case .failure(let appError):
promise(.success(appError))
case .failure(let appError):
promise(.success(appError))
}
}
}
.receive(on: DispatchQueue.main)
}
.eraseToAnyPublisher()
.receive(on: DispatchQueue.main)
.eraseToEffect()
},
saveContext: {
let context = PersistenceController.shared.container.viewContext
Expand Down Expand Up @@ -217,49 +217,49 @@ extension DatabaseClient {
return entity
}
func fetchAppEnv() -> Effect<AppEnv> {
Future { promise in
DispatchQueue.main.async {
promise(.success(fetchOrCreate(entityType: AppEnvMO.self).toEntity()))
Effect.publisher {
Future { promise in
DispatchQueue.main.async {
promise(.success(fetchOrCreate(entityType: AppEnvMO.self).toEntity()))
}
}
.receive(on: DispatchQueue.main)
}
.eraseToAnyPublisher()
.receive(on: DispatchQueue.main)
.eraseToEffect()
}
func fetchAppEnvSynchronously() -> AppEnv {
fetchOrCreate(entityType: AppEnvMO.self).toEntity()
}
func fetchGalleryState(gid: String) -> Effect<GalleryState> {
guard gid.isValidGID else { return .none }
return Future { promise in
DispatchQueue.main.async {
promise(.success(
fetchOrCreate(entityType: GalleryStateMO.self, gid: gid).toEntity()
))
return Effect.publisher {
Future { promise in
DispatchQueue.main.async {
promise(.success(
fetchOrCreate(entityType: GalleryStateMO.self, gid: gid).toEntity()
))
}
}
.receive(on: DispatchQueue.main)
}
.eraseToAnyPublisher()
.receive(on: DispatchQueue.main)
.eraseToEffect()
}
func fetchHistoryGalleries(fetchLimit: Int = 0) -> Effect<[Gallery]> {
Future { promise in
DispatchQueue.main.async {
let predicate = NSPredicate(format: "lastOpenDate != nil")
let sortDescriptor = NSSortDescriptor(
keyPath: \GalleryMO.lastOpenDate, ascending: false
)
let galleries = batchFetch(
entityType: GalleryMO.self, fetchLimit: fetchLimit, predicate: predicate,
findBeforeFetch: false, sortDescriptors: [sortDescriptor]
)
.map { $0.toEntity() }
promise(.success(galleries))
Effect.publisher {
Future { promise in
DispatchQueue.main.async {
let predicate = NSPredicate(format: "lastOpenDate != nil")
let sortDescriptor = NSSortDescriptor(
keyPath: \GalleryMO.lastOpenDate, ascending: false
)
let galleries = batchFetch(
entityType: GalleryMO.self, fetchLimit: fetchLimit, predicate: predicate,
findBeforeFetch: false, sortDescriptors: [sortDescriptor]
)
.map { $0.toEntity() }
promise(.success(galleries))
}
}
.receive(on: DispatchQueue.main)
}
.eraseToAnyPublisher()
.receive(on: DispatchQueue.main)
.eraseToEffect()
}
}
// MARK: FetchAccessor
Expand Down Expand Up @@ -529,14 +529,15 @@ extension DatabaseClient {

// Update User
func updateUserProperty(_ commitChanges: @escaping (inout User) -> Void) -> Effect<Never> {
fetchAppEnv().map(\.user)
.map { (user: User) -> User in
var user = user
commitChanges(&user)
return user
}
.flatMap(updateUser)
.eraseToEffect()
Effect.publisher {
fetchAppEnv().map(\.user)
.map { (user: User) -> User in
var user = user
commitChanges(&user)
return user
}
.flatMap(updateUser)
}
}
func updateGreeting(_ greeting: Greeting) -> Effect<Never> {
updateUserProperty { user in
Expand Down
27 changes: 14 additions & 13 deletions EhPanda/App/Tools/Clients/ImageClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,24 @@ extension ImageClient {
})
},
saveImageToPhotoLibrary: { (image, isAnimated) in
Future { promise in
DispatchQueue.global(qos: .utility).async {
if let data = image.kf.data(format: isAnimated ? .GIF : .unknown) {
PHPhotoLibrary.shared().performChanges {
let request = PHAssetCreationRequest.forAsset()
request.addResource(with: .photo, data: data, options: nil)
} completionHandler: { (isSuccess, _) in
promise(.success(isSuccess))
Effect.publisher {
Future { promise in
DispatchQueue.global(qos: .utility).async {
if let data = image.kf.data(format: isAnimated ? .GIF : .unknown) {
PHPhotoLibrary.shared().performChanges {
let request = PHAssetCreationRequest.forAsset()
request.addResource(with: .photo, data: data, options: nil)
} completionHandler: { (isSuccess, _) in
promise(.success(isSuccess))
}
} else {
promise(.success(false))
}
} else {
promise(.success(false))
}
}
.eraseToAnyPublisher()
.receive(on: DispatchQueue.main)
}
.eraseToAnyPublisher()
.receive(on: DispatchQueue.main)
.eraseToEffect()
},
downloadImage: { url in
Future { promise in
Expand Down
22 changes: 11 additions & 11 deletions EhPanda/App/Tools/Clients/LibraryClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,23 @@ extension LibraryClient {
})
},
analyzeImageColors: { image in
Future { promise in
image.getColors(quality: .lowest) { colors in
promise(.success(colors))
Effect.publisher {
Future { promise in
image.getColors(quality: .lowest) { colors in
promise(.success(colors))
}
}
}
.eraseToAnyPublisher()
.eraseToEffect()
},
calculateWebImageDiskCacheSize: {
Future { promise in
KingfisherManager.shared.cache.calculateDiskStorageSize {
promise(.success(try? $0.get()))
Effect.publisher {
Future { promise in
KingfisherManager.shared.cache.calculateDiskStorageSize {
promise(.success(try? $0.get()))
}
}
.receive(on: DispatchQueue.main)
}
.eraseToAnyPublisher()
.receive(on: DispatchQueue.main)
.eraseToEffect()
}
)
}
Expand Down
13 changes: 7 additions & 6 deletions EhPanda/DataFlow/AppReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,13 @@ struct AppReducer: Reducer {
effects.append(Effect.send(.setting(.setNavigation(.account))))
if !cookieClient.didLogin {
effects.append(
Effect.send(.setting(.account(.setNavigation(.login))))
.delay(
for: .milliseconds(deviceClient.isPad() ? 1200 : 200),
scheduler: DispatchQueue.main
)
.eraseToEffect()
Effect.publisher {
Effect.send(.setting(.account(.setNavigation(.login))))
.delay(
for: .milliseconds(deviceClient.isPad() ? 1200 : 200),
scheduler: DispatchQueue.main
)
}
)
}
return .merge(effects)
Expand Down
30 changes: 20 additions & 10 deletions EhPanda/DataFlow/AppRouteReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,15 @@ struct AppRouteReducer: Reducer {
let (isGalleryImageURL, _, _) = urlClient.analyzeURL(url)
let gid = urlClient.parseGalleryID(url)
guard databaseClient.fetchGallery(gid: gid) == nil else {
return Effect.send(.handleGalleryLink(url))
.delay(for: .milliseconds(delay + 250), scheduler: DispatchQueue.main).eraseToEffect()
return Effect.publisher {
Effect.send(.handleGalleryLink(url))
.delay(for: .milliseconds(delay + 250), scheduler: DispatchQueue.main)
}
}
return Effect.publisher {
Effect.send(.fetchGallery(url, isGalleryImageURL))
.delay(for: .milliseconds(delay), scheduler: DispatchQueue.main)
}
return Effect.send(.fetchGallery(url, isGalleryImageURL))
.delay(for: .milliseconds(delay), scheduler: DispatchQueue.main).eraseToEffect()

case .handleGalleryLink(let url):
let (_, pageIndex, commentID) = urlClient.analyzeURL(url)
Expand All @@ -116,14 +120,18 @@ struct AppRouteReducer: Reducer {
if let pageIndex = pageIndex {
effects.append(Effect.send(.updateReadingProgress(gid, pageIndex)))
effects.append(
Effect.send(.detail(.setNavigation(.reading)))
.delay(for: .milliseconds(500), scheduler: DispatchQueue.main).eraseToEffect()
Effect.publisher {
Effect.send(.detail(.setNavigation(.reading)))
.delay(for: .milliseconds(500), scheduler: DispatchQueue.main)
}
)
} else if let commentID = commentID {
state.detailState.commentsState?.scrollCommentID = commentID
effects.append(
Effect.send(.detail(.setNavigation(.comments(url))))
.delay(for: .milliseconds(500), scheduler: DispatchQueue.main).eraseToEffect()
Effect.publisher {
Effect.send(.detail(.setNavigation(.comments(url))))
.delay(for: .milliseconds(500), scheduler: DispatchQueue.main)
}
)
}
effects.append(Effect.send(.setNavigation(.detail(gid))))
Expand All @@ -148,8 +156,10 @@ struct AppRouteReducer: Reducer {
Effect.send(.handleGalleryLink(url))
)
case .failure:
return Effect.send(.setHUDConfig(.error))
.delay(for: .milliseconds(500), scheduler: DispatchQueue.main).eraseToEffect()
return Effect.publisher {
Effect.send(Action.setHUDConfig(.error))
.delay(for: .milliseconds(500), scheduler: DispatchQueue.main)
}
}

case .fetchGreetingDone(let result):
Expand Down
Loading

0 comments on commit 53c4574

Please sign in to comment.