1
0
mirror of https://github.com/git-touch/git-touch synced 2024-12-17 10:48:50 +01:00
git-touch-android-ios-app/lib/screens/search.dart

152 lines
3.6 KiB
Dart
Raw Normal View History

2019-02-02 17:28:51 +01:00
import 'package:flutter/material.dart';
2019-01-23 12:52:51 +01:00
import 'package:flutter/cupertino.dart';
import 'package:git_touch/models/theme.dart';
2019-09-25 14:51:23 +02:00
import 'package:git_touch/scaffolds/tab.dart';
2019-09-11 13:59:47 +02:00
import 'package:git_touch/widgets/app_bar_title.dart';
2019-09-25 14:51:23 +02:00
import 'package:git_touch/widgets/issue_item.dart';
import 'package:git_touch/widgets/user_item.dart';
import 'package:provider/provider.dart';
2019-09-08 14:07:35 +02:00
import 'package:git_touch/models/settings.dart';
2019-02-10 06:42:02 +01:00
import '../utils/utils.dart';
2019-09-23 12:28:33 +02:00
import 'package:git_touch/widgets/repository_item.dart';
2019-01-23 12:52:51 +01:00
2019-02-02 17:28:51 +01:00
class SearchScreen extends StatefulWidget {
2019-01-23 12:52:51 +01:00
@override
2019-02-02 17:28:51 +01:00
_SearchScreenState createState() => _SearchScreenState();
}
class _SearchScreenState extends State<SearchScreen> {
2019-09-25 14:51:23 +02:00
int _activeTab = 0;
bool _loading = false;
List<List> _payloads = [[], [], []];
2019-01-23 12:52:51 +01:00
2019-09-25 14:51:23 +02:00
TextEditingController _controller;
String get _keyword => _controller.text?.trim() ?? '';
@override
void initState() {
super.initState();
_controller = TextEditingController();
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
Future<void> _query() async {
if (_loading || _keyword.isEmpty) return;
var keyword = _controller.text;
2019-02-10 06:42:02 +01:00
setState(() {
2019-09-25 14:51:23 +02:00
_loading = true;
2019-02-10 06:42:02 +01:00
});
try {
2019-09-08 14:07:35 +02:00
var data = await Provider.of<SettingsModel>(context).query('''
2019-02-10 06:42:02 +01:00
{
2019-09-25 14:51:23 +02:00
repository: search(first: $pageSize, type: REPOSITORY, query: "$keyword") {
2019-02-10 06:42:02 +01:00
nodes {
... on Repository {
$repoChunk
}
}
}
2019-09-25 14:51:23 +02:00
user: search(first: $pageSize, type: USER, query: "$keyword") {
nodes {
... on Organization {
__typename
name
avatarUrl
bio: description
login
}
... on User {
$userGqlChunk
login
}
}
}
issue: search(first: $pageSize, type: ISSUE, query: "$keyword") {
nodes {
... on PullRequest {
__typename
$issueGqlChunk
}
... on Issue {
$issueGqlChunk
}
}
}
2019-02-10 06:42:02 +01:00
}
''');
2019-09-25 14:51:23 +02:00
_payloads[0] = data['repository']['nodes'];
_payloads[1] = data['user']['nodes'];
_payloads[2] = data['issue']['nodes'];
2019-02-10 06:42:02 +01:00
} finally {
setState(() {
2019-09-25 14:51:23 +02:00
_loading = false;
2019-02-10 06:42:02 +01:00
});
}
}
Widget _buildInput() {
switch (Provider.of<ThemeModel>(context).theme) {
2019-09-19 15:10:50 +02:00
case AppThemeType.cupertino:
2019-02-10 06:42:02 +01:00
return CupertinoTextField(
2019-09-25 14:51:23 +02:00
controller: _controller,
2019-02-10 06:42:02 +01:00
// padding: EdgeInsets.all(10),
placeholder: 'Type to search',
clearButtonMode: OverlayVisibilityMode.editing,
2019-09-25 14:51:23 +02:00
onSubmitted: (_) => _query(),
);
default:
return TextField(
onSubmitted: (_) => _query(),
controller: _controller,
);
}
}
Widget _buildItem(data) {
switch (_activeTab) {
case 0:
return RepositoryItem(data);
case 1:
return UserItem.fromData(
data,
isOrganization: data['__typename'] == 'Organization',
2019-02-02 17:28:51 +01:00
);
2019-09-25 14:51:23 +02:00
case 2:
2019-02-02 17:28:51 +01:00
default:
2019-09-25 14:51:23 +02:00
return IssueItem(
payload: data, isPullRequest: data['__typename'] == 'PullRequest');
2019-02-02 17:28:51 +01:00
}
2019-01-23 12:52:51 +01:00
}
2019-02-10 06:42:02 +01:00
@override
Widget build(BuildContext context) {
2019-09-25 14:51:23 +02:00
return TabScaffold(
2019-09-11 13:59:47 +02:00
title: AppBarTitle('Search GitHub Repositories'),
2019-09-25 11:06:36 +02:00
body: Column(
2019-09-25 08:24:20 +02:00
children: <Widget>[
Container(padding: EdgeInsets.all(8), child: _buildInput()),
2019-09-25 14:51:23 +02:00
Column(children: _payloads[_activeTab].map(_buildItem).toList())
2019-09-25 08:24:20 +02:00
],
),
2019-09-25 14:51:23 +02:00
activeTab: _activeTab,
onRefresh: _query,
onTabSwitch: (int index) {
setState(() {
_activeTab = index;
});
if (_payloads[_activeTab].isEmpty) {
_query();
}
},
tabs: ['Repositories', 'Users', 'Issues'],
2019-02-10 06:42:02 +01:00
);
}
2019-01-23 12:52:51 +01:00
}