Skip to content

Commit

Permalink
refactor: adjust proposal creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Zied-Dahmani committed Nov 20, 2024
1 parent 83e68a1 commit 976f908
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 179 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_quill/flutter_quill.dart';
import 'package:flutter_quill/quill_delta.dart';
import 'package:get/get_utils/src/extensions/context_extensions.dart';
import 'package:hypha_wallet/design/hypha_colors.dart';
import 'package:hypha_wallet/design/themes/extensions/theme_extension_provider.dart';
Expand All @@ -27,6 +28,13 @@ class _ProposalContentViewState extends State<ProposalContentView> {
void initState() {
super.initState();

_titleController.text = context.read<ProposalCreationBloc>().state.proposal!.title ?? '';
final String? details = context.read<ProposalCreationBloc>().state.proposal!.details;
if (details!= null) {
final List<dynamic> jsonData = jsonDecode(details);
_quillController.document = Document.fromDelta(Delta.fromJson(jsonData));
}

_titleController.addListener(() {
context.read<ProposalCreationBloc>().add(ProposalCreationEvent.updateProposal({'title': _titleController.text.isEmpty ? null : _titleController.text}));
});
Expand Down
19 changes: 5 additions & 14 deletions lib/ui/proposals/creation/interactor/proposal_creation_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,21 @@ class ProposalCreationBloc extends Bloc<ProposalCreationEvent, ProposalCreationS
final ErrorHandlerManager _errorHandlerManager;

ProposalCreationBloc(this.daos, this._publishProposalUseCase, this._errorHandlerManager) : super(ProposalCreationState(proposal: ProposalCreationModel())) {
on<_Initialize>(_initialize);
on<_UpdateCurrentView>(_updateCurrentView);
on<_UpdateProposal>(_updateProposal);
on<_PublishProposal>(_publishProposal);
on<_ClearPageCommand>((_, emit) => emit(state.copyWith(command: null)));

_pageController = PageController(initialPage: daos.length > 1 ? 0 : 1);
if (daos.length == 1) {
add(ProposalCreationEvent.updateProposal({'dao': daos.first}));
}
}

late final PageController _pageController;

PageController get pageController => _pageController;

void _initialize(_Initialize event, Emitter<ProposalCreationState> emit) {

if (daos.length == 1) {
emit(state.copyWith(proposal: state.proposal!.copyWith({'dao': daos.first})));
}
}

void _updateCurrentView(_UpdateCurrentView event, Emitter<ProposalCreationState> emit) {
if (event.nextViewIndex == -1) {
emit(state.copyWith(command: const PageCommand.navigateBackToProposals()));
Expand Down Expand Up @@ -77,13 +72,9 @@ class ProposalCreationBloc extends Bloc<ProposalCreationEvent, ProposalCreationS
);
}

void _updateProposalFields(updates, emit) {
final ProposalCreationModel proposal = state.proposal!.copyWith(updates);
emit(state.copyWith(proposal: proposal));
}

void _updateProposal(_UpdateProposal event, Emitter<ProposalCreationState> emit) {
_updateProposalFields(event.updates, emit);
final ProposalCreationModel proposal = state.proposal!.copyWith(event.updates);
emit(state.copyWith(proposal: proposal));
}

Future<void> _publishProposal(_PublishProposal event, Emitter<ProposalCreationState> emit) async {
Expand Down
Loading

0 comments on commit 976f908

Please sign in to comment.