Simplify ProfilesList with implicit animations

By A LOT.
This commit is contained in:
Davide De Rosa 2022-04-21 11:43:47 +02:00
parent 0a77be6ac5
commit 759b740d62
1 changed files with 9 additions and 26 deletions

View File

@ -41,8 +41,6 @@ extension OrganizerView {
@State private var isFirstLaunch = true @State private var isFirstLaunch = true
@State private var localHeaders: [Profile.Header] = []
@State private var selectedProfileId: UUID? @State private var selectedProfileId: UUID?
init(alertType: Binding<AlertType?>) { init(alertType: Binding<AlertType?>) {
@ -57,20 +55,13 @@ extension OrganizerView {
debugChanges() debugChanges()
return Group { return Group {
mainView mainView
if localHeaders.isEmpty { if profileManager.headers.isEmpty {
emptyView emptyView
} }
}.onAppear { }.onAppear {
reloadHeaders(profileManager.headers)
performMigrationsIfNeeded() performMigrationsIfNeeded()
}.onChange(of: profileManager.headers) { newHeaders in }.onChange(of: profileManager.headers) {
withAnimation { dismissSelectionIfDeleted(headers: $0)
guard Set(newHeaders) != Set(localHeaders) else {
return
}
reloadHeaders(newHeaders)
dismissSelectionIfDeleted(headers: newHeaders)
}
} }
// from AddProfileView // from AddProfileView
@ -82,10 +73,10 @@ extension OrganizerView {
private var mainView: some View { private var mainView: some View {
List { List {
Section { Section {
ForEach(localHeaders, content: navigationLink(forHeader:)) ForEach(sortedHeaders, content: navigationLink(forHeader:))
.onDelete(perform: removeProfiles) .onDelete(perform: removeProfiles)
} }
} }.animation(.default, value: profileManager.headers)
} }
// FIXME: l10n // FIXME: l10n
@ -139,8 +130,8 @@ extension OrganizerView.ProfilesList {
} }
extension OrganizerView.ProfilesList { extension OrganizerView.ProfilesList {
private func reloadHeaders(_ newHeaders: [Profile.Header]) { private var sortedHeaders: [Profile.Header] {
localHeaders = newHeaders.sorted() profileManager.headers.sorted()
} }
private func preselectIfActiveProfile(_ id: UUID) { private func preselectIfActiveProfile(_ id: UUID) {
@ -168,15 +159,10 @@ extension OrganizerView.ProfilesList {
} }
private func removeProfiles(_ indexSet: IndexSet) { private func removeProfiles(_ indexSet: IndexSet) {
withAnimation { let currentHeaders = sortedHeaders
doRemoveProfiles(indexSet)
}
}
private func doRemoveProfiles(_ indexSet: IndexSet) {
var toDelete: [UUID] = [] var toDelete: [UUID] = []
indexSet.forEach { indexSet.forEach {
toDelete.append(localHeaders[$0].id) toDelete.append(currentHeaders[$0].id)
} }
// clear selection before removal to avoid triggering a bogus navigation push // clear selection before removal to avoid triggering a bogus navigation push
@ -185,9 +171,6 @@ extension OrganizerView.ProfilesList {
} }
profileManager.removeProfiles(withIds: toDelete) profileManager.removeProfiles(withIds: toDelete)
// IMPORTANT: synchronize headers here to prevent .onChange() from animating a second time
localHeaders.remove(atOffsets: indexSet)
} }
private func dismissSelectionIfDeleted(headers: [Profile.Header]) { private func dismissSelectionIfDeleted(headers: [Profile.Header]) {