Skip to content

Commit

Permalink
Migrate to TCA 1.7 (WIP) [skip test]
Browse files Browse the repository at this point in the history
  • Loading branch information
chihchy committed Jul 11, 2024
1 parent 783f613 commit b9c9c17
Show file tree
Hide file tree
Showing 34 changed files with 305 additions and 259 deletions.
2 changes: 1 addition & 1 deletion EhPanda.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -2479,7 +2479,7 @@
repositoryURL = "https://github.com/pointfreeco/swift-composable-architecture.git";
requirement = {
kind = exactVersion;
version = 1.5.6;
version = 1.7.3;
};
};
ABAC82FC26BC4866009F5026 /* XCRemoteSwiftPackageReference "SwiftyOpenCC" */ = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-composable-architecture.git",
"state" : {
"revision" : "3568f01377c6c668aad40d066acf97ce670a1dad",
"version" : "1.5.6"
"revision" : "f815e76b520aacfad4ff35c7e1f036f8add6f4b4",
"version" : "1.7.3"
}
},
{
Expand Down Expand Up @@ -145,6 +145,15 @@
"version" : "1.1.0"
}
},
{
"identity" : "swift-perception",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-perception",
"state" : {
"revision" : "d3ab98dc2887d1cc3bed676f6fa354da4cb22b3c",
"version" : "1.2.4"
}
},
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
Expand Down
1 change: 1 addition & 0 deletions EhPanda/DataFlow/AppDelegateReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import ComposableArchitecture

@Reducer
struct AppDelegateReducer {
@ObservableState
struct State: Equatable {
var migrationState = MigrationReducer.State()
}
Expand Down
3 changes: 2 additions & 1 deletion EhPanda/DataFlow/AppLockReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import ComposableArchitecture

@Reducer
struct AppLockReducer {
@ObservableState
struct State: Equatable {
@BindingState var blurRadius: Double = 0
var blurRadius: Double = 0
var becameInactiveDate: Date?
var isAppLocked = false

Expand Down
13 changes: 5 additions & 8 deletions EhPanda/DataFlow/AppReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ import ComposableArchitecture

@Reducer
struct AppReducer {
@ObservableState
struct State: Equatable {
var appDelegateState = AppDelegateReducer.State()
@BindingState var appRouteState = AppRouteReducer.State()
var appRouteState = AppRouteReducer.State()
var appLockState = AppLockReducer.State()
var tabBarState = TabBarReducer.State()
var homeState = HomeReducer.State()
var favoritesState = FavoritesReducer.State()
var searchRootState = SearchRootReducer.State()
@BindingState var settingState = SettingReducer.State()
var settingState = SettingReducer.State()
}

enum Action: BindableAction {
Expand All @@ -45,14 +46,10 @@ struct AppReducer {
LoggingReducer {
BindingReducer()
.onChange(of: \.appRouteState.route) { _, newValue in
Reduce { _, _ in
return newValue == nil ? .send(.appRoute(.clearSubStates)) : .none
}
Reduce({ _, _ in newValue == nil ? .send(.appRoute(.clearSubStates)) : .none })
}
.onChange(of: \.settingState.setting) { _, _ in
Reduce { _, _ in
return .send(.setting(.syncSetting))
}
Reduce({ _, _ in .send(.setting(.syncSetting)) })
}

Reduce { state, action in
Expand Down
23 changes: 12 additions & 11 deletions EhPanda/DataFlow/AppRouteReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ struct AppRouteReducer {
case newDawn(Greeting)
}

@ObservableState
struct State: Equatable {
@BindingState var route: Route?
var route: Route?
var hudConfig: TTProgressHUDConfig = .loading

@Heap var detailState: DetailReducer.State!
var detailState: Heap<DetailReducer.State>!

init() {
_detailState = .init(.init())
detailState = .init(.init())
}
}

Expand Down Expand Up @@ -57,12 +58,12 @@ struct AppRouteReducer {

var body: some Reducer<State, Action> {
BindingReducer()
.onChange(of: \.route) { _, newValue in
Reduce({ _, _ in newValue == nil ? .send(.clearSubStates) : .none })
}

Reduce { state, action in
switch action {
case .binding(\.$route):
return state.route == nil ? .send(.clearSubStates) : .none

case .binding:
return .none

Expand All @@ -75,7 +76,7 @@ struct AppRouteReducer {
return .none

case .clearSubStates:
state.detailState = .init()
state.detailState.wrappedValue = .init()
return .send(.detail(.teardown))

case .detectClipboardURL:
Expand All @@ -97,7 +98,7 @@ struct AppRouteReducer {
if case .detail = state.route {
delay = 1000
state.route = nil
state.detailState = .init()
state.detailState.wrappedValue = .init()
}
let (isGalleryImageURL, _, _) = urlClient.analyzeURL(url)
let gid = urlClient.parseGalleryID(url)
Expand All @@ -116,7 +117,7 @@ struct AppRouteReducer {
let (_, pageIndex, commentID) = urlClient.analyzeURL(url)
let gid = urlClient.parseGalleryID(url)
var effects = [Effect<Action>]()
state.detailState = .init()
state.detailState.wrappedValue = .init()
effects.append(.send(.detail(.fetchDatabaseInfos(gid))))
if let pageIndex = pageIndex {
effects.append(.send(.updateReadingProgress(gid, pageIndex)))
Expand All @@ -127,7 +128,7 @@ struct AppRouteReducer {
}
)
} else if let commentID = commentID {
state.detailState.commentsState?.scrollCommentID = commentID
state.detailState.wrappedValue.commentsState?.scrollCommentID = commentID
effects.append(
.run { send in
try await Task.sleep(for: .milliseconds(500))
Expand Down Expand Up @@ -190,6 +191,6 @@ struct AppRouteReducer {
hapticsClient: hapticsClient
)

Scope(state: \.detailState, action: /Action.detail, child: DetailReducer.init)
Scope(state: \.detailState.wrappedValue!, action: /Action.detail, child: DetailReducer.init)
}
}
5 changes: 3 additions & 2 deletions EhPanda/View/Detail/Archives/ArchivesReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ struct ArchivesReducer {
case fetchArchive, fetchArchiveFunds, fetchDownloadResponse
}

@ObservableState
struct State: Equatable {
@BindingState var route: Route?
@BindingState var selectedArchive: GalleryArchive.HathArchive?
var route: Route?
var selectedArchive: GalleryArchive.HathArchive?

var loadingState: LoadingState = .idle
var hathArchives = [GalleryArchive.HathArchive]()
Expand Down
25 changes: 13 additions & 12 deletions EhPanda/View/Detail/Comments/CommentsReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,20 @@ struct CommentsReducer {
case postComment, voteComment, fetchGallery
}

@ObservableState
struct State: Equatable {
@BindingState var route: Route?
@BindingState var commentContent = ""
@BindingState var postCommentFocused = false
var route: Route?
var commentContent = ""
var postCommentFocused = false

var hudConfig: TTProgressHUDConfig = .loading
var scrollCommentID: String?
var scrollRowOpacity: Double = 1

@Heap var detailState: DetailReducer.State!
var detailState: Heap<DetailReducer.State>!

init() {
_detailState = .init(.init())
detailState = .init(.init())
}
}

Expand Down Expand Up @@ -74,12 +75,12 @@ struct CommentsReducer {

var body: some Reducer<State, Action> {
BindingReducer()
.onChange(of: \.route) { _, newValue in
Reduce({ _, _ in newValue == nil ? .send(.clearSubStates) : .none })
}

Reduce { state, action in
switch action {
case .binding(\.$route):
return state.route == nil ? .send(.clearSubStates) : .none

case .binding:
return .none

Expand All @@ -88,7 +89,7 @@ struct CommentsReducer {
return route == nil ? .send(.clearSubStates) : .none

case .clearSubStates:
state.detailState = .init()
state.detailState.wrappedValue = .init()
state.commentContent = .init()
state.postCommentFocused = false
return .send(.detail(.teardown))
Expand Down Expand Up @@ -153,7 +154,7 @@ struct CommentsReducer {
}
)
} else if let commentID = commentID {
state.detailState.commentsState?.scrollCommentID = commentID
state.detailState.wrappedValue.commentsState?.wrappedValue.scrollCommentID = commentID
effects.append(
.run { send in
try await Task.sleep(for: .milliseconds(750))
Expand All @@ -171,8 +172,8 @@ struct CommentsReducer {
}

case .onAppear:
if state.detailState == nil {
state.detailState = .init()
if state.detailState.wrappedValue == nil {
state.detailState.wrappedValue = .init()
}
return state.scrollCommentID != nil ? .send(.performScrollOpacityEffect) : .none

Expand Down
43 changes: 22 additions & 21 deletions EhPanda/View/Detail/DetailReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ struct DetailReducer {
case fetchDatabaseInfos, fetchGalleryDetail, rateGallery, favorGallery, unfavorGallery, postComment, voteTag
}

@ObservableState
struct State: Equatable {
@BindingState var route: Route?
@BindingState var commentContent = ""
@BindingState var postCommentFocused = false
var route: Route?
var commentContent = ""
var postCommentFocused = false

var showsNewDawnGreeting = false
var showsUserRating = false
Expand All @@ -52,13 +53,13 @@ struct DetailReducer {
var archivesState = ArchivesReducer.State()
var torrentsState = TorrentsReducer.State()
var previewsState = PreviewsReducer.State()
@Heap var commentsState: CommentsReducer.State?
var commentsState: Heap<CommentsReducer.State?>
var galleryInfosState = GalleryInfosReducer.State()
@Heap var detailSearchState: DetailSearchReducer.State?
var detailSearchState: Heap<DetailSearchReducer.State?>

init() {
_commentsState = .init(nil)
_detailSearchState = .init(nil)
commentsState = .init(nil)
detailSearchState = .init(nil)
}

mutating func updateRating(value: DragGesture.Value) {
Expand Down Expand Up @@ -120,12 +121,12 @@ struct DetailReducer {
var body: some Reducer<State, Action> {
RecurseReducer { (self) in
BindingReducer()
.onChange(of: \.route) { _, newValue in
Reduce({ _, _ in newValue == nil ? .send(.clearSubStates) : .none })
}

Reduce { state, action in
switch action {
case .binding(\.$route):
return state.route == nil ? .send(.clearSubStates) : .none

case .binding:
return .none

Expand All @@ -138,11 +139,11 @@ struct DetailReducer {
state.archivesState = .init()
state.torrentsState = .init()
state.previewsState = .init()
state.commentsState = .init()
state.commentsState.wrappedValue = .init()
state.commentContent = .init()
state.postCommentFocused = false
state.galleryInfosState = .init()
state.detailSearchState = .init()
state.detailSearchState.wrappedValue = .init()
return .merge(
.send(.reading(.teardown)),
.send(.archives(.teardown)),
Expand All @@ -160,11 +161,11 @@ struct DetailReducer {

case .onAppear(let gid, let showsNewDawnGreeting):
state.showsNewDawnGreeting = showsNewDawnGreeting
if state.detailSearchState == nil {
state.detailSearchState = .init()
if state.detailSearchState.wrappedValue == nil {
state.detailSearchState.wrappedValue = .init()
}
if state.commentsState == nil {
state.commentsState = .init()
if state.commentsState.wrappedValue == nil {
state.commentsState.wrappedValue = .init()
}
return .send(.fetchDatabaseInfos(gid))

Expand Down Expand Up @@ -397,8 +398,8 @@ struct DetailReducer {
return .send(.anyGalleryOpsDone(result))

case .comments(.detail(let recursiveAction)):
guard state.commentsState != nil else { return .none }
return self.reduce(into: &state.commentsState!.detailState, action: recursiveAction)
guard state.commentsState.wrappedValue != nil else { return .none }
return self.reduce(into: &state.commentsState.wrappedValue!.detailState, action: recursiveAction)
.map({ Action.comments(.detail($0)) })

case .comments:
Expand All @@ -408,8 +409,8 @@ struct DetailReducer {
return .none

case .detailSearch(.detail(let recursiveAction)):
guard state.detailSearchState != nil else { return .none }
return self.reduce(into: &state.detailSearchState!.detailState, action: recursiveAction)
guard state.detailSearchState.wrappedValue != nil else { return .none }
return self.reduce(into: &state.detailSearchState.wrappedValue!.detailState, action: recursiveAction)
.map({ Action.detailSearch(.detail($0)) })

case .detailSearch:
Expand All @@ -422,7 +423,7 @@ struct DetailReducer {
then: CommentsReducer.init
)
.ifLet(
\.detailSearchState,
\.detailSearchState.wrappedValue,
action: /Action.detailSearch,
then: DetailSearchReducer.init
)
Expand Down
Loading

0 comments on commit b9c9c17

Please sign in to comment.