Skip to content

Commit

Permalink
Inserimento posizione libro via CAP.
Browse files Browse the repository at this point in the history
Took 40 minutes
  • Loading branch information
emiliodallatorre committed Nov 4, 2023
1 parent a8b543b commit 9cfeea4
Show file tree
Hide file tree
Showing 15 changed files with 133 additions and 35 deletions.
1 change: 1 addition & 0 deletions lib/bloc/book_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class BooksBloc {
}
_booksFetcher.sink.add(books);
debugPrint("Inviate al sink tutte le ricerche.");
debugPrint(StackTrace.current.toString());

return books;
}
Expand Down
6 changes: 5 additions & 1 deletion lib/generated/intl/messages_en.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class MessageLookup extends MessageLookupByLibrary {
"insertIsbn": MessageLookupByLibrary.simpleMessage("Insert ISBN"),
"insertSmsCode": MessageLookupByLibrary.simpleMessage(
"Insert the code you received by SMS"),
"insertValidZipCode":
MessageLookupByLibrary.simpleMessage("Insert a valid zip code"),
"inviteAFriend":
MessageLookupByLibrary.simpleMessage("Invite a friend"),
"isbn": MessageLookupByLibrary.simpleMessage("ISBN"),
Expand Down Expand Up @@ -147,6 +149,8 @@ class MessageLookup extends MessageLookupByLibrary {
"wrongCode": MessageLookupByLibrary.simpleMessage(
"This code is not working, try again"),
"wrongField": MessageLookupByLibrary.simpleMessage(
"This field is not filled correctly")
"This field is not filled correctly"),
"zipCodeOfBook": MessageLookupByLibrary.simpleMessage(
"Zip code of the book location")
};
}
6 changes: 5 additions & 1 deletion lib/generated/intl/messages_fr.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class MessageLookup extends MessageLookupByLibrary {
"insertIsbn": MessageLookupByLibrary.simpleMessage("Insérer l\'ISBN"),
"insertSmsCode": MessageLookupByLibrary.simpleMessage(
"Insérez le code que vous avez reçu par SMS"),
"insertValidZipCode": MessageLookupByLibrary.simpleMessage(
"Insérez un code postal valide"),
"inviteAFriend": MessageLookupByLibrary.simpleMessage("Invite un ami"),
"isbn": MessageLookupByLibrary.simpleMessage("ISBN"),
"isbnLengthError": MessageLookupByLibrary.simpleMessage(
Expand Down Expand Up @@ -154,6 +156,8 @@ class MessageLookup extends MessageLookupByLibrary {
"wrongCode": MessageLookupByLibrary.simpleMessage(
"Ce code ne fonctionne pas, réessayez"),
"wrongField": MessageLookupByLibrary.simpleMessage(
"Ce champ n\'est pas rempli correctement")
"Ce champ n\'est pas rempli correctement"),
"zipCodeOfBook":
MessageLookupByLibrary.simpleMessage("Code postal ou est le livre")
};
}
6 changes: 5 additions & 1 deletion lib/generated/intl/messages_it.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ class MessageLookup extends MessageLookupByLibrary {
"insertIsbn": MessageLookupByLibrary.simpleMessage("Inserisci ISBN"),
"insertSmsCode": MessageLookupByLibrary.simpleMessage(
"Inserisci il codice che hai ricevuto tramite SMS"),
"insertValidZipCode":
MessageLookupByLibrary.simpleMessage("Inserisci un CAP valido"),
"inviteAFriend":
MessageLookupByLibrary.simpleMessage("Invita un amico"),
"isbn": MessageLookupByLibrary.simpleMessage("ISBN"),
Expand Down Expand Up @@ -152,6 +154,8 @@ class MessageLookup extends MessageLookupByLibrary {
"wrongCode": MessageLookupByLibrary.simpleMessage(
"Questo codice non è corretto, riprova"),
"wrongField": MessageLookupByLibrary.simpleMessage(
"Inserisci correttamente questo dato")
"Inserisci correttamente questo dato"),
"zipCodeOfBook":
MessageLookupByLibrary.simpleMessage("CAP dove si trova il libro")
};
}
20 changes: 20 additions & 0 deletions lib/generated/l10n.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions lib/interface/pages/chat_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class ChatPage extends StatelessWidget {
trailing: chatsSnapshot.data!.elementAt(index).hasUnreadMessages
? Icon(Icons.circle_notifications, color: Theme.of(context).colorScheme.secondary)
: null,
leading: chatsSnapshot.data!.elementAt(index).recipient.profileImageUrl == null
? null
: CircleAvatar(backgroundImage: NetworkImage(chatsSnapshot.data!.elementAt(index).recipient.profileImageUrl!)),
onTap: () {
Provider.of<ChatBloc>(context, listen: false).chatsListener!.pause();
debugPrint("Ho messo in pausa lo stream delle chat.");
Expand Down
89 changes: 64 additions & 25 deletions lib/interface/screen/book_editor_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,16 @@
* Last modified 20/05/21, 10:07.
*/

import 'dart:io';

import 'package:bookaround/bloc/book_bloc.dart';
import 'package:bookaround/generated/l10n.dart';
import 'package:bookaround/interface/widget/book_cover.dart';
import 'package:bookaround/models/book_model.dart';
import 'package:bookaround/models/place_model.dart';
import 'package:bookaround/models/user_model.dart';
import 'package:bookaround/references.dart';
import 'package:bookaround/resources/helper/book_helper.dart';
import 'package:bookaround/resources/helper/geocoding_helper.dart';
import 'package:flutter/material.dart';
import 'package:flutter_google_places/flutter_google_places.dart';
import 'package:form_field_validator/form_field_validator.dart';
import 'package:google_maps_webservice/places.dart';
import 'package:flutter_geocoder/geocoder.dart';
import 'package:provider/provider.dart';

class BookEditorScreen extends StatefulWidget {
Expand All @@ -29,17 +24,24 @@ class BookEditorScreen extends StatefulWidget {
}

class _BookEditorScreenState extends State<BookEditorScreen> {
BookModel? book;
final GlobalKey<FormFieldState> locationKey = GlobalKey<FormFieldState>();

late BookModel book;

TextEditingController locationController = TextEditingController();
GlobalKey<FormState> formKey = GlobalKey<FormState>();

bool saved = false;
bool initialized = false;
bool hasInsertedZipCode = false;

@override
Widget build(BuildContext context) {
if (book == null) {
if (!initialized) {
book = ModalRoute.of(context)!.settings.arguments as BookModel;
if (book!.location != null) locationController.text = book!.location!.description!;
if (book.location != null) {
locationController.text = book.location!.description!;
hasInsertedZipCode = true;
}
}

return WillPopScope(
Expand Down Expand Up @@ -75,21 +77,22 @@ class _BookEditorScreenState extends State<BookEditorScreen> {

Widget _buildBody(BuildContext context) {
return ListView(
keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
children: [
BookCover(book: book!),
BookCover(book: book),
CheckboxListTile(
value: book!.pencil,
onChanged: (bool? value) => setState(() => book!.pencil = value!),
value: book.pencil,
onChanged: (bool? value) => setState(() => book.pencil = value!),
title: Text(S.current.pencil),
),
CheckboxListTile(
value: book!.highlighting,
onChanged: (bool? value) => setState(() => book!.highlighting = value!),
value: book.highlighting,
onChanged: (bool? value) => setState(() => book.highlighting = value!),
title: Text(S.current.highlight),
),
CheckboxListTile(
value: book!.pen,
onChanged: (bool? value) => setState(() => book!.pen = value!),
value: book.pen,
onChanged: (bool? value) => setState(() => book.pen = value!),
title: Text(S.current.pen),
),
Form(
Expand All @@ -99,11 +102,47 @@ class _BookEditorScreenState extends State<BookEditorScreen> {
child: Column(
children: [
TextFormField(
readOnly: true,
validator: RequiredValidator(errorText: S.current.requiredField),
key: locationKey,
// readOnly: true,
validator: (final String? value) {
if (value == null || value.isEmpty) return S.current.requiredField;
if (int.tryParse(value) == null || value.length != 5) return S.current.insertValidZipCode;

return null;
},
controller: locationController,
decoration: InputDecoration(labelText: S.current.whereIsBook),
onTap: () async {
textInputAction: TextInputAction.search,
keyboardType: TextInputType.numberWithOptions(signed: true),
decoration: InputDecoration(
labelText: S.current.zipCodeOfBook,
suffixIcon: IconButton(
icon: Icon(hasInsertedZipCode ? Icons.close : Icons.check),
onPressed: () async {
if (hasInsertedZipCode) {
locationController.clear();

book.location = null;
book.locationData = null;

hasInsertedZipCode = false;

setState(() {});
} else {
if (locationKey.currentState!.validate()) {
final (Address, Map<String, dynamic>) zipGeocoding = await GeocodingHelper.decodeZipCode(int.parse(locationController.text));
book.locationData = zipGeocoding.$2;
book.location = PlaceModel.fromAddress(zipGeocoding.$1);

debugPrint("Il libro si trova a ${book.location!.toJson().toString()}.");
// locationController.text = zipGeocoding.$1.adminArea;

setState(() {});
}
}
},
),
),
/*onTap: () async {
Prediction? prediction = await PlacesAutocomplete.show(
context: context,
apiKey: References.googleApiKey,
Expand All @@ -124,13 +163,13 @@ class _BookEditorScreenState extends State<BookEditorScreen> {
setState(() {});
}
},
},*/
),
TextFormField(
decoration: InputDecoration(labelText: S.current.note, alignLabelWithHint: true),
textInputAction: TextInputAction.done,
initialValue: book!.note,
onSaved: (String? value) => setState(() => book!.note = value!),
initialValue: book.note,
onSaved: (String? value) => setState(() => book.note = value!),
minLines: 4,
maxLines: 4,
),
Expand All @@ -146,7 +185,7 @@ class _BookEditorScreenState extends State<BookEditorScreen> {
if (formKey.currentState!.validate()) {
formKey.currentState!.save();

await BookHelper.updateBook(book!);
await BookHelper.updateBook(book);
await sellBooksBloc.getUserBooks(Provider.of<UserModel>(context, listen: false).uid!, Provider.of<UserModel>(context, listen: false).blockedUids!);

saved = true;
Expand Down
2 changes: 0 additions & 2 deletions lib/interface/screen/search_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ class SearchScreen extends StatelessWidget {
builder: (BuildContext context, Widget? child) => Scaffold(
appBar: AppBar(
title: TextField(
style: TextStyle(color: Colors.white),
decoration: InputDecoration.collapsed(
hintText: S.current.searchBook,
hintStyle: TextStyle(color: Colors.white),
),
textInputAction: TextInputAction.search,
onSubmitted: (String query) async => await Provider.of<SearchScreenState>(context, listen: false).findBooks(query),
Expand Down
2 changes: 1 addition & 1 deletion lib/interface/widget/message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Message extends StatelessWidget {
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
this.message.body!,
this.message.body,
style: TextStyle(
color: iAmTheSender(context) ? Colors.white : Colors.black,
),
Expand Down
4 changes: 3 additions & 1 deletion lib/l10n/intl_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,7 @@
"blockUser": "Block this user",
"noBlocked": "At the moment, you haven't blocked any user.\nUse the options button in a chat screen to block a user.",
"blockedUsers": "Blocked users",
"unblockUser": "Unblock"
"unblockUser": "Unblock",
"zipCodeOfBook": "Zip code of the book location",
"insertValidZipCode": "Insert a valid zip code"
}
4 changes: 3 additions & 1 deletion lib/l10n/intl_fr.arb
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,7 @@
"blockUser": "Bloquer cet utilisateur",
"noBlocked": "Pour le moment, vous n'avez bloqué aucun utilisateur.\nUtilisez le bouton d'options dans un écran de discussion pour bloquer un utilisateur.",
"blockedUsers": "Utilisateurs bloqués",
"unblockUser": "Déverrouiller"
"unblockUser": "Déverrouiller",
"zipCodeOfBook": "Code postal ou est le livre",
"insertValidZipCode": "Insérez un code postal valide"
}
4 changes: 3 additions & 1 deletion lib/l10n/intl_it.arb
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,7 @@
"blockUser": "Blocca questo utente",
"noBlocked": "Al momento, non hai bloccato nessun utente.\nUtilizza il pulsante delle opzioni in una chat per bloccare un utente.",
"blockedUsers": "Utenti bloccati",
"unblockUser": "Sblocca"
"unblockUser": "Sblocca",
"zipCodeOfBook": "CAP dove si trova il libro",
"insertValidZipCode": "Inserisci un CAP valido"
}
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class Bookaround extends StatelessWidget {
builder: (BuildContext context, Widget? child) => OverlaySupport.global(
child: MaterialApp(
title: References.appName,
theme: ThemeData(colorSchemeSeed: mainColor, fontFamily: "Poppins", useMaterial3: false),
theme: ThemeData(colorSchemeSeed: mainColor, fontFamily: "Poppins", useMaterial3: true),
darkTheme: ThemeData(
fontFamily: "Poppins",
brightness: Brightness.dark,
Expand Down
8 changes: 8 additions & 0 deletions lib/models/place_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Last modified 20/05/21, 10:07.
*/

import 'package:flutter_geocoder/geocoder.dart';
import 'package:google_maps_webservice/places.dart';
import 'package:json_annotation/json_annotation.dart';

Expand Down Expand Up @@ -33,6 +34,13 @@ class PlaceModel {
placeReference: prediction.reference,
);

factory PlaceModel.fromAddress(Address address) => PlaceModel(
id: address.featureName,
description: address.locality ?? address.adminArea ?? address.countryName ?? address.addressLine,
placeId: address.featureName,
placeReference: address.featureName,
);

factory PlaceModel.fromJson(Map<String, dynamic> parsedJson) => _$PlaceModelFromJson(parsedJson);

Map<String, dynamic> toJson() => _$PlaceModelToJson(this);
Expand Down
11 changes: 11 additions & 0 deletions lib/resources/helper/geocoding_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,16 @@ class GeocodingHelper {
return GeoFirePoint(GeoPoint(locations.first.coordinates.latitude!, locations.first.coordinates.longitude!)).data;
}

static Future<(Address, GeoFirePoint)> decodeZipCode(int zipCode) async {
debugPrint("Cerco le coordinate di \"$zipCode\".");

List<Address> locations = await Geocoder.google(References.googleApiKey).findAddressesFromQuery(zipCode.toString() + " Italy Europe");
if (locations.isEmpty) throw "CAP non trovato.";

debugPrint(locations.first.toMap().toString());

return (locations.first, GeoFirePoint(GeoPoint(locations.first.coordinates.latitude!, locations.first.coordinates.longitude!)));
}

static double distanceBetween(LatLng a, LatLng b) => Geolocator.distanceBetween(a.latitude, a.longitude, b.latitude, b.longitude) / 1000;
}

0 comments on commit 9cfeea4

Please sign in to comment.