From 7ffb3f86ac6d0a30eb6ac59b00ff931478d06761 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Fri, 13 Nov 2020 16:25:45 +0100 Subject: [PATCH] RepoSelector: Auto select exact matching item when the user types it in --- lib/apis/githost.dart | 13 +++++++++++++ lib/setup/repo_selector.dart | 32 +++++++++++++++++++++++++++----- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/lib/apis/githost.dart b/lib/apis/githost.dart index 3b9063a3..07f6cf49 100644 --- a/lib/apis/githost.dart +++ b/lib/apis/githost.dart @@ -2,6 +2,8 @@ import 'dart:async'; import 'package:flutter/foundation.dart'; +import 'package:collection/collection.dart'; + typedef OAuthCallback = void Function(GitHostException); abstract class GitHost { @@ -79,8 +81,19 @@ class GitHostRepo { @override String toString() => toJson().toString(); + + @override + bool operator ==(Object other) => + identical(this, other) || + other is GitHostRepo && + runtimeType == other.runtimeType && + _mapEquals(toJson(), other.toJson()); + @override + int get hashCode => toJson().hashCode; } +var _mapEquals = (const MapEquality()).equals; + class GitHostException implements Exception { static const OAuthFailed = GitHostException("OAuthFailed"); static const MissingAccessCode = GitHostException("MissingAccessCode"); diff --git a/lib/setup/repo_selector.dart b/lib/setup/repo_selector.dart index 81e6ca2b..0883f780 100644 --- a/lib/setup/repo_selector.dart +++ b/lib/setup/repo_selector.dart @@ -50,10 +50,27 @@ class GitHostSetupRepoSelectorState extends State { super.initState(); _textController.addListener(() { - setState(() { - selectedRepo = null; - createRepo = false; - }); + var q = _textController.text.toLowerCase(); + if (q.isEmpty) { + setState(() { + selectedRepo = null; + createRepo = false; + }); + return; + } + var repoIndex = repos.indexWhere((r) => + r.name.toLowerCase() == q && r.username == widget.userInfo.username); + if (repoIndex == -1) { + setState(() { + selectedRepo = null; + createRepo = false; + }); + } else { + setState(() { + selectedRepo = repos[repoIndex]; + createRepo = false; + }); + } }); _initStateAysnc(); } @@ -336,13 +353,18 @@ class _RepoTile extends StatelessWidget { ); } - return ListTile( + var tile = ListTile( title: title, trailing: _SmartDateTime(repo.updatedAt, textTheme.caption), selected: selected, contentPadding: const EdgeInsets.all(0.0), onTap: onTap, ); + + return Ink( + color: selected ? Theme.of(context).highlightColor : Colors.transparent, + child: tile, + ); } }