Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
n1k3c committed Dec 27, 2019
1 parent da45550 commit 5f26cfb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 47 deletions.
6 changes: 3 additions & 3 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ class _MyAppState extends State<MyApp> {

Future<void> getLinks() async {
try {
PreviewResponse previewResponse = await LinkPreview.getData('https://googfdsfsdle.com');
PreviewResponse previewResponse = await LinkPreview.getPreview('https://google.com');
_previewData(previewResponse);

PreviewResponse previewResponse2 = await LinkPreview.getData('https://facebook.com');
PreviewResponse previewResponse2 = await LinkPreview.getPreview('https://facebook.com');
_previewData(previewResponse2);

PreviewResponse previewResponse3 = await LinkPreview.getData('https://amazon.com');
PreviewResponse previewResponse3 = await LinkPreview.getPreview('https://amazon.com');
_previewData(previewResponse3);
} on PlatformException {
print('Error occured!!');
Expand Down
48 changes: 4 additions & 44 deletions lib/link_preview.dart
Original file line number Diff line number Diff line change
@@ -1,29 +1,16 @@
import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';

class LinkPreview {
static const MethodChannel methodChannel = MethodChannel('link_preview_channel');
static const EventChannel eventChannel = EventChannel('link_preview_events');
static const MethodChannel _methodChannel =
MethodChannel('link_preview_channel');

static Future<String> getPreview(String url,
{@required Function(PreviewResponse model) onData,
@required Function onError}) async {
return eventChannel
.receiveBroadcastStream(url)
.listen(
(dynamic event) => _mapData(event, onData),
onError: (dynamic error) => onError(error),
)
.asFuture();
}

static Future<PreviewResponse> getData(String url) async {
static Future<PreviewResponse> getPreview(String url) async {
Map<String, dynamic> args = <String, dynamic>{};
args.putIfAbsent("url", () => url);

var result = await methodChannel.invokeMethod("previewLink", args);
var result = await _methodChannel.invokeMethod("previewLink", args);
PreviewResponse previewResponse = _mapResultToResponse(result);
return previewResponse;
}
Expand Down Expand Up @@ -55,33 +42,6 @@ class LinkPreview {

return previewResponse;
}

static _mapData(dynamic event, Function(PreviewResponse model) onData) {
var data = Map<String, dynamic>.from(event);

PreviewStatus status;

// if (data['state'] == 'loading') {
// status = PreviewStatus.loading;
// PreviewResponse previewModel = PreviewResponse(status);
// onData(previewModel);
// } else {
// status = PreviewStatus.complete;
// PreviewResponse previewModel = PreviewResponse(
// status,
// title: data['title'],
// description: data['description'],
// image: data['image'],
// url: data['url'],
// finalUrl: data['final_url'],
// cannonicalUrl: data['cannonical_url'],
// row: data['row'],
// htmlCode: data['html_code'],
//
// );
// onData(previewModel);
// }
}
}

class PreviewResponse {
Expand Down

0 comments on commit 5f26cfb

Please sign in to comment.