This commit is contained in:
2024-04-30 17:08:23 +09:00
parent 3be78ab97c
commit bbf3be7a62
2 changed files with 74 additions and 0 deletions

View File

@ -11,6 +11,7 @@ import 'package:rogapp/pages/settings/settings_controller.dart';
import 'package:rogapp/pages/destination/destination_controller.dart'; import 'package:rogapp/pages/destination/destination_controller.dart';
import 'package:rogapp/pages/index/index_binding.dart'; import 'package:rogapp/pages/index/index_binding.dart';
import 'package:rogapp/pages/index/index_controller.dart';
import 'package:rogapp/routes/app_pages.dart'; import 'package:rogapp/routes/app_pages.dart';
import 'package:rogapp/utils/location_controller.dart'; import 'package:rogapp/utils/location_controller.dart';
import 'package:rogapp/utils/string_values.dart'; import 'package:rogapp/utils/string_values.dart';
@ -25,6 +26,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
Map<String, dynamic> deviceInfo = {}; Map<String, dynamic> deviceInfo = {};
/*
void saveGameState() async { void saveGameState() async {
DestinationController destinationController = DestinationController destinationController =
Get.find<DestinationController>(); Get.find<DestinationController>();
@ -34,7 +36,22 @@ void saveGameState() async {
"rogaining_counted", destinationController.rogainingCounted.value); "rogaining_counted", destinationController.rogainingCounted.value);
pref.setBool("ready_for_goal", DestinationController.ready_for_goal); pref.setBool("ready_for_goal", DestinationController.ready_for_goal);
} }
*/
// 現在のユーザーのIDも一緒に保存するようにします。
void saveGameState() async {
DestinationController destinationController =
Get.find<DestinationController>();
IndexController indexController = Get.find<IndexController>();
SharedPreferences pref = await SharedPreferences.getInstance();
pref.setInt("user_id", indexController.currentUser[0]["user"]["id"]);
pref.setBool("is_in_rog", destinationController.isInRog.value);
pref.setBool(
"rogaining_counted", destinationController.rogainingCounted.value);
pref.setBool("ready_for_goal", DestinationController.ready_for_goal);
}
/*
void restoreGame() async { void restoreGame() async {
SharedPreferences pref = await SharedPreferences.getInstance(); SharedPreferences pref = await SharedPreferences.getInstance();
DestinationController destinationController = DestinationController destinationController =
@ -48,6 +65,24 @@ void restoreGame() async {
//print( //print(
// "--restored -- destinationController.isInRog.value ${pref.getBool("is_in_rog")} -- ${pref.getBool("rogaining_counted")}"); // "--restored -- destinationController.isInRog.value ${pref.getBool("is_in_rog")} -- ${pref.getBool("rogaining_counted")}");
} }
*/
void restoreGame() async {
SharedPreferences pref = await SharedPreferences.getInstance();
IndexController indexController = Get.find<IndexController>();
int? savedUserId = pref.getInt("user_id");
if (indexController.currentUser.isNotEmpty &&
indexController.currentUser[0]["user"]["id"] == savedUserId) {
DestinationController destinationController =
Get.find<DestinationController>();
destinationController.skipGps = false;
destinationController.isInRog.value = pref.getBool("is_in_rog") ?? false;
destinationController.rogainingCounted.value =
pref.getBool("rogaining_counted") ?? false;
DestinationController.ready_for_goal =
pref.getBool("ready_for_goal") ?? false;
}
}
void main() async { void main() async {
WidgetsFlutterBinding.ensureInitialized(); WidgetsFlutterBinding.ensureInitialized();

View File

@ -16,6 +16,8 @@ import 'package:rogapp/services/location_service.dart';
import 'package:rogapp/utils/database_helper.dart'; import 'package:rogapp/utils/database_helper.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import '../../main.dart';
class IndexController extends GetxController { class IndexController extends GetxController {
List<GeoJSONFeatureCollection> locations = <GeoJSONFeatureCollection>[].obs; List<GeoJSONFeatureCollection> locations = <GeoJSONFeatureCollection>[].obs;
@ -250,6 +252,7 @@ class IndexController extends GetxController {
isLoading.value = false; isLoading.value = false;
} }
/*
void logout() async { void logout() async {
locations.clear(); locations.clear();
DatabaseHelper db = DatabaseHelper.instance; DatabaseHelper db = DatabaseHelper.instance;
@ -265,6 +268,23 @@ class IndexController extends GetxController {
final SharedPreferences prefs = await SharedPreferences.getInstance(); final SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.remove("user_token"); await prefs.remove("user_token");
} }
*/
void logout() async {
saveGameState();
locations.clear();
DatabaseHelper db = DatabaseHelper.instance;
db.deleteAllDestinations().then((value) {
DestinationController destinationController =
Get.find<DestinationController>();
destinationController.populateDestinations();
});
currentUser.clear();
cats.clear();
final SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.remove("user_token");
}
// 要検討:エラーハンドリングが行われていますが、エラーメッセージをローカライズすることを検討してください。 // 要検討:エラーハンドリングが行われていますが、エラーメッセージをローカライズすることを検討してください。
// //
@ -299,6 +319,7 @@ class IndexController extends GetxController {
print("saveToDevice: ${val}"); print("saveToDevice: ${val}");
} }
/*
void changeUser(Map<String, dynamic> value, {bool replace = true}) { void changeUser(Map<String, dynamic> value, {bool replace = true}) {
print("---- change user to $value -----"); print("---- change user to $value -----");
currentUser.clear(); currentUser.clear();
@ -316,6 +337,24 @@ class IndexController extends GetxController {
print('--- c rog mode --- ${rogMode.value}'); print('--- c rog mode --- ${rogMode.value}');
Get.toNamed(AppPages.INDEX); Get.toNamed(AppPages.INDEX);
} }
*/
void changeUser(Map<String, dynamic> value, {bool replace = true}) {
currentUser.clear();
currentUser.add(value);
if (replace) {
saveToDevice(currentUser[0]["token"]);
}
isLoading.value = false;
loadLocationsBound();
if (currentUser.isNotEmpty) {
rogMode.value = 0;
restoreGame();
} else {
rogMode.value = 1;
}
Get.toNamed(AppPages.INDEX);
}
loadUserDetailsForToken(String token) async { loadUserDetailsForToken(String token) async {
AuthService.userForToken(token).then((value) { AuthService.userForToken(token).then((value) {