openlp-mobile-remote/lib/src/bloc/settings_bloc.dart

162 lines
4.8 KiB
Dart

// OpenLP Mobile Remote
// ---------------------------------------------------------------------------
// Copyright (c) 2008-2019 OpenLP Developers
// ---------------------------------------------------------------------------
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:meta/meta.dart';
class SettingsState {
String serverIp;
int serverPort;
bool useHttps;
bool needsAuth;
String userId;
String userPassword;
SettingsState({
@required this.serverIp,
@required this.serverPort,
@required this.useHttps,
@required this.needsAuth,
@required this.userId,
@required this.userPassword,
});
SettingsState copyWith({
String serverIp,
int serverPort,
bool useHttps,
bool needsAuth,
String userId,
String userPassword,
}) {
return SettingsState(
serverIp: serverIp ?? this.serverIp,
serverPort: serverPort ?? this.serverPort,
useHttps: useHttps ?? this.useHttps,
needsAuth: needsAuth ?? this.needsAuth,
userId: userId ?? this.userId,
userPassword: userPassword ?? this.userPassword,
);
}
}
abstract class SettingsEvent<T> extends Equatable {
final T value;
SettingsEvent(this.value, [List props = const <dynamic>[]]) : super(props);
}
class SetServerIpEvent extends SettingsEvent<String> {
SetServerIpEvent(String value) : super(value);
}
class SetServerPortEvent extends SettingsEvent<int> {
SetServerPortEvent(int value) : super(value);
}
class SetUseHttpsEvent extends SettingsEvent<bool> {
SetUseHttpsEvent(bool value) : super(value);
}
class SetNeedsAuthEvent extends SettingsEvent<bool> {
SetNeedsAuthEvent(bool value) : super(value);
}
class SetUserIdEvent extends SettingsEvent<String> {
SetUserIdEvent(String value) : super(value);
}
class SetUserPasswordEvent extends SettingsEvent<String> {
SetUserPasswordEvent(String value) : super(value);
}
class SettingsBloc extends Bloc<SettingsEvent, SettingsState> {
@override
SettingsState get initialState => SettingsState(
serverIp: '192.168.1.100',
serverPort: 4316,
useHttps: false,
needsAuth: false,
userId: 'openlp',
userPassword: 'password',
);
void setServerIp(String serverIp) {
if (serverIp == null || serverIp.isEmpty) {
return;
}
dispatch(SetServerIpEvent(serverIp));
}
void setServerPort(String serverPortStr) {
if (serverPortStr == null || serverPortStr.isEmpty) {
return;
}
int serverPort = int.tryParse(serverPortStr);
dispatch(SetServerPortEvent(serverPort));
}
void setUseHttps(bool useHttps) {
dispatch(SetUseHttpsEvent(useHttps));
}
void setNeedsAuth(bool needsAuth) {
dispatch(SetNeedsAuthEvent(needsAuth));
}
void setUserId(String userId) {
if (userId == null || userId.isEmpty) {
return;
}
dispatch(SetUserIdEvent(userId));
}
void setUserPassword(String userPassword) {
if (userPassword == null || userPassword.isEmpty) {
return;
}
dispatch(SetUserPasswordEvent(userPassword));
}
@override
Stream<SettingsState> mapEventToState(SettingsEvent event) async* {
if (event is SetServerIpEvent) {
yield this.currentState.copyWith(serverIp: event.value);
}
if (event is SetServerPortEvent) {
yield this.currentState.copyWith(serverPort: event.value);
}
if (event is SetUseHttpsEvent) {
yield this.currentState.copyWith(useHttps: event.value);
}
if (event is SetNeedsAuthEvent) {
yield this.currentState.copyWith(needsAuth: event.value);
}
if (event is SetUserIdEvent) {
yield this.currentState.copyWith(userId: event.value);
}
if (event is SetUserPasswordEvent) {
yield this.currentState.copyWith(userPassword: event.value);
}
}
}