Hello,
My app includes a list of items sorted by their last used date. I'm trying to implement the following behaviour:
actual: users sees itemX in its previous location
(see sample code below)
Note that when I uncomment line 27, after the user uses itemX
they are immediately returned to the listView, and itemX is the first item.
Is there a way I can get itemX showing at the top, but not getting immediately (jarringly) returned to the listView from the detail view?
thanks!
My app includes a list of items sorted by their last used date. I'm trying to implement the following behaviour:
user sees a list of items (most recently used at the top)
user navigates into detail view for item X in the list
user performs an action to 'use' item X
user navigates back to the list
actual: users sees itemX in its previous location
(see sample code below)
Note that when I uncomment line 27, after the user uses itemX
they are immediately returned to the listView, and itemX is the first item.
Is there a way I can get itemX showing at the top, but not getting immediately (jarringly) returned to the listView from the detail view?
thanks!
Code Block import SwiftUI struct ContentView: View { @ObservedObject var list = ModelList.shared var body: some View { NavigationView { List(list.sorted()) {object in NavigationLink(destination: DetailView(object: object)) { Text(object.title) } } } } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } } struct DetailView: View { var object: ModelObject var body: some View { VStack { Text(object.title) Button("Use") { self.object.usedDate = Date() /* ModelList.shared.objectWillChange.send() */ } } } } class ModelObject: ObservableObject { @Published var usedDate: Date = Date(timeIntervalSince1970: 0) let title: String init(title: String) { self.title = title } } extension ModelObject: Identifiable { } class ModelList: ObservableObject { static let shared = ModelList() @Published var objects: [ModelObject] init() { self.objects = [ModelObject(title: "first"), ModelObject(title: "second")] } func sorted() -> [ModelObject] { return objects.sorted{$0.usedDate > $1.usedDate} } }