Skip to content
This repository has been archived by the owner on Feb 19, 2024. It is now read-only.

Commit

Permalink
fix(jar image): fix async/await flow when adding an image to a jar an…
Browse files Browse the repository at this point in the history
…d then updating home page with

BREAKING CHANGE: refactored jar scoped model

re #107
  • Loading branch information
Scott Clampet authored and Scott Clampet committed Apr 30, 2019
1 parent 5e78f8e commit 92908a8
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 49 deletions.
1 change: 0 additions & 1 deletion fude_app/lib/pages/home/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class HomePage extends StatefulWidget {
class _HomePageState extends State<HomePage> {
@override
initState() {
print('home init state. CurrUserEmail: ${widget.model.currUserEmail}');
widget.model.fetchAllUserJars(widget.model.currUserEmail);
super.initState();
}
Expand Down
23 changes: 12 additions & 11 deletions fude_app/lib/pages/home/home_page_jar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class HomePageJar extends StatelessWidget {
iconSize: 26,
color: Theme.of(context).textTheme.subhead.color,
onPressed: () {
print('jar pressed, ${jar['id']}');
// print('jar pressed, ${jar['id']}');
model.getJarBySelectedId(jar.documentID);
Navigator.push(
context,
Expand Down Expand Up @@ -126,19 +126,20 @@ class HomePageJar extends StatelessWidget {

@override
Widget build(BuildContext context) {
Image jarImage = Image(
image: NetworkImage(jar != null
? jar.data['image']
: 'https://firebasestorage.googleapis.com/v0/b/fude-app.appspot.com/o/Scoot-01.png?alt=media&token=53fc26de-7c61-4076-a0cb-f75487779604'),
fit: BoxFit.cover,
alignment: FractionalOffset(
0.5,
0.5,
),
);
var image = title == null
? ClipRRect(
borderRadius: BorderRadius.circular(30.0),
child: Image.network(
jar['image'] != null
? jar['image']
: 'https://firebasestorage.googleapis.com/v0/b/fude-app.appspot.com/o/Scoot-01.png?alt=media&token=53fc26de-7c61-4076-a0cb-f75487779604',
fit: BoxFit.cover,
alignment: FractionalOffset(
0.5,
0.5,
),
),
child: jarImage,
)
: Container();

Expand Down
46 changes: 25 additions & 21 deletions fude_app/lib/pages/jars/jar_add.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:scoped_model/scoped_model.dart';
import 'dart:io';
Expand Down Expand Up @@ -29,7 +31,7 @@ class _JarPageState extends State<AddJarPage> {
formKey.currentState.save(); // Save our form now.
print(_formData);
model.addJar(_formData);
model.fetchAllUserJars(model.currUserEmail);

Navigator.pop(context);
}
}
Expand Down Expand Up @@ -116,26 +118,28 @@ class _JarPageState extends State<AddJarPage> {
SizedBox(
width: width * 0.055,
),
RaisedButton(
child: Text(
'ADD JAR',
style: TextStyle(
color: Theme.of(context).primaryColor,
fontWeight: FontWeight.bold,
fontSize: 15,
letterSpacing: 5,
),
),
elevation: 7,
highlightElevation: 1,
padding: EdgeInsets.all(15),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0)),
color: Theme.of(context).secondaryHeaderColor,
splashColor: Colors.transparent,
highlightColor: Theme.of(context).primaryColor,
onPressed: () => addJar(model),
),
!model.isLoading
? RaisedButton(
child: Text(
'ADD JAR',
style: TextStyle(
color: Theme.of(context).primaryColor,
fontWeight: FontWeight.bold,
fontSize: 15,
letterSpacing: 5,
),
),
elevation: 7,
highlightElevation: 1,
padding: EdgeInsets.all(15),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0)),
color: Theme.of(context).secondaryHeaderColor,
splashColor: Colors.transparent,
highlightColor: Theme.of(context).primaryColor,
onPressed: () => addJar(model),
)
: CircularProgressIndicator(),
],
)
],
Expand Down
1 change: 0 additions & 1 deletion fude_app/lib/pages/jars/jar_edit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ class _JarPageState extends State<EditJarPage> {
onPressed: () {
model.deleteJar();
model.fetchAllUserJars(model.currUserEmail);

Navigator.pop(context);
})
],
Expand Down
46 changes: 31 additions & 15 deletions fude_app/lib/scoped-models/jar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ mixin JarModel on Model {

void addJar(Map<String, dynamic> data) async {
print('in model.addJar: data: $data');
_isLoading = true;
notifyListeners();

CollectionReference jarCollection = _firestore.collection('jars');
Uri imageLocation;
String imageLocation;
try {
if (data['image'] != null) {
imageLocation = await uploadNoteImageToStorage(data['image']);
Expand All @@ -59,6 +62,8 @@ mixin JarModel on Model {
: imageLocation.toString(),
'isFav': false
});
fetchAllUserJars(user.email);
_isLoading = false;
notifyListeners();
} catch (e) {
print(e);
Expand All @@ -67,7 +72,7 @@ mixin JarModel on Model {

void updateJar(Map<String, dynamic> data) async {
print('updateJar Data: $data');
Uri imageLocation;
String imageLocation;
try {
if (data['image'] != null) {
imageLocation = await uploadNoteImageToStorage(data['image']);
Expand Down Expand Up @@ -146,7 +151,7 @@ mixin JarModel on Model {
void addToJar(String category, String title, String notes, String link,
File image) async {
print('image: ----------- $image');
Uri imageLocation;
String imageLocation;
try {
if (image != null) {
imageLocation = await uploadNoteImageToStorage(image);
Expand All @@ -165,33 +170,39 @@ mixin JarModel on Model {
'image':
imageLocation == null ? _selJar['image'] : imageLocation.toString(),
});

notifyListeners();
} catch (e) {
print(e);
}
}

Future<Uri> uploadNoteImageToStorage(File image) async {
Future<String> uploadNoteImageToStorage(File image) async {
final StorageReference ref =
FirebaseStorage.instance.ref().child('images/');
//Upload the file to firebase
StorageUploadTask uploadTask = ref.putFile(image);
// Waits till the file is uploaded then stores the download url
Uri location = await uploadTask.onComplete.then((val) {
val.ref.getDownloadURL().then((val) {
print(val);
return val; //Val here is Already String
String location;
try {
await uploadTask.onComplete.then((val) async {
await val.ref.getDownloadURL().then((val) {
// print('VAL $val, type: ${val.runtimeType}');
location = val;
});
});
});
} catch (e) {
print(e);
}
// print('location: $location -------');
//returns the download url
print(location);
return location;
}

void updateNote(DocumentSnapshot note, String category, String title,
String notes, String link, File image) async {
print('$category, $title, $notes, $link, $image');
Uri imageLocation;
String imageLocation;
try {
if (image != null) {
imageLocation = await uploadNoteImageToStorage(image);
Expand Down Expand Up @@ -256,8 +267,10 @@ mixin JarModel on Model {
await _firestore
.collection('jars')
.document(_selJar.documentID)
.updateData({'owners' :
!_selJar.data['owners'].contains(email) ? FieldValue.arrayUnion([email]): FieldValue.arrayUnion([])
.updateData({
'owners': !_selJar.data['owners'].contains(email)
? FieldValue.arrayUnion([email])
: FieldValue.arrayUnion([])
});
} catch (e) {
print(e);
Expand All @@ -272,6 +285,8 @@ mixin JarModel on Model {
}

Future<List<dynamic>> fetchAllUserJars(String email) async {
_isLoading = true;
notifyListeners();
_usersJars = [_addJar];
QuerySnapshot jars;
try {
Expand All @@ -282,11 +297,12 @@ mixin JarModel on Model {
jars.documents.forEach((jar) {
_usersJars.insert(1, jar);
});
print('NEW LIST: $_usersJars');
notifyListeners();
// print('NEW LIST: $_usersJars');
} catch (e) {
print(e);
}
_isLoading = false;
notifyListeners();
return _usersJars;
}

Expand Down

0 comments on commit 92908a8

Please sign in to comment.