Add progress indicator when changing name

Also move RequestState to it's own file
parent 5ab2d8b0
......@@ -21,6 +21,8 @@ import 'package:pattle/src/ui/main/settings/settings_bloc.dart';
import 'package:pattle/src/ui/resources/theme.dart';
import 'package:pattle/src/ui/util/user.dart';
import '../../request_state.dart';
class NamePageState extends State<NamePage> {
final bloc = SettingsBloc();
......@@ -92,6 +94,23 @@ class NamePageState extends State<NamePage> {
),
onSubmitted: (_) => setName(),
),
StreamBuilder<RequestState>(
stream: bloc.displayNameStream,
builder: (
BuildContext context,
AsyncSnapshot<RequestState> snapshot,
) {
print(snapshot.connectionState);
if (snapshot.data == RequestState.active) {
return LinearProgressIndicator(
valueColor: AlwaysStoppedAnimation(LightColors.red[300]),
backgroundColor: LightColors.red[100],
);
} else {
return Container(height: 6);
}
},
),
Row(
children: <Widget>[
Padding(
......
......@@ -16,8 +16,10 @@
// along with Pattle. If not, see <https://www.gnu.org/licenses/>.
import 'package:pattle/src/di.dart' as di;
import 'package:rxdart/rxdart.dart';
import '../../bloc.dart';
import '../../request_state.dart';
class SettingsBloc extends Bloc {
final me = di.getLocalUser();
......@@ -28,7 +30,15 @@ class SettingsBloc extends Bloc {
factory SettingsBloc() => _instance;
PublishSubject<RequestState> _displayNameSubj =
PublishSubject<RequestState>();
Stream<RequestState> get displayNameStream => _displayNameSubj.stream;
Future<void> setDisplayName(String name) async {
await me.setName(name);
_displayNameSubj.add(RequestState.active);
await me.setName(name).catchError(_displayNameSubj.addError);
_displayNameSubj.add(RequestState.success);
}
}
// Copyright (C) 2019 Wilko Manger
//
// This file is part of Pattle.
//
// Pattle is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Pattle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with Pattle. If not, see <https://www.gnu.org/licenses/>.
class RequestState {
final int _value;
const RequestState(int value) : _value = value;
static const none = RequestState(0);
static const active = RequestState(1);
static const stillActive = RequestState(2);
static const success = RequestSuccessState();
@override
bool operator ==(other) {
if (other is RequestState) {
return other._value == this._value;
} else {
return false;
}
}
@override
int get hashCode => _value.hashCode;
}
class RequestSuccessState<T> extends RequestState {
final T data;
const RequestSuccessState({this.data}) : super(3);
}
......@@ -26,6 +26,8 @@ import 'package:pattle/src/ui/start/start_bloc.dart';
import 'package:pattle/src/ui/util/lower_case_text_formatter.dart';
import 'package:chopper/chopper.dart';
import '../../../request_state.dart';
class UsernamePage extends StatefulWidget {
@override
State<StatefulWidget> createState() => UsernamePageState();
......
......@@ -25,6 +25,8 @@ import 'package:pattle/src/ui/resources/localizations.dart';
import 'package:pattle/src/ui/start/start_bloc.dart';
import 'package:pattle/src/ui/util/future_or_builder.dart';
import '../../../request_state.dart';
class PasswordPageState extends State<PasswordPage> {
final StartBloc bloc = StartBloc();
......
......@@ -27,6 +27,7 @@ import 'package:pedantic/pedantic.dart';
import '../../app_bloc.dart';
import '../bloc.dart';
import '../request_state.dart';
typedef Request = void Function(Function addError);
typedef Check = bool Function(Function addError);
......@@ -215,35 +216,6 @@ class StartBloc extends Bloc {
}
}
class RequestState {
final int _value;
const RequestState(int value) : _value = value;
static const none = RequestState(0);
static const active = RequestState(1);
static const stillActive = RequestState(2);
static const success = RequestSuccessState();
@override
bool operator ==(other) {
if (other is RequestState) {
return other._value == this._value;
} else {
return false;
}
}
@override
int get hashCode => _value.hashCode;
}
class RequestSuccessState<T> extends RequestState {
final T data;
const RequestSuccessState({this.data}) : super(3);
}
class InvalidUserIdException implements Exception {}
class InvalidHostnameException implements Exception {}
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