| 1 | // Copyright © SixtyFPS GmbH <info@slint.dev> |
| 2 | // SPDX-License-Identifier: MIT |
| 3 | |
| 4 | use std::rc::Rc; |
| 5 | |
| 6 | use crate::mvc::{traits::DateTimeRepository, DateModel, TimeModel}; |
| 7 | use crate::{mvc, Callback}; |
| 8 | |
| 9 | #[derive (Clone)] |
| 10 | pub struct CreateTaskController { |
| 11 | repo: Rc<dyn mvc::traits::DateTimeRepository>, |
| 12 | back_callback: Rc<Callback<(), ()>>, |
| 13 | } |
| 14 | |
| 15 | impl CreateTaskController { |
| 16 | pub fn new(repo: impl DateTimeRepository + 'static) -> Self { |
| 17 | Self { repo: Rc::new(repo), back_callback: Rc::new(Callback::default()) } |
| 18 | } |
| 19 | |
| 20 | pub fn current_date(&self) -> DateModel { |
| 21 | self.repo.current_date() |
| 22 | } |
| 23 | |
| 24 | pub fn current_time(&self) -> TimeModel { |
| 25 | self.repo.current_time() |
| 26 | } |
| 27 | |
| 28 | pub fn date_string(&self, date_model: DateModel) -> String { |
| 29 | self.repo.date_to_string(date_model) |
| 30 | } |
| 31 | |
| 32 | pub fn time_string(&self, time_model: TimeModel) -> String { |
| 33 | self.repo.time_to_string(time_model) |
| 34 | } |
| 35 | |
| 36 | pub fn back(&self) { |
| 37 | self.back_callback.invoke(&()); |
| 38 | } |
| 39 | |
| 40 | pub fn on_back(&self, mut callback: impl FnMut() + 'static) { |
| 41 | self.back_callback.on(move |()| { |
| 42 | callback(); |
| 43 | }); |
| 44 | } |
| 45 | |
| 46 | pub fn time_stamp(&self, date_model: DateModel, time_model: TimeModel) -> i32 { |
| 47 | self.repo.time_stamp(date_model, time_model) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | #[cfg (test)] |
| 52 | mod tests { |
| 53 | use super::*; |
| 54 | use crate::mvc::MockDateTimeRepository; |
| 55 | use std::cell::Cell; |
| 56 | |
| 57 | fn test_controller() -> CreateTaskController { |
| 58 | CreateTaskController::new(MockDateTimeRepository::new( |
| 59 | DateModel { year: 2024, month: 6, day: 12 }, |
| 60 | TimeModel { hour: 13, minute: 30, second: 29 }, |
| 61 | 15, |
| 62 | )) |
| 63 | } |
| 64 | |
| 65 | #[test ] |
| 66 | fn test_current_date() { |
| 67 | let controller = test_controller(); |
| 68 | assert_eq!(controller.current_date(), DateModel { year: 2024, month: 6, day: 12 }); |
| 69 | } |
| 70 | |
| 71 | #[test ] |
| 72 | fn test_current_time() { |
| 73 | let controller = test_controller(); |
| 74 | assert_eq!(controller.current_time(), TimeModel { hour: 13, minute: 30, second: 29 }); |
| 75 | } |
| 76 | |
| 77 | #[test ] |
| 78 | fn test_date_string() { |
| 79 | let controller = test_controller(); |
| 80 | assert_eq!( |
| 81 | controller.date_string(DateModel { year: 2020, month: 10, day: 5 }).as_str(), |
| 82 | "2020/10/5" |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | #[test ] |
| 87 | fn test_time_string() { |
| 88 | let controller = test_controller(); |
| 89 | assert_eq!( |
| 90 | controller.time_string(TimeModel { hour: 10, minute: 12, second: 55 }).as_str(), |
| 91 | "10:12" |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | #[test ] |
| 96 | fn test_back() { |
| 97 | let controller = test_controller(); |
| 98 | |
| 99 | let callback_invoked = Rc::new(Cell::new(false)); |
| 100 | |
| 101 | controller.on_back({ |
| 102 | let callback_invoked = callback_invoked.clone(); |
| 103 | |
| 104 | move || { |
| 105 | callback_invoked.set(true); |
| 106 | } |
| 107 | }); |
| 108 | |
| 109 | controller.back(); |
| 110 | |
| 111 | assert!(callback_invoked.get()); |
| 112 | } |
| 113 | |
| 114 | #[test ] |
| 115 | fn test_time_stamp() { |
| 116 | let controller = test_controller(); |
| 117 | |
| 118 | assert_eq!(controller.time_stamp(DateModel::default(), TimeModel::default()), 15); |
| 119 | } |
| 120 | } |
| 121 | |