Skip to content

Commit cb15290

Browse files
committed
🎨 Format and improve the plugin project
1 parent bbb8148 commit cb15290

7 files changed

+159
-30
lines changed

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ example/windows/flutter/generated_plugins.cmake
2020
example/windows/flutter/generated_plugin_registrant.cc
2121
example/windows/flutter/generated_plugin_registrant.h
2222
windows/deps/*
23+
.vscode/settings.json

‎windows/CMakeLists.txt

+7-3
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,21 @@ set(PLUGIN_NAME "zego_express_engine_plugin")
1111

1212
# Setup deps: native SDK header and link search path
1313
include_directories(${CMAKE_CURRENT_LIST_DIR}/deps/include)
14+
15+
# It seems that flutter doesn't support windows on x86
1416
link_directories(${CMAKE_CURRENT_LIST_DIR}/deps/lib/x64)
1517

1618
add_library(${PLUGIN_NAME} SHARED
17-
"zego_express_engine_plugin.cpp"
19+
${CMAKE_CURRENT_LIST_DIR}/zego_express_engine_plugin.cpp
20+
${CMAKE_CURRENT_LIST_DIR}/internal/ZegoExpressEngineMethodHandler.cpp
21+
${CMAKE_CURRENT_LIST_DIR}/internal/ZegoExpressEngineMethodHandler.h
22+
${CMAKE_CURRENT_LIST_DIR}/internal/ZegoExpressEngineEventHandler.cpp
23+
${CMAKE_CURRENT_LIST_DIR}/internal/ZegoExpressEngineEventHandler.h
1824
)
1925

2026
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /utf-8")
2127
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /utf-8")
2228

23-
24-
2529
apply_standard_settings(${PLUGIN_NAME})
2630
set_target_properties(${PLUGIN_NAME} PROPERTIES
2731
CXX_VISIBILITY_PRESET hidden)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "ZegoExpressEngineEventHandler.h"
2+
3+
#include <memory>
4+
5+
void ZegoExpressEngineEventHandler::setEventSink(std::unique_ptr<flutter::EventSink<flutter::EncodableValue>>&& eventSink)
6+
{
7+
eventSink_ = std::move(eventSink);
8+
}
9+
10+
void ZegoExpressEngineEventHandler::clearEventSink()
11+
{
12+
eventSink_.reset();
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#pragma once
2+
3+
#include <flutter/event_channel.h>
4+
5+
#include <ZegoExpressSDK.h>
6+
using namespace ZEGO;
7+
8+
class ZegoExpressEngineEventHandler
9+
{
10+
public:
11+
~ZegoExpressEngineEventHandler(){}
12+
13+
static ZegoExpressEngineEventHandler& getInstance()
14+
{
15+
static ZegoExpressEngineEventHandler m_instance;
16+
return m_instance;
17+
}
18+
19+
void setEventSink(std::unique_ptr<flutter::EventSink<flutter::EncodableValue>> &&eventSink);
20+
void clearEventSink();
21+
22+
private:
23+
std::unique_ptr<flutter::EventSink<flutter::EncodableValue>> eventSink_;
24+
25+
private:
26+
ZegoExpressEngineEventHandler() = default;
27+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "ZegoExpressEngineMethodHandler.h"
2+
3+
#include <variant>
4+
#include <flutter/encodable_value.h>
5+
6+
std::string ZegoExpressEngineMethodHandler::getVersion()
7+
{
8+
return EXPRESS::ZegoExpressSDK::getVersion();
9+
}
10+
11+
void ZegoExpressEngineMethodHandler::createEngine(const flutter::MethodCall<flutter::EncodableValue>& method_call,
12+
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result)
13+
{
14+
auto argument = std::get<flutter::EncodableMap>(*method_call.arguments());
15+
unsigned int appID = (unsigned int)std::get<int64_t>(argument["appID"]);
16+
std::string appSign = std::get<std::string>(argument["appSign"]);
17+
bool isTestEnv = std::get<bool>(argument["isTestEnv"]);
18+
int scenario = std::get<int32_t>(argument["scenario"]);
19+
20+
EXPRESS::ZegoExpressSDK::createEngine(appID, appSign, isTestEnv, (EXPRESS::ZegoScenario)scenario, nullptr);
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#pragma once
2+
3+
#include <flutter/method_channel.h>
4+
5+
#include <ZegoExpressSDK.h>
6+
using namespace ZEGO;
7+
8+
class ZegoExpressEngineMethodHandler
9+
{
10+
public:
11+
~ZegoExpressEngineMethodHandler(){}
12+
13+
static ZegoExpressEngineMethodHandler & getInstance()
14+
{
15+
static ZegoExpressEngineMethodHandler m_instance;
16+
return m_instance;
17+
}
18+
19+
public:
20+
std::string getVersion();
21+
void createEngine(const flutter::MethodCall<flutter::EncodableValue>& method_call,
22+
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result);
23+
24+
25+
private:
26+
ZegoExpressEngineMethodHandler() = default;
27+
};

‎windows/zego_express_engine_plugin.cpp

+63-27
Original file line numberDiff line numberDiff line change
@@ -7,67 +7,103 @@
77
#include <VersionHelpers.h>
88

99
#include <flutter/method_channel.h>
10+
#include <flutter/event_channel.h>
1011
#include <flutter/plugin_registrar_windows.h>
1112
#include <flutter/standard_method_codec.h>
1213

1314
#include <map>
1415
#include <memory>
1516
#include <sstream>
1617

17-
#include <ZegoExpressSDK.h>
18-
using namespace ZEGO;
18+
#include "internal/ZegoExpressEngineMethodHandler.h"
19+
#include "internal/ZegoExpressEngineEventHandler.h"
1920

20-
namespace {
21+
//namespace {
2122

22-
class ZegoExpressEnginePlugin : public flutter::Plugin {
23-
public:
24-
static void RegisterWithRegistrar(flutter::PluginRegistrarWindows *registrar);
23+
class ZegoExpressEnginePlugin : public flutter::Plugin, public flutter::StreamHandler<flutter::EncodableValue> {
24+
25+
public:
26+
static void RegisterWithRegistrar(flutter::PluginRegistrarWindows *registrar);
2527

26-
ZegoExpressEnginePlugin();
28+
ZegoExpressEnginePlugin();
2729

28-
virtual ~ZegoExpressEnginePlugin();
30+
31+
virtual ~ZegoExpressEnginePlugin();
32+
33+
protected:
34+
virtual std::unique_ptr<flutter::StreamHandlerError<flutter::EncodableValue>> OnListenInternal(
35+
const flutter::EncodableValue* arguments,
36+
std::unique_ptr<flutter::EventSink<flutter::EncodableValue>>&& events) override;
37+
38+
// Implementation of the public interface, to be provided by subclasses.
39+
virtual std::unique_ptr<flutter::StreamHandlerError<flutter::EncodableValue>> OnCancelInternal(
40+
const flutter::EncodableValue* arguments) override;
41+
42+
43+
private:
44+
// Called when a method is called on this plugin's channel from Dart.
45+
void HandleMethodCall(const flutter::MethodCall<flutter::EncodableValue> &method_call,
46+
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result);
2947

30-
private:
31-
// Called when a method is called on this plugin's channel from Dart.
32-
void HandleMethodCall(
33-
const flutter::MethodCall<flutter::EncodableValue> &method_call,
34-
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result);
3548
};
3649

3750
// static
38-
void ZegoExpressEnginePlugin::RegisterWithRegistrar(
39-
flutter::PluginRegistrarWindows *registrar) {
40-
auto channel =
41-
std::make_unique<flutter::MethodChannel<flutter::EncodableValue>>(
42-
registrar->messenger(), "plugins.zego.im/zego_express_engine",
43-
&flutter::StandardMethodCodec::GetInstance());
51+
void ZegoExpressEnginePlugin::RegisterWithRegistrar(flutter::PluginRegistrarWindows *registrar) {
52+
auto methodChannel = std::make_unique<flutter::MethodChannel<flutter::EncodableValue>>
53+
(registrar->messenger(), "plugins.zego.im/zego_express_engine", &flutter::StandardMethodCodec::GetInstance());
4454

45-
auto plugin = std::make_unique<ZegoExpressEnginePlugin>();
55+
auto eventChannel = std::make_unique<flutter::EventChannel<flutter::EncodableValue>>
56+
(registrar->messenger(), "plugins.zego.im/zego_express_event_handler", &flutter::StandardMethodCodec::GetInstance());
4657

47-
channel->SetMethodCallHandler(
48-
[plugin_pointer = plugin.get()](const auto &call, auto result) {
49-
plugin_pointer->HandleMethodCall(call, std::move(result));
50-
});
58+
auto plugin = std::make_unique<ZegoExpressEnginePlugin>();
5159

52-
registrar->AddPlugin(std::move(plugin));
60+
eventChannel->SetStreamHandler(std::move(plugin));
61+
62+
methodChannel->SetMethodCallHandler(
63+
[plugin_pointer = plugin.get()](const auto &call, auto result) {
64+
plugin_pointer->HandleMethodCall(call, std::move(result));
65+
});
66+
67+
68+
registrar->AddPlugin(std::move(plugin));
5369
}
5470

5571
ZegoExpressEnginePlugin::ZegoExpressEnginePlugin() {}
5672

5773
ZegoExpressEnginePlugin::~ZegoExpressEnginePlugin() {}
5874

75+
std::unique_ptr<flutter::StreamHandlerError<flutter::EncodableValue>> ZegoExpressEnginePlugin::OnListenInternal(
76+
const flutter::EncodableValue* arguments,
77+
std::unique_ptr<flutter::EventSink<flutter::EncodableValue>>&& events) {
78+
79+
ZegoExpressEngineEventHandler::getInstance().setEventSink(std::move(events));
80+
std::cout << "on listen event" << std::endl;
81+
82+
return nullptr;
83+
}
84+
85+
std::unique_ptr<flutter::StreamHandlerError<flutter::EncodableValue>> ZegoExpressEnginePlugin::OnCancelInternal(
86+
const flutter::EncodableValue* arguments) {
87+
88+
ZegoExpressEngineEventHandler::getInstance().clearEventSink();
89+
std::cout << "on listen event" << std::endl;
90+
91+
return nullptr;
92+
}
93+
5994
void ZegoExpressEnginePlugin::HandleMethodCall(
6095
const flutter::MethodCall<flutter::EncodableValue> &method_call,
6196
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
6297
if(method_call.method_name() == "getVersion") {
6398

64-
result->Success(flutter::EncodableValue(EXPRESS::ZegoExpressSDK::getVersion()));
99+
auto version = ZegoExpressEngineMethodHandler::getInstance().getVersion();
100+
result->Success(flutter::EncodableValue(version));
65101
} else {
66102
result->NotImplemented();
67103
}
68104
}
69105

70-
} // namespace
106+
//} // namespace
71107

72108
void ZegoExpressEnginePluginRegisterWithRegistrar(
73109
FlutterDesktopPluginRegistrarRef registrar) {

0 commit comments

Comments
 (0)