Browse Source

feat : 本地同步到服务器

dev
徐振升 17 hours ago
parent
commit
2d2f9d2d5c
  1. 3
      lib/data/repositories/file_repository.dart
  2. 5
      lib/data/repositories/problem_repository.dart
  3. 23
      lib/modules/problem/controllers/problem_controller.dart
  4. 9
      lib/modules/problem/controllers/problem_form_controller.dart
  5. 4
      lib/modules/problem/views/problem_page.dart
  6. 4
      lib/modules/problem/views/problem_upload_page.dart
  7. 5
      lib/modules/problem/views/widgets/problem_card.dart

3
lib/data/repositories/file_repository.dart

@ -9,8 +9,7 @@ import 'package:problem_check_system/data/providers/http_provider.dart';
class FileRepository { class FileRepository {
final HttpProvider _httpProvider = Get.find<HttpProvider>(); final HttpProvider _httpProvider = Get.find<HttpProvider>();
/// TODO /// @param imageFilePath
/// @param imageFile
/// @param cancelToken /// @param cancelToken
/// @param onSendProgress /// @param onSendProgress
/// @return URL /// @return URL

5
lib/data/repositories/problem_repository.dart

@ -74,12 +74,13 @@ class ProblemRepository extends GetxService {
/// put /// put
Future<Response> put( Future<Response> put(
String id,
Map<String, Object?> apiPayload, Map<String, Object?> apiPayload,
CancelToken cancelToken, CancelToken cancelToken,
) async { ) async {
// 3. // 3.
final response = await httpProvider.post( final response = await httpProvider.put(
ApiEndpoints.postProblem, ApiEndpoints.putProblemById(id),
data: apiPayload, data: apiPayload,
cancelToken: cancelToken, cancelToken: cancelToken,
); );

23
lib/modules/problem/controllers/problem_controller.dart

@ -175,6 +175,8 @@ class ProblemController extends GetxController
clearSelection(); clearSelection();
// //
loadUnUploadedProblems(); loadUnUploadedProblems();
// problems
loadProblems();
} on DioException catch (e) { } on DioException catch (e) {
Get.back(); Get.back();
if (CancelToken.isCancel(e)) { if (CancelToken.isCancel(e)) {
@ -269,8 +271,11 @@ class ProblemController extends GetxController
}, },
); );
problemRepository.updateProblem(updatedProblem); if (updatedProblem.syncStatus == ProblemSyncStatus.untracked) {
// updatedProblems.add(updatedProblem); problemRepository.deleteProblem(updatedProblem.id!);
} else {
problemRepository.updateProblem(updatedProblem);
}
} }
// return updatedProblems; // return updatedProblems;
} on DioException { } on DioException {
@ -348,7 +353,11 @@ class ProblemController extends GetxController
response = await problemRepository.post(apiPayload!, cancelToken); response = await problemRepository.post(apiPayload!, cancelToken);
break; break;
case ProblemSyncStatus.pendingUpdate: case ProblemSyncStatus.pendingUpdate:
response = await problemRepository.put(apiPayload!, cancelToken); response = await problemRepository.put(
problem.id!,
apiPayload!,
cancelToken,
);
break; break;
case ProblemSyncStatus.pendingDelete: case ProblemSyncStatus.pendingDelete:
response = await problemRepository.delete(problem.id!, cancelToken); response = await problemRepository.delete(problem.id!, cancelToken);
@ -365,7 +374,9 @@ class ProblemController extends GetxController
// none // none
return problem.copyWith( return problem.copyWith(
syncStatus: ProblemSyncStatus.synced, // none syncStatus: problem.syncStatus != ProblemSyncStatus.pendingDelete
? ProblemSyncStatus.synced
: ProblemSyncStatus.untracked, // none
imageUrls: updatedImageMetadata, imageUrls: updatedImageMetadata,
); );
} else { } else {
@ -642,8 +653,8 @@ class ProblemController extends GetxController
} }
} }
Future<void> toProblemFormPageAndRefresh() async { Future<void> toProblemFormPageAndRefresh({Problem? problem}) async {
await Get.toNamed(AppRoutes.problemForm); await Get.toNamed(AppRoutes.problemForm, arguments: problem);
loadProblems(); loadProblems();
} }
} }

9
lib/modules/problem/controllers/problem_form_controller.dart

@ -142,6 +142,7 @@ class ProblemFormController extends GetxController {
final List<ImageMetadata> imagePaths = await _saveImagesToLocal(); final List<ImageMetadata> imagePaths = await _saveImagesToLocal();
if (problem != null) { if (problem != null) {
//
final updatedProblem = problem!.copyWith( final updatedProblem = problem!.copyWith(
description: descriptionController.text, description: descriptionController.text,
location: locationController.text, location: locationController.text,
@ -151,10 +152,8 @@ class ProblemFormController extends GetxController {
final modifyProblem = ProblemStateManager.modifyProblem(updatedProblem); final modifyProblem = ProblemStateManager.modifyProblem(updatedProblem);
await problemRepository.updateProblem(modifyProblem); await problemRepository.updateProblem(modifyProblem);
Get.back(result: true); //
Get.snackbar('成功', '问题已更新');
} else { } else {
// //
final newProblem = ProblemStateManager.createNewProblem( final newProblem = ProblemStateManager.createNewProblem(
description: descriptionController.text, description: descriptionController.text,
location: locationController.text, location: locationController.text,
@ -162,9 +161,9 @@ class ProblemFormController extends GetxController {
); );
await problemRepository.insertProblem(newProblem); await problemRepository.insertProblem(newProblem);
Get.back(result: true); //
Get.snackbar('成功', '问题已保存');
} }
Get.back(result: true); //
Get.snackbar('成功', '问题已更新');
} catch (e) { } catch (e) {
Get.snackbar('错误', '保存问题失败: $e'); Get.snackbar('错误', '保存问题失败: $e');
} finally { } finally {

4
lib/modules/problem/views/problem_page.dart

@ -112,7 +112,9 @@ class ProblemPage extends GetView<ProblemController> {
padding: EdgeInsets.only(bottom: 24.h), // padding: EdgeInsets.only(bottom: 24.h), //
child: FloatingActionButton( child: FloatingActionButton(
heroTag: "btn_add", heroTag: "btn_add",
onPressed: controller.toProblemFormPageAndRefresh, onPressed: () {
controller.toProblemFormPageAndRefresh();
},
shape: const CircleBorder(), shape: const CircleBorder(),
backgroundColor: Colors.blue[300], backgroundColor: Colors.blue[300],
foregroundColor: Colors.white, foregroundColor: Colors.white,

4
lib/modules/problem/views/problem_upload_page.dart

@ -27,8 +27,8 @@ class ProblemUploadPage extends GetView<ProblemController> {
// leading: IconButton( // leading: IconButton(
// icon: Icon(Icons.close), // icon: Icon(Icons.close),
// onPressed: () { // onPressed: () {
// Get.back();// TODO // Get.back();
// controller.clearSelection(); // controller.loadProblems();
// }, // },
// ), // ),
actions: [ actions: [

5
lib/modules/problem/views/widgets/problem_card.dart

@ -5,6 +5,7 @@ import 'package:intl/intl.dart';
import 'package:problem_check_system/app/routes/app_routes.dart'; import 'package:problem_check_system/app/routes/app_routes.dart';
import 'package:problem_check_system/data/models/problem_sync_status.dart'; import 'package:problem_check_system/data/models/problem_sync_status.dart';
import 'package:problem_check_system/data/models/problem_model.dart'; import 'package:problem_check_system/data/models/problem_model.dart';
import 'package:problem_check_system/modules/problem/controllers/problem_controller.dart';
import 'package:problem_check_system/modules/problem/views/widgets/custom_button.dart'; import 'package:problem_check_system/modules/problem/views/widgets/custom_button.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart'; import 'package:tdesign_flutter/tdesign_flutter.dart';
import 'dart:io'; // import 'dart:io'; //
@ -12,7 +13,7 @@ import 'dart:io'; // 添加文件操作支持
// //
enum ProblemCardViewType { buttons, checkbox } enum ProblemCardViewType { buttons, checkbox }
class ProblemCard extends StatelessWidget { class ProblemCard extends GetView<ProblemController> {
final Problem problem; final Problem problem;
final ProblemCardViewType viewType; final ProblemCardViewType viewType;
final Function(Problem, bool)? onChanged; final Function(Problem, bool)? onChanged;
@ -212,7 +213,7 @@ class ProblemCard extends StatelessWidget {
CustomButton( CustomButton(
text: '修改', text: '修改',
onTap: () { onTap: () {
Get.toNamed(AppRoutes.problemForm, arguments: problem); controller.toProblemFormPageAndRefresh(problem: problem);
}, },
), ),
if (!isDeleted) SizedBox(width: 8.w), if (!isDeleted) SizedBox(width: 8.w),

Loading…
Cancel
Save