Skip to content

Commit

Permalink
Declarative fields (#27)
Browse files Browse the repository at this point in the history
* Make fields in edit_call_screen.dart declarative

* Address analyzer warnings

* Make fields in new_call_screen.dart declarative

* Remove print

* Bump version and build number
  • Loading branch information
GroovinChip authored May 17, 2021
1 parent dd9a070 commit 66d7a53
Show file tree
Hide file tree
Showing 7 changed files with 310 additions and 249 deletions.
2 changes: 2 additions & 0 deletions lib/firebase/firebase_auth_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ String generateNonce([int length = 32]) {
final charset =
'0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._';
final random = Random.secure();

return List.generate(length, (_) => charset[random.nextInt(charset.length)])
.join();
}
Expand All @@ -21,6 +22,7 @@ String generateNonce([int length = 32]) {
String sha256ofString(String input) {
final bytes = utf8.encode(input);
final digest = sha256.convert(bytes);

return digest.toString();
}

Expand Down
247 changes: 134 additions & 113 deletions lib/screens/edit_call_screen.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:bluejay/bluejay.dart';
import 'package:call_manager/data_models/call.dart';
import 'package:call_manager/firebase/firebase.dart';
import 'package:call_manager/provided.dart';
Expand Down Expand Up @@ -25,11 +26,7 @@ class EditCallScreen extends StatefulWidget {
class _EditCallScreenState extends State<EditCallScreen>
with FirebaseMixin, Provided {
Contact? selectedContact;
//TextField controllers
final _descriptionFieldController = TextEditingController();
final _nameFieldController = TextEditingController();
final _phoneFieldController = TextEditingController();

final _formKey = GlobalKey<FormState>();

@override
// ignore: long-method, code-metrics
Expand All @@ -45,128 +42,152 @@ class _EditCallScreenState extends State<EditCallScreen>
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
TypeAheadFormField(
suggestionsCallback: contactsUtility.searchContactsWithQuery,
itemBuilder: (context, dynamic contact) {
return ListTile(
leading: ContactAvatar(contact: contact),
title: Text(contact.displayName),
);
},
transitionBuilder: (context, suggestionsBox, controller) {
return suggestionsBox;
},
onSuggestionSelected: (dynamic contact) {
selectedContact = contact;
_nameFieldController.text = selectedContact!.displayName!;
if (selectedContact!.phones!.length > 1) {
showModalBottomSheet(
context: context,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
builder: (_) => MultiplePhoneNumbersSheet(
selectedContact: selectedContact,
child: Form(
key: _formKey,
child: Column(
children: [
TextEditingControllerBuilder(
text: widget.call.name!,
builder: (_, controller) {
return TypeAheadFormField(
suggestionsCallback:
contactsUtility.searchContactsWithQuery,
itemBuilder: (context, dynamic contact) {
return ListTile(
leading: ContactAvatar(contact: contact),
title: Text(contact.displayName),
);
},
transitionBuilder: (context, suggestionsBox, controller) {
return suggestionsBox;
},
onSuggestionSelected: (dynamic contact) {
selectedContact = contact;
controller.text = selectedContact!.displayName!;
if (selectedContact!.phones!.length > 1) {
showModalBottomSheet(
context: context,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
builder: (_) => MultiplePhoneNumbersSheet(
selectedContact: selectedContact,
),
).then((value) => widget.call.phoneNumber = value);
} else {
widget.call.phoneNumber =
selectedContact!.phones!.first.value!;
}
},
validator: (value) => value == null || value.isEmpty
? 'This field is required'
: null,
onSaved: (contactName) {
controller.text = contactName!;
widget.call.name = contactName;
},
textFieldConfiguration: TextFieldConfiguration(
textCapitalization: TextCapitalization.words,
controller: controller,
keyboardType: TextInputType.text,
maxLines: 1,
decoration: InputDecoration(
prefixIcon: Icon(
Icons.person_outline,
color: theme.iconTheme.color,
),
suffixIcon: ClearButton(
onPressed: () {
controller.clear();
widget.call.name = controller.text;
},
),
labelText: 'Name',
),
),
).then((value) => _phoneFieldController.text = value);
} else {
_phoneFieldController.text =
selectedContact!.phones!.first.value!;
}
},
validator: (input) => input == null || input == ''
? 'This field is required'
: null,
onSaved: (contactName) =>
_nameFieldController.text = contactName!,
textFieldConfiguration: TextFieldConfiguration(
textCapitalization: TextCapitalization.words,
controller: _nameFieldController,
keyboardType: TextInputType.text,
maxLines: 1,
decoration: InputDecoration(
prefixIcon: Icon(
Icons.person_outline,
color: theme.iconTheme.color,
),
suffixIcon: ClearButton(
onPressed: () => _nameFieldController.text = '',
),
labelText: widget.call.name,
),
);
},
),
),
const SizedBox(height: 16.0),
TextField(
keyboardType: TextInputType.phone,
maxLines: 1,
autofocus: false,
controller: _phoneFieldController,
decoration: InputDecoration(
prefixIcon: Icon(
Icons.phone_outlined,
color: theme.iconTheme.color,
),
suffixIcon: ClearButton(
onPressed: () => _phoneFieldController.text = '',
),
labelText: widget.call.phoneNumber,
const SizedBox(height: 16.0),
TextEditingControllerBuilder(
text: widget.call.phoneNumber!,
builder: (_, controller) {
return TextFormField(
keyboardType: TextInputType.phone,
maxLines: 1,
autofocus: false,
controller: controller,
onChanged: (value) => widget.call.phoneNumber = value,
validator: (value) => value == null || value.isEmpty
? 'This field is required'
: null,
decoration: InputDecoration(
prefixIcon: Icon(
Icons.phone_outlined,
color: theme.iconTheme.color,
),
suffixIcon: ClearButton(
onPressed: () {
controller.clear();
widget.call.phoneNumber = controller.text;
},
),
labelText: 'Phone number',
),
);
},
),
),
const SizedBox(height: 16.0),
TextFormField(
keyboardType: TextInputType.multiline,
textCapitalization: TextCapitalization.sentences,
maxLines: 2,
autofocus: false,
controller: _descriptionFieldController,
decoration: InputDecoration(
labelText: widget.call.hasDescription
? widget.call.description
: 'Description',
prefixIcon: Icon(
Icons.comment_outlined,
color: theme.iconTheme.color,
),
suffixIcon: ClearButton(
onPressed: () => _descriptionFieldController.text = '',
),
const SizedBox(height: 16.0),
TextEditingControllerBuilder(
text: widget.call.description ?? '',
builder: (_, controller) {
return TextFormField(
keyboardType: TextInputType.multiline,
textCapitalization: TextCapitalization.sentences,
maxLines: 2,
autofocus: false,
controller: controller,
onChanged: (value) => widget.call.description = value,
decoration: InputDecoration(
labelText: 'Description',
prefixIcon: Icon(
Icons.comment_outlined,
color: theme.iconTheme.color,
),
suffixIcon: ClearButton(
onPressed: () {
controller.clear();
widget.call.description = controller.text;
},
),
),
);
},
),
),
],
],
),
),
),
),
floatingActionButton: !MediaQuery.of(context).keyboardOpen
? FloatingActionButton.extended(
highlightElevation: 2.0,
onPressed: () {
if (_nameFieldController.text.isNotEmpty) {
widget.call.name = _nameFieldController.text;
}

if (_phoneFieldController.text.isNotEmpty) {
widget.call.phoneNumber = _phoneFieldController.text;
}
_formKey.currentState!.save();
if (_formKey.currentState!.validate()) {
if (selectedContact != null) {
widget.call.avatar = selectedContact?.avatar != null
? String.fromCharCodes(selectedContact!.avatar!)
: '';
}

if (_descriptionFieldController.text.isNotEmpty) {
widget.call.description = _descriptionFieldController.text;
}
firestore
.calls(currentUser!.uid)
.doc(widget.call.id)
.update(widget.call.toJson());

if (selectedContact != null) {
widget.call.avatar = selectedContact?.avatar != null
? String.fromCharCodes(selectedContact!.avatar!)
: '';
Navigator.of(context).pop();
}

firestore
.calls(currentUser!.uid)
.doc(widget.call.id)
.update(widget.call.toJson());

Navigator.of(context).pop();
},
tooltip: 'Save',
elevation: 2.0,
Expand Down
Loading

0 comments on commit 66d7a53

Please sign in to comment.