105 lines
2.7 KiB
Dart
105 lines
2.7 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:food_hub_app/apis/moment.dart';
|
|
import 'package:food_hub_app/apis/user.dart';
|
|
import 'package:food_hub_app/models/moment.dart';
|
|
import 'package:food_hub_app/models/session.dart';
|
|
|
|
class UserProvider with ChangeNotifier {
|
|
late User currentUser = User.getEmpty();
|
|
bool isLoading = false;
|
|
String? error = '';
|
|
List<Moment> momentList = [];
|
|
late User userFormItem;
|
|
|
|
Future<void> refreshUser(int id) async {
|
|
if (isLoading) return;
|
|
|
|
try {
|
|
isLoading = true;
|
|
error = '';
|
|
notifyListeners();
|
|
|
|
final result = await queryUserApi(id);
|
|
currentUser = result;
|
|
} catch (e) {
|
|
error = '加载数据失败: $e';
|
|
debugPrint('加载数据失败: $e');
|
|
} finally {
|
|
isLoading = false;
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
Future<void> queryMomentByUserId(int userId) async {
|
|
if (isLoading) return;
|
|
|
|
try {
|
|
isLoading = true;
|
|
error = '';
|
|
notifyListeners();
|
|
|
|
final result = await queryMomentListByUserIdApi(userId);
|
|
momentList = result;
|
|
notifyListeners();
|
|
} catch (e) {
|
|
error = '加载数据失败: $e';
|
|
debugPrint('加载数据失败: $e');
|
|
} finally {
|
|
isLoading = false;
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
void initUserForm(User user) {
|
|
userFormItem = user;
|
|
notifyListeners();
|
|
}
|
|
|
|
void updateUserFormItem({
|
|
String? username,
|
|
int? gender,
|
|
String? phoneNumber,
|
|
String? email,
|
|
DateTime? birthDate,
|
|
String? avatar,
|
|
List<String>? area,
|
|
String? address,
|
|
String? job,
|
|
List<String>? tags,
|
|
String? description
|
|
}) {
|
|
if (username != null) userFormItem.username = username;
|
|
if (gender != null) userFormItem.gender = gender;
|
|
if (phoneNumber != null) userFormItem.phoneNumber = phoneNumber;
|
|
if (email != null) userFormItem.email = email;
|
|
if (birthDate != null) userFormItem.birthDate = birthDate;
|
|
if (avatar != null) userFormItem.avatar = avatar;
|
|
if (area != null) userFormItem.area = area;
|
|
if (address != null) userFormItem.address = address;
|
|
if (job != null) userFormItem.job = job;
|
|
if (tags != null) userFormItem.tags = tags;
|
|
if (description != null) userFormItem.description = description;
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<bool> handleUser() async {
|
|
if (isLoading) return true;
|
|
|
|
try {
|
|
isLoading = true;
|
|
error = null;
|
|
notifyListeners();
|
|
|
|
await updateUserApi(userFormItem.id!, userFormItem);
|
|
return true;
|
|
} catch (e) {
|
|
error = '处理数据失败: $e';
|
|
debugPrint('处理数据失败: $e');
|
|
return false;
|
|
} finally {
|
|
isLoading = false;
|
|
notifyListeners();
|
|
}
|
|
}
|
|
}
|