Skip to content

Commit

Permalink
Move widgets for quickly creating lessons to own file. (#1787)
Browse files Browse the repository at this point in the history
Move `TimetableQuickCreateDialog` to
`timetable_quick_create_dialog.dart` and delete some unused code.
  • Loading branch information
Jonas-Sander authored Nov 3, 2024
1 parent a1c9442 commit a9d543b
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 459 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
// Copyright (c) 2024 Sharezone UG (haftungsbeschränkt)
// Licensed under the EUPL-1.2-or-later.
//
// You may obtain a copy of the Licence at:
// https://joinup.ec.europa.eu/software/page/eupl
//
// SPDX-License-Identifier: EUPL-1.2

import 'package:bloc_provider/bloc_provider.dart';
import 'package:flutter/material.dart';
import 'package:group_domain_models/group_domain_models.dart';
import 'package:sharezone/groups/group_join/group_join_page.dart';
import 'package:sharezone/groups/group_permission.dart';
import 'package:sharezone/groups/src/pages/course/course_card.dart';
import 'package:sharezone/groups/src/pages/course/create/pages/course_template_page.dart';
import 'package:sharezone/main/application_bloc.dart';
import 'package:sharezone/timetable/timetable_add/timetable_add_page.dart';
import 'package:sharezone_widgets/sharezone_widgets.dart';

import '../../bloc/timetable_selection_bloc.dart';

class TimetableQuickCreateDialog extends StatelessWidget {
final EmptyPeriodSelection periodSelection;
final TimetableSelectionBloc selectionBloc;

const TimetableQuickCreateDialog({
super.key,
required this.periodSelection,
required this.selectionBloc,
});

@override
Widget build(BuildContext context) {
final height = MediaQuery.of(context).size.height;
return SizedBox(
height: height - (height / 5),
child: StreamBuilder<List<Course>>(
stream: BlocProvider.of<SharezoneContext>(context)
.api
.course
.streamCourses(),
builder: (context, snapshot) {
final courses = snapshot.data ?? [];
final isCourseListEmpty = courses.isEmpty;
_sortCoursesByAlphabet(courses);
return CustomScrollView(
slivers: <Widget>[
_AppBar(),
SliverToBoxAdapter(
child: SafeArea(
child: isCourseListEmpty
? _EmptyCourseList()
: Column(
mainAxisSize: MainAxisSize.min,
children: courses
.map((course) => _QuickCreateCourseTile(
course: course,
selectionBloc: selectionBloc,
periodSelection: periodSelection,
))
.toList(),
),
),
)
],
);
},
),
);
}

void _sortCoursesByAlphabet(List<Course> courses) {
courses.sort((a, b) => a.name.compareTo(b.name));
}
}

class _AppBar extends StatelessWidget {
@override
Widget build(BuildContext context) {
return SliverAppBar(
pinned: true,
backgroundColor: Theme.of(context).isDarkTheme ? null : Colors.white,
centerTitle: false,
forceElevated: true,
automaticallyImplyLeading: false,
elevation: 0,
actions: <Widget>[
const _CreateCourseIconButton(),
const _JoinGroupIconButton(),
CloseIconButton(color: _getIconColor(context)),
],
title: Text("Stunde hinzufügen",
style: Theme.of(context).textTheme.titleLarge),
);
}
}

class _JoinGroupIconButton extends StatelessWidget {
const _JoinGroupIconButton();

@override
Widget build(BuildContext context) {
return IconButton(
icon: const Icon(Icons.vpn_key),
tooltip: 'Kurs/Klasse beitreten',
color: _getIconColor(context),
onPressed: () => openGroupJoinPage(context),
);
}
}

class _CreateCourseIconButton extends StatelessWidget {
const _CreateCourseIconButton();

@override
Widget build(BuildContext context) {
return IconButton(
icon: const Icon(Icons.add),
tooltip: 'Kurs erstellen',
color: _getIconColor(context),
onPressed: () => Navigator.pushNamed(context, CourseTemplatePage.tag),
);
}
}

class _EmptyCourseList extends StatelessWidget {
@override
Widget build(BuildContext context) {
final height = MediaQuery.of(context).size.height;
final modalSheetHeight = height - (height / 5);
const appBarHeight = 56.0;
return Material(
color: Theme.of(context).isDarkTheme
? Theme.of(context).scaffoldBackgroundColor
: Colors.white,
child: SizedBox(
height: modalSheetHeight - appBarHeight,
child: Center(
child: PlaceholderModel(
iconSize: const Size(175, 175),
title: "Du bist noch keinem Kurs, bzw. keiner Klasse beigetreten!",
svgPath: 'assets/icons/ghost.svg',
animateSVG: true,
subtitle: Padding(
padding: const EdgeInsets.only(top: 12),
child: Row(
children: <Widget>[
Expanded(
child: CourseManagementButton(
iconData: Icons.add,
title: "Kurs erstellen",
onTap: () =>
Navigator.pushNamed(context, CourseTemplatePage.tag),
),
),
const SizedBox(width: 12),
Expanded(
child: CourseManagementButton(
iconData: Icons.vpn_key,
title: "Kurs beitreten",
onTap: () => openGroupJoinPage(context),
),
),
],
),
),
),
),
),
);
}
}

class _QuickCreateCourseTile extends StatelessWidget {
const _QuickCreateCourseTile({
required this.course,
required this.selectionBloc,
required this.periodSelection,
});

final Course course;
final TimetableSelectionBloc selectionBloc;
final EmptyPeriodSelection periodSelection;

@override
Widget build(BuildContext context) {
final hasCreatorPermissions =
course.myRole.hasPermission(GroupPermission.contentCreation);
return ListTile(
enabled: hasCreatorPermissions,
title: Text(course.name),
leading: CourseCircleAvatar(
courseId: course.id,
abbreviation: course.abbreviation,
),
trailing: !hasCreatorPermissions ? const Icon(Icons.lock) : null,
onTap: () {
selectionBloc.createLesson(
BlocProvider.of<SharezoneContext>(context).api.timetable,
periodSelection,
course,
);
Navigator.pop(context, course);
},
);
}
}

Color? _getIconColor(BuildContext context) =>
Theme.of(context).isDarkTheme ? Colors.grey : Colors.grey[600];
Loading

0 comments on commit a9d543b

Please sign in to comment.