Skip to content

Commit

Permalink
Create community done. Registration time username regex check fixed.
Browse files Browse the repository at this point in the history
Signed-off-by: Nishchal Siddharth Pandey <nisiddharth@gmail.com>
  • Loading branch information
nisiddharth committed Oct 18, 2020
1 parent 1a3b2b4 commit 58803d8
Show file tree
Hide file tree
Showing 3 changed files with 246 additions and 63 deletions.
261 changes: 224 additions & 37 deletions lib/app_screens/create_community.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import 'dart:io';

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

class CreateGroup extends StatefulWidget {
@override
Expand All @@ -16,6 +20,10 @@ class _CreateGroupState extends State<CreateGroup> {
int _radioVisibility = 0;
int _radioPosts = 0;
int _radioVerification = 0;
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
String noticeText = "Add Cover Photo";
int mediaType = 0; // 0 for none, 1 for image, 2 for video
File media;

void _handleRadioValueChange1(int value) {
setState(() {
Expand Down Expand Up @@ -48,6 +56,7 @@ class _CreateGroupState extends State<CreateGroup> {
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
appBar: AppBar(
title: Text(
'Create Community',
Expand Down Expand Up @@ -105,24 +114,53 @@ class _CreateGroupState extends State<CreateGroup> {
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
),
FlatButton(
child: Row(
children: <Widget>[
Icon(
Icons.add_box,
size: 24,
),
Padding(
padding: const EdgeInsets.only(left: 8.0),
child: Text(
'Add Cover Photo',
style: TextStyle(fontSize: 18),
mediaType == 0
? FlatButton(
child: Row(
children: <Widget>[
Icon(
Icons.add_box,
size: 24,
),
Padding(
padding: const EdgeInsets.only(left: 8.0),
child: Text(
noticeText,
style: TextStyle(fontSize: 18),
),
)
],
),
onPressed: () {
_showImagePicker(context);
},
)
],
),
onPressed: () {},
),
: Row(
children: <Widget>[
SizedBox(
width: 15,
),
Text(
'$noticeText',
style: TextStyle(
fontSize: 18.0,
fontWeight: FontWeight.bold,
),
),
Spacer(),
IconButton(
icon: Icon(Icons.cancel),
onPressed: () {
setState(() {
noticeText = "Add Cover Photo";
mediaType = 0;
photoUrl = null;
media = null;
});
},
)
],
),
Padding(
padding: EdgeInsets.all(8.0),
child: Container(
Expand Down Expand Up @@ -380,31 +418,180 @@ class _CreateGroupState extends State<CreateGroup> {
);
}

createCommunity() {
username = FirebaseAuth.instance.currentUser.displayName;
createCommunity() async {
String commName = nameController.text;
String about = aboutController.text;
int time = DateTime.now().millisecondsSinceEpoch;
List<String> nameSearchList = List();
String temp = "";
for (int i = 0; i < commName.length; i++) {
temp = temp + commName[i];
nameSearchList.add(temp);
if (commName.isEmpty || RegExp("[^a-z^A-Z^0-9]+").hasMatch(commName) || commName.length < 3 || commName.length > 25) {
await showAlertDialog(context);
return;
}
_scaffoldKey.currentState.showSnackBar(SnackBar(
behavior: SnackBarBehavior.floating,
duration: Duration(hours: 1),
content: Row(
children: <Widget>[
CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(Colors.grey),
),
SizedBox(
width: 15,
),
Text("Uploading...")
],
),
));
try {
username = FirebaseAuth.instance.currentUser.displayName;
FirebaseFirestore instance = FirebaseFirestore.instance;

final snapShot =
await instance.collection('communities').doc(commName).get();
if (snapShot != null && snapShot.exists) {
_scaffoldKey.currentState.hideCurrentSnackBar();
print("Failed, community name exists.");
_scaffoldKey.currentState.showSnackBar(SnackBar(
behavior: SnackBarBehavior.floating,
content: Text('Community name already exists. Try different.'),
));
return;
}

String about = aboutController.text;
int time = DateTime.now().millisecondsSinceEpoch;

if (mediaType == 1) await uploadMedia(commName);

List<String> nameSearchList = List();
String temp = "";
for (int i = 0; i < commName.length; i++) {
temp = temp + commName[i];
nameSearchList.add(temp);
}

instance.collection('communities').doc(commName).set({
'name': commName,
'about': about,
'photoUrl': photoUrl,
'privacy': _radioPrivacy,
'visibility': _radioVisibility,
'posts': _radioPosts,
'verification': _radioVerification,
'nameSearch': nameSearchList,
'creationTime': time,
});

instance.collection('communities/$commName/admins').doc(username).set({});

_scaffoldKey.currentState.hideCurrentSnackBar();
_scaffoldKey.currentState.showSnackBar(SnackBar(
behavior: SnackBarBehavior.floating,
content: Text('Community creation done.'),
));
} catch (e) {
print(e.toString());
_scaffoldKey.currentState.hideCurrentSnackBar();
_scaffoldKey.currentState.showSnackBar(SnackBar(
behavior: SnackBarBehavior.floating,
content: Text('Community creation failed.'),
));
}
}

FirebaseFirestore instance = FirebaseFirestore.instance;
instance.collection('communities').doc(commName).set({
'name': commName,
'about': about,
'photoUrl': photoUrl,
'privacy': _radioPrivacy,
'visibility': _radioVisibility,
'posts': _radioPosts,
'verification': _radioVerification,
'nameSearch': nameSearchList,
'creationTime': time,
_showImagePicker(context) {
showModalBottomSheet(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(top: Radius.circular(15.0)),
),
context: context,
builder: (BuildContext bc) {
return SafeArea(
child: Container(
child: Wrap(
children: [
ListTile(
leading: Icon(Icons.photo_library),
title: Text('From Gallery'),
onTap: () {
_imageFromGallery();
Navigator.of(context).pop();
},
),
ListTile(
leading: Icon(Icons.photo_camera),
title: Text('Camera'),
onTap: () {
_imageFromCamera();
Navigator.of(context).pop();
},
),
],
)));
});
}

_imageFromGallery() async {
final file = await ImagePicker().getImage(
source: ImageSource.gallery,
maxHeight: 1024,
maxWidth: 1024,
imageQuality: 90);
if (file == null) return null;
media = File(file.path);
setState(() {
noticeText = "Image selected for upload!";
mediaType = 1;
});
}

instance.collection('communities/$commName/admins').doc(username).set({});
_imageFromCamera() async {
final file = await ImagePicker().getImage(
source: ImageSource.camera,
maxHeight: 1024,
maxWidth: 1024,
imageQuality: 90);
if (file == null) return null;
media = File(file.path);
setState(() {
noticeText = "Image selected for upload!";
mediaType = 1;
});
}

uploadMedia(String key) async {
StorageUploadTask uploadTask;
if (mediaType == 1)
uploadTask = FirebaseStorage.instance
.ref()
.child('coverPhotos/$key.jpg')
.putFile(media);
StorageTaskSnapshot storageSnap = await uploadTask.onComplete;
photoUrl = await storageSnap.ref.getDownloadURL();
debugPrint("Successful media upload!");
return photoUrl;
}

showAlertDialog(BuildContext context) async {
Widget okButton = FlatButton(
child: Text("OK"),
onPressed: () {
Navigator.of(context).pop();
},
);

// set up the AlertDialog
AlertDialog alert = AlertDialog(
title: Text("Community name should be alphanumeric"),
content: Text("No special characters or spaces allowed. Length should be between 3 and 25 (inclusive)"),
actions: [
okButton,
],
);

// show the dialog
await showDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
);
}
}
3 changes: 3 additions & 0 deletions lib/app_screens/create_post.dart
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,9 @@ class _CreatePostState extends State<CreatePost> {

_showImagePicker(context) {
showModalBottomSheet(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(top: Radius.circular(15.0)),
),
context: context,
builder: (BuildContext bc) {
return SafeArea(
Expand Down
Loading

0 comments on commit 58803d8

Please sign in to comment.