Skip to content

Commit

Permalink
Merge pull request #643 from enshkn/development-mobile
Browse files Browse the repository at this point in the history
#642 nearby stories geolocation issue
  • Loading branch information
hdenizdogan authored Jan 3, 2024
2 parents 25569c8 + ccf589c commit 966cc94
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 43 deletions.
17 changes: 17 additions & 0 deletions mobile/lib/_application/splash/splash_cubit.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'package:auto_route/auto_route.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:injectable/injectable.dart';
import 'package:location/location.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:swe/_application/session/session_cubit.dart';
import 'package:swe/_core/storage/hive/i_cache_service.dart';
import 'package:swe/_core/utility/record_utils.dart';
Expand Down Expand Up @@ -29,6 +31,7 @@ final class SplashCubit extends BaseCubit<SplashState> {
await Future.wait(
[
initCacheManagers(),
getCurrentLocation(),
Future.delayed(const Duration(seconds: 2), () {}),
],
);
Expand Down Expand Up @@ -84,4 +87,18 @@ final class SplashCubit extends BaseCubit<SplashState> {
},
);
}

Future<void> getCurrentLocation() async {
late LocationData currentLocation;
final locationController = Location();
final prefs = await SharedPreferences.getInstance();

currentLocation = await locationController.getLocation();
if (currentLocation.latitude != null && currentLocation.longitude != null) {
await prefs.setDouble('latitude', currentLocation.latitude!);
await prefs.setDouble('longitude', currentLocation.longitude!);
}

// currentLocation = await locationController.getLocation();
}
}
55 changes: 34 additions & 21 deletions mobile/lib/_presentation/app/view/nearby/view/nearby.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:geocode/geocode.dart';
import 'package:location/location.dart';
import 'package:share_plus/share_plus.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:swe/_application/session/session_cubit.dart';
import 'package:swe/_application/session/session_state.dart';
import 'package:swe/_application/story/story_cubit.dart';
Expand All @@ -18,6 +20,7 @@ import 'package:swe/_presentation/widgets/base/base_list_view.dart';
import 'package:swe/_presentation/widgets/card/story_card.dart';
import 'package:swe/_presentation/widgets/textformfield/app_text_form_field.dart';
import 'package:swe/_presentation/widgets/wrapper/favorite_wrapper.dart';
import 'package:geocoding/geocoding.dart';

@RoutePage()
class NearbyView extends StatefulWidget {
Expand All @@ -29,8 +32,10 @@ class NearbyView extends StatefulWidget {

class _NearbyViewState extends State<NearbyView> {
final FocusNode _focusNode = FocusNode();
final Location _locationController = Location();
//final Location _locationController = Location();
late TextEditingController _radiusController;
String adress = '';
Address? _currentAddress;
GetNearbyStoriesModel model = const GetNearbyStoriesModel(
radius: 0,
latitude: 0,
Expand All @@ -41,22 +46,39 @@ class _NearbyViewState extends State<NearbyView> {
@override
void initState() {
_radiusController = TextEditingController();
getCurrentLocation();
super.initState();
}

Future<void> getCurrentLocation() async {
currentLocation = await _locationController.getLocation();
if (currentLocation.latitude != null && currentLocation.longitude != null) {
final prefs = await SharedPreferences.getInstance();
final latitude = prefs.getDouble('latitude');
final longtitude = prefs.getDouble('longitude');
if (latitude != null && longtitude != null) {
currentLocation = LocationData.fromMap({
'latitude': latitude,
'longitude': longtitude,
});
model = GetNearbyStoriesModel(
radius: 10,
latitude: currentLocation.latitude,
longitude: currentLocation.longitude,
);
currentAddress = await GeoCode().reverseGeocoding(
latitude: currentLocation.latitude!,
longitude: currentLocation.longitude!,
latitude: latitude,
longitude: longtitude,
);

await placemarkFromCoordinates(latitude, longtitude).then((placemarks) {
const output = 'No results found.';
if (placemarks.isNotEmpty) {
_currentAddress = Address(
streetAddress: placemarks[0].street.toString(),
city: placemarks[0].locality.toString(),
region: placemarks[0].administrativeArea.toString(),
countryCode: placemarks[0].isoCountryCode.toString(),
);
}

setState(() {
currentAddress = _currentAddress;
});
});
}
}

Expand All @@ -70,17 +92,8 @@ class _NearbyViewState extends State<NearbyView> {
onCubitReady: (cubit) async {
cubit.setContext(context);
cubit.init();

await getCurrentLocation();
if (currentLocation.latitude != null &&
currentLocation.longitude != null) {
model = GetNearbyStoriesModel(
radius: _radiusController.text != ''
? int.parse(_radiusController.text)
: 10,
latitude: currentLocation.latitude,
longitude: currentLocation.longitude,
);
}
await cubit.getNearbyStories(model);
},
builder: (context, cubit, state) {
Expand Down Expand Up @@ -170,7 +183,7 @@ class _NearbyViewState extends State<NearbyView> {
padding: const EdgeInsets.all(16),
child: SizedBox(
child: Text(
'*Your Current Location: ${currentAddress!.city}, ${currentAddress!.streetAddress}, ${currentAddress!.region} ',
'*Your Current Location: ${currentAddress!.city}, ${currentAddress!.streetAddress}, ${currentAddress!.region}, ${currentAddress!.countryCode} ',
),
),
),
Expand Down
26 changes: 15 additions & 11 deletions mobile/lib/_presentation/app/view/search/search_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter/material.dart';
import 'package:google_map_location_picker/map_location_picker.dart'
hide Location;
import 'package:location/location.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:swe/_application/session/session_cubit.dart';
import 'package:swe/_application/session/session_state.dart';
import 'package:swe/_application/story/story_cubit.dart';
Expand Down Expand Up @@ -46,17 +47,20 @@ class _SearchViewState extends State<SearchView> with ScrollAnimMixin {

Future<void> getCurrentLocation() async {
LocationData currentLocation;
Future.delayed(const Duration(seconds: 5), () async {
currentLocation = await _locationController.getLocation();
if (currentLocation.latitude != null &&
currentLocation.longitude != null) {
setState(() {
_currentPosition =
LatLng(currentLocation.latitude!, currentLocation.longitude!);
locationLoading = false;
});
}
});
final prefs = await SharedPreferences.getInstance();
final latitude = prefs.getDouble('latitude');
final longtitude = prefs.getDouble('longitude');
if (latitude != null && longtitude != null) {
currentLocation = LocationData.fromMap({
'latitude': latitude,
'longitude': longtitude,
});
setState(() {
_currentPosition =
LatLng(currentLocation.latitude!, currentLocation.longitude!);
locationLoading = false;
});
}
}

@override
Expand Down
26 changes: 15 additions & 11 deletions mobile/lib/_presentation/app/view/timeline/view/timeline_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter/material.dart';
import 'package:google_map_location_picker/map_location_picker.dart'
hide Location;
import 'package:location/location.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:swe/_application/profile/profile_cubit.dart';
import 'package:swe/_application/profile/profile_state.dart';
import 'package:swe/_application/session/session_cubit.dart';
Expand Down Expand Up @@ -47,17 +48,20 @@ class _TimelineViewState extends State<TimelineView> with ScrollAnimMixin {

Future<void> getCurrentLocation() async {
LocationData currentLocation;
Future.delayed(const Duration(seconds: 5), () async {
currentLocation = await _locationController.getLocation();
if (currentLocation.latitude != null &&
currentLocation.longitude != null) {
setState(() {
_currentPosition =
LatLng(currentLocation.latitude!, currentLocation.longitude!);
locationLoading = false;
});
}
});
final prefs = await SharedPreferences.getInstance();
final latitude = prefs.getDouble('latitude');
final longtitude = prefs.getDouble('longitude');
if (latitude != null && longtitude != null) {
currentLocation = LocationData.fromMap({
'latitude': latitude,
'longitude': longtitude,
});
setState(() {
_currentPosition =
LatLng(currentLocation.latitude!, currentLocation.longitude!);
locationLoading = false;
});
}
}

@override
Expand Down
32 changes: 32 additions & 0 deletions mobile/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,38 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.3"
geocoding:
dependency: "direct main"
description:
name: geocoding
sha256: e1dc0ac56666d9ed1d5a9ae5543ce9eb5986db6209cc7600103487d09192059c
url: "https://pub.dev"
source: hosted
version: "2.1.1"
geocoding_android:
dependency: transitive
description:
name: geocoding_android
sha256: "609db1d71bc364dd9d0616f72a41c01e0c74f3a3807efb85e0d5a67e57baf50f"
url: "https://pub.dev"
source: hosted
version: "2.1.2"
geocoding_ios:
dependency: transitive
description:
name: geocoding_ios
sha256: "8f79e380abb640ef4d88baee8bb65390058c802601158d0813dc990b36b189d2"
url: "https://pub.dev"
source: hosted
version: "2.1.1"
geocoding_platform_interface:
dependency: transitive
description:
name: geocoding_platform_interface
sha256: "8848605d307d844d89937cdb4b8ad7dfa880552078f310fa24d8a460f6dddab4"
url: "https://pub.dev"
source: hosted
version: "2.0.1"
geolocator:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions mobile/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ dependencies:
url: https://github.com/amineglr/google_map_location_picker.git
image_picker: ^0.8.3
geocode: ^1.0.3
geocoding: ^2.1.1


dev_dependencies:
Expand Down

0 comments on commit 966cc94

Please sign in to comment.