Fix no errors shown when necessary on UsernamePage

Also fix loading spinner showing too soon if the
request was not the first one.
parent 7d6b3b8a
...@@ -15,6 +15,9 @@ ...@@ -15,6 +15,9 @@
// You should have received a copy of the GNU Affero General Public License // You should have received a copy of the GNU Affero General Public License
// along with Pattle. If not, see <https://www.gnu.org/licenses/>. // along with Pattle. If not, see <https://www.gnu.org/licenses/>.
import 'dart:async';
import 'package:async/async.dart';
import 'package:matrix_sdk/matrix_sdk.dart'; import 'package:matrix_sdk/matrix_sdk.dart';
import 'package:pattle/src/di.dart' as di; import 'package:pattle/src/di.dart' as di;
import 'package:rxdart/rxdart.dart'; import 'package:rxdart/rxdart.dart';
...@@ -56,25 +59,30 @@ class StartBloc { ...@@ -56,25 +59,30 @@ class StartBloc {
Observable<UsernameAvailableState> get isUsernameAvailable Observable<UsernameAvailableState> get isUsernameAvailable
=> _isUsernameAvailableSubj.stream.distinct(); => _isUsernameAvailableSubj.stream.distinct();
void checkUsernameAvailability(String username) { StreamSubscription stillCheckingSubscription;
Future<void> checkUsernameAvailability(String username) async {
if (username == null) { if (username == null) {
return; return;
} }
await stillCheckingSubscription?.cancel();
_isUsernameAvailableSubj.add(UsernameAvailableState.checking); _isUsernameAvailableSubj.add(UsernameAvailableState.checking);
// If after three seconds it's still checking, change state to // If after three seconds it's still checking, change state to
// 'stillChecking'. // 'stillChecking'.
Future.delayed(loadingTime).then((_) { Future.delayed(loadingTime).then((_) async {
_isUsernameAvailableSubj.stream.listen((state) { await stillCheckingSubscription?.cancel();
if (state == UsernameAvailableState.checking) { stillCheckingSubscription = _isUsernameAvailableSubj.stream.listen((state) {
_isUsernameAvailableSubj.add(UsernameAvailableState.stillChecking); if (state == UsernameAvailableState.checking) {
} _isUsernameAvailableSubj.add(UsernameAvailableState.stillChecking);
}); }
},
);
}); });
var addError = (error) { final addError = (error) {
_isUsernameAvailableSubj.addError(error); stillCheckingSubscription?.cancel();
_isUsernameAvailableSubj.add(UsernameAvailableState.none); _isUsernameAvailableSubj.add(UsernameAvailableState.none);
_isUsernameAvailableSubj.addError(error);
}; };
var user; var user;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment