Skip to content

Commit

Permalink
fix scroll on mobile
Browse files Browse the repository at this point in the history
This is untested as of yet
  • Loading branch information
WesselvanDam committed Sep 19, 2024
1 parent f570387 commit 0623c3d
Show file tree
Hide file tree
Showing 12 changed files with 190 additions and 173 deletions.
2 changes: 0 additions & 2 deletions lib/models/token.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class Token with _$Token {
required String id,
required String name,
required String thumbnailLink,
required String? accessToken,
required DateTime createdTime,
required TokenPartialInfo fromInfo,
required TokenPartialInfo toInfo,
Expand All @@ -25,7 +24,6 @@ class Token with _$Token {
id: file.id!,
name: file.name!,
thumbnailLink: file.thumbnailLink!,
accessToken: accessToken,
createdTime: file.createdTime ?? DateTime.now(),
fromInfo: TokenPartialInfo.fromInfo(file),
toInfo: TokenPartialInfo.toInfo(file),
Expand Down
23 changes: 15 additions & 8 deletions lib/providers/token_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,33 @@ class Tokens extends _$Tokens {
.addToken(image)
.then((token) => update((tokens) => [token, ...tokens]))
.then((_) => true)
.catchError((error) => false);
.catchError((error) {
debugPrint('Error adding token: $error');
return false;
});
}

Future<bool> updateToken(Token token) async {
return update((tokens) {
final index = tokens.indexWhere((t) => t.id == token.id);
tokens[index] = token;
return tokens;
}).then((_) => DriveServiceApi().updateToken(token)).then((updatedToken) {
debugPrint('Updated Token equals Token: ${updatedToken == token}');
debugPrint("Updated Token: $updatedToken");
debugPrint("Token: $token");
return updatedToken == token;
}).catchError((error) => false);
})
.then((_) => DriveServiceApi().updateToken(token))
.then((updatedToken) => updatedToken == token)
.catchError((error) {
debugPrint('Error updating token: $error');
return false;
});
}

Future<bool> deleteToken(Token token) async {
return update((tokens) => tokens..remove(token))
.then((_) => DriveServiceApi().deleteToken(token))
.then((_) => true)
.catchError((error) => false);
.catchError((error) {
debugPrint('Error deleting token: $error');
return false;
});
}
}
79 changes: 43 additions & 36 deletions lib/screens/Details/details_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:thank_you_token/providers/token_provider.dart';
import 'package:thank_you_token/screens/Details/edit_provider.dart';
import 'package:thank_you_token/screens/Details/local/details_info.dart';
import 'package:thank_you_token/utils/extensions.dart';
import 'package:thank_you_token/widgets/adaptive_scroll.dart';
import 'package:thank_you_token/widgets/token_image.dart';

class DetailsScreen extends ConsumerWidget {
Expand All @@ -15,8 +16,10 @@ class DetailsScreen extends ConsumerWidget {
ref.read(tokenEditProvider.notifier).setToken(token);
}

void _handleSave(WidgetRef ref) {
ref.read(tokensProvider.notifier).updateToken(ref.read(tokenEditProvider)!);
void _handleSave(WidgetRef ref, Token token) {
if (ref.read(tokenEditProvider) != token) {
ref.read(tokensProvider.notifier).updateToken(ref.read(tokenEditProvider)!);
}
ref.read(tokenEditProvider.notifier).setToken(null);
}

Expand All @@ -38,43 +41,47 @@ class DetailsScreen extends ConsumerWidget {
return const SizedBox();
}

return Column(
mainAxisSize: MainAxisSize.min,
children: [
AspectRatio(
aspectRatio: 16 / 9,
child: TokenImage(token, borderRadius: 24),
),
ButtonBar(
alignment: MainAxisAlignment.center,
return AdaptiveScrollView(slivers: [
SliverToBoxAdapter(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
if (isEditing)
FilledButton.icon(
icon: const Icon(Icons.save),
label: const Text('Save'),
onPressed: () => _handleSave(ref),
)
else
FilledButton.tonalIcon(
icon: const Icon(Icons.edit),
label: const Text('Edit'),
onPressed: () => _handleEdit(ref, token),
),
OutlinedButton.icon(
icon: const Icon(Icons.delete),
label: const Text('Delete'),
onPressed: () => _handleDelete(context, ref, token),
AspectRatio(
aspectRatio: 16 / 9,
child: TokenImage(token, borderRadius: 24),
),
Column(
children: [
DetailsInfo(token.fromInfo),
const Divider(indent: 24, endIndent: 24),
DetailsInfo(token.toInfo),
],
),
ButtonBar(
alignment: MainAxisAlignment.end,
children: [
OutlinedButton.icon(
icon: const Icon(Icons.delete),
label: const Text('Delete'),
onPressed: () => _handleDelete(context, ref, token),
),
if (isEditing)
FilledButton.icon(
icon: const Icon(Icons.save),
label: const Text('Save'),
onPressed: () => _handleSave(ref, token),
)
else
FilledButton.tonalIcon(
icon: const Icon(Icons.edit),
label: const Text('Edit'),
onPressed: () => _handleEdit(ref, token),
),
],
),
],
),
Column(
children: [
DetailsInfo(token.fromInfo),
const Divider(indent: 24, endIndent: 24),
DetailsInfo(token.toInfo),
],
),
],
);
),
]);
}
}
5 changes: 4 additions & 1 deletion lib/screens/Details/local/details_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ class DetailsInfo extends ConsumerWidget {
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(info.message ?? 'No description added'),
child: Text(
info.message ?? 'No description added',
style: Theme.of(context).textTheme.bodyMedium,
),
)
],
);
Expand Down
101 changes: 22 additions & 79 deletions lib/screens/Home/home_screen.dart
Original file line number Diff line number Diff line change
@@ -1,61 +1,42 @@
import 'package:flutter/material.dart';
import 'package:flutter_animate/flutter_animate.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:thank_you_token/models/token.dart';
import 'package:thank_you_token/providers/token_provider.dart';
import 'package:thank_you_token/providers/user_provider.dart';
import 'package:thank_you_token/screens/Home/local/token_grid.dart';
import 'package:thank_you_token/services/auth/auth_service.dart';
import 'package:thank_you_token/widgets/add_token_card.dart';
import 'package:thank_you_token/widgets/token_card.dart';
import 'package:thank_you_token/widgets/adaptive_scroll.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:web_smooth_scroll/web_smooth_scroll.dart';

class HomeScreen extends ConsumerStatefulWidget {
class HomeScreen extends ConsumerWidget {
const HomeScreen({super.key});

@override
ConsumerState<ConsumerStatefulWidget> createState() => _HomeScreenState();
}

class _HomeScreenState extends ConsumerState<HomeScreen> {
late ScrollController _scrollController;

@override
void initState() {
super.initState();
_scrollController = ScrollController();
}

@override
Widget build(BuildContext context) {
Widget build(BuildContext context, WidgetRef ref) {
final user = ref.watch(userProvider).value;
final tokens = ref.watch(tokensProvider);

return WebSmoothScroll(
controller: _scrollController,
child: CustomScrollView(
controller: _scrollController,
physics: const NeverScrollableScrollPhysics(),
slivers: [
SliverAppBar.large(
leading: Padding(
padding: const EdgeInsets.all(8.0),
child: Image.asset('assets/images/glyph_color.png'),
),
title: Text(
'Welcome, ${user?.displayName?.split(' ').first ?? 'Guest'}',
),
actions: actions(),
return AdaptiveScrollView(
slivers: [
SliverAppBar.large(
leading: Padding(
padding: const EdgeInsets.all(8.0),
child: Image.asset('assets/images/glyph_color.png'),
),
title: Text(
'Welcome, ${user?.displayName?.split(' ').first ?? 'Guest'}',
),
tokenGrid(tokens),
],
),
actions: actions(ref),
),
const TokenGrid(),
],
);
}

List<Widget> actions() => [
List<Widget> actions(WidgetRef ref) => [
PopupMenuButton<String>(
popUpAnimationStyle: AnimationStyle(curve: Curves.easeInOut),
popUpAnimationStyle: AnimationStyle(
curve: Curves.easeInOut,
duration: const Duration(milliseconds: 400),
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
Expand Down Expand Up @@ -89,42 +70,4 @@ class _HomeScreenState extends ConsumerState<HomeScreen> {
},
),
];

Widget tokenGrid(AsyncValue<List<Token>> tokens) {
return tokens.when(
data: (data) => SliverGrid.builder(
gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
maxCrossAxisExtent: 400,
crossAxisSpacing: 8,
mainAxisSpacing: 8,
childAspectRatio: 5 / 4,
),
itemBuilder: (context, index) => index == 0
? const AddTokenCard()
: TokenCard(token: data[index - 1]),
itemCount: data.length + 1,
),
loading: () => SliverFillRemaining(
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const CircularProgressIndicator(),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'If this takes too long, try refreshing by tapping the refresh icon. You may have blocked automatic popups.',
style: Theme.of(context).textTheme.labelLarge,
).animate().fadeIn(delay: const Duration(seconds: 2)),
),
],
),
),
),
error: (error, stackTrace) => SliverFillRemaining(
child: Center(
child: Text('Error: $error'),
),
));
}
}
50 changes: 50 additions & 0 deletions lib/screens/Home/local/token_grid.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import 'package:flutter/material.dart';
import 'package:flutter_animate/flutter_animate.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:thank_you_token/providers/token_provider.dart';
import 'package:thank_you_token/widgets/add_token_card.dart';
import 'package:thank_you_token/widgets/token_card.dart';

class TokenGrid extends ConsumerWidget {
const TokenGrid({super.key});

@override
Widget build(BuildContext context, WidgetRef ref) {
final tokens = ref.watch(tokensProvider);
return tokens.when(
data: (data) => SliverGrid.builder(
gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
maxCrossAxisExtent: 400,
crossAxisSpacing: 8,
mainAxisSpacing: 8,
childAspectRatio: 5 / 4,
),
itemBuilder: (context, index) => index == 0
? const AddTokenCard()
: TokenCard(token: data[index - 1]),
itemCount: data.length + 1,
),
loading: () => SliverFillRemaining(
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const CircularProgressIndicator(),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'If this takes too long, try refreshing via the menu. You may have blocked automatic popups.',
style: Theme.of(context).textTheme.labelLarge,
).animate().fadeIn(delay: const Duration(seconds: 3)),
),
],
),
),
),
error: (error, stackTrace) => SliverFillRemaining(
child: Center(
child: Text('Error: $error'),
),
));
}
}
9 changes: 2 additions & 7 deletions lib/services/drive/drive_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class DriveServiceApi {
final isAuthorised = await googleSignIn.canAccessScopes(scopes);
if (isAuthorised) return true;
return googleSignIn.requestScopes(scopes).then((isAuthorisedNow) {
debugPrint("Request result: $isAuthorisedNow");
return isAuthorisedNow;
});
}
Expand All @@ -31,10 +30,10 @@ class DriveServiceApi {

Future<List<Token>> fetchTokens() async {
final isAuthorised = await isAppAuthorised();
debugPrint("Is Authorised now: $isAuthorised");
if (!isAuthorised) {
throw Exception(
'You have not authorised the app to access your Google Drive');
'You have not authorised the app to access your Google Drive',
);
}

final client = await getClient();
Expand Down Expand Up @@ -71,7 +70,6 @@ class DriveServiceApi {
Future<Token> addToken(XFile image) async {
final client = await getClient();
final api = DriveApi(client);
debugPrint("Mime Type: ${image.mimeType}");
final name = '${DateTime.now().millisecondsSinceEpoch}';
return api.files
.create(
Expand All @@ -85,7 +83,6 @@ class DriveServiceApi {
uploadOptions: ResumableUploadOptions(),
)
.then((file) {
debugPrint("Upload success: $file");
return Token.fromFile(file);
}).catchError((error) {
debugPrint("Error: $error");
Expand All @@ -99,9 +96,7 @@ class DriveServiceApi {
final file = File();
file.properties = token.propertiesToJson();
file.description = token.encodeDescription();
debugPrint("File properties: ${file.properties}");
return api.files.update(file, token.id, $fields: fields).then((file) {
debugPrint("Updated File: $file");
return Token.fromFile(file);
}).catchError((error) {
debugPrint("Error: $error");
Expand Down
Loading

0 comments on commit 0623c3d

Please sign in to comment.