diff --git a/lib/services/db_helper.dart b/lib/services/db_helper.dart index 7027bc0..1d95b31 100644 --- a/lib/services/db_helper.dart +++ b/lib/services/db_helper.dart @@ -3,6 +3,7 @@ import 'package:shared_preferences/shared_preferences.dart'; import 'package:supabase_flutter/supabase_flutter.dart'; import '../models/feed_item.dart'; +import '../models/profile.dart'; class DbHelper { static Future get _prefs async => @@ -65,6 +66,12 @@ class DbHelper { return false; } + static void logout() async { + final prefs = await _prefs; + await prefs.remove('username'); + _supabase.auth.signOut(); + } + static Future> fetchFeed() async { List items = []; final res = await _supabase.from('feed').select(); @@ -74,12 +81,18 @@ class DbHelper { return items; } - static void logout() async { - final prefs = await _prefs; - await prefs.remove('username'); - _supabase.auth.signOut(); + static Future> fetchProfiles() async { + List items = []; + final res = await _supabase.from('profiles').select(); + for (final map in res) { + items.add(Profile.fromMap(map)); + } + return items; } static Stream>> get feedStream => _supabase.from('feed').stream(primaryKey: ['timestamp']); + + static Stream>> get profilesStream => + _supabase.from('profiles').stream(primaryKey: ['id']); } diff --git a/lib/services/profiles_provider.dart b/lib/services/profiles_provider.dart new file mode 100644 index 0000000..67f0fc1 --- /dev/null +++ b/lib/services/profiles_provider.dart @@ -0,0 +1,31 @@ +import 'package:flutter/material.dart'; + +import '../models/profile.dart'; +import 'db_helper.dart'; + +class Profiles extends ChangeNotifier { + final List _profiles = []; + Stream>>? _profilesStream; + + List get profiles { + if (_profilesStream == null) { + DbHelper.fetchProfiles().then((value) { + _profiles.clear(); + _profiles.addAll(value); + }); + _profilesStream = DbHelper.profilesStream; + _profilesStream!.listen( + (event) => _updateProfile(event), + ); + } + return _profiles; + } + + _updateProfile(List> data) { + final List profiles = data.map((e) => Profile.fromMap(e)).toList(); + + _profiles.clear(); + _profiles.addAll(profiles); + notifyListeners(); + } +}