1
0
mirror of https://github.com/git-touch/git-touch synced 2024-12-19 11:43:30 +01:00
git-touch-android-ios-app/lib/screens/search.dart

195 lines
5.0 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-10-02 08:58:11 +02:00
import 'package:git_touch/scaffolds/common.dart';
2019-09-25 14:51:23 +02:00
import 'package:git_touch/widgets/issue_item.dart';
2019-09-29 16:10:15 +02:00
import 'package:git_touch/widgets/loading.dart';
2019-09-25 14:51:23 +02:00
import 'package:git_touch/widgets/user_item.dart';
2019-09-29 16:10:15 +02:00
import 'package:primer/primer.dart';
import 'package:provider/provider.dart';
2019-09-27 14:52:38 +02:00
import 'package:git_touch/models/auth.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-27 14:52:38 +02:00
var data = await Provider.of<AuthModel>(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
}
}
}
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-09-29 16:10:15 +02:00
return Container(
color: Colors.white,
child: CupertinoTextField(
prefix: Row(
children: <Widget>[
SizedBox(width: 8),
Icon(Octicons.search, size: 20, color: PrimerColors.gray400),
],
),
placeholder: 'Search',
clearButtonMode: OverlayVisibilityMode.editing,
textInputAction: TextInputAction.go,
onSubmitted: (_) => _query(),
controller: _controller,
),
2019-09-25 14:51:23 +02:00
);
default:
return TextField(
2019-10-03 04:46:53 +02:00
decoration: InputDecoration.collapsed(hintText: 'Search'),
2019-09-29 16:10:15 +02:00
textInputAction: TextInputAction.go,
2019-09-25 14:51:23 +02:00
onSubmitted: (_) => _query(),
controller: _controller,
);
}
}
2019-09-29 16:10:15 +02:00
_onTabSwitch(int index) {
setState(() {
_activeTab = index;
});
if (_payloads[_activeTab].isEmpty) {
_query();
}
}
static const tabs = ['Repositories', 'Users', 'Issues'];
2019-09-25 14:51:23 +02:00
Widget _buildItem(data) {
switch (_activeTab) {
case 0:
return RepositoryItem(data);
case 1:
2019-11-02 12:32:52 +01:00
return UserItem.fromData(data);
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-29 16:10:15 +02:00
final theme = Provider.of<ThemeModel>(context).theme;
final scaffold = CommonScaffold(
title: _buildInput(),
body: SingleChildScrollView(
2019-10-03 04:46:53 +02:00
child: Column(
children: [
if (theme == AppThemeType.cupertino)
Center(
child: Padding(
padding: EdgeInsets.symmetric(vertical: 8),
child: CupertinoSlidingSegmentedControl(
2019-10-03 04:46:53 +02:00
groupValue: _activeTab,
onValueChanged: _onTabSwitch,
children: tabs.asMap().map((key, text) => MapEntry(
key,
Padding(
padding: const EdgeInsets.symmetric(horizontal: 8),
child: Text(text, style: TextStyle(fontSize: 14)),
))),
),
),
2019-09-29 16:10:15 +02:00
),
2019-10-03 04:46:53 +02:00
if (_loading)
Loading()
else
..._payloads[_activeTab].map(_buildItem).toList(),
],
),
2019-09-29 16:10:15 +02:00
),
bottom: TabBar(
onTap: _onTabSwitch,
tabs: tabs.map((text) => Tab(text: text.toUpperCase())).toList(),
2019-09-25 08:24:20 +02:00
),
2019-02-10 06:42:02 +01:00
);
2019-09-29 16:10:15 +02:00
if (theme == AppThemeType.material) {
return DefaultTabController(
length: tabs.length,
child: scaffold,
);
} else {
return scaffold;
}
2019-02-10 06:42:02 +01:00
}
2019-01-23 12:52:51 +01:00
}