frontend compliant with backend
This commit is contained in:
@@ -3,80 +3,102 @@ import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
|
||||
class SinglePreference {
|
||||
String slug;
|
||||
String name;
|
||||
String description;
|
||||
int value;
|
||||
int minVal;
|
||||
int maxVal;
|
||||
Icon icon;
|
||||
String key;
|
||||
|
||||
SinglePreference({
|
||||
required this.slug,
|
||||
required this.name,
|
||||
required this.description,
|
||||
required this.value,
|
||||
required this.icon,
|
||||
required this.key,
|
||||
this.minVal = 0,
|
||||
this.maxVal = 5,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
class UserPreferences {
|
||||
List<SinglePreference> preferences = [
|
||||
SinglePreference(
|
||||
name: "Sightseeing",
|
||||
description: "How much do you like sightseeing?",
|
||||
value: 0,
|
||||
icon: Icon(Icons.church),
|
||||
key: "sightseeing",
|
||||
),
|
||||
SinglePreference(
|
||||
name: "Shopping",
|
||||
description: "How much do you like shopping?",
|
||||
value: 0,
|
||||
icon: Icon(Icons.shopping_bag),
|
||||
key: "shopping",
|
||||
),
|
||||
SinglePreference(
|
||||
name: "Foods & Drinks",
|
||||
description: "How much do you like eating?",
|
||||
value: 0,
|
||||
icon: Icon(Icons.restaurant),
|
||||
key: "eating",
|
||||
),
|
||||
SinglePreference(
|
||||
name: "Nightlife",
|
||||
description: "How much do you like nightlife?",
|
||||
value: 0,
|
||||
icon: Icon(Icons.wine_bar),
|
||||
key: "nightlife",
|
||||
),
|
||||
SinglePreference(
|
||||
name: "Nature",
|
||||
description: "How much do you like nature?",
|
||||
value: 0,
|
||||
icon: Icon(Icons.landscape),
|
||||
key: "nature",
|
||||
),
|
||||
SinglePreference(
|
||||
name: "Culture",
|
||||
description: "How much do you like culture?",
|
||||
value: 0,
|
||||
icon: Icon(Icons.palette),
|
||||
key: "culture",
|
||||
),
|
||||
];
|
||||
|
||||
|
||||
void save() async {
|
||||
SharedPreferences sharedPrefs = await SharedPreferences.getInstance();
|
||||
for (SinglePreference pref in preferences) {
|
||||
sharedPrefs.setInt(pref.key, pref.value);
|
||||
}
|
||||
sharedPrefs.setInt('pref_$slug', value);
|
||||
}
|
||||
|
||||
void load() async {
|
||||
SharedPreferences sharedPrefs = await SharedPreferences.getInstance();
|
||||
for (SinglePreference pref in preferences) {
|
||||
pref.value = sharedPrefs.getInt(pref.key) ?? 0;
|
||||
value = sharedPrefs.getInt('pref_$slug') ?? minVal;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class UserPreferences {
|
||||
SinglePreference sightseeing = SinglePreference(
|
||||
name: "Sightseeing",
|
||||
slug: "sightseeing",
|
||||
description: "How much do you like sightseeing?",
|
||||
value: 0,
|
||||
icon: Icon(Icons.church),
|
||||
);
|
||||
SinglePreference shopping = SinglePreference(
|
||||
name: "Shopping",
|
||||
slug: "shopping",
|
||||
description: "How much do you like shopping?",
|
||||
value: 0,
|
||||
icon: Icon(Icons.shopping_bag),
|
||||
);
|
||||
SinglePreference nature = SinglePreference(
|
||||
name: "Nature",
|
||||
slug: "nature",
|
||||
description: "How much do you like nature?",
|
||||
value: 0,
|
||||
icon: Icon(Icons.landscape),
|
||||
);
|
||||
|
||||
SinglePreference maxTime = SinglePreference(
|
||||
name: "Trip duration",
|
||||
slug: "duration",
|
||||
description: "How long do you want your trip to be?",
|
||||
value: 30,
|
||||
minVal: 30,
|
||||
maxVal: 720,
|
||||
icon: Icon(Icons.timer),
|
||||
);
|
||||
SinglePreference maxDetour = SinglePreference(
|
||||
name: "Trip detours",
|
||||
slug: "detours",
|
||||
description: "Are you okay with roaming even if makes the trip longer?",
|
||||
value: 0,
|
||||
maxVal: 30,
|
||||
icon: Icon(Icons.loupe_sharp),
|
||||
);
|
||||
|
||||
|
||||
|
||||
Future<void> load() async {
|
||||
for (SinglePreference pref in [sightseeing, shopping, nature, maxTime, maxDetour]) {
|
||||
pref.load();
|
||||
}
|
||||
}
|
||||
|
||||
String toJson() {
|
||||
// This is "opinionated" JSON, corresponding to the backend's expectations
|
||||
return '''
|
||||
{
|
||||
"sightseeing": {"type": "sightseeing", "score": ${sightseeing.value}},
|
||||
"shopping": {"type": "shopping", "score": ${shopping.value}},
|
||||
"nature": {"type": "nature", "score": ${nature.value}},
|
||||
"max_time_minutes": ${maxTime.value},
|
||||
"detour_tolerance_minute": ${maxDetour.value}
|
||||
}
|
||||
''';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Future<UserPreferences> loadUserPreferences() async {
|
||||
UserPreferences prefs = UserPreferences();
|
||||
await prefs.load();
|
||||
return prefs;
|
||||
}
|
Reference in New Issue
Block a user