1 /*
2 * Copyright (c) 2024 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/cj_frontend/cppview/lazy_foreach_func.h"
17
18 #include "bridge/cj_frontend/cppview/data_change_listener.h"
19 #include "bridge/cj_frontend/runtime/cj_runtime_delegate.h"
20
21 using namespace OHOS::Ace;
22 using namespace OHOS::Ace::Framework;
23 using namespace OHOS::FFI;
24 using namespace std;
25
26 namespace OHOS::Ace::Framework {
27
GenerateKey(int64_t index)28 string LazyForEachFuncs::GenerateKey(int64_t index)
29 {
30 auto funcs = CJRuntimeDelegate::GetInstance()->GetCJFuncs();
31 if (!funcs.atCOHOSAceFrameworkLazyForEachFuncsGenerateKey) {
32 LOGE("Failed to invoke cj function: LazyForEachFuncs GenerateKey, return empty id!");
33 return "";
34 }
35 auto cstr = funcs.atCOHOSAceFrameworkLazyForEachFuncsGenerateKey(GetID(), index);
36 std::string res = cstr.value;
37 cstr.free(cstr.value);
38 return res;
39 }
40
GenerateItem(int64_t index)41 void LazyForEachFuncs::GenerateItem(int64_t index)
42 {
43 auto funcs = CJRuntimeDelegate::GetInstance()->GetCJFuncs();
44 void (*cjFunc)(int64_t, int64_t) = funcs.atCOHOSAceFrameworkLazyForEachFuncsGenerateItem;
45 if (!cjFunc) {
46 LOGE("Failed to invoke cj function: LazyForEachFuncs GenerateItem!");
47 return;
48 }
49 cjFunc(GetID(), index);
50 }
51
GetTotalCount()52 int64_t LazyForEachFuncs::GetTotalCount()
53 {
54 auto funcs = CJRuntimeDelegate::GetInstance()->GetCJFuncs();
55 auto cjFunc = funcs.atCOHOSAceFrameworkLazyForEachFuncsGetTotalCount;
56 if (!cjFunc) {
57 LOGE("Failed to invoke cj function: LazyForEachFuncs GetTotalCount, return 0!");
58 return 0;
59 }
60 int64_t count = cjFunc(GetID());
61 return count;
62 }
63
RegisterListenerFunc(const sptr<CJDataChangeListener> & listener)64 void LazyForEachFuncs::RegisterListenerFunc(const sptr<CJDataChangeListener>& listener)
65 {
66 auto funcs = CJRuntimeDelegate::GetInstance()->GetCJFuncs();
67 auto cjFunc = funcs.atCOHOSAceFrameworkLazyForEachFuncsDataChangeListenerRegister;
68 if (!cjFunc) {
69 LOGE("Failed to invoke cj function: LazyForEachFuncs DataChangeListenerRegister!");
70 return;
71 }
72 if (!listener) {
73 LOGE("Failed to invoke cj function: LazyForEachFuncs DataChangeListenerRegister!");
74 return;
75 }
76 cjFunc(GetID(), listener->GetID());
77 }
78
UnRegisterListenerFunc(const sptr<CJDataChangeListener> & listener)79 void LazyForEachFuncs::UnRegisterListenerFunc(const sptr<CJDataChangeListener>& listener)
80 {
81 auto funcs = CJRuntimeDelegate::GetInstance()->GetCJFuncs();
82 auto cjFunc = funcs.atCOHOSAceFrameworkLazyForEachFuncsDataChangeListenerUnregister;
83 if (!cjFunc) {
84 LOGE("Failed to invoke cj function: LazyForEachFuncs DataChangeListenerUnregister!");
85 return;
86 }
87 if (!listener) {
88 LOGE("Failed to invoke cj function: LazyForEachFuncs DataChangeListenerUnregister!");
89 return;
90 }
91 cjFunc(GetID(), listener->GetID());
92 }
93
MarkLazyForEachProcess(const std::string & key)94 void LazyForEachFuncs::MarkLazyForEachProcess(const std::string& key)
95 {
96 auto funcs = CJRuntimeDelegate::GetInstance()->GetCJFuncs();
97 auto cjFunc = funcs.atCOHOSAceFrameworkLazyForEachFuncsMarkLazy;
98 if (!cjFunc) {
99 LOGE("Failed to invoke cj function: LazyForEachFuncs MarkLazy!");
100 return;
101 }
102 cjFunc(GetID(), key.c_str());
103 }
104
ResetLazyForEachProcess()105 void LazyForEachFuncs::ResetLazyForEachProcess()
106 {
107 auto funcs = CJRuntimeDelegate::GetInstance()->GetCJFuncs();
108 auto cjFunc = funcs.atCOHOSAceFrameworkLazyForEachFuncsResetLazy;
109 if (!cjFunc) {
110 LOGE("Failed to invoke cj function: LazyForEachFuncs ResetLazy!");
111 return;
112 }
113 cjFunc(GetID());
114 }
115
RemoveChildGroupById(const std::string & composedId)116 void LazyForEachFuncs::RemoveChildGroupById(const std::string& composedId)
117 {
118 auto funcs = CJRuntimeDelegate::GetInstance()->GetCJFuncs();
119 auto cjFunc = funcs.atCOHOSAceFrameworkLazyForEachFuncsRemoveChildGroup;
120 if (!cjFunc) {
121 LOGE("Failed to invoke cj function: LazyForEachFuncs RemoveChildGroup!");
122 return;
123 }
124 cjFunc(GetID(), composedId.c_str());
125 }
126
127 } // namespace OHOS::Ace::Framework
128