ui: Migrate AddContactPage to Redux

This commit is contained in:
PapaTutuWawa 2021-12-24 20:33:04 +01:00
parent 3396a36b47
commit a88ceb7897
7 changed files with 150 additions and 108 deletions

View File

@ -6,7 +6,7 @@ import 'ui/pages/newconversation.dart';
import 'ui/pages/login/login.dart';
import 'ui/pages/register.dart';
import 'ui/pages/intro.dart';
import 'ui/pages/addcontact.dart';
import 'ui/pages/addcontact/addcontact.dart';
import 'repositories/roster.dart';
import 'package:flutter_redux/flutter_redux.dart';

View File

@ -0,0 +1,5 @@
class AddContactAction {
final String jid;
AddContactAction({ required this.jid });
}

View File

@ -0,0 +1,10 @@
import "package:moxxyv2/ui/pages/addcontact/state.dart";
import "package:moxxyv2/redux/addcontact/actions.dart";
AddContactPageState addContactPageReducer(AddContactPageState state, dynamic action) {
if (action is AddContactAction) {
return state.copyWith(doingWork: true);
}
return state;
}

View File

@ -2,8 +2,10 @@ import "dart:collection";
import "package:moxxyv2/redux/conversation/reducers.dart";
import "package:moxxyv2/redux/conversations/reducers.dart";
import "package:moxxyv2/redux/login/reducers.dart";
import "package:moxxyv2/redux/addcontact/reducers.dart";
import "package:moxxyv2/ui/pages/login/state.dart";
import "package:moxxyv2/ui/pages/conversation/state.dart";
import "package:moxxyv2/ui/pages/addcontact/state.dart";
import "package:moxxyv2/models/message.dart";
import "package:moxxyv2/models/conversation.dart";
@ -12,7 +14,8 @@ MoxxyState moxxyReducer(MoxxyState state, dynamic action) {
messages: messageReducer(state.messages, action),
conversations: conversationReducer(state.conversations, action),
loginPageState: loginReducer(state.loginPageState, action),
conversationPageState: conversationPageReducer(state.conversationPageState, action)
conversationPageState: conversationPageReducer(state.conversationPageState, action),
addContactPageState: addContactPageReducer(state.addContactPageState, action)
);
}
@ -21,7 +24,8 @@ class MoxxyState {
final List<Conversation> conversations;
final LoginPageState loginPageState;
final ConversationPageState conversationPageState;
final AddContactPageState addContactPageState;
const MoxxyState({ required this.messages, required this.conversations, required this.loginPageState, required this.conversationPageState });
MoxxyState.initialState() : messages = HashMap(), conversations = List.empty(growable: true), loginPageState = LoginPageState(doingWork: false, showPassword: false), conversationPageState = ConversationPageState(showSendButton: false);
const MoxxyState({ required this.messages, required this.conversations, required this.loginPageState, required this.conversationPageState, required this.addContactPageState });
MoxxyState.initialState() : messages = HashMap(), conversations = List.empty(growable: true), loginPageState = LoginPageState(doingWork: false, showPassword: false), conversationPageState = ConversationPageState(showSendButton: false), addContactPageState = AddContactPageState(doingWork: false);
}

View File

@ -1,104 +0,0 @@
import 'package:flutter/material.dart';
import 'package:moxxyv2/ui/widgets/topbar.dart';
class AddContactPage extends StatefulWidget {
const AddContactPage({ Key? key }) : super(key: key);
@override
_AddContactPageState createState() => _AddContactPageState();
}
class _AddContactPageState extends State<AddContactPage> {
bool _doingWork = false;
void _addToRoster(BuildContext context) {
setState(() {
this._doingWork = true;
});
Future.delayed(
Duration(seconds: 3),
() {
Navigator.pushNamedAndRemoveUntil(
context,
"/conversation",
ModalRoute.withName("/conversations"));
}
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: PreferredSize(
preferredSize: Size.fromHeight(60),
child: BorderlessTopbar(
children: [
BackButton(),
Text(
"Add new contact",
style: TextStyle(
fontSize: 19
)
)
]
)
),
// TODO: The TextFields look a bit too smal
// TODO: Hide the LinearProgressIndicator if we're not doing anything
// TODO: Disable the inputs and the BackButton if we're working on loggin in
body: Column(
children: [
Visibility(
visible: this._doingWork,
child: LinearProgressIndicator(value: null)
),
Padding(
padding: EdgeInsets.only(top: 8.0, left: 16.0, right: 16.0),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15),
border: Border.all(
width: 1,
color: Colors.purple
)
),
child: TextField(
maxLines: 1,
enabled: !this._doingWork,
decoration: InputDecoration(
labelText: "XMPP-Address",
border: InputBorder.none,
contentPadding: EdgeInsets.only(top: 4.0, bottom: 4.0, left: 8.0, right: 8.0),
suffixIcon: Padding(
padding: EdgeInsetsDirectional.only(end: 6.0),
child: Icon(Icons.qr_code)
)
)
)
)
),
Padding(
padding: EdgeInsets.all(16.0),
child: Text("You can add a contact either by typing in their XMPP address or by scanning their QR code")
),
Row(
children: [
Expanded(
child: Padding(
padding: EdgeInsets.all(16.0),
child: ElevatedButton(
child: Text("Add to contacts"),
// TODO: Add to roster and open a chat
onPressed: this._doingWork ? null : () => _addToRoster(context)
)
)
)
]
)
]
)
);
}
}

View File

@ -0,0 +1,116 @@
import 'package:flutter/material.dart';
import 'package:moxxyv2/ui/widgets/topbar.dart';
import 'package:moxxyv2/redux/state.dart';
import 'package:moxxyv2/redux/addcontact/actions.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:redux/redux.dart';
class _AddContactPageViewModel {
final bool doingWork;
final void Function(String jid) addContact;
_AddContactPageViewModel({ required this.addContact, required this.doingWork});
}
class AddContactPage extends StatelessWidget {TextEditingController controller = TextEditingController();
void _addToRoster(BuildContext context, _AddContactPageViewModel viewModel) {
viewModel.addContact(this.controller.text);
// TODO: Remove
// TODO: Redirect to a new conversation with the new contact
Future.delayed(
Duration(seconds: 3),
() {
Navigator.pushNamedAndRemoveUntil(
context,
"/conversations",
(route) => false);
}
);
}
@override
Widget build(BuildContext context) {
return StoreConnector<MoxxyState, _AddContactPageViewModel>(
converter: (store) => _AddContactPageViewModel(
doingWork: store.state.addContactPageState.doingWork,
addContact: (jid) => store.dispatch(AddContactAction(jid: jid))
),
builder: (context, viewModel) => Scaffold(
appBar: PreferredSize(
preferredSize: Size.fromHeight(60),
child: BorderlessTopbar(
children: [
BackButton(),
Text(
"Add new contact",
style: TextStyle(
fontSize: 19
)
)
]
)
),
// TODO: The TextFields look a bit too smal
// TODO: Hide the LinearProgressIndicator if we're not doing anything
// TODO: Disable the inputs and the BackButton if we're working on loggin in
body: Column(
children: [
Visibility(
visible: viewModel.doingWork,
child: LinearProgressIndicator(value: null)
),
Padding(
padding: EdgeInsets.only(top: 8.0, left: 16.0, right: 16.0),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15),
border: Border.all(
width: 1,
color: Colors.purple
)
),
child: TextField(
maxLines: 1,
enabled: !viewModel.doingWork,
controller: this.controller,
decoration: InputDecoration(
labelText: "XMPP-Address",
border: InputBorder.none,
contentPadding: EdgeInsets.only(top: 4.0, bottom: 4.0, left: 8.0, right: 8.0),
suffixIcon: Padding(
padding: EdgeInsetsDirectional.only(end: 6.0),
child: Icon(Icons.qr_code)
)
)
)
)
),
Padding(
padding: EdgeInsets.all(16.0),
child: Text("You can add a contact either by typing in their XMPP address or by scanning their QR code")
),
Row(
children: [
Expanded(
child: Padding(
padding: EdgeInsets.all(16.0),
child: ElevatedButton(
child: Text("Add to contacts"),
// TODO: Add to roster and open a chat
onPressed: viewModel.doingWork ? null : () => _addToRoster(context, viewModel)
)
)
)
]
)
]
)
)
);
}
}

View File

@ -0,0 +1,11 @@
class AddContactPageState {
final bool doingWork;
AddContactPageState({ required this.doingWork });
AddContactPageState copyWith({ bool? doingWork }) {
return AddContactPageState(
doingWork: doingWork ?? this.doingWork
);
}
}