Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Thread selection issue on iPad #835

Merged
merged 1 commit into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Mail/Utils/NavigationStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@
*/

import Foundation
import SwiftUI
import MailCore
import SwiftUI

class NavigationStore: ObservableObject {
/// Something that represents the state of navigation
final class NavigationStore: ObservableObject {
@Published var messageReply: MessageReply?

/// Represents the state of navigation
///
/// The selected thread is the last in collection, by convention.
@Published var threadPath = [Thread]()
}
4 changes: 4 additions & 0 deletions Mail/Views/Thread List/ThreadListCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import SwiftUI

struct ThreadListCell: View {
@EnvironmentObject var splitViewManager: SplitViewManager
@EnvironmentObject var navigationStore: NavigationStore

let viewModel: ThreadListViewModel
@ObservedObject var multipleSelectionViewModel: ThreadListMultipleSelectionViewModel
Expand Down Expand Up @@ -77,7 +78,10 @@ struct ThreadListCell: View {
if splitViewManager.splitViewController?.splitBehavior == .overlay {
splitViewManager.splitViewController?.hide(.supplementary)
}

// Update both viewModel and navigationStore on the truth.
viewModel.selectedThread = thread
navigationStore.threadPath = [thread]
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Mail/Views/Thread List/ThreadListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import MailResources
import RealmSwift
import SwiftUI

class FlushAlertState: Identifiable {
final class FlushAlertState: Identifiable {
let id = UUID()
let deletedMessages: Int?
let completion: () async -> Void
Expand Down
23 changes: 18 additions & 5 deletions Mail/Views/Thread List/ThreadListViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ final class DateSection: Identifiable, Equatable {
}
}

@MainActor class ThreadListViewModel: ObservableObject {
@MainActor final class ThreadListViewModel: ObservableObject {
let mailboxManager: MailboxManager

@Published var folder: Folder
Expand Down Expand Up @@ -224,10 +224,23 @@ final class DateSection: Identifiable, Equatable {
}

func nextThreadIfNeeded(from threads: [Thread]) {
guard !isCompact,
!threads.isEmpty,
!threads.contains(where: { $0.uid == selectedThread?.uid }),
let lastIndex = selectedThreadIndex else { return }
// iPad only
guard !isCompact else {
return
}

// No more threads ?
guard !threads.isEmpty else {
selectedThread = nil
return
}

// Matching thread to selection state
guard !threads.contains(where: { $0.uid == selectedThread?.uid }),
let lastIndex = selectedThreadIndex else {
return
}

let validIndex = min(lastIndex, threads.count - 1)
selectedThread = threads[validIndex]
}
Expand Down
11 changes: 5 additions & 6 deletions Mail/Views/Thread/ThreadView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,12 @@ struct ThreadView: View {
.frame(maxWidth: .infinity)
}
.onChange(of: thread.messages) { newMessagesList in
if newMessagesList.isEmpty || thread.messageInFolderCount == 0 {
if isCompactWindow {
dismiss() // For iPhone
} else {
navigationStore.threadPath = [] // For iPad
}
guard isCompactWindow, newMessagesList.isEmpty || thread.messageInFolderCount == 0 else {
return
}

// Dismiss on iPhone only
dismiss()
}
.matomoView(view: [MatomoUtils.View.threadView.displayName, "Main"])
}
Expand Down