From 4e691bfd5f7e7546c3dfb7d146b786f7429cdde9 Mon Sep 17 00:00:00 2001 From: Rayhan Date: Mon, 24 Jun 2024 17:50:58 +0200 Subject: [PATCH] now historic are sort by date --- .../Game/Historic/HistoricListVM.swift | 7 +++++++ .../ViewModel/Game/Historic/HistoricVM.swift | 6 ++++++ .../DouShouQi_App/Views/Game/HistoricView.swift | 17 +++++++++-------- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/DouShouQi_App/DouShouQi_App/ViewModel/Game/Historic/HistoricListVM.swift b/DouShouQi_App/DouShouQi_App/ViewModel/Game/Historic/HistoricListVM.swift index 1cd55b6..ac649b8 100644 --- a/DouShouQi_App/DouShouQi_App/ViewModel/Game/Historic/HistoricListVM.swift +++ b/DouShouQi_App/DouShouQi_App/ViewModel/Game/Historic/HistoricListVM.swift @@ -24,6 +24,13 @@ class HistoricListVM: ObservableObject { return historicList } + public func groupByDate() -> [String: [HistoricVM]] { + let groupedDictionary = Dictionary(grouping: historicList) { historicVM in + return historicVM.date + } + return groupedDictionary + } + init() { historicList = [] diff --git a/DouShouQi_App/DouShouQi_App/ViewModel/Game/Historic/HistoricVM.swift b/DouShouQi_App/DouShouQi_App/ViewModel/Game/Historic/HistoricVM.swift index 1cab0e4..cd788d9 100644 --- a/DouShouQi_App/DouShouQi_App/ViewModel/Game/Historic/HistoricVM.swift +++ b/DouShouQi_App/DouShouQi_App/ViewModel/Game/Historic/HistoricVM.swift @@ -20,6 +20,12 @@ class HistoricVM: ObservableObject, Identifiable, Hashable{ } @Published var historic: Historique + var date: String { + let dateFormatter = DateFormatter() + dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss" + return dateFormatter.string(from: Date()) + } + init(historic: Historique) { self.historic = historic diff --git a/DouShouQi_App/DouShouQi_App/Views/Game/HistoricView.swift b/DouShouQi_App/DouShouQi_App/Views/Game/HistoricView.swift index cda96d9..033af73 100644 --- a/DouShouQi_App/DouShouQi_App/Views/Game/HistoricView.swift +++ b/DouShouQi_App/DouShouQi_App/Views/Game/HistoricView.swift @@ -1,7 +1,4 @@ import SwiftUI -#if DEBUG -import DouShouQiModel -#endif struct HistoricView: View { @@ -11,15 +8,19 @@ struct HistoricView: View { VStack { TitlePageFrame(Text: "Historic", ImageWidth: 200, ImageHeight: 200) - VStack { - List(historicListVM.historicList) { historicVM in - GameResumeFrame(historicVm: historicVM) + // Afficher chaque groupe de dates dans une section distincte + ForEach(historicListVM.groupByDate().sorted(by: { $0.key > $1.key }), id: \.key) { key, historicVMs in + VStack(alignment: .leading) { + Text(key) // Affiche la date comme titre de section + + List(historicVMs) { historicVM in + GameResumeFrame(historicVm: historicVM) + } } + .padding(.horizontal, 10) } - .padding(.horizontal, 10) Spacer() - } } }