Skip to content

Commit

Permalink
Merge pull request #2 from 0Nom4D/flutter-poc
Browse files Browse the repository at this point in the history
Merging User Profile Page + Main Page First Previews
  • Loading branch information
0Nom4D authored Oct 20, 2021
2 parents a884bd0 + dfbefcd commit 366faad
Show file tree
Hide file tree
Showing 14 changed files with 212 additions and 165 deletions.
49 changes: 49 additions & 0 deletions lib/controller/main_page_ctrl.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import 'package:draw/draw.dart';
import 'package:f_redditech/models/api_launcher.dart';
import 'package:f_redditech/views/loading_page.dart';
import 'package:f_redditech/views/main_page.dart';
import 'package:flutter/cupertino.dart';

class MainPageController extends StatefulWidget {
MainPageController({Key? key}) : super(key: key);

@override
_MainPageController createState() => _MainPageController();
}

class _MainPageController extends State<MainPageController> {

ApiLauncher redditApi = ApiLauncher();
List<Submission>? posts;

@override
void initState() {
super.initState();
posts = null;
}

void retrievePosts() async {
List<Submission>? tmp;

if (redditApi.isFlowCreated() == false)
await redditApi.createRedditFlow();
if (posts == null) {
tmp = await redditApi.getFrontPagePosts().then((value) {
return (value);
});
setState(() {
posts = tmp;
});
}
}

@override
Widget build(BuildContext context) {
retrievePosts();
if (posts == null)
return (LoadingScreen());
return (MainPage(
posts: posts,
));
}
}
15 changes: 9 additions & 6 deletions lib/controller/user_page_ctrl.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:f_redditech/views/loading_page.dart';
import 'package:f_redditech/views/loading_page.dart';
import 'package:f_redditech/models/api_launcher.dart';
import 'package:f_redditech/views/user_page.dart';
import 'package:flutter/material.dart';
Expand All @@ -23,15 +23,18 @@ class _UserPageController extends State<UserPageController> {
}

void connectToApi() async {
await redditApi.createRedditFlow();
myProfile = await redditApi.getMe().then((value) {
Redditor? tmp;

if (redditApi.isFlowCreated() == false)
await redditApi.createRedditFlow();
tmp = await redditApi.getMe().then((value) {
if (value != null)
return (value);
return (null);
});
print(myProfile!.awardeeKarma);
print(myProfile!.awarderKarma);
print(myProfile!.commentKarma);
setState(() {
myProfile = tmp;
});
}

@override
Expand Down
4 changes: 2 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// import 'package:f_redditech/views/main_page.dart';
import 'package:f_redditech/controller/main_page_ctrl.dart';
import 'package:f_redditech/views/home_page.dart';
import 'controller/user_page_ctrl.dart';
import 'package:flutter/material.dart';
Expand All @@ -22,7 +22,7 @@ class MyApp extends StatelessWidget {
initialRoute: '/',
routes: {
'/': (BuildContext context) => const HomePage(),
// '/home': (BuildContext context) => MainPage(postData: []),
'/home': (BuildContext context) => MainPageController(),
'/profile': (BuildContext context) => UserPageController(),
},
);
Expand Down
69 changes: 0 additions & 69 deletions lib/main_menu_page.dart

This file was deleted.

68 changes: 0 additions & 68 deletions lib/main_page.dart

This file was deleted.

20 changes: 15 additions & 5 deletions lib/models/api_launcher.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import 'package:flutter_web_auth/flutter_web_auth.dart';
import 'package:draw/draw.dart';
import 'dart:io' show Platform;

class ApiLauncher {

static final ApiLauncher _apiLauncher = ApiLauncher._internal();

factory ApiLauncher() => _apiLauncher;

Reddit? redditApi;
late Reddit redditApi;

bool isConnected = false;

Expand All @@ -22,13 +21,13 @@ class ApiLauncher {
userAgent: "Qwiddo",
redirectUri: Uri.parse("reddit://success"),
);
final authUrl = redditApi?.auth.url(["*"], "Qwiddo", compactLogin: true);
final authUrl = redditApi.auth.url(["*"], "Qwiddo", compactLogin: true);
final result = await FlutterWebAuth.authenticate(
url: authUrl.toString(),
callbackUrlScheme: "reddit"
);
String? code = Uri.parse(result).queryParameters['code'];
await redditApi?.auth.authorize(code.toString());
await redditApi.auth.authorize(code.toString());
isConnected = true;
}

Expand All @@ -37,12 +36,23 @@ class ApiLauncher {

if (isFlowCreated() == false)
this.createRedditFlow();
retrievedUser = await redditApi?.user.me();
retrievedUser = await redditApi.user.me();
if (retrievedUser == null)
return (null);
return (retrievedUser);
}

Future<List<Submission>> getFrontPagePosts() async {
List<Submission> posts = [];

if (isFlowCreated() == false)
this.createRedditFlow();
await for (var frontPost in redditApi.front.hot(limit: 5)) {
posts.add(frontPost as Submission);
}
return (posts);
}

bool isFlowCreated() {
return this.isConnected;
}
Expand Down
63 changes: 63 additions & 0 deletions lib/views/main_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import 'package:flutter/material.dart';
import 'package:draw/draw.dart';

class MainPage extends StatefulWidget {

final List<Submission>? posts;

MainPage({Key? key, required this.posts}) : super(key: key);

late final int postsLength;

@override
_MainPageState createState() => _MainPageState();

}

class _MainPageState extends State<MainPage> {

@override
void initState() {
super.initState();
widget.postsLength = 0;
}

_createdPostCard(Submission newPosts) {
print(newPosts.data!);
return (Card(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
ListTile(
leading: CircleAvatar(
radius: 25.0,
),
title: Text(newPosts.title),
subtitle: newPosts.body == null ? Text(""): Text(newPosts.body!),
)
],
)
));
}

List<Widget> _listCreation() {
List<Widget> _allCards = [];

for (int i = 0; i < widget.posts!.length; i++) {
_allCards.add(_createdPostCard(widget.posts![i]));
}
return (_allCards);
}

@override
Widget build(BuildContext context) {
int idx = 0;

return Scaffold(
appBar: AppBar(),
body: ListView(
children: _listCreation(),
)
);
}
}
Loading

0 comments on commit 366faad

Please sign in to comment.