import 'dart:typed_data'; import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:provider/provider.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:teso/Classes/API%20Clasess/Post.dart'; import 'package:teso/Classes/API%20Clasess/PostFav.dart'; import 'package:teso/Classes/Firebase/Posts.dart'; import 'package:teso/Classes/TesoUser.dart'; import 'package:teso/Pages/Sub_Pages/Posts/deletePost.dart'; import 'package:teso/Services/uservideo_controller_service.dart'; import 'package:teso/blocs/video_player/uservideo_player_bloc.dart'; import 'package:teso/blocs/video_player/uservideo_player_event.dart'; import 'package:teso/blocs/video_player/uservideo_player_state.dart'; import 'package:teso/providers/user_provider.dart'; import 'package:teso/util/SizeConfig.dart'; import 'package:numeral/numeral.dart'; import 'package:teso/GeneralWidgets/widgets/uservideo_player_widget.dart'; import 'comment.dart'; // ignore: must_be_immutable class UserPosts extends StatefulWidget { FBPosts postedAd; UserPosts({Key key, this.postedAd}) : super(key: key); @override _UserPostsState createState() => _UserPostsState(); } class _UserPostsState extends State { bool favoured = false; Uint8List imageBitmap; var document; var userDoc; bool likeShow = false; void sharing() async { await rootBundle .load("assets/images/rawLogoOverlay.png") .then((value) => setState(() { imageBitmap = value.buffer.asUint8List(); })); Provider.of(context, listen: false).downloadVideo( widget.postedAd.postID, widget.postedAd.playbackID, widget.postedAd.rendition, imageBitmap, context); } @override void dispose() { rootBundle.evict("assets/images/rawLogoOverlay.png"); super.dispose(); } void commentsDialog(BuildContext context) { if (userDoc == null) { FirebaseFirestore.instance .collection("users") .doc(widget.postedAd.publisherID) .get() .then((value) { setState(() { userDoc = value.data(); }); }); } else { showModalBottomSheet( context: context, shape: RoundedRectangleBorder( borderRadius: BorderRadius.vertical(top: Radius.circular(20.0)), ), builder: (BuildContext bc) { return ClipRRect( borderRadius: BorderRadius.only( topLeft: Radius.circular(20.0), topRight: Radius.circular(20.0), ), child: CommentSection( postedAd: new Post( aspect: widget.postedAd.aspect, assetID: widget.postedAd.assetID, playbackID: widget.postedAd.playbackID, postID: widget.postedAd.postID, publisherID: widget.postedAd.publisherID, title: widget.postedAd.title, rendition: widget.postedAd.rendition, timestamp: widget.postedAd.timestamp, ), user: TesoUser( username: userDoc["username"], userGUID: userDoc["id"], )), ); }, ); } } void likePost() { setState(() { likeShow = true; }); SharedPreferences.getInstance().then((value) { String cid = value.getString("id"); PostFav liked = new PostFav(); liked.admirerId = cid; liked.countId = DateTime.now().toString() + "$cid"; liked.timestamp = DateTime.now().toIso8601String(); liked.postId = widget.postedAd.postID; setState(() { widget.postedAd.likes++; favoured = true; }); Provider.of(context, listen: false).addLike(liked); }); } void dislikePost() { SharedPreferences.getInstance().then((value) { // String cid = value.getString("id"); setState(() { widget.postedAd.likes--; favoured = false; }); Provider.of(context, listen: false) .deleteLike(widget.postedAd.postID); }); } @override void initState() { super.initState(); _likedListen(); FirebaseFirestore.instance .collection("posts") .doc(widget.postedAd.postID) .get() .then((value) { setState(() { document = value.data(); }); }); FirebaseFirestore.instance .collection("users") .doc(widget.postedAd.publisherID) .get() .then((value) { setState(() { userDoc = value.data(); }); }); // SharedPreferences.getInstance().then((value) { // String cid = value.getString("id"); // if (widget.postedAd.likes // .map((e) => e.admirerId) // .toList() // .contains(cid)) { // setState(() { // favoured = true; // }); // } // }); // _future = initializeController(); } _likedListen() { SharedPreferences.getInstance().then((value) { String cid = value.getString("id"); FirebaseFirestore.instance .collection("posts") .doc(widget.postedAd.postID) .collection("likes") .snapshots() .listen((event) { setState(() { favoured = event.docs.any((element) => element.data()["admirerID"] == cid); widget.postedAd.likes = event.docs.length; }); }); }); } @override Widget build(BuildContext context) { return Scaffold( appBar: PreferredSize( child: AppBar( automaticallyImplyLeading: false, backgroundColor: Colors.transparent, leading: _backWidget(context), ), preferredSize: Size.fromHeight(40)), extendBodyBehindAppBar: true, body: Container( width: MediaQuery.of(context).size.width, height: MediaQuery.of(context).size.height, color: Colors.black, child: Stack( children: [ _buildVideoPlayer(), Align( alignment: Alignment.bottomRight, child: Container( margin: EdgeInsets.only( right: 10, ), width: 50, height: MediaQuery.of(context).size.width * 0.7, child: Column( children: [ _favoriteWidget(context), SizedBox( height: 20, ), _commentWidget(context), SizedBox( height: 20, ), Container( height: 30, child: InkWell( onTap: () => sharing(), child: Icon( Icons.share, size: 30, color: Colors.white, ), ), ), SizedBox( height: 20, ), _deleteWidget(context), ], ), ), ), _nameDescription(context), ], ), ), ); } Widget _buildVideoPlayer() { return BlocProvider( create: (context) => VideoPlayerBloc( RepositoryProvider.of(context)) ..add(VideoSelectedEvent(widget.postedAd)), child: BlocBuilder( builder: (context, state) { return Container(child: _getPlayer(context, state)); }, ), ); } Widget _getPlayer(BuildContext context, VideoPlayerState state) { // final screenWidth = MediaQuery.of(context).size.width; // final containerHeight = screenWidth / ASPECT_RATIO; if (state is VideoPlayerStateLoaded) { return GestureDetector( onDoubleTap: () { if (widget.postedAd.campaignID != null) { if (favoured) { return null; } else { likePost(); } } else { if (favoured) { dislikePost(); } else { likePost(); } } }, child: Stack( children: [ VideoPlayerWidget( key: Key(state.video.playbackID), controller: state.controller, ad: widget.postedAd, ), AnimatedOpacity( opacity: likeShow ? 1 : 0, duration: Duration(seconds: 2), onEnd: () { setState(() { likeShow = false; }); }, child: Container( width: double.infinity, height: MediaQuery.of(context).size.height, child: Center( child: Image.asset("assets/lovw.gif"), ), ), ), ], ), ); } if (state is VideoPlayerStateLoading) { return Container(); } if (state is VideoPlayerStateError) { return Container(); } return Container(); } Widget _favoriteWidget(BuildContext context) { return Container( height: 50, child: InkWell( onTap: () { if (widget.postedAd.campaignID != null) { if (favoured) { return null; } else { likePost(); } } else { if (favoured) { dislikePost(); } else { likePost(); } } }, child: new Wrap( direction: Axis.vertical, children: [ Container( width: 50, height: 30, child: Center( child: Icon( Icons.favorite, size: 30, color: favoured ? Colors.red : Colors.white, ), ), ), Container( height: 20, width: 50, child: Center( child: Text( Numeral(widget.postedAd.likes).value().toString(), style: TextStyle( fontWeight: FontWeight.bold, color: Colors.white, ), ), ), ), ], ), ), ); } Widget _commentWidget(BuildContext context) { return Container( height: 50, child: InkWell( onTap: () => commentsDialog(context), child: new Wrap( direction: Axis.vertical, children: [ Container( width: 50, height: 30, child: Center( child: Icon( Icons.comment, size: 30, color: Colors.white, ), ), ), Container( height: 20, width: 50, child: Center( child: Text( Numeral(widget.postedAd.comments).value().toString(), style: TextStyle( fontWeight: FontWeight.bold, color: Colors.white, ), ), ), ), ], ), ), ); } Widget _nameDescription(BuildContext context) { return Align( alignment: Alignment.bottomLeft, child: Container( margin: EdgeInsets.symmetric( horizontal: 10, vertical: MediaQuery.of(context).size.height * 0.05, ), child: new Wrap( direction: Axis.vertical, children: [ new RichText( maxLines: 5, text: TextSpan( text: userDoc != null ? userDoc["username"] != null ? "@" + userDoc["username"] : "" : "", style: TextStyle( color: Colors.white, fontSize: SizeConfig.safeBlockHorizontal * 4.3, fontWeight: FontWeight.bold, ), ), ), SizedBox(height: 5), Container( margin: EdgeInsets.only(bottom: 20), width: MediaQuery.of(context).size.width * 0.7, child: Text( widget.postedAd.title != null ? widget.postedAd.title : "", style: TextStyle( color: Colors.white, fontSize: SizeConfig.safeBlockHorizontal * 4.3, height: 1.5, ), maxLines: 4, overflow: TextOverflow.ellipsis, textDirection: TextDirection.rtl, textAlign: TextAlign.left, ), ), ], ), ), ); } Widget _deleteWidget(BuildContext context) { return Container( height: 30, child: GestureDetector( onTap: () async { bool result = await Navigator.push( context, PageRouteBuilder( opaque: false, pageBuilder: (_, __, ___) => DeletePost(), ), ); if (result) { Provider.of(context, listen: false).deletePost(Post( aspect: widget.postedAd.aspect, playbackID: widget.postedAd.playbackID, postID: widget.postedAd.postID, publisherID: widget.postedAd.publisherID, assetID: widget.postedAd.assetID, timestamp: widget.postedAd.timestamp, title: widget.postedAd.title, )); Navigator.pop(context); } }, child: Icon( Icons.delete, size: 28, color: Colors.white, ), ), ); } Widget _backWidget(BuildContext context) { return GestureDetector( onTap: () { Navigator.pop(context); }, child: Container( height: 40, width: 40, decoration: BoxDecoration( color: Colors.transparent, ), child: Icon( Icons.arrow_back_ios, color: Colors.white, ), ), ); } }