Skip to content

Commit

Permalink
Redditech - Adding list of post in subreddits page + Filters + Adding…
Browse files Browse the repository at this point in the history
… subreddit icon
  • Loading branch information
0Nom4D committed Oct 29, 2021
1 parent 5fdd40f commit cb96acd
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 17 deletions.
21 changes: 20 additions & 1 deletion lib/api_service/api_launcher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class ApiLauncher {
/// If [getWholeNew] boolean is true, it wipes the post data list and re-fill it.
/// If [getWholeNew] boolean is false, it adds at the end of the list.
static getFrontPagePosts(PostDataModel postsDataNotifier, bool getWholeNew) async {
List<SubredditRef> fetchedForIcon = [];
ApiLauncher redditApi = ApiLauncher();
Submission fetchedSubmission;
List<Post> posts = [];
Expand Down Expand Up @@ -125,6 +126,14 @@ class ApiLauncher {
await for (var frontPost in chosenMethod) {
fetchedSubmission = frontPost as Submission;
newPost = Post.fromMap(fetchedSubmission);
fetchedForIcon = await redditApi.redditApi.subreddits.searchByName(newPost.subReddit, exact: true);
if (fetchedForIcon.length > 0) {
Subreddit tmp = (await fetchedForIcon[0].populate());
if (tmp.data!["mobile_banner_image"].toString() == "")
newPost.subIcon = tmp.data!["mobile_banner_image"].toString().replaceAll("amp;", "");
else
newPost.subIcon = tmp.data!["banner_background_image"].toString().replaceAll("amp;", "");
}
posts.add(newPost);
}
if (getWholeNew == false)
Expand All @@ -139,7 +148,7 @@ class ApiLauncher {
Future<List<Subreddit>> searchSubs(String query) async {
ApiLauncher redditApi = ApiLauncher();
List<Subreddit> fetchedSubs = [];
List<SubredditRef> fetched;
List<SubredditRef> fetched = [];

if (redditApi.isFlowCreated() == false)
await redditApi.createRedditFlow();
Expand All @@ -159,10 +168,12 @@ class ApiLauncher {
}

static getSubredditPosts(SubRedditModel subRedditDatas, bool getWholeNew) async {
List<SubredditRef> fetchedForIcon = [];
ApiLauncher redditApi = ApiLauncher();
Submission fetchedSubmission;
List<Post> posts = [];
var chosenMethod;
Subreddit tmp;
String? after;
Post newPost;

Expand Down Expand Up @@ -205,6 +216,14 @@ class ApiLauncher {
await for (var frontPost in chosenMethod) {
fetchedSubmission = frontPost as Submission;
newPost = Post.fromMap(fetchedSubmission);
fetchedForIcon = await redditApi.redditApi.subreddits.searchByName(newPost.subReddit, exact: true);
if (fetchedForIcon.length > 0) {
tmp = (await fetchedForIcon[0].populate());
if (tmp.data!["mobile_banner_image"].toString() == "")
newPost.subIcon = tmp.data!["mobile_banner_image"].toString().replaceAll("amp;", "");
else
newPost.subIcon = tmp.data!["banner_background_image"].toString().replaceAll("amp;", "");
}
posts.add(newPost);
}
if (getWholeNew == false)
Expand Down
2 changes: 2 additions & 0 deletions lib/models/post.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class Post {
/// Post body
String body = "";

String subIcon = "";

/// Post subreddit's name
String subReddit = "";

Expand Down
1 change: 0 additions & 1 deletion lib/widgets/base_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ class _BasePageState extends State<BasePage> {
BottomNavigationBarItem(icon: Icon(Icons.home, size: 30), label: 'Accueil'),
BottomNavigationBarItem(icon: Icon(Icons.search, size: 30), label: "Rechercher"),
BottomNavigationBarItem(icon: Icon(Icons.account_circle, size: 30), label: 'Mon Profil'),
// BottomNavigationBarItem(icon: Icon(Icons.language, size: 30), label: 'Communautés'),
// BottomNavigationBarItem(icon: Icon(Icons.settings, size: 30), label: 'Paramètres')
],
elevation: 5.0,
Expand Down
3 changes: 1 addition & 2 deletions lib/widgets/main_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,7 @@ class PostCard extends StatelessWidget {
expandedColor: Colors.white,
key: postCard,
leading: CircleAvatar(
backgroundImage: NetworkImage(
"https://media.tenor.com/images/e627ecf80ad5b216c47a6fb939a51890/tenor.gif")
backgroundImage: post.subIcon == "" ? null : NetworkImage(post.subIcon)
),
title: Text(post.subReddit),
subtitle: Text(post.username + " • " + totalTime),
Expand Down
2 changes: 1 addition & 1 deletion lib/widgets/search_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class _SearchPageState extends State<SearchPage> {
leading: CircleAvatar(
backgroundImage: post!.iconImg == "" ? null : NetworkImage(post.iconImg),
),
title: Text(post.subName),
title: Text("r/" + post.subName),
isThreeLine: true,
subtitle: Text(post.nbFollowers.toString() + " membres"),
onTap: () {
Expand Down
10 changes: 10 additions & 0 deletions lib/widgets/settings_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'package:flutter/cupertino.dart';

class SettingsPage extends StatelessWidget {

@override
Widget build(BuildContext context) {
return Container();
}

}
133 changes: 121 additions & 12 deletions lib/widgets/subreddit_page.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import 'package:f_redditech/providers/subreddit_post_datas.dart';
import 'package:f_redditech/api_service/api_launcher.dart';
import 'package:f_redditech/widgets/main_page.dart';
import 'package:f_redditech/models/sub_info.dart';
import 'package:f_redditech/providers/subreddit_post_datas.dart';
import 'package:provider/provider.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'loading_page.dart';

class SubredditPage extends StatefulWidget {

Expand All @@ -24,12 +27,111 @@ class _SubredditPageState extends State<SubredditPage> {
super.initState();
}

/// Method designed to make a cooldown between fetching and [ListView] display
_fetchEntry() async {
await Future.delayed(Duration(milliseconds: 250));
}

@override
Widget build(BuildContext context) {
return Consumer<SubRedditModel>(
builder: (context, provider, child) {
return SubredditHeader(
subRedditInfo: provider.subredditInfo,
SubRedditModel subredditData = Provider.of<SubRedditModel>(context);
ScrollController listController = ScrollController();

return FutureBuilder(
future: _fetchEntry(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting)
return LoadingScreen();
return Stack(
children: <Widget>[
SubredditHeader(
subRedditInfo: Provider.of<SubRedditModel>(context).subredditInfo,
),
Container(
padding: EdgeInsets.fromLTRB(0, (MediaQuery.of(context).size.height) - 275, 0, 0),
child: Consumer<SubRedditModel>(
builder: (context, provider, _) =>
NotificationListener<ScrollEndNotification>(
child: ListView.builder(
controller: listController,
itemCount: provider.subredditPostTilesList.length,
itemBuilder: (context, index) {
return PostCard(
post: provider.subredditPostTilesList[index],
);
}
),
onNotification: (ScrollEndNotification scrollInfo) {
if (listController.position.atEdge) {
if (listController.position.pixels != 0)
ApiLauncher.getSubredditPosts(provider, false);
else
ApiLauncher.getSubredditPosts(provider, true);
}
return true;
}
)
)
),
Container(
padding: EdgeInsets.fromLTRB(0, (MediaQuery.of(context).size.height) - 325, 0, 0),
alignment: Alignment.topCenter,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.white.withOpacity(0.2),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5),
),
),
onPressed: () {
showModalBottomSheet<void>(
context: context,
builder: (BuildContext context) {
return Container(
child: Wrap(
children: [
ListTile(
title: Text("Nouveautés"),
onTap: () {
subredditData.changeCategory("new", "Nouveautés");
Navigator.pop(context);
}
),
ListTile(
title: Text("Au top"),
onTap: () {
subredditData.changeCategory("top", "Au top");
Navigator.pop(context);
}
),
ListTile(
title: Text("Populaires"),
onTap: () {
subredditData.changeCategory("hot", "Populaires");
Navigator.pop(context);
}
),
ListTile(
title: Text("En hausse"),
onTap: () {
subredditData.changeCategory("rising", "En hausse");
Navigator.pop(context);
}
)
],
)
);
}
);
},
child: Text(subredditData.categoryType,
style: TextStyle(
color: Colors.black
),
),
),
)
]
);
}
);
Expand All @@ -50,7 +152,7 @@ class SubredditHeader extends StatelessWidget {
Column(
children: <Widget>[
Container(
height: 325.0,
height: 225.0,
decoration: BoxDecoration(
color: Colors.white54,
image: subRedditInfo!.bannerImg == "" ? null : DecorationImage(
Expand All @@ -67,7 +169,7 @@ class SubredditHeader extends StatelessWidget {
],
),
Positioned(
top: 200.0,
top: 100.0,
left: 25.0,
child: Container(
height: 150.0,
Expand All @@ -79,11 +181,11 @@ class SubredditHeader extends StatelessWidget {
),
),
Positioned(
top: 355.0,
top: 255.0,
left: 25.0,
child: RichText(
text: TextSpan(
text: subRedditInfo!.subName,
text: "r/" + subRedditInfo!.subName,
style: TextStyle(
fontSize: 20.0,
color: Colors.black,
Expand All @@ -92,8 +194,15 @@ class SubredditHeader extends StatelessWidget {
),
),
Positioned(
top: 255,
left: (MediaQuery.of(context).size.width) - 125,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.deepOrange,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5),
),
),
onPressed: () {
SubRedditModel thisSubReddit = Provider.of<SubRedditModel>(context, listen: false);
thisSubReddit.subscribe();
Expand All @@ -102,7 +211,7 @@ class SubredditHeader extends StatelessWidget {
),
),
Positioned(
top: 390,
top: 290,
left: 25.0,
child: Text(
subRedditInfo!.nbFollowers.toString() + " membres",
Expand All @@ -113,7 +222,7 @@ class SubredditHeader extends StatelessWidget {
)
),
Positioned(
top: 425,
top: 325,
left: 25,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
Expand Down

0 comments on commit cb96acd

Please sign in to comment.