Skip to content

Commit

Permalink
fix: Thread selection issue on iPad
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien-coye committed Jun 27, 2023
1 parent f9cb09c commit 4c48bad
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 14 deletions.
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

0 comments on commit 4c48bad

Please sign in to comment.