Skip to content

Commit

Permalink
feat: select branch with picker
Browse files Browse the repository at this point in the history
  • Loading branch information
pd4d10 committed Sep 29, 2019
1 parent cba1173 commit ce00adf
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 44 deletions.
42 changes: 35 additions & 7 deletions lib/models/theme.dart
Expand Up @@ -2,7 +2,6 @@ import 'dart:io';
import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:git_touch/widgets/picker.dart';
import 'package:shared_preferences/shared_preferences.dart';

class DialogOption<T> {
Expand All @@ -17,6 +16,26 @@ class AppThemeType {
static const values = [AppThemeType.material, AppThemeType.cupertino];
}

class PickerItem<T> {
final T value;
final String text;
PickerItem(this.value, {@required this.text});
}

class PickerGroupItem<T> {
final T value;
final List<PickerItem<T>> items;
final Function(T value) onChange;
final Function(T value) onClose;

PickerGroupItem({
@required this.value,
@required this.items,
this.onChange,
this.onClose,
});
}

class ThemeModel with ChangeNotifier {
static const storageKey = 'theme';

Expand Down Expand Up @@ -195,6 +214,8 @@ class ThemeModel with ChangeNotifier {

static Timer _debounce;

String _selectedItem;

showPicker(BuildContext context, PickerGroupItem<String> groupItem) async {
switch (theme) {
case AppThemeType.cupertino:
Expand All @@ -213,18 +234,25 @@ class ThemeModel with ChangeNotifier {
.toList()
.indexWhere((v) => v.value == groupItem.value)),
onSelectedItemChanged: (index) {
if (_debounce?.isActive ?? false) {
_debounce.cancel();
}
_selectedItem = groupItem.items[index].value;

_debounce = Timer(const Duration(milliseconds: 500), () {
groupItem.onChange(groupItem.items[index].value);
});
if (groupItem.onChange != null) {
if (_debounce?.isActive ?? false) {
_debounce.cancel();
}

_debounce = Timer(const Duration(milliseconds: 500), () {
groupItem.onChange(_selectedItem);
});
}
},
),
);
},
);
if (groupItem.onClose != null) {
groupItem.onClose(_selectedItem);
}
}
}
}
1 change: 0 additions & 1 deletion lib/screens/code_theme.dart
Expand Up @@ -6,7 +6,6 @@ import 'package:git_touch/models/code.dart';
import 'package:git_touch/models/theme.dart';
import 'package:git_touch/scaffolds/single.dart';
import 'package:git_touch/widgets/app_bar_title.dart';
import 'package:git_touch/widgets/picker.dart';
import 'package:git_touch/widgets/table_view.dart';
import 'package:provider/provider.dart';

Expand Down
34 changes: 18 additions & 16 deletions lib/screens/repository.dart
Expand Up @@ -101,7 +101,7 @@ class RepositoryScreen extends StatelessWidget {
}
}
}
refs(first: 10, refPrefix: "refs/heads/") {
refs(first: 100, refPrefix: "refs/heads/") {
totalCount
nodes {
name
Expand Down Expand Up @@ -338,22 +338,24 @@ class RepositoryScreen extends StatelessWidget {
var refs = payload['refs']['nodes'] as List;
if (refs.length < 2) return;

// FIXME: Show all branches
var result = await Provider.of<ThemeModel>(context)
.showDialogOptions(
await Provider.of<ThemeModel>(context).showPicker(
context,
PickerGroupItem(
value: payload[branchInfoKey]['name'],
items: refs
.map((b) => PickerItem(b['name'] as String,
text: (b['name'] as String)))
.toList(),
onClose: (result) {
Provider.of<ThemeModel>(context)
.pushReplacementRoute(
context,
refs
.map((b) => DialogOption(
value: b['name'] as String,
widget: Text(b['name'] as String)))
.toList());

if (result != null) {
Provider.of<ThemeModel>(context).pushReplacementRoute(
context,
(_) => RepositoryScreen(owner, name,
branch: result));
}
(_) => RepositoryScreen(owner, name,
branch: result),
);
},
),
);
},
),
],
Expand Down
20 changes: 0 additions & 20 deletions lib/widgets/picker.dart

This file was deleted.

0 comments on commit ce00adf

Please sign in to comment.