re factor rog
This commit is contained in:
14
lib/features/services/auth_repo.dart
Normal file
14
lib/features/services/auth_repo.dart
Normal file
@ -0,0 +1,14 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:rogapp/features/data/user.dart';
|
||||
import 'package:rogapp/features/services/auth_service.dart';
|
||||
|
||||
final authProvider =
|
||||
FutureProvider.family<User?, String>((ref, credentials) async {
|
||||
// Assuming credentials is a combined string of "email|password"
|
||||
List<String> parts = credentials.split('|');
|
||||
String email = parts[0];
|
||||
String password = parts[1];
|
||||
|
||||
AuthService authService = AuthService();
|
||||
return await authService.userLogin(email, password);
|
||||
});
|
||||
44
lib/features/services/auth_service.dart
Normal file
44
lib/features/services/auth_service.dart
Normal file
@ -0,0 +1,44 @@
|
||||
import 'package:rogapp/features/data/user.dart';
|
||||
|
||||
import '../utils/const.dart';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
class AuthService {
|
||||
Dio dio = Dio();
|
||||
|
||||
Future<User?> userLogin(String email, String password) async {
|
||||
final String serverUrl = ConstValues.currentServer();
|
||||
final String url = '$serverUrl/api/login/';
|
||||
|
||||
try {
|
||||
final response = await dio.post(
|
||||
url,
|
||||
data: {
|
||||
'email': email,
|
||||
'password': password,
|
||||
},
|
||||
options: Options(
|
||||
headers: {
|
||||
'Content-Type': 'application/json; charset=UTF-8',
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
if (response.statusCode == 200 && response.data != null) {
|
||||
final data = response.data;
|
||||
User user = User.fromMap(data["user"]);
|
||||
final String token = data["token"];
|
||||
user.auth_token = token;
|
||||
return user;
|
||||
}
|
||||
return null;
|
||||
} on DioException catch (e) {
|
||||
print("Dio error: ${e.response?.statusCode} - ${e.message}");
|
||||
return null;
|
||||
} catch (e) {
|
||||
print("Unexpected error: $e");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
32
lib/features/services/checkpoint_repo.dart
Normal file
32
lib/features/services/checkpoint_repo.dart
Normal file
@ -0,0 +1,32 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:rogapp/features/data/checkpoint.dart';
|
||||
import 'package:rogapp/features/data/data_provider.dart';
|
||||
import 'package:rogapp/features/services/checkpoint_service.dart'; // Import your ApiService file
|
||||
|
||||
Future<void> storeFetchedFeatures(FeatureCollection featureCollection) async {
|
||||
final dataProvider =
|
||||
DataProvider(); // Ensure DataProvider is initialized appropriately
|
||||
|
||||
for (Feature feature in featureCollection.features) {
|
||||
await dataProvider.insertMarker(feature);
|
||||
}
|
||||
}
|
||||
|
||||
final checkpointProvider = FutureProvider<List<Feature>>((ref) async {
|
||||
final dataProvider =
|
||||
DataProvider(); // Assuming you create or access an instance here
|
||||
final markers = await dataProvider.getMarkers();
|
||||
return markers;
|
||||
});
|
||||
|
||||
final featureCheckpointCollectionProvider =
|
||||
FutureProvider<FeatureCollection>((ref) async {
|
||||
final apiService =
|
||||
ApiService(); // Ensure ApiService is initialized appropriately
|
||||
final featureCollection = await apiService.fetchFeatures();
|
||||
|
||||
// Store the fetched features in the local database
|
||||
await storeFetchedFeatures(featureCollection);
|
||||
|
||||
return featureCollection; // Return the fetched feature collection
|
||||
});
|
||||
27
lib/features/services/checkpoint_service.dart
Normal file
27
lib/features/services/checkpoint_service.dart
Normal file
@ -0,0 +1,27 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:rogapp/features/data/checkpoint.dart';
|
||||
|
||||
class ApiService {
|
||||
final Dio _dio = Dio();
|
||||
|
||||
Future<FeatureCollection> fetchFeatures() async {
|
||||
try {
|
||||
final tmp_url =
|
||||
"https://rogaining.sumasen.net/api/inbound?rog=True&grp=養老ロゲ&ln1=136.52675654298724&la1=35.19710769333327&ln2=136.52675654298724&la2=35.38264844179004&ln3=136.65061968584442&la3=35.38264844179004&ln4=136.65061968584442&la4=35.19710769333327";
|
||||
final response = await _dio.get(tmp_url);
|
||||
if (response.statusCode == 200) {
|
||||
return FeatureCollection.fromJson(response.data);
|
||||
} else {
|
||||
throw Exception('Failed to load features');
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
// Handle Dio errors here
|
||||
print('Dio error: $e');
|
||||
throw Exception('Failed to load features');
|
||||
} catch (e) {
|
||||
// Handle any other errors
|
||||
print('General error: $e');
|
||||
throw Exception('Failed to load features');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user