1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "bridge/declarative_frontend/jsview/models/calendar_model_impl.h" 17 18 #include "bridge/declarative_frontend/view_stack_processor.h" 19 20 namespace OHOS::Ace::Framework { Create(const CalendarModelData & calendarModelData)21void CalendarModelImpl::Create(const CalendarModelData& calendarModelData) 22 { 23 auto calendarComponent = AceType::MakeRefPtr<OHOS::Ace::CalendarComponentV2>("", "calendar"); 24 calendarComponent->SetCalendarDate(calendarModelData.date); 25 calendarComponent->SetCalendarData(calendarModelData.currentData); 26 calendarComponent->SetCalendarData(calendarModelData.preData); 27 calendarComponent->SetCalendarData(calendarModelData.nextData); 28 calendarComponent->SetControllerV2(AceType::DynamicCast<CalendarControllerV2>(calendarModelData.controller)); 29 auto pipeline = PipelineBase::GetCurrentContext(); 30 CHECK_NULL_VOID(pipeline); 31 auto theme = pipeline->GetTheme<CalendarTheme>(); 32 CHECK_NULL_VOID(theme); 33 calendarComponent->SetCalendarTheme(theme); 34 calendarComponent->SetV2Component(true); 35 ViewStackProcessor::GetInstance()->Push(calendarComponent); 36 } 37 SetOffDays(const std::string & offDays)38void CalendarModelImpl::SetOffDays(const std::string& offDays) 39 { 40 auto component = GetComponent(); 41 CHECK_NULL_VOID(component); 42 component->SetOffDays(offDays); 43 } 44 SetShowHoliday(const bool showHoliday)45void CalendarModelImpl::SetShowHoliday(const bool showHoliday) 46 { 47 auto component = GetComponent(); 48 CHECK_NULL_VOID(component); 49 component->SetShowHoliday(showHoliday); 50 } 51 SetShowLunar(const bool showLunar)52void CalendarModelImpl::SetShowLunar(const bool showLunar) 53 { 54 auto component = GetComponent(); 55 CHECK_NULL_VOID(component); 56 component->SetShowLunar(showLunar); 57 } 58 SetStartOfWeek(const int32_t startOfWeek)59void CalendarModelImpl::SetStartOfWeek(const int32_t startOfWeek) 60 { 61 auto component = GetComponent(); 62 CHECK_NULL_VOID(component); 63 int32_t startOfWeekMax = 7; 64 int32_t startOfWeekMin = 0; 65 if (startOfWeek >= startOfWeekMin && startOfWeek < startOfWeekMax) { 66 component->SetStartDayOfWeek(startOfWeek); 67 } 68 } 69 SetNeedSlide(const bool needSlide)70void CalendarModelImpl::SetNeedSlide(const bool needSlide) 71 { 72 auto component = GetComponent(); 73 CHECK_NULL_VOID(component); 74 component->SetNeedSlide(needSlide); 75 } 76 SetSelectedChangeEvent(std::function<void (const std::string &)> && selectedChangeEvent)77void CalendarModelImpl::SetSelectedChangeEvent(std::function<void(const std::string&)>&& selectedChangeEvent) 78 { 79 auto component = GetComponent(); 80 CHECK_NULL_VOID(component); 81 auto onSelectedChangeId = EventMarker(std::move(selectedChangeEvent)); 82 component->SetSelectedChangeEvent(onSelectedChangeId); 83 } 84 SetOnRequestDataEvent(std::function<void (const std::string &)> && requestData)85void CalendarModelImpl::SetOnRequestDataEvent(std::function<void(const std::string&)>&& requestData) 86 { 87 auto component = GetComponent(); 88 CHECK_NULL_VOID(component); 89 auto onRequestDataId = EventMarker(std::move(requestData)); 90 component->SetRequestDataEvent(onRequestDataId); 91 } 92 SetDirection(const int32_t dir)93void CalendarModelImpl::SetDirection(const int32_t dir) 94 { 95 auto component = GetComponent(); 96 CHECK_NULL_VOID(component); 97 if (dir == 0) { 98 component->SetAxis(Axis::VERTICAL); 99 } else if (dir == 1) { 100 component->SetAxis(Axis::HORIZONTAL); 101 } 102 } 103 SetCurrentDayStyle(const CurrentDayStyleData & currentDayStyleData,const CurrentDayStyleData & CurrentDayStyleDataImpl)104void CalendarModelImpl::SetCurrentDayStyle(const CurrentDayStyleData& currentDayStyleData, 105 const CurrentDayStyleData& CurrentDayStyleDataImpl) 106 { 107 auto component = GetComponent(); 108 CHECK_NULL_VOID(component); 109 auto& themePtr = component->GetCalendarTheme(); 110 CHECK_NULL_VOID(themePtr); 111 auto& theme = themePtr->GetCalendarTheme(); 112 113 if (CurrentDayStyleDataImpl.dayColor.has_value()) { 114 theme.dayColor = CurrentDayStyleDataImpl.dayColor.value(); 115 } 116 117 if (CurrentDayStyleDataImpl.lunarColor.has_value()) { 118 theme.lunarColor = CurrentDayStyleDataImpl.lunarColor.value(); 119 } 120 121 if (CurrentDayStyleDataImpl.markLunarColor.has_value()) { 122 theme.markLunarColor = CurrentDayStyleDataImpl.markLunarColor.value(); 123 } 124 125 if (CurrentDayStyleDataImpl.dayFontSize.has_value()) { 126 theme.dayFontSize = CurrentDayStyleDataImpl.dayFontSize.value(); 127 } 128 129 if (CurrentDayStyleDataImpl.lunarDayFontSize.has_value()) { 130 theme.lunarDayFontSize = CurrentDayStyleDataImpl.lunarDayFontSize.value(); 131 } 132 133 if (CurrentDayStyleDataImpl.dayHeight.has_value()) { 134 theme.dayHeight = CurrentDayStyleDataImpl.dayHeight.value(); 135 } 136 137 if (CurrentDayStyleDataImpl.dayWidth.has_value()) { 138 theme.dayWidth = CurrentDayStyleDataImpl.dayWidth.value(); 139 } 140 141 if (CurrentDayStyleDataImpl.gregorianCalendarHeight.has_value()) { 142 theme.gregorianCalendarHeight = CurrentDayStyleDataImpl.gregorianCalendarHeight.value(); 143 } 144 145 if (CurrentDayStyleDataImpl.lunarHeight.has_value()) { 146 theme.lunarHeight = CurrentDayStyleDataImpl.lunarHeight.value(); 147 } 148 149 if (CurrentDayStyleDataImpl.dayYAxisOffset.has_value()) { 150 theme.dayYAxisOffset = CurrentDayStyleDataImpl.dayYAxisOffset.value(); 151 } 152 153 if (CurrentDayStyleDataImpl.lunarDayYAxisOffset.has_value()) { 154 theme.lunarDayYAxisOffset = CurrentDayStyleDataImpl.lunarDayYAxisOffset.value(); 155 } 156 157 if (CurrentDayStyleDataImpl.underscoreXAxisOffset.has_value()) { 158 theme.underscoreXAxisOffset = CurrentDayStyleDataImpl.underscoreXAxisOffset.value(); 159 } 160 161 if (CurrentDayStyleDataImpl.underscoreYAxisOffset.has_value()) { 162 theme.underscoreYAxisOffset = CurrentDayStyleDataImpl.underscoreYAxisOffset.value(); 163 } 164 165 if (CurrentDayStyleDataImpl.scheduleMarkerXAxisOffset.has_value()) { 166 theme.scheduleMarkerXAxisOffset = CurrentDayStyleDataImpl.scheduleMarkerXAxisOffset.value(); 167 } 168 169 if (CurrentDayStyleDataImpl.scheduleMarkerYAxisOffset.has_value()) { 170 theme.scheduleMarkerYAxisOffset = CurrentDayStyleDataImpl.scheduleMarkerYAxisOffset.value(); 171 } 172 173 if (CurrentDayStyleDataImpl.colSpace.has_value()) { 174 theme.colSpace = CurrentDayStyleDataImpl.colSpace.value(); 175 } 176 177 if (CurrentDayStyleDataImpl.dailyFiveRowSpace.has_value()) { 178 theme.dailyFiveRowSpace = CurrentDayStyleDataImpl.dailyFiveRowSpace.value(); 179 } 180 181 if (CurrentDayStyleDataImpl.dailySixRowSpace.has_value()) { 182 theme.dailySixRowSpace = CurrentDayStyleDataImpl.dailySixRowSpace.value(); 183 } 184 185 if (CurrentDayStyleDataImpl.underscoreWidth.has_value()) { 186 theme.underscoreWidth = CurrentDayStyleDataImpl.underscoreWidth.value(); 187 } 188 189 if (CurrentDayStyleDataImpl.underscoreLength.has_value()) { 190 theme.underscoreLength = CurrentDayStyleDataImpl.underscoreLength.value(); 191 } 192 193 if (CurrentDayStyleDataImpl.scheduleMarkerRadius.has_value()) { 194 theme.scheduleMarkerRadius = CurrentDayStyleDataImpl.scheduleMarkerRadius.value(); 195 } 196 197 if (CurrentDayStyleDataImpl.boundaryRowOffset.has_value()) { 198 theme.boundaryRowOffset = CurrentDayStyleDataImpl.boundaryRowOffset.value(); 199 } 200 201 if (CurrentDayStyleDataImpl.boundaryColOffset.has_value()) { 202 theme.boundaryColOffset = CurrentDayStyleDataImpl.boundaryColOffset.value(); 203 } 204 205 if (CurrentDayStyleDataImpl.touchCircleStrokeWidth.has_value()) { 206 theme.touchCircleStrokeWidth = CurrentDayStyleDataImpl.touchCircleStrokeWidth.value(); 207 } 208 } 209 SetNonCurrentDayStyle(const NonCurrentDayStyleData & nonCurrentDayStyleData)210void CalendarModelImpl::SetNonCurrentDayStyle(const NonCurrentDayStyleData& nonCurrentDayStyleData) 211 { 212 auto component = GetComponent(); 213 CHECK_NULL_VOID(component); 214 auto& themePtr = component->GetCalendarTheme(); 215 CHECK_NULL_VOID(themePtr); 216 auto& theme = themePtr->GetCalendarTheme(); 217 218 if (nonCurrentDayStyleData.nonCurrentMonthDayColor.has_value()) { 219 theme.nonCurrentMonthDayColor = nonCurrentDayStyleData.nonCurrentMonthDayColor.value(); 220 } 221 222 if (nonCurrentDayStyleData.nonCurrentMonthLunarColor.has_value()) { 223 theme.nonCurrentMonthLunarColor = nonCurrentDayStyleData.nonCurrentMonthLunarColor.value(); 224 } 225 226 if (nonCurrentDayStyleData.nonCurrentMonthWorkDayMarkColor.has_value()) { 227 theme.nonCurrentMonthWorkDayMarkColor = nonCurrentDayStyleData.nonCurrentMonthWorkDayMarkColor.value(); 228 } 229 230 if (nonCurrentDayStyleData.nonCurrentMonthOffDayMarkColor.has_value()) { 231 theme.nonCurrentMonthOffDayMarkColor = nonCurrentDayStyleData.nonCurrentMonthOffDayMarkColor.value(); 232 } 233 } 234 SetTodayStyle(const TodayStyleData & style)235void CalendarModelImpl::SetTodayStyle(const TodayStyleData& style) 236 { 237 auto component = GetComponent(); 238 CHECK_NULL_VOID(component); 239 auto& themePtr = component->GetCalendarTheme(); 240 CHECK_NULL_VOID(themePtr); 241 auto& theme = themePtr->GetCalendarTheme(); 242 243 if (style.focusedDayColor.has_value()) { 244 theme.focusedDayColor = style.focusedDayColor.value(); 245 } 246 247 if (style.focusedLunarColor.has_value()) { 248 theme.focusedLunarColor = style.focusedLunarColor.value(); 249 } 250 251 if (style.focusedAreaBackgroundColor.has_value()) { 252 theme.focusedAreaBackgroundColor = style.focusedAreaBackgroundColor.value(); 253 } 254 255 if (style.focusedAreaRadius.has_value()) { 256 theme.focusedAreaRadius = style.focusedAreaRadius.value(); 257 } 258 } 259 SetWeekStyle(const WeekStyleData & style)260void CalendarModelImpl::SetWeekStyle(const WeekStyleData& style) 261 { 262 auto component = GetComponent(); 263 CHECK_NULL_VOID(component); 264 auto& themePtr = component->GetCalendarTheme(); 265 CHECK_NULL_VOID(themePtr); 266 auto& theme = themePtr->GetCalendarTheme(); 267 268 if (style.weekColor.has_value()) { 269 theme.weekColor = style.weekColor.value(); 270 } 271 272 if (style.weekendDayColor.has_value()) { 273 theme.weekendDayColor = style.weekendDayColor.value(); 274 } 275 276 if (style.weekendLunarColor.has_value()) { 277 theme.weekendLunarColor = style.weekendLunarColor.value(); 278 } 279 280 if (style.weekFontSize.has_value()) { 281 theme.weekFontSize = style.weekFontSize.value(); 282 } 283 if (style.weekHeight.has_value()) { 284 theme.weekHeight = style.weekHeight.value(); 285 } 286 287 if (style.weekWidth.has_value()) { 288 theme.weekWidth = style.weekWidth.value(); 289 } 290 291 if (style.weekAndDayRowSpace.has_value()) { 292 theme.weekAndDayRowSpace = style.weekAndDayRowSpace.value(); 293 } 294 } 295 SetWorkStateStyle(const WorkStateStyleData & style)296void CalendarModelImpl::SetWorkStateStyle(const WorkStateStyleData& style) 297 { 298 auto component = GetComponent(); 299 CHECK_NULL_VOID(component); 300 auto& themePtr = component->GetCalendarTheme(); 301 CHECK_NULL_VOID(themePtr); 302 auto& theme = themePtr->GetCalendarTheme(); 303 304 if (style.workDayMarkColor.has_value()) { 305 theme.workDayMarkColor = style.workDayMarkColor.value(); 306 } 307 308 if (style.offDayMarkColor.has_value()) { 309 theme.offDayMarkColor = style.offDayMarkColor.value(); 310 } 311 312 if (style.workDayMarkSize.has_value()) { 313 theme.workDayMarkSize = style.workDayMarkSize.value(); 314 } 315 316 if (style.offDayMarkSize.has_value()) { 317 theme.offDayMarkSize = style.offDayMarkSize.value(); 318 } 319 if (style.workStateWidth.has_value()) { 320 theme.workStateWidth = style.workStateWidth.value(); 321 } 322 323 if (style.workStateHorizontalMovingDistance.has_value()) { 324 theme.workStateHorizontalMovingDistance = style.workStateHorizontalMovingDistance.value(); 325 } 326 327 if (style.workStateVerticalMovingDistance.has_value()) { 328 theme.workStateVerticalMovingDistance = style.workStateVerticalMovingDistance.value(); 329 } 330 } 331 GetComponent()332RefPtr<CalendarComponentV2> CalendarModelImpl::GetComponent() 333 { 334 auto stack = ViewStackProcessor::GetInstance(); 335 if (!stack) { 336 return nullptr; 337 } 338 auto component = AceType::DynamicCast<CalendarComponentV2>(stack->GetMainComponent()); 339 if (AceApplicationInfo::GetInstance().IsRightToLeft()) { 340 component->SetTextDirection(TextDirection::RTL); 341 } 342 343 return component; 344 } 345 } // namespace OHOS::Ace::Framework 346