mirror of
				https://github.com/AppFlowy-IO/AppFlowy.git
				synced 2025-11-04 12:03:28 +00:00 
			
		
		
		
	# Conflicts: # frontend/appflowy_flutter/lib/plugins/database_view/application/database_view_service.dart # frontend/appflowy_flutter/lib/plugins/document/presentation/plugins/base/link_to_page_widget.dart # frontend/appflowy_flutter/lib/plugins/trash/application/trash_bloc.dart # frontend/appflowy_flutter/lib/plugins/trash/application/trash_service.dart # frontend/appflowy_flutter/lib/workspace/application/app/app_bloc.dart # frontend/appflowy_flutter/lib/workspace/application/app/app_service.dart # frontend/appflowy_flutter/lib/workspace/application/menu/menu_bloc.dart # frontend/appflowy_flutter/lib/workspace/application/workspace/workspace_service.dart # frontend/appflowy_flutter/lib/workspace/presentation/home/menu/app/header/header.dart # frontend/appflowy_flutter/test/bloc_test/grid_test/filter/filter_util.dart # frontend/appflowy_flutter/test/bloc_test/grid_test/grid_bloc_test.dart # frontend/appflowy_flutter/test/bloc_test/grid_test/util.dart # frontend/appflowy_flutter/test/bloc_test/home_test/view_bloc_test.dart # frontend/rust-lib/flowy-database/src/services/database/database_editor.rs # frontend/rust-lib/flowy-database/src/services/persistence/migration/database_view_migration.rs
		
			
				
	
	
		
			109 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
			
		
		
	
	
			109 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
import 'package:dartz/dartz.dart';
 | 
						|
import 'package:appflowy_backend/log.dart';
 | 
						|
import 'package:appflowy_backend/protobuf/flowy-folder2/trash.pb.dart';
 | 
						|
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
 | 
						|
import 'package:flutter_bloc/flutter_bloc.dart';
 | 
						|
import 'package:freezed_annotation/freezed_annotation.dart';
 | 
						|
import 'package:appflowy/plugins/trash/application/trash_service.dart';
 | 
						|
import 'package:appflowy/plugins/trash/application/trash_listener.dart';
 | 
						|
 | 
						|
part 'trash_bloc.freezed.dart';
 | 
						|
 | 
						|
class TrashBloc extends Bloc<TrashEvent, TrashState> {
 | 
						|
  final TrashService _service;
 | 
						|
  final TrashListener _listener;
 | 
						|
  TrashBloc()
 | 
						|
      : _service = TrashService(),
 | 
						|
        _listener = TrashListener(),
 | 
						|
        super(TrashState.init()) {
 | 
						|
    on<TrashEvent>((event, emit) async {
 | 
						|
      await event.map(
 | 
						|
        initial: (e) async {
 | 
						|
          _listener.start(trashUpdated: _listenTrashUpdated);
 | 
						|
          final result = await _service.readTrash();
 | 
						|
          emit(
 | 
						|
            result.fold(
 | 
						|
              (object) => state.copyWith(
 | 
						|
                objects: object.items,
 | 
						|
                successOrFailure: left(unit),
 | 
						|
              ),
 | 
						|
              (error) => state.copyWith(successOrFailure: right(error)),
 | 
						|
            ),
 | 
						|
          );
 | 
						|
        },
 | 
						|
        didReceiveTrash: (e) async {
 | 
						|
          emit(state.copyWith(objects: e.trash));
 | 
						|
        },
 | 
						|
        putback: (e) async {
 | 
						|
          final result = await _service.putback(e.trashId);
 | 
						|
          await _handleResult(result, emit);
 | 
						|
        },
 | 
						|
        delete: (e) async {
 | 
						|
          final result = await _service.deleteViews([e.trash.id]);
 | 
						|
          await _handleResult(result, emit);
 | 
						|
        },
 | 
						|
        deleteAll: (e) async {
 | 
						|
          final result = await _service.deleteAll();
 | 
						|
          await _handleResult(result, emit);
 | 
						|
        },
 | 
						|
        restoreAll: (e) async {
 | 
						|
          final result = await _service.restoreAll();
 | 
						|
          await _handleResult(result, emit);
 | 
						|
        },
 | 
						|
      );
 | 
						|
    });
 | 
						|
  }
 | 
						|
 | 
						|
  Future<void> _handleResult(
 | 
						|
    Either<dynamic, FlowyError> result,
 | 
						|
    Emitter<TrashState> emit,
 | 
						|
  ) async {
 | 
						|
    emit(
 | 
						|
      result.fold(
 | 
						|
        (l) => state.copyWith(successOrFailure: left(unit)),
 | 
						|
        (error) => state.copyWith(successOrFailure: right(error)),
 | 
						|
      ),
 | 
						|
    );
 | 
						|
  }
 | 
						|
 | 
						|
  void _listenTrashUpdated(Either<List<TrashPB>, FlowyError> trashOrFailed) {
 | 
						|
    trashOrFailed.fold(
 | 
						|
      (trash) {
 | 
						|
        add(TrashEvent.didReceiveTrash(trash));
 | 
						|
      },
 | 
						|
      (error) {
 | 
						|
        Log.error(error);
 | 
						|
      },
 | 
						|
    );
 | 
						|
  }
 | 
						|
 | 
						|
  @override
 | 
						|
  Future<void> close() async {
 | 
						|
    await _listener.close();
 | 
						|
    return super.close();
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
@freezed
 | 
						|
class TrashEvent with _$TrashEvent {
 | 
						|
  const factory TrashEvent.initial() = Initial;
 | 
						|
  const factory TrashEvent.didReceiveTrash(List<TrashPB> trash) = ReceiveTrash;
 | 
						|
  const factory TrashEvent.putback(String trashId) = Putback;
 | 
						|
  const factory TrashEvent.delete(TrashPB trash) = Delete;
 | 
						|
  const factory TrashEvent.restoreAll() = RestoreAll;
 | 
						|
  const factory TrashEvent.deleteAll() = DeleteAll;
 | 
						|
}
 | 
						|
 | 
						|
@freezed
 | 
						|
class TrashState with _$TrashState {
 | 
						|
  const factory TrashState({
 | 
						|
    required List<TrashPB> objects,
 | 
						|
    required Either<Unit, FlowyError> successOrFailure,
 | 
						|
  }) = _TrashState;
 | 
						|
 | 
						|
  factory TrashState.init() => TrashState(
 | 
						|
        objects: [],
 | 
						|
        successOrFailure: left(unit),
 | 
						|
      );
 | 
						|
}
 |