git-touch-android-ios-app/lib/providers/user.dart

64 lines
1.1 KiB
Dart
Raw Normal View History

2019-01-23 15:55:16 +01:00
import 'package:flutter/widgets.dart';
import 'dart:async';
import 'package:rxdart/subjects.dart';
import 'package:rxdart/rxdart.dart';
import '../utils.dart';
Future queryUser(String login) async {
var data = await query('''
{
user(login: "$login") {
name
avatarUrl
bio
email
repositories {
totalCount
}
starredRepositories {
totalCount
}
followers {
totalCount
}
following {
totalCount
}
}
}
''');
return data['user'];
}
class UserBloc {
Map<String, dynamic> _userDict = {};
final _user = BehaviorSubject(seedValue: null);
fetchUser(String login) async {
var user = await queryUser(login);
_userDict[login] = user;
return user;
}
UserBloc() {}
}
class UserProvider extends InheritedWidget {
final UserBloc bloc;
UserProvider({
Key key,
Widget child,
@required UserBloc bloc,
}) : bloc = bloc,
super(key: key, child: child);
@override
bool updateShouldNotify(InheritedWidget oldWidget) => true;
static UserBloc of(BuildContext context) =>
(context.inheritFromWidgetOfExactType(UserProvider) as UserProvider).bloc;
}