|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter_riverpod/flutter_riverpod.dart'; |
| 3 | +import 'package:tail_app/Backend/analytics.dart'; |
| 4 | +import 'package:tail_app/Backend/firebase.dart'; |
| 5 | +import 'package:tail_app/Frontend/Widgets/tail_blog.dart'; |
| 6 | +import 'package:tail_app/Frontend/Widgets/tail_blog_image.dart'; |
| 7 | +import 'package:tail_app/Frontend/Widgets/uwu_text.dart'; |
| 8 | +import 'package:tail_app/Frontend/utils.dart'; |
| 9 | +import 'package:tail_app/constants.dart'; |
| 10 | + |
| 11 | +class CoshubFeed extends ConsumerStatefulWidget { |
| 12 | + const CoshubFeed({super.key}); |
| 13 | + |
| 14 | + @override |
| 15 | + ConsumerState<CoshubFeed> createState() => _CoshubFeedState(); |
| 16 | +} |
| 17 | + |
| 18 | +List<CosHubPost> results = []; |
| 19 | + |
| 20 | +class _CoshubFeedState extends ConsumerState<CoshubFeed> { |
| 21 | + FeedState feedState = FeedState.loading; |
| 22 | + |
| 23 | + @override |
| 24 | + Widget build(BuildContext context) { |
| 25 | + return AnimatedCrossFade( |
| 26 | + alignment: Alignment.topCenter, |
| 27 | + firstChild: feedState == FeedState.loading |
| 28 | + ? Center( |
| 29 | + child: CircularProgressIndicator(), |
| 30 | + ) |
| 31 | + : [FeedState.noInternet, FeedState.error].contains(feedState) |
| 32 | + ? const Center( |
| 33 | + child: Opacity( |
| 34 | + opacity: 0.5, |
| 35 | + child: Icon( |
| 36 | + Icons.signal_cellular_connected_no_internet_0_bar, |
| 37 | + size: 150, |
| 38 | + ), |
| 39 | + ), |
| 40 | + ) |
| 41 | + : Container(), |
| 42 | + secondChild: ListView.builder( |
| 43 | + scrollDirection: Axis.horizontal, |
| 44 | + shrinkWrap: true, |
| 45 | + itemCount: results.length, |
| 46 | + itemBuilder: (BuildContext context, int index) { |
| 47 | + CosHubPost post = results[index]; |
| 48 | + return Center( |
| 49 | + child: Padding( |
| 50 | + padding: const EdgeInsets.all(8.0), |
| 51 | + child: Card( |
| 52 | + clipBehavior: Clip.antiAlias, |
| 53 | + child: SizedBox( |
| 54 | + width: 300, |
| 55 | + child: Semantics( |
| 56 | + label: 'A button to view the CosHub post by: ${post.username}', |
| 57 | + child: InkWell( |
| 58 | + onTap: () async { |
| 59 | + await launchExternalUrl(url: post.url, analyticsLabel: "CosHub Post"); |
| 60 | + }, |
| 61 | + child: Stack( |
| 62 | + alignment: Alignment.bottomCenter, |
| 63 | + children: <Widget>[ |
| 64 | + if (post.thumbnailUrl != "") ...[ |
| 65 | + SizedBox.expand( |
| 66 | + child: TailBlogImage( |
| 67 | + url: post.thumbnailUrl, |
| 68 | + ), |
| 69 | + ), |
| 70 | + ], |
| 71 | + Card( |
| 72 | + clipBehavior: Clip.antiAlias, |
| 73 | + margin: EdgeInsets.zero, |
| 74 | + elevation: 2, |
| 75 | + child: ListTile( |
| 76 | + //leading: Icon(feedItem.feedType.icon), |
| 77 | + trailing: const Icon(Icons.open_in_browser), |
| 78 | + title: Text(convertToUwU(post.username)), subtitle: post.character != null ? Text(convertToUwU(post.character!)) : null, |
| 79 | + leading: post.profileThumbnailUrl != null |
| 80 | + ? ClipOval( |
| 81 | + child: SizedBox.fromSize( |
| 82 | + size: Size.fromRadius(24), |
| 83 | + child: TailBlogImage( |
| 84 | + url: post.profileThumbnailUrl!, |
| 85 | + ), |
| 86 | + ), |
| 87 | + ) |
| 88 | + : null, |
| 89 | + ), |
| 90 | + ), |
| 91 | + ], |
| 92 | + ), |
| 93 | + ), |
| 94 | + ), |
| 95 | + ), |
| 96 | + ), |
| 97 | + ), |
| 98 | + ); |
| 99 | + }, |
| 100 | + ), |
| 101 | + crossFadeState: results.isNotEmpty ? CrossFadeState.showSecond : CrossFadeState.showFirst, |
| 102 | + duration: animationTransitionDuration, |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + @override |
| 107 | + void dispose() { |
| 108 | + super.dispose(); |
| 109 | + //client.dispose(); |
| 110 | + } |
| 111 | + |
| 112 | + @override |
| 113 | + void initState() { |
| 114 | + super.initState(); |
| 115 | + getFeed(); |
| 116 | + } |
| 117 | + |
| 118 | + Future<void> getFeed() async { |
| 119 | + if (results.isNotEmpty) { |
| 120 | + setState(() { |
| 121 | + feedState = FeedState.loaded; |
| 122 | + }); |
| 123 | + return; |
| 124 | + } |
| 125 | + if (await isLimitedDataEnvironment()) { |
| 126 | + setState(() { |
| 127 | + feedState = FeedState.noInternet; |
| 128 | + }); |
| 129 | + return; |
| 130 | + } |
| 131 | + List<CosHubPost> cosHubPosts = await ref.watch(getCosHubPostsProvider.future); |
| 132 | + setState(() { |
| 133 | + results = cosHubPosts; |
| 134 | + feedState = FeedState.loaded; |
| 135 | + }); |
| 136 | + } |
| 137 | +} |
0 commit comments