Last active
April 7, 2020 04:30
-
-
Save threetwotwo/51944f9ed161a3b4d9c0e2bbc75d1086 to your computer and use it in GitHub Desktop.
Story Page View
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:cloud_firestore/cloud_firestore.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:nutes/core/models/story.dart'; | |
import 'package:nutes/core/services/repository.dart'; | |
import 'package:nutes/ui/screens/my_profile_screen.dart'; | |
import 'package:nutes/ui/screens/profile_screen.dart'; | |
import 'package:nutes/ui/shared/loading_indicator.dart'; | |
import 'package:nutes/ui/widgets/story_view.dart'; | |
class StoryPageView extends StatefulWidget { | |
final int initialPage; | |
final List<UserStory> userStories; | |
final double topPadding; | |
static show(context, | |
{int initialPage, | |
List<UserStory> userStories, | |
double topPadding, | |
Function(int) onPageChange}) => | |
showModalBottomSheet( | |
context: context, | |
isScrollControlled: true, | |
useRootNavigator: true, | |
builder: (context) => Container( | |
height: MediaQuery.of(context).size.height, | |
child: StoryPageView( | |
initialPage: initialPage, | |
userStories: userStories, | |
topPadding: topPadding + 8, | |
), | |
), | |
); | |
const StoryPageView({ | |
Key key, | |
this.userStories, | |
this.initialPage, | |
this.topPadding, | |
}) : super(key: key); | |
@override | |
_StoryPageViewState createState() => _StoryPageViewState(); | |
} | |
class _StoryPageViewState extends State<StoryPageView> { | |
PageController controller; | |
final auth = Repo.auth; | |
List<UserStory> userStories; | |
Map<String, Timestamp> momentsSeen = {}; | |
Map<String, dynamic> seenStories; | |
@override | |
void dispose() { | |
print('disposed, should update moments seen in firestore'); | |
print(momentsSeen); | |
Repo.updateSeenStories(momentsSeen); | |
super.dispose(); | |
} | |
@override | |
void initState() { | |
controller = PageController(initialPage: widget.initialPage); | |
userStories = widget.userStories; | |
_getSeenStories(); | |
super.initState(); | |
} | |
_getSeenStories() async { | |
final result = await Repo.getSeenStories(); | |
setState(() { | |
seenStories = result; | |
}); | |
} | |
nextPage() { | |
return controller.nextPage( | |
duration: Duration(milliseconds: 400), | |
curve: Curves.easeInOut, | |
); | |
} | |
previousPage() { | |
return controller.previousPage( | |
duration: Duration(milliseconds: 400), | |
curve: Curves.easeInOut, | |
); | |
} | |
_getStoryForIndex(int index) async { | |
final currentUserStory = userStories[index]; | |
final result = await Repo.getStoryForUser(currentUserStory.uploader.uid); | |
setState(() { | |
userStories[index] = currentUserStory.copyWith(story: result); | |
}); | |
} | |
_pop() { | |
//TODO: prevent popping twice at the last moment | |
if (mounted) return Navigator.pop(context); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return seenStories == null | |
? Container( | |
color: Colors.black87, | |
child: Center(child: LoadingIndicator()), | |
) | |
: PageView.builder( | |
itemCount: userStories.length, | |
controller: controller, | |
itemBuilder: (context, storyIndex) { | |
final userStory = userStories[storyIndex]; | |
if (userStory.story == null) { | |
_getStoryForIndex(storyIndex); | |
return Container( | |
color: Colors.black87, | |
child: Center( | |
child: CircularProgressIndicator( | |
valueColor: AlwaysStoppedAnimation(Colors.white), | |
strokeWidth: 1, | |
), | |
), | |
); | |
} | |
return StoryView( | |
isOwner: auth.uid == userStory.uploader.uid, | |
seenStories: seenStories, | |
topPadding: widget.topPadding, | |
onAvatarTapped: (user) => | |
Navigator.of(context).push(MaterialPageRoute( | |
builder: (context) => user.uid == auth.uid | |
? MyProfileScreen(isRoot: false) | |
: ProfileScreen( | |
uid: user.uid, | |
))), | |
uploader: userStory.uploader, | |
story: userStory.story, | |
isFirstStory: storyIndex == 0, | |
onFlashForward: storyIndex == widget.userStories.length - 1 | |
? () => _pop() | |
: nextPage, | |
onFlashBack: previousPage, | |
onMomentChanged: (val) { | |
final storyTimestamp = | |
userStories[storyIndex].story.moments[val].timestamp; | |
final seenTimestamp = | |
(seenStories[userStory.uploader.uid] as Timestamp); | |
if (seenTimestamp == null || | |
seenTimestamp.seconds < storyTimestamp.seconds) | |
momentsSeen[userStories[storyIndex].uploader.uid] = | |
storyTimestamp; | |
}, | |
); | |
}, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment