Skip to content

Commit

Permalink
Change how the thabloids work (#523)
Browse files Browse the repository at this point in the history
  • Loading branch information
JAicewizard authored Nov 13, 2024
1 parent 946f416 commit af5b4f9
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 58 deletions.
5 changes: 5 additions & 0 deletions lib/api/api_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,11 @@ abstract class ApiRepository {
int? offset,
});

/// Get a [Thabloid].
Future<Thabloid> getThabloid({
required int pk,
});

/// Get a list of [Slide]s.
///
/// Use `limit` and `offset` for pagination. [ListResponse.count] is the
Expand Down
14 changes: 14 additions & 0 deletions lib/api/concrexit_api_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,20 @@ class ConcrexitApiRepository implements ApiRepository {
);
}

@override
Future<Thabloid> getThabloid({
required int pk,
}) async {
return sandbox(() async {
final uri = _uri(
path: '/thabloid/thabloids/$pk',
);

final response = await _handleExceptions(() => _client.get(uri));
return Thabloid.fromJson(_jsonDecode(response));
});
}

@override
Future<ListResponse<Slide>> getSlides({
int? limit,
Expand Down
22 changes: 22 additions & 0 deletions lib/blocs/thabloid_cubit.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'dart:async';

import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:reaxit/api/api_repository.dart';
import 'package:reaxit/models/thabliod.dart';

class ThabloidCubit extends Cubit<Thabloid> {
final ApiRepository api;
Thabloid thabloid;
Timer _debounceTimer = Timer(const Duration(hours: 1), () => {});

ThabloidCubit(this.api, this.thabloid) : super(thabloid);

Future<String> getTitle() async {
if (!_debounceTimer.isActive) {
Thabloid thabloid = await api.getThabloid(pk: this.thabloid.pk);
emit(thabloid);
_debounceTimer = Timer(const Duration(hours: 1), () => {});
}
return thabloid.file;
}
}
4 changes: 2 additions & 2 deletions lib/blocs/thabloid_list_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import 'package:reaxit/models/thabliod.dart';

typedef ThabloidListState = ListState<Thabloid>;

class ThabloidCubit extends SingleListCubit<Thabloid> {
ThabloidCubit(super.api);
class ThabloidListCubit extends SingleListCubit<Thabloid> {
ThabloidListCubit(super.api);

static const int firstPageSize = 30;

Expand Down
4 changes: 2 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,8 @@ class _ThaliAppState extends State<ThaliApp> {
lazy: false,
),
BlocProvider(
create: (_) =>
ThabloidCubit(apiRepository)..cachedLoad(),
create: (_) => ThabloidListCubit(apiRepository)
..cachedLoad(),
lazy: false,
),
BlocProvider(
Expand Down
29 changes: 10 additions & 19 deletions lib/ui/screens/thabloids_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:intl/intl.dart';
import 'package:reaxit/api/api_repository.dart';
import 'package:reaxit/blocs.dart';
import 'package:reaxit/blocs/thabloid_list_cubit.dart';
import 'package:reaxit/models/thabliod.dart';
Expand All @@ -17,8 +18,6 @@ class ThabloidScreen extends StatefulWidget {
class _ThabloidScreenState extends State<ThabloidScreen> {
late ScrollController _controller;
late CalendarCubit _cubit;
GlobalKey todayKey = GlobalKey();
GlobalKey thisMonthKey = GlobalKey();

@override
void initState() {
Expand Down Expand Up @@ -47,22 +46,18 @@ class _ThabloidScreenState extends State<ThabloidScreen> {
title: const Text('THABLOIDS'),
),
drawer: MenuDrawer(),
body: BlocBuilder<ThabloidCubit, ThabloidListState>(
builder: (context, calendarState) {
if (calendarState.hasException) {
return ErrorScrollView(calendarState.message!);
} else if (calendarState.isLoading) {
body: BlocBuilder<ThabloidListCubit, ThabloidListState>(
builder: (context, thabloidsState) {
if (thabloidsState.hasException) {
return ErrorScrollView(thabloidsState.message!);
} else if (thabloidsState.isLoading) {
return const Center(child: CircularProgressIndicator());
} else {
todayKey = GlobalKey();
thisMonthKey = GlobalKey();
return ThabloidsScrollView(
key: const PageStorageKey('thabloids'),
controller: _controller,
thabloidState: calendarState,
todayKey: todayKey,
thisMonthKey: thisMonthKey,
thabloids: calendarState.results,
thabloidState: thabloidsState,
thabloids: thabloidsState.results,
);
}
},
Expand All @@ -81,9 +76,6 @@ class ThabloidsScrollView extends StatelessWidget {
static final monthFormatter = DateFormat('MMMM');
static final monthYearFormatter = DateFormat('MMMM yyyy');

final GlobalKey? todayKey;
final GlobalKey? thisMonthKey;

final Key centerkey = UniqueKey();
final ScrollController controller;
final ThabloidListState thabloidState;
Expand All @@ -93,12 +85,11 @@ class ThabloidsScrollView extends StatelessWidget {
{super.key,
required this.controller,
required this.thabloidState,
this.todayKey,
this.thisMonthKey,
required this.thabloids});

@override
Widget build(BuildContext context) {
ApiRepository api = context.read();
return Column(
children: [
Expanded(
Expand All @@ -119,7 +110,7 @@ class ThabloidsScrollView extends StatelessWidget {
childAspectRatio: 1 / sqrt(2),
),
delegate: SliverChildBuilderDelegate(
(_, index) => ThabloidDetailCard(thabloids[index]),
(_, index) => ThabloidDetailCard(thabloids[index], api),
childCount: thabloids.length,
),
),
Expand Down
78 changes: 43 additions & 35 deletions lib/ui/widgets/thabloid_tile.dart
Original file line number Diff line number Diff line change
@@ -1,53 +1,61 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:reaxit/api/api_repository.dart';
import 'package:reaxit/blocs/thabloid_cubit.dart';
import 'package:reaxit/models/thabliod.dart';
import 'package:reaxit/ui/widgets/cached_image.dart';
import 'package:url_launcher/url_launcher.dart';

class ThabloidDetailCard extends StatelessWidget {
final Thabloid thabloid;
final ThabloidCubit cubit;
ThabloidDetailCard(Thabloid thabloid, ApiRepository api)
: cubit = ThabloidCubit(api, thabloid);

const ThabloidDetailCard(this.thabloid);

void _openThabloid() {
launchUrl(Uri.parse(thabloid.file), mode: LaunchMode.externalApplication);
void _openThabloid() async {
launchUrl(Uri.parse(await cubit.getTitle()),
mode: LaunchMode.externalApplication);
}

@override
Widget build(BuildContext context) {
final textTheme = Theme.of(context).textTheme;
return Stack(
children: [
CachedImage(
placeholder: 'assets/img/thabloid_placeholder.png',
imageUrl: thabloid.cover,
),
Align(
alignment: Alignment.bottomRight,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'${thabloid.year}-${thabloid.year + 1} nr. ${thabloid.issue}',
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: textTheme.titleMedium?.copyWith(shadows: [
const Shadow(
offset: Offset(0.0, 0.0),
blurRadius: 10.0,
color: Color.fromARGB(255, 0, 0, 0),
),
]),
return BlocBuilder<ThabloidCubit, Thabloid>(
builder: (context, thabloidsState) {
Thabloid thabloid = thabloidsState;
return Stack(
children: [
CachedImage(
placeholder: 'assets/img/thabloid_placeholder.png',
imageUrl: thabloid.cover,
),
Align(
alignment: Alignment.bottomRight,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'${thabloid.year}-${thabloid.year + 1} nr. ${thabloid.issue}',
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: textTheme.titleMedium?.copyWith(shadows: [
const Shadow(
offset: Offset(0.0, 0.0),
blurRadius: 10.0,
color: Color.fromARGB(255, 0, 0, 0),
),
]),
),
),
),
),
Positioned.fill(
child: Material(
color: Colors.transparent,
child: InkWell(
onTap: _openThabloid,
Positioned.fill(
child: Material(
color: Colors.transparent,
child: InkWell(
onTap: _openThabloid,
),
),
),
),
],
);
],
);
});
}
}

0 comments on commit af5b4f9

Please sign in to comment.