Skip to content

Commit

Permalink
migrating examples
Browse files Browse the repository at this point in the history
  • Loading branch information
pavanpodila committed Apr 1, 2021
1 parent 78468ca commit e5fcc0f
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 30 deletions.
2 changes: 1 addition & 1 deletion mobx_examples/lib/clock/clock.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Clock {
}

late final Atom _atom;
late final Timer? _timer;
Timer? _timer;

void _startTimer() {
print('Clock started ticking');
Expand Down
2 changes: 1 addition & 1 deletion mobx_examples/lib/form/form_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'dart:ui';

import 'package:mobx/mobx.dart';
// ignore: import_of_legacy_library_into_null_safe
import 'package:validators/validators.dart';
import 'package:validators2/validators.dart';

part 'form_store.g.dart';

Expand Down
6 changes: 3 additions & 3 deletions mobx_examples/lib/github/github_store.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion mobx_examples/lib/github/github_widgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class RepositoryListView extends StatelessWidget {
],
),
subtitle: Text(
repo.description ?? '',
repo.description,
overflow: TextOverflow.fade,
),
);
Expand Down
83 changes: 83 additions & 0 deletions mobx_examples/lib/hackernews/hn_api.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#! /usr/bin/env dart

import 'dart:async';
import 'dart:convert';

import 'package:http/http.dart' as http;

enum Type { newest, top }

extension on Type {
Uri get uri {
switch (this) {
case Type.newest:
return Uri.parse(
'https://hacker-news.firebaseio.com/v0/newstories.json');

case Type.top:
return Uri.parse(
'https://hacker-news.firebaseio.com/v0/topstories.json');
}
}
}

class FeedItem {
FeedItem(
{required this.score,
required this.title,
this.url,
required this.author});

final int score;
final String title;
final String? url;
final String author;

@override
String toString() =>
'${this.author}: ${this.title}(${this.score}⭐️), ${this.url ?? 'no-url'}';
}

class HNApi {
Future<List<FeedItem>> newest() async {
final ids = await _getItems(Type.newest);
final items = await Future.wait(ids.map(_getItem));

return items.toList(growable: false);
}

Future<List<FeedItem>> top() async {
final ids = await _getItems(Type.top);
final items = await Future.wait(ids.map(_getItem));

return items.toList(growable: false);
}

Future<FeedItem> _getItem(int id) async {
final response = await http
.get(Uri.parse('https://hacker-news.firebaseio.com/v0/item/$id.json'));
final json = jsonDecode(response.body) as Map<String, dynamic>;

return FeedItem(
score: json['score'],
title: json['title'],
author: json['by'],
url: json['url']);
}

Future<List<int>> _getItems(Type type, {int count = 25}) async {
final response = await http.get(type.uri);

final ids = (jsonDecode(response.body) as List)
.take(count)
.map((e) => e as int)
.toList(growable: false);

return ids;
}
}

Future<void> main() async {
final list = await HNApi().top();
print(list);
}
20 changes: 9 additions & 11 deletions mobx_examples/lib/hackernews/news_store.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:hnpwa_client/hnpwa_client.dart';
import 'package:mobx/mobx.dart';
import 'package:mobx_examples/hackernews/hn_api.dart';
import 'package:url_launcher/url_launcher.dart';

part 'news_store.g.dart';
Expand All @@ -9,21 +9,19 @@ enum FeedType { latest, top }
class HackerNewsStore = _HackerNewsStore with _$HackerNewsStore;

abstract class _HackerNewsStore with Store {
final HnpwaClient _client = HnpwaClient();
final _hnApi = HNApi();

@observable
ObservableFuture<List<FeedItem>> latestItemsFuture;
ObservableFuture<List<FeedItem>>? latestItemsFuture;

@observable
ObservableFuture<List<FeedItem>> topItemsFuture;
ObservableFuture<List<FeedItem>>? topItemsFuture;

@action
Future fetchLatest() => latestItemsFuture =
ObservableFuture(_client.newest().then((Feed feed) => feed.items));
Future fetchLatest() => latestItemsFuture = ObservableFuture(_hnApi.newest());

@action
Future fetchTop() => topItemsFuture =
ObservableFuture(_client.news().then((Feed feed) => feed.items));
Future fetchTop() => topItemsFuture = ObservableFuture(_hnApi.top());

void loadNews(FeedType type) {
if (type == FeedType.latest && latestItemsFuture == null) {
Expand All @@ -34,9 +32,9 @@ abstract class _HackerNewsStore with Store {
}

// ignore: avoid_void_async
void openUrl(String url) async {
if (await canLaunch(url)) {
await launch(url);
void openUrl(String? url) async {
if (await canLaunch(url ?? '')) {
await launch(url!);
} else {
print('Could not open $url');
}
Expand Down
8 changes: 4 additions & 4 deletions mobx_examples/lib/hackernews/news_store.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions mobx_examples/lib/hackernews/news_widgets.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:hnpwa_client/hnpwa_client.dart';
import 'package:mobx/mobx.dart';
import 'package:mobx_examples/hackernews/hn_api.dart';

import 'package:mobx_examples/hackernews/news_store.dart';

Expand All @@ -16,7 +16,7 @@ class _HackerNewsExampleState extends State<HackerNewsExample>
with SingleTickerProviderStateMixin {
final HackerNewsStore store = HackerNewsStore();

TabController _tabController;
late TabController _tabController;
final _tabs = [FeedType.latest, FeedType.top];

@override
Expand Down Expand Up @@ -62,6 +62,10 @@ class FeedItemsView extends StatelessWidget {
? store.latestItemsFuture
: store.topItemsFuture;

if (future == null) {
return const CircularProgressIndicator();
}

switch (future.status) {
case FutureStatus.pending:
return Column(
Expand Down Expand Up @@ -98,15 +102,14 @@ class FeedItemsView extends StatelessWidget {
final item = items[index];
return ListTile(
leading: Text(
'${item.points}',
'${item.score}',
style: const TextStyle(fontSize: 20),
),
title: Text(
item.title,
style: const TextStyle(fontWeight: FontWeight.bold),
),
subtitle: Text(
'- ${item.user}, ${item.commentsCount} comment(s)'),
subtitle: Text('- ${item.author}'),
onTap: () => store.openUrl(item.url),
);
}),
Expand Down
2 changes: 1 addition & 1 deletion mobx_examples/lib/random_stream/random_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ abstract class _RandomStore with Store {

late final StreamController<int> _streamController;

late final ObservableStream<int> randomStream;
late final ObservableStream<int?> randomStream;

// ignore: avoid_void_async
void dispose() async {
Expand Down
6 changes: 3 additions & 3 deletions mobx_examples/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ dependencies:
flutter:
sdk: flutter
flutter_mobx: ^2.0.0-nullsafety.0
# github: ^8.0.1
# hnpwa_client: ^2.0.0
github: ^8.0.1
http: ^0.13.0
mobx: ^2.0.0-nullsafety.0
mobx_codegen: ^2.0.0-nullsafety.0
provider: ^5.0.0
shared_preferences:
url_launcher:
validators: ^2.0.0
validators2: ^3.0.0

dev_dependencies:
build_runner: ^1.12.2
Expand Down

0 comments on commit e5fcc0f

Please sign in to comment.