Skip to content

Commit

Permalink
feat(#75) : refactor logic to delete reference
Browse files Browse the repository at this point in the history
  • Loading branch information
JordyHers committed Jun 8, 2023
1 parent bc846c0 commit 39bac05
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
8 changes: 0 additions & 8 deletions lib/app/pages/child_details_page.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dart:async';

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/material.dart';
import 'package:parental_control/app/helpers/parsing_extension.dart';
Expand Down Expand Up @@ -39,13 +38,6 @@ class _ChildDetailsPageState extends State<ChildDetailsPage> {
Future<void> _deleteUserPictureAndChild(
BuildContext context, ChildModel model) async {
try {
/// Deleting the user picture
if (model.image != null) {
final storageReference = FirebaseStorage.instance.refFromURL(model.image!);
await storageReference.delete();
}

// Deleting the child
await widget.database.deleteChild(model);
} on FirebaseException catch (e) {
await showExceptionAlertDialog(
Expand Down
3 changes: 2 additions & 1 deletion lib/services/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ class FirestoreDatabase implements Database {

@override
Future<void> deleteChild(ChildModel model) async {
await _service.deleteData(path: APIPath.child(uid, model.id));
await _service.deleteData(
path: APIPath.child(uid, model.id), image: model.image);
}

@override
Expand Down
11 changes: 8 additions & 3 deletions lib/services/firestore_service.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_storage/firebase_storage.dart';

class FirestoreService {
FirestoreService._();
Expand Down Expand Up @@ -37,15 +38,19 @@ class FirestoreService {
required String path,
required Map<String, dynamic> data,
}) async {
// await FirebaseFirestore.instance.collection('Notifications').
// doc().set({'message': 'HomeWork Time'});
final reference = FirebaseFirestore.instance.collection(path).doc();
print('$path: $data');
await reference.set(data);
}

Future<void> deleteData({required String path}) async {
Future<void> deleteData({required String path, String? image}) async {
final reference = FirebaseFirestore.instance.doc(path);

/// Deleting the child's picture
if (image != null) {
final storageReference = FirebaseStorage.instance.refFromURL(image);
await storageReference.delete();
}
print('delete: $path');
await reference.delete();
}
Expand Down

0 comments on commit 39bac05

Please sign in to comment.