avioli / uni_links

Flutter plugin for accepting incoming links.
BSD 2-Clause "Simplified" License
563 stars 312 forks source link

Improvement? Difference between background and foreground #34

Open timrijckaert opened 5 years ago

timrijckaert commented 5 years ago

Hi,

I wonder why the distinction was made between receiving values in the background and foreground.

Now you have to check if the initial deeplink was not null.

final initialLink = await getInitialLink();
if (initialLink != null) {
  ...
}

As well as also listen on the stream when a deeplink was received in the foreground.

getLinksStream().listen(...)

We implemented a simple wrapper inside our app which deals with this.
Only exposes 1 stream where all events are posted including the initial one.

I wondered if it was something you'd be interested in to include in the lib already?

The helper class itself.

import 'dart:async';

import 'package:rxdart/rxdart.dart';
import 'package:uni_links/uni_links.dart';

/// Adds a wrapper around the uni_links library.
class DeepLinkHelper {
  StreamSubscription<String> _deepLinkStreamSubscription;
  final BehaviorSubject<String> _deepLinkSink = BehaviorSubject();
  Stream<String> get deepLinkStream => _deepLinkSink.stream;

  DeepLinkHelper() {
    _postInitialLink();
    _deepLinkStreamSubscription = getLinksStream().listen(_deepLinkSink.add);
  }

  void _postInitialLink() async {
    try {
      final initialLink = await getInitialLink();
      if (initialLink != null) {
        _deepLinkSink.add(initialLink);
      }
    } finally {}
  }

  void dispose() {
    _deepLinkStreamSubscription.cancel();
  }
}
deandreamatias commented 5 years ago

@timrijckaert You have a example of use for this class? Thanks

timrijckaert commented 5 years ago

Like I said, we use it as one gateway of all things deeplink and it has been working perfectly so far.

AzadCoder commented 3 years ago

Please provide an example.