import 'dart:developer'; import 'package:flutter/material.dart'; import 'package:path/path.dart'; import 'package:provider/provider.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:surface/providers/config.dart'; import 'package:surface/providers/sn_network.dart'; import 'package:surface/providers/widget.dart'; import 'package:surface/types/account.dart'; class UserProvider extends ChangeNotifier { bool isAuthorized = false; SnAccount? user; late final SnNetworkProvider _sn; late final HomeWidgetProvider _home; late final ConfigProvider _config; UserProvider(BuildContext context) { _sn = context.read(); _home = context.read(); _config = context.read(); } Future get atk async { final prefs = await SharedPreferences.getInstance(); return prefs.getString(kAtkStoreKey); } Future initialize() async { final value = _config.prefs.getString(kAtkStoreKey); isAuthorized = value != null; notifyListeners(); refreshUser().then((value) { if (value != null) { log('Logged in as @${value.name}'); _home.saveWidgetData('user', value.toJson()); } }); } Future refreshUser() async { final resp = await _sn.client.get('/cgi/id/users/me'); final out = SnAccount.fromJson(resp.data); isAuthorized = true; user = out; notifyListeners(); return out; } void logoutUser() async { _sn.clearTokenPair(); isAuthorized = false; user = null; notifyListeners(); } }