.github
android
api
assets
debian
ios
lib
controllers
providers
channel.dart
chat_call.dart
config.dart
experience.dart
link_preview.dart
navigation.dart
notification.dart
post.dart
relationship.dart
sn_attachment.dart
sn_network.dart
sn_sticker.dart
special_day.dart
theme.dart
user_directory.dart
userinfo.dart
websocket.dart
widget.dart
screens
types
widgets
firebase_options.dart
main.dart
router.dart
theme.dart
linux
macos
snap
web
windows
.gitignore
.metadata
.roadsignrc
README.md
analysis_options.yaml
build.yaml
devtools_options.yaml
firebase.json
pubspec.lock
pubspec.yaml
roadsign.toml
64 lines
1.6 KiB
Dart
64 lines
1.6 KiB
Dart
import 'dart:developer';
|
|
|
|
import 'package:flutter/material.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/types/account.dart';
|
|
|
|
class UserProvider extends ChangeNotifier {
|
|
bool isAuthorized = false;
|
|
SnAccount? user;
|
|
|
|
late final SnNetworkProvider _sn;
|
|
late final ConfigProvider _config;
|
|
|
|
UserProvider(BuildContext context) {
|
|
_sn = context.read<SnNetworkProvider>();
|
|
_config = context.read<ConfigProvider>();
|
|
}
|
|
|
|
Future<String?> get atk async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
return prefs.getString(kAtkStoreKey);
|
|
}
|
|
|
|
Future<void> initialize() async {
|
|
final value = _config.prefs.getString(kAtkStoreKey);
|
|
isAuthorized = value != null;
|
|
notifyListeners();
|
|
refreshUser().then((value) async {
|
|
if (value != null) {
|
|
log('Logged in as @${value.name}');
|
|
log('Atk: ${await atk}');
|
|
}
|
|
});
|
|
}
|
|
|
|
Future<SnAccount?> 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();
|
|
}
|
|
|
|
void setLanguage(String? value) {
|
|
if (value == null) return;
|
|
if (user == null) return;
|
|
user = user!.copyWith(language: value);
|
|
notifyListeners();
|
|
}
|
|
}
|