123b3eb3cSopenharmony_ci/*
223b3eb3cSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
323b3eb3cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
423b3eb3cSopenharmony_ci * you may not use this file except in compliance with the License.
523b3eb3cSopenharmony_ci * You may obtain a copy of the License at
623b3eb3cSopenharmony_ci *
723b3eb3cSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
823b3eb3cSopenharmony_ci *
923b3eb3cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1023b3eb3cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1123b3eb3cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1223b3eb3cSopenharmony_ci * See the License for the specific language governing permissions and
1323b3eb3cSopenharmony_ci * limitations under the License.
1423b3eb3cSopenharmony_ci */
1523b3eb3cSopenharmony_ci
1623b3eb3cSopenharmony_ci#include "movingphoto_model_ng.h"
1723b3eb3cSopenharmony_ci#include "movingphoto_node.h"
1823b3eb3cSopenharmony_ci
1923b3eb3cSopenharmony_ci#include "core/components_ng/pattern/image/image_pattern.h"
2023b3eb3cSopenharmony_ci#include "core/components_ng/pattern/linear_layout/linear_layout_pattern.h"
2123b3eb3cSopenharmony_ci
2223b3eb3cSopenharmony_cinamespace OHOS::Ace::NG {
2323b3eb3cSopenharmony_ci
2423b3eb3cSopenharmony_cinamespace {
2523b3eb3cSopenharmony_ci    constexpr int32_t ERROR_CODE = -1;
2623b3eb3cSopenharmony_ci}
2723b3eb3cSopenharmony_ci
2823b3eb3cSopenharmony_civoid MovingPhotoModelNG::Create(const RefPtr<MovingPhotoController>& controller)
2923b3eb3cSopenharmony_ci{
3023b3eb3cSopenharmony_ci    auto* stack = ViewStackProcessor::GetInstance();
3123b3eb3cSopenharmony_ci    auto nodeId = stack->ClaimNodeId();
3223b3eb3cSopenharmony_ci    ACE_LAYOUT_SCOPED_TRACE("Create[%s][self:%d]", V2::MOVING_PHOTO_ETS_TAG, nodeId);
3323b3eb3cSopenharmony_ci    auto movingPhotoNode = MovingPhotoNode::GetOrCreateMovingPhotoNode(
3423b3eb3cSopenharmony_ci        V2::MOVING_PHOTO_ETS_TAG, nodeId, [controller]() {
3523b3eb3cSopenharmony_ci            return AceType::MakeRefPtr<MovingPhotoPattern>(controller);
3623b3eb3cSopenharmony_ci        });
3723b3eb3cSopenharmony_ci    CHECK_NULL_VOID(movingPhotoNode);
3823b3eb3cSopenharmony_ci    stack->Push(movingPhotoNode);
3923b3eb3cSopenharmony_ci
4023b3eb3cSopenharmony_ci    bool hasVideoNode = movingPhotoNode->HasVideoNode();
4123b3eb3cSopenharmony_ci    if (!hasVideoNode) {
4223b3eb3cSopenharmony_ci        auto videoId = movingPhotoNode->GetVideoId();
4323b3eb3cSopenharmony_ci        auto videoNode = FrameNode::GetOrCreateFrameNode(
4423b3eb3cSopenharmony_ci            V2::COLUMN_ETS_TAG, videoId, []() { return AceType::MakeRefPtr<LinearLayoutPattern>(true); });
4523b3eb3cSopenharmony_ci        CHECK_NULL_VOID(videoNode);
4623b3eb3cSopenharmony_ci        movingPhotoNode->AddChild(videoNode);
4723b3eb3cSopenharmony_ci    }
4823b3eb3cSopenharmony_ci    bool hasImageNode = movingPhotoNode->HasImageNode();
4923b3eb3cSopenharmony_ci    if (!hasImageNode) {
5023b3eb3cSopenharmony_ci        auto imageId = movingPhotoNode->GetImageId();
5123b3eb3cSopenharmony_ci        auto imageNode = FrameNode::GetOrCreateFrameNode(
5223b3eb3cSopenharmony_ci            V2::IMAGE_ETS_TAG, imageId, []() { return AceType::MakeRefPtr<ImagePattern>(); });
5323b3eb3cSopenharmony_ci        CHECK_NULL_VOID(imageNode);
5423b3eb3cSopenharmony_ci        movingPhotoNode->AddChild(imageNode);
5523b3eb3cSopenharmony_ci    }
5623b3eb3cSopenharmony_ci}
5723b3eb3cSopenharmony_ci
5823b3eb3cSopenharmony_civoid MovingPhotoModelNG::SetImageSrc(const std::string& value)
5923b3eb3cSopenharmony_ci{
6023b3eb3cSopenharmony_ci    auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
6123b3eb3cSopenharmony_ci    CHECK_NULL_VOID(frameNode);
6223b3eb3cSopenharmony_ci    auto layoutProperty = AceType::DynamicCast<MovingPhotoLayoutProperty>(frameNode->GetLayoutProperty());
6323b3eb3cSopenharmony_ci    CHECK_NULL_VOID(layoutProperty);
6423b3eb3cSopenharmony_ci    auto pipeline = PipelineBase::GetCurrentContext();
6523b3eb3cSopenharmony_ci    CHECK_NULL_VOID(pipeline);
6623b3eb3cSopenharmony_ci    auto dataProvider = AceType::DynamicCast<DataProviderManagerStandard>(pipeline->GetDataProviderManager());
6723b3eb3cSopenharmony_ci    CHECK_NULL_VOID(dataProvider);
6823b3eb3cSopenharmony_ci    auto movingPhotoPattern = AceType::DynamicCast<MovingPhotoPattern>(frameNode->GetPattern());
6923b3eb3cSopenharmony_ci    CHECK_NULL_VOID(movingPhotoPattern);
7023b3eb3cSopenharmony_ci    bool updateVideo = true;
7123b3eb3cSopenharmony_ci    if (layoutProperty->HasMovingPhotoUri()) {
7223b3eb3cSopenharmony_ci        auto movingPhotoUri = layoutProperty->GetMovingPhotoUri().value();
7323b3eb3cSopenharmony_ci        int64_t dateModified = dataProvider->GetMovingPhotoDateModified(value);
7423b3eb3cSopenharmony_ci        if (dateModified == ERROR_CODE) {
7523b3eb3cSopenharmony_ci            TAG_LOGW(AceLogTag::ACE_MOVING_PHOTO, "MovingPhotoDateModified return -1.");
7623b3eb3cSopenharmony_ci        }
7723b3eb3cSopenharmony_ci        int64_t currentDateModified = movingPhotoPattern->GetCurrentDateModified();
7823b3eb3cSopenharmony_ci        if (movingPhotoUri == value && dateModified == currentDateModified) {
7923b3eb3cSopenharmony_ci            TAG_LOGW(AceLogTag::ACE_MOVING_PHOTO, "src not changed.");
8023b3eb3cSopenharmony_ci            return;
8123b3eb3cSopenharmony_ci        }
8223b3eb3cSopenharmony_ci        updateVideo = false;
8323b3eb3cSopenharmony_ci        movingPhotoPattern->UpdateCurrentDateModified(dateModified);
8423b3eb3cSopenharmony_ci    }
8523b3eb3cSopenharmony_ci    ACE_UPDATE_LAYOUT_PROPERTY(MovingPhotoLayoutProperty, MovingPhotoUri, value);
8623b3eb3cSopenharmony_ci
8723b3eb3cSopenharmony_ci    std::string imageSrc = dataProvider->GetMovingPhotoImageUri(value);
8823b3eb3cSopenharmony_ci    imageSrc += "?date_modified=" + std::to_string(movingPhotoPattern->GetCurrentDateModified());
8923b3eb3cSopenharmony_ci    ImageSourceInfo src;
9023b3eb3cSopenharmony_ci    src.SetSrc(imageSrc);
9123b3eb3cSopenharmony_ci    ACE_UPDATE_LAYOUT_PROPERTY(MovingPhotoLayoutProperty, ImageSourceInfo, src);
9223b3eb3cSopenharmony_ci
9323b3eb3cSopenharmony_ci    if (!updateVideo) {
9423b3eb3cSopenharmony_ci        return;
9523b3eb3cSopenharmony_ci    }
9623b3eb3cSopenharmony_ci    int32_t fd = dataProvider->ReadMovingPhotoVideo(value);
9723b3eb3cSopenharmony_ci    ACE_UPDATE_LAYOUT_PROPERTY(MovingPhotoLayoutProperty, VideoSource, fd);
9823b3eb3cSopenharmony_ci}
9923b3eb3cSopenharmony_ci
10023b3eb3cSopenharmony_civoid MovingPhotoModelNG::SetMuted(bool value)
10123b3eb3cSopenharmony_ci{
10223b3eb3cSopenharmony_ci    auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
10323b3eb3cSopenharmony_ci    CHECK_NULL_VOID(frameNode);
10423b3eb3cSopenharmony_ci    auto movingPhotoPattern = AceType::DynamicCast<MovingPhotoPattern>(frameNode->GetPattern());
10523b3eb3cSopenharmony_ci    movingPhotoPattern->UpdateMuted(value);
10623b3eb3cSopenharmony_ci}
10723b3eb3cSopenharmony_ci
10823b3eb3cSopenharmony_civoid MovingPhotoModelNG::SetObjectFit(ImageFit objectFit)
10923b3eb3cSopenharmony_ci{
11023b3eb3cSopenharmony_ci    ACE_UPDATE_LAYOUT_PROPERTY(MovingPhotoLayoutProperty, ObjectFit, objectFit);
11123b3eb3cSopenharmony_ci}
11223b3eb3cSopenharmony_ci
11323b3eb3cSopenharmony_civoid MovingPhotoModelNG::SetOnComplete(MovingPhotoEventFunc&& onComplete)
11423b3eb3cSopenharmony_ci{
11523b3eb3cSopenharmony_ci    auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
11623b3eb3cSopenharmony_ci    CHECK_NULL_VOID(frameNode);
11723b3eb3cSopenharmony_ci    auto eventHub = frameNode->GetEventHub<MovingPhotoEventHub>();
11823b3eb3cSopenharmony_ci    CHECK_NULL_VOID(eventHub);
11923b3eb3cSopenharmony_ci    eventHub->SetOnComplete(std::move(onComplete));
12023b3eb3cSopenharmony_ci}
12123b3eb3cSopenharmony_ci
12223b3eb3cSopenharmony_civoid MovingPhotoModelNG::SetOnStart(MovingPhotoEventFunc&& onStart)
12323b3eb3cSopenharmony_ci{
12423b3eb3cSopenharmony_ci    auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
12523b3eb3cSopenharmony_ci    CHECK_NULL_VOID(frameNode);
12623b3eb3cSopenharmony_ci    auto eventHub = frameNode->GetEventHub<MovingPhotoEventHub>();
12723b3eb3cSopenharmony_ci    CHECK_NULL_VOID(eventHub);
12823b3eb3cSopenharmony_ci    eventHub->SetOnStart(std::move(onStart));
12923b3eb3cSopenharmony_ci}
13023b3eb3cSopenharmony_ci
13123b3eb3cSopenharmony_civoid MovingPhotoModelNG::SetOnStop(MovingPhotoEventFunc&& onStop)
13223b3eb3cSopenharmony_ci{
13323b3eb3cSopenharmony_ci    auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
13423b3eb3cSopenharmony_ci    CHECK_NULL_VOID(frameNode);
13523b3eb3cSopenharmony_ci    auto eventHub = frameNode->GetEventHub<MovingPhotoEventHub>();
13623b3eb3cSopenharmony_ci    CHECK_NULL_VOID(eventHub);
13723b3eb3cSopenharmony_ci    eventHub->SetOnStop(std::move(onStop));
13823b3eb3cSopenharmony_ci}
13923b3eb3cSopenharmony_ci
14023b3eb3cSopenharmony_civoid MovingPhotoModelNG::SetOnPause(MovingPhotoEventFunc&& onPause)
14123b3eb3cSopenharmony_ci{
14223b3eb3cSopenharmony_ci    auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
14323b3eb3cSopenharmony_ci    CHECK_NULL_VOID(frameNode);
14423b3eb3cSopenharmony_ci    auto eventHub = frameNode->GetEventHub<MovingPhotoEventHub>();
14523b3eb3cSopenharmony_ci    CHECK_NULL_VOID(eventHub);
14623b3eb3cSopenharmony_ci    eventHub->SetOnPause(std::move(onPause));
14723b3eb3cSopenharmony_ci}
14823b3eb3cSopenharmony_ci
14923b3eb3cSopenharmony_civoid MovingPhotoModelNG::SetOnFinish(MovingPhotoEventFunc&& onFinish)
15023b3eb3cSopenharmony_ci{
15123b3eb3cSopenharmony_ci    auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
15223b3eb3cSopenharmony_ci    CHECK_NULL_VOID(frameNode);
15323b3eb3cSopenharmony_ci    auto eventHub = frameNode->GetEventHub<MovingPhotoEventHub>();
15423b3eb3cSopenharmony_ci    CHECK_NULL_VOID(eventHub);
15523b3eb3cSopenharmony_ci    eventHub->SetOnFinish(std::move(onFinish));
15623b3eb3cSopenharmony_ci}
15723b3eb3cSopenharmony_ci
15823b3eb3cSopenharmony_civoid MovingPhotoModelNG::SetOnError(MovingPhotoEventFunc&& onError)
15923b3eb3cSopenharmony_ci{
16023b3eb3cSopenharmony_ci    auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
16123b3eb3cSopenharmony_ci    CHECK_NULL_VOID(frameNode);
16223b3eb3cSopenharmony_ci    auto eventHub = frameNode->GetEventHub<MovingPhotoEventHub>();
16323b3eb3cSopenharmony_ci    CHECK_NULL_VOID(eventHub);
16423b3eb3cSopenharmony_ci    eventHub->SetOnError(std::move(onError));
16523b3eb3cSopenharmony_ci}
16623b3eb3cSopenharmony_ci
16723b3eb3cSopenharmony_civoid MovingPhotoModelNG::AutoPlayPeriod(int64_t startTime, int64_t endTime)
16823b3eb3cSopenharmony_ci{
16923b3eb3cSopenharmony_ci    auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
17023b3eb3cSopenharmony_ci    CHECK_NULL_VOID(frameNode);
17123b3eb3cSopenharmony_ci    auto movingPhotoPattern = AceType::DynamicCast<MovingPhotoPattern>(frameNode->GetPattern());
17223b3eb3cSopenharmony_ci    CHECK_NULL_VOID(movingPhotoPattern);
17323b3eb3cSopenharmony_ci    movingPhotoPattern->AutoPlayPeriod(startTime, endTime);
17423b3eb3cSopenharmony_ci}
17523b3eb3cSopenharmony_ci
17623b3eb3cSopenharmony_civoid MovingPhotoModelNG::AutoPlay(bool isAutoPlay)
17723b3eb3cSopenharmony_ci{
17823b3eb3cSopenharmony_ci    auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
17923b3eb3cSopenharmony_ci    CHECK_NULL_VOID(frameNode);
18023b3eb3cSopenharmony_ci    auto movingPhotoPattern = AceType::DynamicCast<MovingPhotoPattern>(frameNode->GetPattern());
18123b3eb3cSopenharmony_ci    CHECK_NULL_VOID(movingPhotoPattern);
18223b3eb3cSopenharmony_ci    movingPhotoPattern->AutoPlay(isAutoPlay);
18323b3eb3cSopenharmony_ci}
18423b3eb3cSopenharmony_ci
18523b3eb3cSopenharmony_civoid MovingPhotoModelNG::RepeatPlay(bool isRepeatPlay)
18623b3eb3cSopenharmony_ci{
18723b3eb3cSopenharmony_ci    auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
18823b3eb3cSopenharmony_ci    CHECK_NULL_VOID(frameNode);
18923b3eb3cSopenharmony_ci    auto movingPhotoPattern = AceType::DynamicCast<MovingPhotoPattern>(frameNode->GetPattern());
19023b3eb3cSopenharmony_ci    CHECK_NULL_VOID(movingPhotoPattern);
19123b3eb3cSopenharmony_ci    movingPhotoPattern->RepeatPlay(isRepeatPlay);
19223b3eb3cSopenharmony_ci}
19323b3eb3cSopenharmony_ci
19423b3eb3cSopenharmony_civoid MovingPhotoModelNG::EnableAnalyzer(bool enabled)
19523b3eb3cSopenharmony_ci{
19623b3eb3cSopenharmony_ci    auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
19723b3eb3cSopenharmony_ci    CHECK_NULL_VOID(frameNode);
19823b3eb3cSopenharmony_ci    auto movingPhotoPattern = AceType::DynamicCast<MovingPhotoPattern>(frameNode->GetPattern());
19923b3eb3cSopenharmony_ci    CHECK_NULL_VOID(movingPhotoPattern);
20023b3eb3cSopenharmony_ci    movingPhotoPattern->EnableAnalyzer(enabled);
20123b3eb3cSopenharmony_ci}
20223b3eb3cSopenharmony_ci
20323b3eb3cSopenharmony_civoid MovingPhotoModelNG::SetImageAIOptions(void* options)
20423b3eb3cSopenharmony_ci{
20523b3eb3cSopenharmony_ci    auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
20623b3eb3cSopenharmony_ci    CHECK_NULL_VOID(frameNode);
20723b3eb3cSopenharmony_ci    auto movingPhotoPattern = AceType::DynamicCast<MovingPhotoPattern>(frameNode->GetPattern());
20823b3eb3cSopenharmony_ci    CHECK_NULL_VOID(movingPhotoPattern);
20923b3eb3cSopenharmony_ci    movingPhotoPattern->SetImageAIOptions(options);
21023b3eb3cSopenharmony_ci}
21123b3eb3cSopenharmony_ci} // namespace OHOS::Ace::NG