Skip to content

Commit

Permalink
Change position using drag and drop (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
lockieRichter authored Mar 3, 2024
1 parent a6b3eb4 commit 6520b13
Show file tree
Hide file tree
Showing 11 changed files with 609 additions and 291 deletions.
3 changes: 3 additions & 0 deletions ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
<key>CADisableMinimumFrameDurationOnPhone</key>
<true />
<!-- Google Sign-in Section -->
<key>GIDClientID</key>
<!-- Copied from GoogleService-Info.plist key CLIENT_ID -->
<string>150599422814-eorssa86b5b5l2p2h4ri8bhg82jqsovj.apps.googleusercontent.com</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import 'package:dantex/src/data/authentication/authentication_repository.dart';
import 'package:dantex/src/data/authentication/entity/dante_user.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:google_sign_in/google_sign_in.dart';

class FirebaseAuthenticationRepository implements AuthenticationRepository {
final FirebaseAuth _fbAuth;
final GoogleSignIn googleSignIn;

FirebaseAuthenticationRepository(this._fbAuth);
FirebaseAuthenticationRepository(this._fbAuth, this.googleSignIn);

@override
Stream<DanteUser?> get authStateChanges =>
Expand Down Expand Up @@ -89,14 +91,22 @@ class FirebaseAuthenticationRepository implements AuthenticationRepository {
}

@override
Future<UserCredential> loginWithGoogle() {
Future<UserCredential> loginWithGoogle() async {
if (kIsWeb) {
return _fbAuth.signInWithPopup(
GoogleAuthProvider(),
);
} else {
return _fbAuth.signInWithProvider(
GoogleAuthProvider(),
final googleSignInAccount = await googleSignIn.signIn();
final GoogleSignInAuthentication? googleAuth =
await googleSignInAccount?.authentication;

final credential = GoogleAuthProvider.credential(
accessToken: googleAuth?.accessToken,
idToken: googleAuth?.idToken,
);
return _fbAuth.signInWithCredential(
credential,
);
}
}
Expand Down
2 changes: 2 additions & 0 deletions lib/src/data/book/book_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,6 @@ abstract class BookRepository {
Future<void> deleteNotes(String bookId);

Future<void> saveNotes(String bookId, String notes);

Future<void> updatePositions(List<Book> books);
}
10 changes: 10 additions & 0 deletions lib/src/data/book/firebase_book_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,16 @@ class FirebaseBookRepository implements BookRepository {
Future<void> saveNotes(String bookId, String notes) {
return _booksRef().child(bookId).update({'notes': notes});
}

@override
Future<void> updatePositions(List<Book> books) async {
final Map<String, Object?> updateMap = {};
for (var i = 0; i < books.length; i++) {
final book = books[i];
updateMap[book.id] = book.copyWith(position: i).toJson();
}
await _booksRef().update(updateMap);
}
}

extension DataSnapshotExtension on DataSnapshot {
Expand Down
5 changes: 2 additions & 3 deletions lib/src/providers/authentication.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ AuthenticationRepository authenticationRepository(
) =>
FirebaseAuthenticationRepository(
ref.watch(firebaseAuthProvider),
ref.watch(googleSignInProvider),
);

@riverpod
Expand All @@ -42,13 +43,11 @@ Future<DanteUser?> user(UserRef ref) {
return ref.watch(authenticationRepositoryProvider).getAccount();
}

@riverpod
@Riverpod(keepAlive: true)
GoogleSignIn googleSignIn(
GoogleSignInRef ref,
) =>
GoogleSignIn(
clientId:
'150599422814-moto7djse1tf7vtso7slemniki76ohg6.apps.googleusercontent.com',
scopes: [
DriveApi.driveFileScope,
],
Expand Down
1 change: 1 addition & 0 deletions lib/src/ui/book/book_item_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class BookItemWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return InkWell(
borderRadius: BorderRadius.circular(12.0),
onTap: () {
context.go(
DanteRoute.bookDetail.navigationUrl.replaceAll(':bookId', _book.id),
Expand Down
65 changes: 53 additions & 12 deletions lib/src/ui/main/book_state_page.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';

import 'package:dantex/src/data/book/book_repository.dart';
import 'package:dantex/src/data/book/book_sort_strategy.dart';
import 'package:dantex/src/data/book/entity/book.dart';
import 'package:dantex/src/data/book/entity/book_state.dart';
import 'package:dantex/src/providers/book.dart';
Expand All @@ -20,6 +21,8 @@ class BookStatePage extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
return ref.watch(booksForStateProvider(_state)).when(
skipLoadingOnRefresh: true,
skipLoadingOnReload: true,
data: (data) => _BooksScreen(books: data, state: _state),
error: (error, stackTrace) => GenericErrorWidget(error),
loading: () => const Center(
Expand Down Expand Up @@ -52,26 +55,64 @@ class _BooksScreen extends ConsumerWidget {
_buildLargeLayout(bookRepository, columns: 3),
DeviceFormFactor.tablet =>
_buildLargeLayout(bookRepository, columns: 2),
DeviceFormFactor.phone => _buildPhoneLayout(
bookRepository,
),
DeviceFormFactor.phone => _buildPhoneLayout(bookRepository, ref),
};
},
);
}

Widget _buildPhoneLayout(BookRepository bookRepository) {
return ListView.separated(
padding: const EdgeInsets.all(16.0),
Widget _buildPhoneLayout(BookRepository bookRepository, WidgetRef ref) {
return ReorderableListView.builder(
proxyDecorator: (child, index, animation) {
return Material(
color: Colors.transparent,
child: Stack(
children: [
Positioned(
top: 0,
left: 0,
right: 0,
bottom: 16,
child: Material(
borderRadius: BorderRadius.circular(16),
elevation: 24,
shadowColor: Colors.black.withOpacity(0.6),
),
),
child,
],
),
);
},
padding: const EdgeInsets.symmetric(horizontal: 16.0),
physics: const BouncingScrollPhysics(),
itemCount: books.length,
itemBuilder: (context, index) => _buildItem(
books[index],
bookRepository,
useMobileLayout: true,
itemBuilder: (context, index) => Column(
key: ValueKey(books[index].id),
children: [
_buildItem(
books[index],
bookRepository,
useMobileLayout: true,
),
const SizedBox(
height: 16,
),
],
),
separatorBuilder: (BuildContext context, int index) =>
const SizedBox(height: 16),
onReorder: (oldIndex, newIndex) async {
if (ref.read(sortingStrategyProvider) != BookSortStrategy.position) {
ref
.read(sortingStrategyProvider.notifier)
.set(BookSortStrategy.position);
}
if (oldIndex < newIndex) {
newIndex -= 1;
}
final Book book = books.removeAt(oldIndex);
books.insert(newIndex, book);
await bookRepository.updatePositions(books);
},
);
}

Expand Down
46 changes: 35 additions & 11 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,30 @@ packages:
url: "https://pub.dev"
source: hosted
version: "6.7.1"
leak_tracker:
dependency: transitive
description:
name: leak_tracker
sha256: "78eb209deea09858f5269f5a5b02be4049535f568c07b275096836f01ea323fa"
url: "https://pub.dev"
source: hosted
version: "10.0.0"
leak_tracker_flutter_testing:
dependency: transitive
description:
name: leak_tracker_flutter_testing
sha256: b46c5e37c19120a8a01918cfaf293547f47269f7cb4b0058f21531c2465d6ef0
url: "https://pub.dev"
source: hosted
version: "2.0.1"
leak_tracker_testing:
dependency: transitive
description:
name: leak_tracker_testing
sha256: a597f72a664dbd293f3bfc51f9ba69816f84dcd403cdac7066cb3f6003f3ab47
url: "https://pub.dev"
source: hosted
version: "2.0.1"
lints:
dependency: transitive
description:
Expand Down Expand Up @@ -865,26 +889,26 @@ packages:
dependency: transitive
description:
name: matcher
sha256: "1803e76e6653768d64ed8ff2e1e67bea3ad4b923eb5c56a295c3e634bad5960e"
sha256: d2323aa2060500f906aa31a895b4030b6da3ebdcc5619d14ce1aada65cd161cb
url: "https://pub.dev"
source: hosted
version: "0.12.16"
version: "0.12.16+1"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
sha256: "9528f2f296073ff54cb9fee677df673ace1218163c3bc7628093e7eed5203d41"
sha256: "0e0a020085b65b6083975e499759762399b4475f766c21668c4ecca34ea74e5a"
url: "https://pub.dev"
source: hosted
version: "0.5.0"
version: "0.8.0"
meta:
dependency: transitive
description:
name: meta
sha256: a6e590c838b18133bb482a2745ad77c5bb7715fb0451209e1a7567d416678b8e
sha256: d584fa6707a52763a52446f02cc621b077888fb63b93bbcb1143a7be5a0c0c04
url: "https://pub.dev"
source: hosted
version: "1.10.0"
version: "1.11.0"
mime:
dependency: transitive
description:
Expand Down Expand Up @@ -921,10 +945,10 @@ packages:
dependency: transitive
description:
name: path
sha256: "8829d8a55c13fc0e37127c29fedf290c102f4e40ae94ada574091fe0ff96c917"
sha256: "087ce49c3f0dc39180befefc60fdb4acd8f8620e5682fe2476afd0b3688bb4af"
url: "https://pub.dev"
source: hosted
version: "1.8.3"
version: "1.9.0"
path_parsing:
dependency: transitive
description:
Expand Down Expand Up @@ -1462,10 +1486,10 @@ packages:
dependency: transitive
description:
name: web
sha256: afe077240a270dcfd2aafe77602b4113645af95d0ad31128cc02bce5ac5d5152
sha256: "4188706108906f002b3a293509234588823c8c979dc83304e229ff400c996b05"
url: "https://pub.dev"
source: hosted
version: "0.3.0"
version: "0.4.2"
web_socket_channel:
dependency: transitive
description:
Expand Down Expand Up @@ -1507,5 +1531,5 @@ packages:
source: hosted
version: "3.1.2"
sdks:
dart: ">=3.2.0 <4.0.0"
dart: ">=3.3.0 <4.0.0"
flutter: ">=3.16.0"
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ publish_to: 'none'
version: 1.0.0

environment:
sdk: '>=3.0.6 <4.0.0'
sdk: '>=3.3.0 <4.0.0'

dependencies:
flutter:
Expand Down
Loading

0 comments on commit 6520b13

Please sign in to comment.