Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle missing camera in demo #41

Merged
merged 6 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions demos/supabase-todolist/lib/attachments/camera_helpers.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import 'package:camera/camera.dart';
import 'package:flutter/widgets.dart';

late final CameraDescription? camera;
import 'package:powersync_flutter_demo/powersync.dart';

Future<CameraDescription?> setupCamera() async {
// Ensure that plugin services are initialized so that `availableCameras()`
// can be called before `runApp()`
WidgetsFlutterBinding.ensureInitialized();
// Obtain a list of the available cameras on the device.
final cameras = await availableCameras();
// Get a specific camera from the list of available cameras.
final camera = cameras.isNotEmpty ? cameras.first : null;
return camera;
try {
final cameras = await availableCameras();
// Get a specific camera from the list of available cameras.
final camera = cameras.isNotEmpty ? cameras.first : null;
return camera;
} catch (e) {
// Camera is not supported on all platforms
log.warning('Failed to setup camera: $e');
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ import 'dart:async';
import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
import 'package:powersync/powersync.dart' as powersync;
import 'package:powersync_flutter_demo/attachments/camera_helpers.dart';
import 'package:powersync_flutter_demo/attachments/queue.dart';
import 'package:powersync_flutter_demo/models/todo_item.dart';
import 'package:powersync_flutter_demo/powersync.dart';

class TakePhotoWidget extends StatefulWidget {
final String todoId;
final CameraDescription camera;

const TakePhotoWidget({super.key, required this.todoId});
const TakePhotoWidget(
{super.key, required this.todoId, required this.camera});

@override
State<StatefulWidget> createState() {
Expand All @@ -28,7 +29,7 @@ class _TakePhotoWidgetState extends State<TakePhotoWidget> {
super.initState();

_cameraController = CameraController(
camera!,
widget.camera,
ResolutionPreset.medium,
);

Expand Down
80 changes: 52 additions & 28 deletions demos/supabase-todolist/lib/attachments/photo_widget.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:powersync_flutter_demo/attachments/camera_helpers.dart';
import 'package:powersync_flutter_demo/attachments/photo_capture_widget.dart';
import 'package:powersync_flutter_demo/attachments/queue.dart';

Expand All @@ -19,31 +20,58 @@ class PhotoWidget extends StatefulWidget {
}
}

class _ResolvedPhotoState {
String? photoPath;
bool fileExists;

_ResolvedPhotoState({required this.photoPath, required this.fileExists});
}

class _PhotoWidgetState extends State<PhotoWidget> {
late String photoPath;

Future<Map<String, dynamic>> _getPhoto(photoId) async {
Future<_ResolvedPhotoState> _getPhotoState(photoId) async {
if (photoId == null) {
return {"photoPath": null, "fileExists": false};
return _ResolvedPhotoState(photoPath: null, fileExists: false);
}
photoPath = await attachmentQueue.getLocalUri('$photoId.jpg');

bool fileExists = await File(photoPath).exists();

return {"photoPath": photoPath, "fileExists": fileExists};
return _ResolvedPhotoState(photoPath: photoPath, fileExists: fileExists);
}

@override
Widget build(BuildContext context) {
return FutureBuilder(
future: _getPhoto(widget.todo.photoId),
builder: (BuildContext context, AsyncSnapshot snapshot) {
future: _getPhotoState(widget.todo.photoId),
builder: (BuildContext context,
AsyncSnapshot<_ResolvedPhotoState> snapshot) {
if (snapshot.data == null) {
return Container();
}
final data = snapshot.data!;
Widget takePhotoButton = ElevatedButton(
onPressed: () {
onPressed: () async {
final camera = await setupCamera();
if (!mounted) return;

if (camera == null) {
const snackBar = SnackBar(
content: Text('No camera available'),
backgroundColor:
Colors.red, // Optional: to highlight it's an error
);

ScaffoldMessenger.of(context).showSnackBar(snackBar);
return;
}

Navigator.push(
context,
MaterialPageRoute(
builder: (context) => TakePhotoWidget(todoId: widget.todo.id),
builder: (context) =>
TakePhotoWidget(todoId: widget.todo.id, camera: camera),
),
);
},
Expand All @@ -54,29 +82,25 @@ class _PhotoWidgetState extends State<PhotoWidget> {
return takePhotoButton;
}

if (snapshot.hasData) {
String filePath = snapshot.data['photoPath'];
bool fileIsDownloading = !snapshot.data['fileExists'];

if (fileIsDownloading) {
return const Text("Downloading...");
}

File imageFile = File(filePath);
int lastModified = imageFile.existsSync()
? imageFile.lastModifiedSync().millisecondsSinceEpoch
: 0;
Key key = ObjectKey('$filePath:$lastModified');

return Image.file(
key: key,
imageFile,
width: 50,
height: 50,
);
String? filePath = data.photoPath;
bool fileIsDownloading = !data.fileExists;

if (fileIsDownloading) {
return const Text("Downloading...");
}

return takePhotoButton;
File imageFile = File(filePath!);
int lastModified = imageFile.existsSync()
? imageFile.lastModifiedSync().millisecondsSinceEpoch
: 0;
Key key = ObjectKey('$filePath:$lastModified');

return Image.file(
key: key,
imageFile,
width: 50,
height: 50,
);
});
}
}
3 changes: 0 additions & 3 deletions demos/supabase-todolist/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:logging/logging.dart';
import 'package:powersync_flutter_demo/app_config.dart';
import 'package:powersync_flutter_demo/attachments/camera_helpers.dart';
import 'package:powersync_flutter_demo/attachments/queue.dart';
import 'package:powersync_flutter_demo/models/schema.dart';

Expand Down Expand Up @@ -38,8 +37,6 @@ void main() async {
initializeAttachmentQueue(db);
}

camera = await setupCamera();

final loggedIn = isLoggedIn();
runApp(MyApp(loggedIn: loggedIn));
}
Expand Down