Skip to content

Commit

Permalink
Fix problems
Browse files Browse the repository at this point in the history
  • Loading branch information
naclcaleb committed Dec 23, 2023
1 parent be8bd19 commit 8f09f9f
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 41 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
Clone this repository into your existing Flutter app, and rename the folder to `lib`.

Then, add the following dependencies to your Flutter project:
```bash
flutter pub add go_router get_it provider firebase_core firebase_auth firebase_storage shared_preferences image_picker uuid http feather_icons emoji_picker_flutter cached_network_image
```
25 changes: 0 additions & 25 deletions model/services/camera_service.dart

This file was deleted.

91 changes: 91 additions & 0 deletions model/services/media_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import 'dart:developer';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import '../auth/auth_controller.dart';
import '../error_handlers/info_exception.dart';
import '../../service_locator.dart';
import 'package:uuid/uuid.dart';
import '../../components/system_alert.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'error_service.dart';

class MediaService {

final FirebaseStorage _storage = sl();
final AuthController _authController = sl();
final ErrorService _errorService = sl();

final Uuid uuid = const Uuid();

Future<XFile?> getUserChosenVideo(BuildContext context) async {

//Get the image picker
final ImagePicker picker = ImagePicker();

final XFile? video = await picker.pickVideo(source: ImageSource.gallery);

return video;

}

Future<File?> getUserChosenImage(BuildContext context) async {

//Open the image picker
final ImagePicker picker = ImagePicker();

bool? usingCamera = await SophiaAlert.showPlatformDialog(context, 'Choose a file source', 'Would you like to select from your photo gallery or take a new picture?', [
const AlertAction(buttonText: 'Camera', returnValue: true, type: AlertActionType.normal),
const AlertAction(buttonText: 'Gallery', returnValue: false, type: AlertActionType.normal),
]);

if (usingCamera == null) return null;

final XFile? image = await picker.pickImage(source: usingCamera ? ImageSource.camera : ImageSource.gallery);

if (image == null) return null;

final file = File(image.path);

return file;

}

Future<String?> uploadFile(File mediaFile, String path, [void Function(TaskSnapshot taskSnapshot)? streamListener, String contentType = 'image/jpeg']) async {
final itemRef = _storage.ref().child(path);
final uploadTask = itemRef.putFile(mediaFile, SettableMetadata(
contentType: contentType
));

//Allow for monitoring upload progress
uploadTask.snapshotEvents.listen((TaskSnapshot snapshot) {

if (snapshot.state == TaskState.error) {
_errorService.receiveError(const InfoException('Error uploading file'));
}

streamListener?.call(snapshot);

});

final snapshot = await uploadTask.whenComplete(() => null);

if (snapshot.state == TaskState.error) {
return null;
}

final url = await snapshot.ref.getDownloadURL();

return url;

}

String uniqueFilename() {
return uuid.v4();
}

String getUserStoragePath() {
return 'users/${_authController.getCurrentUserId()}';
}

}
33 changes: 33 additions & 0 deletions model/services/navigation_chain_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import 'dart:collection';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import '../../pages/base_page.dart';

class NavigationChainService {

final Queue<String> _currentNavigationChain = Queue();

void triggerNavigationChain(BuildContext context, List<String> routes) {
_currentNavigationChain.clear();
_currentNavigationChain.addAll(routes);

_triggerNextNavigation(context);
}

void _triggerNextNavigation(BuildContext context) {
if (_currentNavigationChain.isNotEmpty) {
WidgetsBinding.instance.addPostFrameCallback((_) {
context.push(_currentNavigationChain.removeFirst());
});
}
}

void registerPage(BuildContext context) {
//If we're a root page (i.e. there are no SophiaPages above us), navigate to the next route
if (BasePageParentIndicator.maybeOf(context) == null && context.mounted) {
_triggerNextNavigation(context);
}
}

}
18 changes: 18 additions & 0 deletions pages/base_tab_view.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import '../components/basic/tab_bar.dart';

class BaseTabView extends StatelessWidget {

final StatefulNavigationShell navigationShell;

const BaseTabView({super.key, required this.navigationShell});

@override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: TabBar(currentIndex: navigationShell.currentIndex, navigationShell: navigationShell,),
body: navigationShell,
);
}
}
8 changes: 4 additions & 4 deletions routes.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'pages/base_tab_view.dart';
import 'pages/home/home_page.dart';
import 'pages/profile/profile_page_tab_wrapper.dart';
import 'pages/tab1/tab1_page.dart';
import 'pages/tab2/tab2_page.dart';
import 'splash_screen.dart';

final GlobalKey<NavigatorState> _rootNavigatorKey = GlobalKey<NavigatorState>();
Expand All @@ -19,11 +19,11 @@ final mainRouter = GoRouter(

//Routes for tabs go in here
StatefulShellBranch(navigatorKey: _tab1NavigatorKey, routes: [
GoRoute(path: '/tab1', builder: (context, state) => const HomePage()),
GoRoute(path: '/tab1', builder: (context, state) => const Tab1Page()),
]),

StatefulShellBranch(navigatorKey: _tab2NavigatorKey, routes: [
GoRoute(path: '/tab2', builder: (context, state) => ProfilePageTabWrapper()),
GoRoute(path: '/tab2', builder: (context, state) => const Tab2Page()),
])

],
Expand Down
12 changes: 0 additions & 12 deletions service_locator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,11 @@ import 'model/auth/auth_controller.dart';
import 'model/error_handlers/error_service_plugins/api_error_plugin.dart';
import 'model/managers/cached_loadable_manager.dart';
import 'model/managers/cached_paginated_loadable_manager.dart';
import 'model/managers/category_manager.dart';
import 'model/managers/post_manager.dart';
import 'model/managers/user_manager.dart';
import 'model/services/auth_service.dart';
import 'model/managers/group_manager.dart';
import 'model/services/camera_service.dart';
import 'model/services/error_service.dart';
import 'model/services/media_service.dart';
import 'model/services/navigation_chain_service.dart';
import 'model/services/notification_service.dart';
import 'model/services/search_service.dart';

import 'model/error_handlers/error_service_plugins/firebase_auth_error_plugin.dart';
import 'model/error_handlers/error_service_plugins/info_exception_plugin.dart';
Expand All @@ -42,20 +36,14 @@ void initLocator() {
sl.registerLazySingleton(() => Server( sl() ));

sl.registerLazySingleton(() => AuthService(FirebaseAuth.instance, sl<Server>()));
sl.registerLazySingleton(() => SearchService());

sl.registerLazySingleton(() => NotificationService());
sl.registerLazySingleton(() => MediaService());
sl.registerLazySingleton(() => CameraService());
sl.registerLazySingleton(() => PreferencesService());
sl.registerLazySingleton(() => NavigationChainService());
sl.registerLazySingleton(() => GlobalAppThemeConfig());

//Managers
sl.registerLazySingleton(() => GroupManager(sl<Server>()));
sl.registerLazySingleton(() => PostManager(sl<Server>()));
sl.registerLazySingleton(() => UserManager(sl<Server>()));
sl.registerLazySingleton(() => CategoryManager());
sl.registerLazySingleton(() => CachedPaginatedLoadableManager());
sl.registerLazySingleton(() => CachedLoadableManager());

Expand Down

0 comments on commit 8f09f9f

Please sign in to comment.