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

122 lines
3.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 NATIVEABILITYFWK_ABILITY_SLICE_H
#define NATIVEABILITYFWK_ABILITY_SLICE_H
#include <typeinfo>
#include "Slice.h"
#include "wearable_log.h"
namespace OHOS {
enum class SliceState : uint8_t {
UNINITIALIZED,
START,
RESUME,
PAUSE,
STOP
};
template<class V, class P, class M = void>
class AbilitySlice : public Slice {
public:
AbilitySlice<V, P, M>()
:view_(nullptr), presenter_(nullptr), state_(SliceState::UNINITIALIZED) {}
~AbilitySlice() {}
void OnStart() override
{
if ((presenter_ != nullptr) || (view_ != nullptr)) {
return;
}
presenter_ = new P();
view_ = new V();
presenter_->Attach(view_);
view_->Attach(presenter_);
WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Slice %s OnStart", typeid(view_).name());
view_->SetupView();
view_->OnStart();
WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Slice %s OnStart", typeid(presenter_).name());
presenter_->OnStart();
state_ = SliceState::START;
}
void OnResume() override
{
if ((state_ != SliceState::START) && (state_ != SliceState::PAUSE)) { // avoid repeated resume
return;
}
if (presenter_ != nullptr) {
WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Slice %s OnResume", typeid(presenter_).name());
presenter_->OnResume();
}
if (view_ != nullptr) {
view_->Draw();
}
state_ = SliceState::RESUME;
}
void OnPause() override
{
if (state_ != SliceState::RESUME) { // avoid repeated pause
return;
}
if (presenter_ != nullptr) {
WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Slice %s OnPause", typeid(presenter_).name());
presenter_->OnPause();
}
state_ = SliceState::PAUSE;
}
void OnStop() override
{
if (view_ != nullptr) {
view_->TearDownView();
WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Slice %s OnStop", typeid(view_).name());
view_->OnStop();
delete view_;
view_ = nullptr;
}
if (presenter_ != nullptr) {
WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Slice %s OnStop", typeid(presenter_).name());
presenter_->OnStop();
delete presenter_;
presenter_ = nullptr;
}
state_ = SliceState::STOP;
}
UIViewGroup* GetSliceContainer() override
{
if (view_ != nullptr) {
return view_->GetRootContainer();
} else {
return nullptr;
}
}
private:
AbilitySlice(const AbilitySlice &);
AbilitySlice &operator=(const AbilitySlice &);
V* view_;
P* presenter_;
SliceState state_;
};
} // namespace OHOS
#endif // NATIVEABILITY_ABILITY_SLICE_H