Skip to content

Commit

Permalink
Content controller implemented.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alejandro-FA committed Oct 29, 2024
1 parent 9babc5a commit 6375a6d
Show file tree
Hide file tree
Showing 10 changed files with 320 additions and 56 deletions.
14 changes: 14 additions & 0 deletions lib/controllers/content_controller.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'package:riverpod_annotation/riverpod_annotation.dart';

import '../repositories/content_repository.dart';

part 'content_controller.g.dart';

@riverpod
class ContentController extends _$ContentController {
@override
Future<String> build(String fileName) async {
final contentRepository = ref.watch(contentRepositoryProvider);
return contentRepository.loadContent(fileName);
}
}
176 changes: 176 additions & 0 deletions lib/controllers/content_controller.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 19 additions & 10 deletions lib/pages/cv.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,22 @@ import '../widgets/page_scaffold.dart';
import '../widgets/timeline.dart';

@RoutePage()
class CVPage extends ConsumerWidget {
class CVPage extends StatelessWidget {
const CVPage({super.key});

@override
Widget build(BuildContext context, WidgetRef ref) {
Widget build(BuildContext context) {
final theme = Theme.of(context);
final textTheme = theme.textTheme;
final screenWidth = MediaQuery.of(context).size.width;
final double padding = max(
MaterialWindowClass.of(context) <= MaterialWindowClass.medium ? 25 : 50,
(screenWidth - MaterialWindowClass.large.minDP) / 2,
);
final cvFile = AppLocalizations.of(context).cvFile;
final fileRepository = ref.watch(storageServiceProvider);

return PageScaffold(
title: 'Curriculum Vitae | Alejandro Fernández Alburquerque',
floatingActionButton: FloatingActionButton.extended(
label: Text('Download CV', style: textTheme.titleMedium),
tooltip: 'Download CV',
onPressed: () async => fileRepository.download(cvFile),
icon: const Icon(Icons.download),
),
floatingActionButton: const _DownloadCVButton(),
slivers: [
SliverPadding(
padding: EdgeInsets.symmetric(
Expand Down Expand Up @@ -121,6 +114,22 @@ For more information check my Research page.""",
}
}

class _DownloadCVButton extends ConsumerWidget {
const _DownloadCVButton();

@override
Widget build(BuildContext context, WidgetRef ref) {
final textTheme = Theme.of(context).textTheme;

return FloatingActionButton.extended(
label: Text('Download CV', style: textTheme.titleMedium),
tooltip: 'Download CV',
onPressed: () async => ref.read(storageServiceProvider).downloadCV(),
icon: const Icon(Icons.download),
);
}
}

// TODO: Change build functions with widgets
class MyTimelineEvent implements TimelineEvent {
const MyTimelineEvent({
Expand Down
46 changes: 20 additions & 26 deletions lib/pages/research.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import 'dart:math';

import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart' show rootBundle;
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../controllers/content_controller.dart';
import '../theme/material_window_class.dart';
import '../widgets/page_scaffold.dart';

Expand Down Expand Up @@ -36,38 +36,32 @@ class ResearchPage extends StatelessWidget {
),
sliver: SliverList.builder(
itemCount: contentFiles.length,
itemBuilder: (context, index) {
final basePath = AppLocalizations.of(context).contentPath;
final filePath = '$basePath${contentFiles[index]}';
return Card(
child: ResearchItem(contentPath: filePath),
);
},
itemBuilder: (context, index) => Card(
child: _ResearchItem(contentFile: contentFiles[index]),
),
),
),
],
);
}
}

// TODO: wait for content to load before loading page.
class ResearchItem extends StatelessWidget {
ResearchItem({required String contentPath, super.key})
: content = rootBundle.loadString(contentPath);
class _ResearchItem extends ConsumerWidget {
const _ResearchItem({required this.contentFile});

final Future<String> content;
final String contentFile;

@override
Widget build(BuildContext context) => Padding(
padding: const EdgeInsets.all(20),
child: FutureBuilder(
future: content,
builder: (context, snapshot) {
if (snapshot.hasData) {
return MarkdownBody(data: snapshot.data!);
}
return const SizedBox.shrink();
},
),
);
Widget build(BuildContext context, WidgetRef ref) {
final content = ref.watch(contentControllerProvider(contentFile));

return Padding(
padding: const EdgeInsets.all(20),
child: switch (content) {
AsyncData(:final value) => MarkdownBody(data: value),
AsyncError() => const Text('Oops, something unexpected happened'),
_ => const SizedBox.shrink(),
},
);
}
}
32 changes: 32 additions & 0 deletions lib/repositories/content_repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'dart:ui';

import 'package:flutter/services.dart' show rootBundle;
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:path/path.dart' as path;
import 'package:riverpod_annotation/riverpod_annotation.dart';

import '../controllers/localization_controller.dart';

part 'content_repository.g.dart';

class ContentRepository {
const ContentRepository({required this.locale});

final Locale locale;

Future<String> loadContent(String fileName) async {
final fullPath = path.join(contentPath, fileName);
return rootBundle.loadString(fullPath);
}

String get contentPath => switch (locale.languageCode) {
'es' => 'assets/content/es',
_ => 'assets/content/en',
};
}

@riverpod
ContentRepository contentRepository(Ref ref) {
final locale = ref.watch(localizationControllerProvider);
return ContentRepository(locale: locale);
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6375a6d

Please sign in to comment.