49 lines
1.2 KiB
Dart
49 lines
1.2 KiB
Dart
|
import 'dart:async';
|
||
|
|
||
|
import 'package:flutter/services.dart';
|
||
|
|
||
|
abstract class Bloc {
|
||
|
void dispose();
|
||
|
}
|
||
|
|
||
|
class DeepLinkBloc extends Bloc {
|
||
|
//Event Channel creation
|
||
|
static const stream =
|
||
|
const EventChannel('https://demo.endi.coop/login?nextpage=%2F');
|
||
|
|
||
|
//Method channel creation
|
||
|
static const platform = const MethodChannel('https://demo.endi.coop');
|
||
|
|
||
|
StreamController<String> _stateController = StreamController();
|
||
|
|
||
|
Stream<String> get state => _stateController.stream;
|
||
|
|
||
|
Sink<String> get stateSink => _stateController.sink;
|
||
|
|
||
|
//Adding the listener into contructor
|
||
|
DeepLinkBloc() {
|
||
|
//Checking application start by deep link
|
||
|
startUri().then(_onRedirected);
|
||
|
//Checking broadcast stream, if deep link was clicked in opened appication
|
||
|
stream.receiveBroadcastStream().listen((d) => _onRedirected(d));
|
||
|
}
|
||
|
|
||
|
_onRedirected(String uri) {
|
||
|
// Throw deep link URI into the BloC's stream
|
||
|
stateSink.add(uri);
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
void dispose() {
|
||
|
_stateController.close();
|
||
|
}
|
||
|
|
||
|
Future<String> startUri() async {
|
||
|
try {
|
||
|
return platform.invokeMethod('initialLink');
|
||
|
} on PlatformException catch (e) {
|
||
|
return "Failed to Invoke: '${e.message}'.";
|
||
|
}
|
||
|
}
|
||
|
}
|