* Copyright (c) 2024 Hunan OpenValley Digital Industry Development Co., Ltd.
* 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.
*/
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter_inappwebview_platform_interface/flutter_inappwebview_platform_interface.dart';
@immutable
class OhosPathHandlerCreationParams
extends PlatformPathHandlerCreationParams {
OhosPathHandlerCreationParams(
PlatformPathHandlerCreationParams params,
) : super(path: params.path);
factory OhosPathHandlerCreationParams.fromPlatformPathHandlerCreationParams(
PlatformPathHandlerCreationParams params) {
return OhosPathHandlerCreationParams(params);
}
}
abstract class OhosPathHandler
implements ChannelController, PlatformPathHandler {
final String _id = IdGenerator.generate();
@override
late final PlatformPathHandlerEvents? eventHandler;
@override
late final String path;
void _init(PlatformPathHandlerCreationParams params) {
this.path = params.path;
channel = MethodChannel(
'com.pichillilorenzo/flutter_inappwebview_custompathhandler_${_id}');
handler = _handleMethod;
initMethodCallHandler();
}
Future<dynamic> _handleMethod(MethodCall call) async {
switch (call.method) {
case "handle":
String path = call.arguments["path"];
return (await eventHandler?.handle(path))?.toMap();
default:
throw UnimplementedError("Unimplemented ${call.method} method");
}
}
@override
Map<String, dynamic> toMap() {
return {"path": path, "type": type, "id": _id};
}
@override
Map<String, dynamic> toJson() {
return toMap();
}
@override
String toString() {
return 'OhosPathHandler{path: $path, type: $type}';
}
@override
void dispose() {
disposeChannel();
eventHandler = null;
}
}
@immutable
class OhosAssetsPathHandlerCreationParams
extends PlatformAssetsPathHandlerCreationParams {
OhosAssetsPathHandlerCreationParams(
PlatformAssetsPathHandlerCreationParams params,
) : super(params);
factory OhosAssetsPathHandlerCreationParams.fromPlatformAssetsPathHandlerCreationParams(
PlatformAssetsPathHandlerCreationParams params) {
return OhosAssetsPathHandlerCreationParams(params);
}
}
class OhosAssetsPathHandler extends PlatformAssetsPathHandler
with OhosPathHandler, ChannelController {
OhosAssetsPathHandler(PlatformAssetsPathHandlerCreationParams params)
: super.implementation(
params is OhosAssetsPathHandlerCreationParams
? params
: OhosAssetsPathHandlerCreationParams
.fromPlatformAssetsPathHandlerCreationParams(params),
) {
_init(params);
}
}
@immutable
class OhosResourcesPathHandlerCreationParams
extends PlatformResourcesPathHandlerCreationParams {
OhosResourcesPathHandlerCreationParams(
PlatformResourcesPathHandlerCreationParams params,
) : super(params);
factory OhosResourcesPathHandlerCreationParams.fromPlatformResourcesPathHandlerCreationParams(
PlatformResourcesPathHandlerCreationParams params) {
return OhosResourcesPathHandlerCreationParams(params);
}
}
class OhosResourcesPathHandler extends PlatformResourcesPathHandler
with OhosPathHandler, ChannelController {
OhosResourcesPathHandler(PlatformResourcesPathHandlerCreationParams params)
: super.implementation(
params is OhosResourcesPathHandlerCreationParams
? params
: OhosResourcesPathHandlerCreationParams
.fromPlatformResourcesPathHandlerCreationParams(params),
) {
_init(params);
}
}
@immutable
class OhosInternalStoragePathHandlerCreationParams
extends PlatformInternalStoragePathHandlerCreationParams {
OhosInternalStoragePathHandlerCreationParams(
PlatformInternalStoragePathHandlerCreationParams params,
) : super(params, directory: params.directory);
factory OhosInternalStoragePathHandlerCreationParams.fromPlatformInternalStoragePathHandlerCreationParams(
PlatformInternalStoragePathHandlerCreationParams params) {
return OhosInternalStoragePathHandlerCreationParams(params);
}
}
class OhosInternalStoragePathHandler
extends PlatformInternalStoragePathHandler
with OhosPathHandler, ChannelController {
OhosInternalStoragePathHandler(
PlatformInternalStoragePathHandlerCreationParams params)
: super.implementation(
params is OhosInternalStoragePathHandlerCreationParams
? params
: OhosInternalStoragePathHandlerCreationParams
.fromPlatformInternalStoragePathHandlerCreationParams(params),
) {
_init(params);
}
OhosInternalStoragePathHandlerCreationParams get _internalParams =>
params as OhosInternalStoragePathHandlerCreationParams;
@override
String get directory => _internalParams.directory;
@override
Map<String, dynamic> toMap() {
return {...toMap(), 'directory': directory};
}
}
@immutable
class OhosCustomPathHandlerCreationParams
extends PlatformCustomPathHandlerCreationParams {
OhosCustomPathHandlerCreationParams(
PlatformCustomPathHandlerCreationParams params,
) : super(params);
factory OhosCustomPathHandlerCreationParams.fromPlatformCustomPathHandlerCreationParams(
PlatformCustomPathHandlerCreationParams params) {
return OhosCustomPathHandlerCreationParams(params);
}
}
class OhosCustomPathHandler extends PlatformCustomPathHandler
with OhosPathHandler, ChannelController {
OhosCustomPathHandler(PlatformCustomPathHandlerCreationParams params)
: super.implementation(
params is OhosCustomPathHandlerCreationParams
? params
: OhosCustomPathHandlerCreationParams
.fromPlatformCustomPathHandlerCreationParams(params),
) {
_init(params);
}
}