Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
import 'package:flutter/material.dart';

import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:we_find/model/model.dart';
import 'package:we_find/providers/fav_course_provider.dart';
import 'package:we_find/providers/lang_provider.dart';
import 'package:we_find/screens/home_screen.dart';
import 'package:we_find/styles/theme.dart' as Theme;

void main() => runApp(appProvider(WeFindApp()));
void main() async =>
runApp(appProvider(WeFindApp(), await _getSavedFavorites()));

Future<Set<CourseID>> _getSavedFavorites() async {
// obtain shared preferences
final prefs = await SharedPreferences.getInstance();

String? favscsv = prefs.getString("favorites");

if (favscsv == null) return {};

return favscsv.split(',').map((e) => CourseID(e)).toSet();
}

/// Wraps the supplied [appRoot] into a [MultiProvider] that gives access to
/// all the shared states for the Widget-tree.
MultiProvider appProvider(Widget appRoot) => MultiProvider(
MultiProvider appProvider(Widget appRoot, Set<CourseID> favs) => MultiProvider(
providers: [
ChangeNotifierProvider<FavCourseProvider>(
create: (_) => FavCourseProvider(),
create: (_) => FavCourseProvider(favs),
),
ChangeNotifierProvider<LangProvider>(
create: (_) => LangProvider(),
Expand All @@ -23,13 +37,34 @@ MultiProvider appProvider(Widget appRoot) => MultiProvider(
);

class WeFindApp extends StatelessWidget {
FavCourseProvider? _favCourses;

@override
Widget build(BuildContext context) {
_favCourses = Provider.of<FavCourseProvider>(context);
return MaterialApp(
title: 'we:find',
theme: Theme.lightThemeData,
home: HomeScreen(),
debugShowCheckedModeBanner: false,
);
}

@override
void detached() async {
if (_favCourses == null) return;

String ret = "";
bool first = true;
for (CourseID eachCourseID in _favCourses!.courses) {
if (!first) {
ret += ",";
}
ret += eachCourseID.id!;
first = false;
}

final prefs = await SharedPreferences.getInstance();
print(await prefs.setString("favorites", ret));
}
}
6 changes: 4 additions & 2 deletions lib/providers/fav_course_provider.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:we_find/model/model.dart';
import 'package:we_find/model/model_wrapped.dart';

/// Provider that keeps track of the [Course] instances
/// that were marked as favourite by the user
class FavCourseProvider with ChangeNotifier {
final Set<CourseID> _courses = {};
final Set<CourseID> _courses;

FavCourseProvider(this._courses);

Set<CourseID> get courses => _courses;

Expand Down
105 changes: 105 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
ffi:
dependency: transitive
description:
name: ffi
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.0"
file:
dependency: transitive
description:
Expand Down Expand Up @@ -296,6 +303,27 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.1"
path_provider_linux:
dependency: transitive
description:
name: path_provider_linux
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
path_provider_platform_interface:
dependency: transitive
description:
name: path_provider_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.1"
path_provider_windows:
dependency: transitive
description:
name: path_provider_windows
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.1"
pedantic:
dependency: transitive
description:
Expand All @@ -310,13 +338,34 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "4.1.0"
platform:
dependency: transitive
description:
name: platform
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.0"
plugin_platform_interface:
dependency: transitive
description:
name: plugin_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
pool:
dependency: transitive
description:
name: pool
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.0"
process:
dependency: transitive
description:
name: process
url: "https://pub.dartlang.org"
source: hosted
version: "4.2.1"
provider:
dependency: "direct main"
description:
Expand All @@ -338,6 +387,48 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.1"
shared_preferences:
dependency: "direct main"
description:
name: shared_preferences
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.5"
shared_preferences_linux:
dependency: transitive
description:
name: shared_preferences_linux
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
shared_preferences_macos:
dependency: transitive
description:
name: shared_preferences_macos
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
shared_preferences_platform_interface:
dependency: transitive
description:
name: shared_preferences_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
shared_preferences_web:
dependency: transitive
description:
name: shared_preferences_web
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
shared_preferences_windows:
dependency: transitive
description:
name: shared_preferences_windows
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
shelf:
dependency: transitive
description:
Expand Down Expand Up @@ -546,6 +637,20 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.4"
win32:
dependency: transitive
description:
name: win32
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.5"
xdg_directories:
dependency: transitive
description:
name: xdg_directories
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.0"
xml:
dependency: "direct main"
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ dependencies:
provider: ^5.0.0
xml: ^5.1.0
flutter_html: ^2.0.0
shared_preferences: ^2.0.5


dev_dependencies:
Expand Down