Update duration format for buttons
This commit is contained in:
		| @@ -108,3 +108,28 @@ class DataController: ObservableObject { | ||||
|     //        return nil | ||||
|     //    } | ||||
| } | ||||
|  | ||||
|  | ||||
| extension Int64 { | ||||
|     func convertDurationSecondsToString() -> String { | ||||
|         var result = "" | ||||
|         // Separate into days, hours, minutes and seconds and take the largest one | ||||
|         let days: Int64 = self / 86400 | ||||
|         let hours: Int64 = self / 60 / 60 % 60 | ||||
|         let minutes: Int64 = self / 60 % 60 | ||||
|         let seconds: Int64 = self % 60 | ||||
|         if days > 0 { | ||||
|             result = "\(days)d" | ||||
|         } else if hours > 0 { | ||||
|             result = "\(hours)h" | ||||
|         } else if minutes > 0 { | ||||
|             result = "\(minutes)min" | ||||
|         } else if seconds > 0 { | ||||
|             result = "\(seconds)s" | ||||
|         } else { | ||||
|             result = "\(self)" | ||||
|         } | ||||
|  | ||||
|         return result | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -28,16 +28,16 @@ public class Flashcard: NSManagedObject { | ||||
|             allCases.sorted {$0.rawValue < $1.rawValue } | ||||
|         } | ||||
|          | ||||
|         static func getNext(milestone: SpacedRepetitionMilestoneEnum?) -> SpacedRepetitionMilestoneEnum? { | ||||
|         static func getNext(milestone: SpacedRepetitionMilestoneEnum?) -> SpacedRepetitionMilestoneEnum { | ||||
|             let sorted = SpacedRepetitionMilestoneEnum.allCasesSorted | ||||
|             if milestone == nil { | ||||
|                 return sorted.first | ||||
|                 return SpacedRepetitionMilestoneEnum.TenMinutes | ||||
|             } | ||||
|             let milestoneIndex = sorted.firstIndex(where: {$0.rawValue == milestone!.rawValue})! | ||||
|             if milestoneIndex < SpacedRepetitionMilestoneEnum.allCasesSorted.count { | ||||
|                 return sorted[milestoneIndex + 1] | ||||
|             } | ||||
|             return nil | ||||
|             return SpacedRepetitionMilestoneEnum.OneYear | ||||
|         } | ||||
|  | ||||
|         static func getMilestoneFromInt(value: Int64) -> SpacedRepetitionMilestoneEnum { | ||||
|   | ||||
| @@ -36,35 +36,35 @@ struct AnkiView: View { | ||||
|                         //                        .font(.subheadline) | ||||
|                         //                        .foregroundStyle(.gray) | ||||
|                         HStack(alignment: .center) { | ||||
|                             // TODO: Fix timeText, maybe using DateIntervallFormatter? | ||||
|                             // TODO: Maybe create an algorithm to take into account the shownCount and not just always restart from 1 min? | ||||
|                             NextRepetitionButtonView( | ||||
|                                 buttonText: "Wrong", | ||||
|                                 nextMilestone: flashcards.first!.getSpacedRepetitionMilestone(), | ||||
|                                 nextMilestone: DataController.SpacedRepetitionMilestoneEnum.OneMinute, | ||||
|                                 flashcardId: flashcards.first!.id!, | ||||
|                                 width: geometry.size.width, | ||||
|                                 color: .red, | ||||
|                                 geometry: geometry, | ||||
|                                 timeText: "1m", | ||||
|                                 timeText: DataController.SpacedRepetitionMilestoneEnum.OneMinute.rawValue.convertDurationSecondsToString(), | ||||
|                                 showDescription: $showDescription | ||||
|                             ) | ||||
|                             NextRepetitionButtonView( | ||||
|                                 buttonText: "Correct", | ||||
|                                 nextMilestone: Flashcard.SpacedRepetitionMilestoneEnum.getNext(milestone: flashcards.first!.getSpacedRepetitionMilestone()), | ||||
|                                 nextMilestone: flashcards.first!.getSpacedRepetitionMilestone(), | ||||
|                                 flashcardId: flashcards.first!.id!, | ||||
|                                 width:geometry.size.width, | ||||
|                                 color: .orange, | ||||
|                                 geometry: geometry, | ||||
|                                 timeText: "10m", | ||||
|                                 timeText: flashcards.first!.getSpacedRepetitionMilestone().rawValue.convertDurationSecondsToString(), | ||||
|                                 showDescription: $showDescription | ||||
|                             ) | ||||
|                             NextRepetitionButtonView( | ||||
|                                 buttonText: "Easy", | ||||
|                                 nextMilestone: Flashcard.SpacedRepetitionMilestoneEnum.getNext(milestone: Flashcard.SpacedRepetitionMilestoneEnum.getNext(milestone: flashcards.first!.getSpacedRepetitionMilestone())), | ||||
|                                 nextMilestone: Flashcard.SpacedRepetitionMilestoneEnum.getNext(milestone: flashcards.first!.getSpacedRepetitionMilestone()), | ||||
|                                 flashcardId: flashcards.first!.id!, | ||||
|                                 width: geometry.size.width, | ||||
|                                 color: .green, | ||||
|                                 geometry: geometry, | ||||
|                                 timeText: "1h", | ||||
|                                 timeText: Flashcard.SpacedRepetitionMilestoneEnum.getNext(milestone: flashcards.first!.getSpacedRepetitionMilestone()).rawValue.convertDurationSecondsToString(), | ||||
|                                 showDescription: $showDescription | ||||
|                             ) | ||||
|                         } | ||||
|   | ||||
| @@ -39,7 +39,7 @@ struct NextRepetitionButtonView: View { | ||||
|             }) { | ||||
|                 VStack { | ||||
|                     Text(buttonText) | ||||
|                     Text(">" + timeText) | ||||
|                     Text(timeText) | ||||
|                         .font(.footnote) | ||||
|                         .bold() | ||||
|                 } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user