Skip to content

Commit

Permalink
feature/add-new-home-screen (#7)(#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
codewithJosh authored Nov 25, 2022
2 parents 444c203 + 3a36d6f commit c603a33
Show file tree
Hide file tree
Showing 9 changed files with 562 additions and 73 deletions.
204 changes: 204 additions & 0 deletions lib/adapters/update_adapter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
import 'package:cached_network_image/cached_network_image.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:news_expose_2k21/comment_screen.dart';
import 'package:news_expose_2k21/functions.dart';
import 'package:news_expose_2k21/models/user_model.dart';

class Update extends StatefulWidget {
final String updateId;
final String updateImage;
final String updateContent;
final Timestamp updateTimestamp;
final String userId;
final Map seen;

const Update(
{Key? key,
required this.updateId,
required this.updateImage,
required this.updateContent,
required this.updateTimestamp,
required this.userId,
required this.seen})
: super(key: key);

factory Update.fromDocument(final documentSnapshot) => Update(
updateId: documentSnapshot['update_id'],
updateImage: documentSnapshot['update_image'],
updateContent: documentSnapshot['update_content'],
updateTimestamp: documentSnapshot['update_timestamp'],
userId: documentSnapshot['user_id'],
seen: documentSnapshot['Seen'],
);

seenCount(final seen) {
if (seen == null) {
return 0;
} else {
int count = 0;
seen.values.forEach((eachValue) {
if (eachValue == true) {
count++;
}
});
return count;
}
}

@override
State<Update> createState() => _UpdateState();
}

class _UpdateState extends State<Update> {
late final _updateId = widget.updateId;
late final _updateImage = widget.updateImage;
late final _updateContent = widget.updateContent;
late final _updateTimestamp = widget.updateTimestamp;
late final _userId = widget.userId;
late final _seen = widget.seen;
late int _seenCount = widget.seenCount(_seen);
bool _isSeen = false;

_initHead() => Padding(
padding: const EdgeInsets.only(top: 8.0),
child: FutureBuilder(
future: usersRef.doc(_userId).get(),
builder: (context, dataSnapshot) {
if (!dataSnapshot.hasData) {
return buildCircularProgress();
}

final user = User.fromDocument(dataSnapshot.data);
return ListTile(
leading: user.userImage!.isNotEmpty
? CircleAvatar(
radius: 25.0,
backgroundImage:
CachedNetworkImageProvider(user.userImage!),
backgroundColor: colorEerieBlack,
)
: CircleAvatar(
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(22.0),
gradient: linearProfile,
),
),
),
title: initTitle2(user.userBio,
size: 17.0, fontWeight: FontWeight.bold, fontFamily: ''),
subtitle: initTitle2(
'${initUpdateTimestamp(_updateTimestamp)} · ${user.userName}'),
);
},
),
);

_initBody() => Column(children: <Widget>[
_updateContent.isNotEmpty
? Container(
alignment: Alignment.topLeft, child: initTitle2(_updateContent))
: Container(),
const SizedBox(
height: 10.0,
),
GestureDetector(
onDoubleTap: () => _onSeen(), child: Image.network(_updateImage)),
]);

_initFoot(final context) => Padding(
padding: const EdgeInsets.symmetric(vertical: 18.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
GestureDetector(
onTap: () => _onSeen(),
child: Row(
children: <Widget>[
SizedBox(
width: 30.0,
height: 25.0,
child: SvgPicture.string(
_isSeen ? createSeenUIButton : createUnseenUIButton,
allowDrawingOutsideViewBox: true,
),
),
const SizedBox(
width: 18.0,
),
initTitle2('$_seenCount',
size: 17.0, color: _isSeen ? colorFulvous : Colors.white),
],
),
),
GestureDetector(
onTap: () => _onComment(
context,
_updateId,
),
child: Row(
children: <Widget>[
SizedBox(
width: 25.0,
height: 25.0,
child: SvgPicture.string(
createCommentUIButton,
allowDrawingOutsideViewBox: true,
),
),
const SizedBox(
width: 18.0,
),
initTitle2('0', size: 17.0),
],
),
),
],
),
);

_onSeen() {
updatesRef.doc(_updateId).update({'Seen.$userId': !_isSeen});

setState(() {
_seenCount += _isSeen ? -1 : 1;
_isSeen = !_isSeen;
});
}

_onComment(final context, final updateId) =>
Navigator.push(context, MaterialPageRoute(builder: (context) => CommentScreen(
updateId: updateId,
)));

@override
void initState() {
super.initState();
_isSeen = _seen[userId] == true;
}

@override
Widget build(BuildContext context) => Container(
margin: const EdgeInsets.all(18.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(25.0),
color: colorEerieBlack,
),
child: Column(
children: <Widget>[
_initHead(),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 18.0),
child: Column(
children: <Widget>[
_initBody(),
_initFoot(context),
],
),
),
],
),
);
}
17 changes: 17 additions & 0 deletions lib/comment_screen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'package:flutter/material.dart';

class CommentScreen extends StatefulWidget {
final String updateId;

const CommentScreen({Key? key, required this.updateId}) : super(key: key);

@override
State<CommentScreen> createState() => _CommentScreenState();
}

class _CommentScreenState extends State<CommentScreen> {
@override
Widget build(BuildContext context) {
return Container();
}
}
74 changes: 33 additions & 41 deletions lib/create_update_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class CreateUpdateScreen extends StatefulWidget {
}

class _CreateUpdateScreenState extends State<CreateUpdateScreen> {
File? _uri;
late String _content;
late File _uri = widget.uri;
late String _updateContent;
bool _isUploading = false;
int _pendingRequests = 0;

Expand Down Expand Up @@ -118,56 +118,48 @@ class _CreateUpdateScreenState extends State<CreateUpdateScreen> {
);

_onCreateUpdate(final context) async {
if (_uri != null) {
setState(() {
_isUploading = true;
_pendingRequests++;
});
setState(() {
_isUploading = true;
_pendingRequests++;
});

buildFlutterToast('Currently Uploading Please Wait', colorFulvous,
isLong: true);
buildFlutterToast('Currently Uploading Please Wait', colorFulvous,
isLong: true);

if (_pendingRequests == 1) {
final ref = firebaseStorage
.ref()
.child('Updates')
.child(randomAlphaNumeric(9) + extension(_uri!.path));
if (_pendingRequests == 1) {
final ref = firebaseStorage
.ref()
.child('Updates')
.child(randomAlphaNumeric(9) + extension(_uri.path));

final uploadTask = ref.putFile(_uri!);
final uploadTask = ref.putFile(_uri);

final updateImage =
await (await uploadTask.whenComplete(() {})).ref.getDownloadURL();
final updateImage =
await (await uploadTask.whenComplete(() {})).ref.getDownloadURL();

if (updateImage.isNotEmpty) {
final updateId = updates.doc().id;
if (updateImage.isNotEmpty) {
final updateId = updatesRef.doc().id;

addUpdates() async {
final documentSnapshot = await updates.doc(updateId).get();
addUpdates() async {
final documentSnapshot = await updatesRef.doc(updateId).get();

if (!documentSnapshot.exists) {
updates.doc(updateId).set({
'update_id': updateId,
'update_image': updateImage,
'update_content': _content,
'update_timestamp': Timestamp.now(),
'user_id': userId,
'Seen': {},
});
}
if (!documentSnapshot.exists) {
updatesRef.doc(updateId).set({
'update_id': updateId,
'update_image': updateImage,
'update_content': _updateContent,
'update_timestamp': Timestamp.now(),
'user_id': userId,
'Seen': {},
});
}

addUpdates().then((value) => Navigator.pop(context));
}

addUpdates().then((value) => Navigator.pop(context));
}
}
}

@override
void initState() {
super.initState();
_uri = widget.uri;
}

@override
Widget build(BuildContext context) {
final height = MediaQuery.of(context).size.height;
Expand Down Expand Up @@ -218,7 +210,7 @@ class _CreateUpdateScreenState extends State<CreateUpdateScreen> {
child: ClipRRect(
borderRadius: BorderRadius.circular(6),
child: Image.file(
_uri!,
_uri,
fit: BoxFit.cover,
),
),
Expand Down Expand Up @@ -257,7 +249,7 @@ class _CreateUpdateScreenState extends State<CreateUpdateScreen> {
),
),
onChanged: (input) =>
_content = input.trim(),
_updateContent = input.trim(),
textInputAction: TextInputAction.newline,
),
),
Expand Down
Loading

0 comments on commit c603a33

Please sign in to comment.