mcu_hi3321_watch/application/wearable/nativeabilityfwk/include/Presenter.h
2025-05-26 20:15:20 +08:00

135 lines
4.4 KiB
C++

/*
* Copyright (c) CompanyNameMagicTag.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef APPFWK_PRESENTER_H
#define APPFWK_PRESENTER_H
#include "components/root_view.h"
#ifndef TJD_BOARD_ENABLE
#include "KeyInputListener.h"
#endif
#include "dock/focus_manager.h"
#include "PageTransitionMgr.h"
#include "NativeAbility.h"
#include "BackDragListener.h"
#ifdef TJD_BOARD_ENABLE
#include "TjdUiOnKeyListener.h"
#endif
namespace OHOS {
template<class V, class M = void>
class Presenter {
public:
Presenter() : view_(nullptr), model_(nullptr) {}
virtual ~Presenter() {}
/**
* @brief Scheduled by slice when it is started and before transition animator starts if exsit
*
* You can create non-ui resource and update ui based on Model data here
*/
virtual void OnStart() {}
/**
* @brief Scheduled by slice when it will be visiable and after transition animator ends if exsit
*
* You can set custom input event listener, request focus, and start its own animator here.
* Please call Presenter::OnResume in your implemention
*/
virtual void OnResume()
{
// make sure root container was added because it maybe removed during transition when current snap is false
if (view_->GetRootContainer()->GetParent() == nullptr) {
RootView::GetInstance()->Add(view_->GetRootContainer());
}
// Reset the default key listener
#ifdef TJD_BOARD_ENABLE
TJD::TjdUiCommonOnKeyListener::OnResume();
#else
RootView::GetInstance()->SetOnKeyActListener(KeyInputListener::GetInstance());
#endif
UIView* backView = PageTransitionMgr::GetInstance().GetTopSlideBackImage();
if (backView == nullptr) {
return;
}
BackDragListener::GetInstance()->ClearStatus();
BackDragListener::GetInstance()->SetCurrentAndTargetView(view_->GetRootContainer(), backView);
BackDragListener::GetInstance()->SetDragToTargetAction([]() { NativeAbility::GetInstance().BackToPreSlice(); });
view_->GetRootContainer()->SetOnDragListener(BackDragListener::GetInstance());
}
/**
* @brief Scheduled by slice when another slice will be started and before slice transition animator starts if exsit
*
* You can clear input event listener, clear focus, and stop animator here.
* Please call Presenter::OnPause in your implemention if default key listener need to be cleared.
*/
virtual void OnPause()
{
// Ensure that key listener is disabled during transition if exist
#ifdef TJD_BOARD_ENABLE
TJD::TjdUiCommonOnKeyListener::OnPause();
view_->GetRootContainer()->SetOnDragListener(nullptr);
//RootView::GetInstance()->ClearOnKeyActListener();
//FocusManager::GetInstance()->ClearFocus();
#else
RootView::GetInstance()->ClearOnKeyActListener();
view_->GetRootContainer()->SetOnDragListener(nullptr);
FocusManager::GetInstance()->ClearFocus();
#endif
}
/**
* @brief Presenter lifecycle scheduled by slice when it will be invisible and another slice will be visible
*
* You can destroy non-ui resources here
*/
virtual void OnStop() {}
/**
* @brief This function will attach view to this presenter
*
* @param view view pointer defined by developer
*/
void Attach(V* view)
{
view_ = view;
}
/**
* @brief This function will attach model to this presenter
*
* @param model model pointer defined by developer
*/
void Attach(M* model)
{
model_ = model;
}
protected:
V* view_;
M* model_;
BackDragListener* backDragListener_ = nullptr;
private:
Presenter(const Presenter&) = delete;
Presenter& operator=(const Presenter&) = delete;
Presenter(Presenter&&) = delete;
Presenter& operator=(Presenter&&) = delete;
};
}
#endif