Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Common theme #267

Merged
merged 7 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions admin/lib/presentation/app/app.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:survey_admin/presentation/app/di/injector.dart';
import 'package:survey_admin/presentation/pages/builder/builder_cubit.dart';
import 'package:survey_admin/presentation/pages/builder/builder_page.dart';
import 'package:survey_admin/presentation/utils/utils.dart';

Expand All @@ -26,10 +23,7 @@ class _AppState extends State<App> {
GlobalCupertinoLocalizations.delegate,
],
theme: AppTheme.theme,
home: BlocProvider(
create: (context) => i.get<BuilderCubit>(),
child: const BuilderPage(),
),
home: const BuilderPage(),
);
}
}
8 changes: 7 additions & 1 deletion admin/lib/presentation/app/di/injector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import 'package:survey_admin/data/repositories/session_storage_repository_impl.d
import 'package:survey_admin/domain/repository_interfaces/file_system_repository.dart.dart';
import 'package:survey_admin/domain/repository_interfaces/session_storage_repository.dart';
import 'package:survey_admin/presentation/pages/builder/builder_cubit.dart';
import 'package:survey_admin/presentation/pages/new_question_page/new_question_cubit.dart';
import 'package:survey_admin/presentation/utils/common_data.dart';
import 'package:survey_sdk/survey_sdk.dart';

//ignore_for_file: prefer-static-class
GetIt get i => GetIt.instance;
Expand Down Expand Up @@ -46,7 +48,11 @@ void _initRepositories() {
}

void _initCubits() {
i.registerFactory<BuilderCubit>(() => BuilderCubit(i.get(), i.get()));
i
..registerFactory<BuilderCubit>(() => BuilderCubit(i.get(), i.get()))
..registerFactoryParam<NewQuestionCubit, SurveyData, void>(
(data, _) => NewQuestionCubit(data),
);
}

void initCommonData(BuildContext context) {
Expand Down
27 changes: 3 additions & 24 deletions admin/lib/presentation/pages/builder/builder_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,9 @@ class BuilderCubit extends Cubit<BuilderState> {
Clipboard.setData(ClipboardData(text: jsonText));
}

void updateCommon(QuestionData data) {
var surveyData = state.surveyData;
final common = state.surveyData.commonTheme;
switch (data.type) {
case QuestionTypes.choice:
surveyData = surveyData.copyWith(
commonTheme: common.copyWith(choice: data as ChoiceQuestionData),
);
case QuestionTypes.input:
surveyData = surveyData.copyWith(
commonTheme: common.copyWith(input: data as InputQuestionData),
);

case QuestionTypes.info:
surveyData = surveyData.copyWith(
commonTheme: common.copyWith(info: data as InfoQuestionData),
);
case QuestionTypes.slider:
surveyData = surveyData.copyWith(
commonTheme: common.copyWith(slider: data as SliderQuestionData),
);
}
_sessionStorageRepository.saveSurveyData(surveyData);
emit(state.copyWith(surveyData: surveyData));
void updateCommonTheme(SurveyData data) {
_sessionStorageRepository.saveSurveyData(data);
emit(state.copyWith(surveyData: data));
}

void select(QuestionData data) {
Expand Down
64 changes: 40 additions & 24 deletions admin/lib/presentation/pages/builder/builder_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,39 @@ import 'package:survey_admin/presentation/app/di/injector.dart';
import 'package:survey_admin/presentation/app/localization/app_localizations_ext.dart';
import 'package:survey_admin/presentation/pages/builder/builder_cubit.dart';
import 'package:survey_admin/presentation/pages/builder/builder_state.dart';
import 'package:survey_admin/presentation/widgets/builder_page/editor_bar.dart';
import 'package:survey_admin/presentation/widgets/builder_page/phone_view.dart';
import 'package:survey_admin/presentation/widgets/builder_page/question_list.dart';
import 'package:survey_admin/presentation/widgets/editor_bar.dart';
import 'package:survey_admin/presentation/widgets/export_floating_window.dart';
import 'package:survey_sdk/survey_sdk.dart';

class BuilderPage extends StatefulWidget {
class BuilderPage extends StatelessWidget {
const BuilderPage({super.key});

@override
State<BuilderPage> createState() => _BuilderPageState();
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => i.get<BuilderCubit>(),
child: const _Content(),
);
}
}

class _BuilderPageState extends State<BuilderPage>
class _Content extends StatefulWidget {
const _Content();

@override
State<_Content> createState() => _ContentState();
}

class _ContentState extends State<_Content>
with SingleTickerProviderStateMixin {
late final SurveyController _surveyController;
late final BuilderCubit _cubit = context.read<BuilderCubit>();
late final TabController _tabController;

static const tabLength = 2;

BuilderCubit cubit(BuildContext context) => context.read<BuilderCubit>();

@override
void initState() {
super.initState();
Expand All @@ -35,16 +47,17 @@ class _BuilderPageState extends State<BuilderPage>
}

void _onChangePage() {
final questions = _cubit.state.surveyData.questions;
final cubit = this.cubit(context);
final questions = cubit.state.surveyData.questions;
final index = _surveyController.pageController.page;
final question = index == questions.length
? _cubit.state.surveyData.endPage
? cubit.state.surveyData.endPage
: index != null && index % 1 == 0
? questions[index.toInt()]
: null;

if (question != null) {
_cubit.select(question);
cubit.select(question);
}
}

Expand Down Expand Up @@ -105,10 +118,12 @@ class _BuilderPageState extends State<BuilderPage>

@override
Widget build(BuildContext context) {
final cubit = this.cubit(context);
return BlocConsumer<BuilderCubit, BuilderState>(
listener: (_, newState) {
if (newState is ImportErrorSurveyDataBuilderState) _showImportDialog();

if (newState is ImportErrorSurveyDataBuilderState) {
_showImportDialog();
}
final selected =
newState is EditQuestionBuilderState ? newState.selectedIndex : 0;
if (selected != 0) {
Expand All @@ -120,21 +135,21 @@ class _BuilderPageState extends State<BuilderPage>
appBar: AppBar(
title: _BuilderPageTabBar(
tabController: _tabController,
onTapEditMode: _cubit.openEditMode,
onTapPreviewMode: _cubit.openPreviewMode,
onTapEditMode: cubit.openEditMode,
onTapPreviewMode: cubit.openPreviewMode,
),
actions: [
_ImportButton(
onImportPressed: () {
_cubit.importData();
cubit.importData();
_tabController.animateTo(0);
_cubit.openEditMode();
cubit.openEditMode();
},
),
_ExportButton(
isButtonActive: _cubit.state.surveyData.questions.isEmpty,
downloadSurveyData: _cubit.downloadSurveyData,
copySurveyData: _cubit.copySurveyData,
isButtonActive: cubit.state.surveyData.questions.isEmpty,
downloadSurveyData: cubit.downloadSurveyData,
copySurveyData: cubit.copySurveyData,
),
],
centerTitle: true,
Expand All @@ -143,13 +158,14 @@ class _BuilderPageState extends State<BuilderPage>
children: [
QuestionList(
isEditMode: state is EditQuestionBuilderState,
onDelete: _cubit.deleteQuestionData,
onSelect: _cubit.select,
onAdd: _cubit.addQuestionData,
questions: _cubit.state.surveyData.questions,
onUpdate: _cubit.updateQuestions,
onDelete: cubit.deleteQuestionData,
onSelect: cubit.select,
onAdd: cubit.addQuestionData,
data: cubit.state.surveyData,
onUpdate: cubit.updateQuestions,
selectedIndex: _selectedIndex(state),
endPage: state.surveyData.endPage,
onDataUpdate: cubit.updateCommonTheme,
),
Expanded(
child: PhoneView(
Expand All @@ -162,7 +178,7 @@ class _BuilderPageState extends State<BuilderPage>
),
EditorBar(
isEditMode: state is EditQuestionBuilderState,
onChange: _cubit.updateQuestionData,
onChange: cubit.updateQuestionData,
editableQuestion: _editableQuestion(state),
questionsAmount: state.surveyData.questions.length,
),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:survey_admin/presentation/pages/new_question_page/new_question_state.dart';
import 'package:survey_admin/presentation/pages/new_question_page/new_question_tabs.dart';
import 'package:survey_sdk/survey_sdk.dart';

class NewQuestionCubit extends Cubit<NewQuestionState> {
NewQuestionCubit(SurveyData data)
: super(
NewQuestionState(data: data),
);

void selectTab(NewQuestionTabs tab) => emit(
state.copyWith(selectedTab: tab),
);

void updateData(QuestionData data) {
var surveyData = state.data;
final common = surveyData.commonTheme;
switch (data.type) {
case QuestionTypes.choice:
surveyData = surveyData.copyWith(
commonTheme: common.copyWith(choice: data as ChoiceQuestionData),
);
case QuestionTypes.input:
surveyData = surveyData.copyWith(
commonTheme: common.copyWith(input: data as InputQuestionData),
);

case QuestionTypes.info:
surveyData = surveyData.copyWith(
commonTheme: common.copyWith(info: data as InfoQuestionData),
);
case QuestionTypes.slider:
surveyData = surveyData.copyWith(
commonTheme: common.copyWith(slider: data as SliderQuestionData),
);
}
emit(state.copyWith(data: surveyData));
}
}
Loading