Added profile screen with API interaction, better .env usage
This commit is contained in:
@@ -2,7 +2,6 @@ import 'package:flutter/material.dart';
|
|||||||
import 'views/home_screen.dart';
|
import 'views/home_screen.dart';
|
||||||
import 'views/profile_screen.dart';
|
import 'views/profile_screen.dart';
|
||||||
import 'views/login_screen.dart';
|
import 'views/login_screen.dart';
|
||||||
import 'methods/api.dart';
|
|
||||||
import 'views/init_screen.dart';
|
import 'views/init_screen.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
@@ -15,7 +14,7 @@ class MyApp extends StatelessWidget {
|
|||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return MaterialApp(
|
return MaterialApp(
|
||||||
title: '42 API Client',
|
title: 'Fluty',
|
||||||
initialRoute: '/',
|
initialRoute: '/',
|
||||||
onGenerateRoute: (settings) {
|
onGenerateRoute: (settings) {
|
||||||
final uri = Uri.parse(settings.name ?? '/');
|
final uri = Uri.parse(settings.name ?? '/');
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import 'package:shared_preferences/shared_preferences.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
import 'package:http/http.dart';
|
import 'package:http/http.dart' as http;
|
||||||
|
import 'dart:convert';
|
||||||
|
|
||||||
Future<bool> checkToken() async
|
Future<bool> checkToken() async
|
||||||
{
|
{
|
||||||
@@ -9,10 +10,26 @@ Future<bool> checkToken() async
|
|||||||
return date.isAfter(DateTime.now());
|
return date.isAfter(DateTime.now());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Future<Map<String, dynamic>> fetchProfileData(String login) async
|
Future<Map<String, dynamic>> fetchProfileData(String login) async
|
||||||
// {
|
{
|
||||||
//
|
final uri = Uri.https(
|
||||||
// }
|
'api.intra.42.fr',
|
||||||
|
'/v2/users/$login',
|
||||||
|
);
|
||||||
|
final token = await getToken() ?? '';
|
||||||
|
final response = await http.get(
|
||||||
|
uri,
|
||||||
|
headers: {
|
||||||
|
'Authorization': 'Bearer $token',
|
||||||
|
'Accept': 'application/json',
|
||||||
|
},
|
||||||
|
);
|
||||||
|
if (response.statusCode == 200) {
|
||||||
|
return jsonDecode(response.body) as Map<String, dynamic>;
|
||||||
|
}
|
||||||
|
else { throw Exception('Failed to fetch user: ${response.statusCode}'); }
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> saveToken(String token, String refresh, DateTime expiration) async
|
Future<void> saveToken(String token, String refresh, DateTime expiration) async
|
||||||
{
|
{
|
||||||
final prefs = await SharedPreferences.getInstance();
|
final prefs = await SharedPreferences.getInstance();
|
||||||
|
|||||||
@@ -13,11 +13,10 @@ class _HomeScreenState extends State<HomeScreen> {
|
|||||||
|
|
||||||
void _handleInput(BuildContext context, String input) {
|
void _handleInput(BuildContext context, String input) {
|
||||||
// Replace with your desired logic
|
// Replace with your desired logic
|
||||||
print("Input received: $input");
|
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
SnackBar(content: Text('You entered: $input')),
|
SnackBar(content: Text('Checking profile of: $input')),
|
||||||
);
|
);
|
||||||
Navigator.pushReplacementNamed(context, '/profile/${input}');
|
Navigator.pushReplacementNamed(context, '/profile/$input');
|
||||||
}
|
}
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
@@ -39,7 +38,7 @@ class _HomeScreenState extends State<HomeScreen> {
|
|||||||
TextField(
|
TextField(
|
||||||
controller: _controller,
|
controller: _controller,
|
||||||
decoration: const InputDecoration(
|
decoration: const InputDecoration(
|
||||||
hintText: 'Enter something...',
|
hintText: 'Enter login',
|
||||||
border: OutlineInputBorder(),
|
border: OutlineInputBorder(),
|
||||||
filled: true,
|
filled: true,
|
||||||
fillColor: Colors.white70,
|
fillColor: Colors.white70,
|
||||||
@@ -50,14 +49,7 @@ class _HomeScreenState extends State<HomeScreen> {
|
|||||||
onPressed: () {
|
onPressed: () {
|
||||||
_handleInput(context, _controller.text);
|
_handleInput(context, _controller.text);
|
||||||
},
|
},
|
||||||
child: const Text('Submit Input'),
|
child: const Text('Submit'),
|
||||||
),
|
|
||||||
const SizedBox(height: 20),
|
|
||||||
ElevatedButton(
|
|
||||||
onPressed: () {
|
|
||||||
Navigator.pushNamed(context, '/profile');
|
|
||||||
},
|
|
||||||
child: const Text('Go to Profile'),
|
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
|
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import '../methods/api.dart';
|
import '../methods/api.dart';
|
||||||
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
||||||
|
|
||||||
Future<bool> initApp() async {
|
Future<bool> initApp() async {
|
||||||
|
await dotenv.load();
|
||||||
return await checkToken();
|
return await checkToken();
|
||||||
}
|
}
|
||||||
class InitScreen extends StatelessWidget {
|
class InitScreen extends StatelessWidget {
|
||||||
@@ -25,7 +27,7 @@ class InitScreen extends StatelessWidget {
|
|||||||
switch (snapshot.connectionState)
|
switch (snapshot.connectionState)
|
||||||
{
|
{
|
||||||
case ConnectionState.waiting:
|
case ConnectionState.waiting:
|
||||||
return Center(
|
return const Center(
|
||||||
child: Image(
|
child: Image(
|
||||||
image: AssetImage("assets/images/42_logo.png"),
|
image: AssetImage("assets/images/42_logo.png"),
|
||||||
width: 100,
|
width: 100,
|
||||||
@@ -53,7 +55,7 @@ class InitScreen extends StatelessWidget {
|
|||||||
Future.microtask(() {
|
Future.microtask(() {
|
||||||
Navigator.pushReplacementNamed(context, '/login');
|
Navigator.pushReplacementNamed(context, '/login');
|
||||||
});
|
});
|
||||||
return Center(
|
return const Center(
|
||||||
child: Text(
|
child: Text(
|
||||||
"Redirect"
|
"Redirect"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -2,21 +2,21 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
import 'package:flutter_appauth/flutter_appauth.dart';
|
import 'package:flutter_appauth/flutter_appauth.dart';
|
||||||
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
|
||||||
import 'package:swifty/methods/api.dart';
|
import 'package:swifty/methods/api.dart';
|
||||||
|
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
||||||
|
|
||||||
const FlutterAppAuth _appAuth = FlutterAppAuth();
|
const FlutterAppAuth _appAuth = FlutterAppAuth();
|
||||||
|
|
||||||
Future<void> redirect_to_oauth(BuildContext context) async {
|
Future<void> redirect_to_oauth(BuildContext context) async {
|
||||||
final String _clientId = "CLIENT TODO";
|
String _clientId = dotenv.get('CLIENT-ID');
|
||||||
final String _clientSecret = "SECRET TODO";
|
String _clientSecret = dotenv.get('CLIENT-SECRET');
|
||||||
final String _redirectUrl = 'swifty-companion://oauth2/callback';
|
const String _redirectUrl = 'swifty-companion://oauth2/callback';
|
||||||
final String _authorizationEndpoint = 'https://api.intra.42.fr/oauth/authorize';
|
const String _authorizationEndpoint = 'https://api.intra.42.fr/oauth/authorize';
|
||||||
final String _tokenEndpoint = 'https://api.intra.42.fr/oauth/token';
|
const String _tokenEndpoint = 'https://api.intra.42.fr/oauth/token';
|
||||||
final request = AuthorizationRequest(
|
final request = AuthorizationRequest(
|
||||||
_clientId,
|
_clientId,
|
||||||
_redirectUrl,
|
_redirectUrl,
|
||||||
serviceConfiguration: AuthorizationServiceConfiguration(
|
serviceConfiguration: const AuthorizationServiceConfiguration(
|
||||||
authorizationEndpoint: _authorizationEndpoint,
|
authorizationEndpoint: _authorizationEndpoint,
|
||||||
tokenEndpoint: _tokenEndpoint,
|
tokenEndpoint: _tokenEndpoint,
|
||||||
));
|
));
|
||||||
@@ -29,7 +29,7 @@ Future<void> redirect_to_oauth(BuildContext context) async {
|
|||||||
clientSecret: _clientSecret,
|
clientSecret: _clientSecret,
|
||||||
authorizationCode: result.authorizationCode,
|
authorizationCode: result.authorizationCode,
|
||||||
grantType: 'authorization_code',
|
grantType: 'authorization_code',
|
||||||
serviceConfiguration: AuthorizationServiceConfiguration(
|
serviceConfiguration: const AuthorizationServiceConfiguration(
|
||||||
authorizationEndpoint: "https://api.intra.42.fr/oauth/authorize",
|
authorizationEndpoint: "https://api.intra.42.fr/oauth/authorize",
|
||||||
tokenEndpoint: _tokenEndpoint,
|
tokenEndpoint: _tokenEndpoint,
|
||||||
),
|
),
|
||||||
@@ -53,14 +53,23 @@ class LoginScreen extends StatelessWidget {
|
|||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(title: const Text("Login")),
|
appBar: AppBar(title: const Text("Login")),
|
||||||
body: Center(
|
body: Stack(
|
||||||
|
fit: StackFit.expand,
|
||||||
|
children: [
|
||||||
|
Image.asset(
|
||||||
|
'assets/images/cluster-photo-00.jpg',
|
||||||
|
fit: BoxFit.cover,
|
||||||
|
),
|
||||||
|
Center(
|
||||||
child: ElevatedButton(
|
child: ElevatedButton(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
redirect_to_oauth(context);
|
redirect_to_oauth(context);
|
||||||
},
|
},
|
||||||
child: const Text('Login'),
|
child: const Text('Login into 42 API'),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
// profile_screen.dart
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:swifty/methods/api.dart';
|
||||||
|
import 'package:swifty/views/login_screen.dart';
|
||||||
|
|
||||||
class ProfileScreen extends StatelessWidget {
|
class ProfileScreen extends StatelessWidget {
|
||||||
final String login;
|
final String login;
|
||||||
@@ -7,16 +8,162 @@ class ProfileScreen extends StatelessWidget {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
return FutureBuilder(
|
||||||
|
future: fetchProfileData(login),
|
||||||
|
builder: (context, snapshot) {
|
||||||
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||||
|
return const Scaffold(
|
||||||
|
body: Center(child: CircularProgressIndicator()),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (snapshot.hasError) {
|
||||||
|
if (snapshot.error.toString().contains('401')) { redirect_to_oauth(context); }
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(title: const Text("Profile")),
|
|
||||||
body: Center(
|
body: Center(
|
||||||
child: ElevatedButton(
|
child: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: [
|
||||||
|
const Text("This user does not exists or is hidden from you !"),
|
||||||
|
const SizedBox(height: 16),
|
||||||
|
ElevatedButton(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
Navigator.pushNamed(context, '/login');
|
Navigator.pushReplacementNamed(context, '/home');
|
||||||
},
|
},
|
||||||
child: Text('Go to ${login}'),
|
child: const Text("Bring me back !!"),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if (snapshot.hasData && snapshot.connectionState == ConnectionState.done) {
|
||||||
|
final Map<String, dynamic> list = snapshot.data ?? {};
|
||||||
|
final name = list['usual_full_name'] ?? 'Unknown';
|
||||||
|
final imageUrl = list['image']?['versions']?['medium'] ?? '';
|
||||||
|
final correctionsPoints = list['correction_point'] ?? 0;
|
||||||
|
final level = list['cursus_users']?[1]?['level']?.toDouble() ?? 0.0;
|
||||||
|
final location = list['location'] ?? 'Unavailable';
|
||||||
|
final skills = list['cursus_users']?[1]?['skills'];
|
||||||
|
final unfilteredProjects = list['projects_users'];
|
||||||
|
final projects = unfilteredProjects.where((p) => p['status'] != 'creating_group' && p['status'] != 'searching_a_group').toList();
|
||||||
|
|
||||||
|
return DefaultTabController(
|
||||||
|
length: 2,
|
||||||
|
child: Scaffold(
|
||||||
|
appBar: AppBar(
|
||||||
|
title: const Text("Profile"),
|
||||||
|
leading: IconButton(
|
||||||
|
icon: const Icon(Icons.arrow_back),
|
||||||
|
onPressed: () => Navigator.pushReplacementNamed(context, '/home'),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
body: Column(
|
||||||
|
children: [
|
||||||
|
Padding(
|
||||||
|
padding: const EdgeInsets.all(16),
|
||||||
|
child: Row(
|
||||||
|
children: [
|
||||||
|
if (imageUrl.isNotEmpty)
|
||||||
|
CircleAvatar(
|
||||||
|
radius: 40,
|
||||||
|
backgroundImage: NetworkImage(imageUrl),
|
||||||
|
),
|
||||||
|
const SizedBox(width: 16),
|
||||||
|
Expanded(
|
||||||
|
child: Column(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
children: [
|
||||||
|
Text(name, style: const TextStyle(fontSize: 20, fontWeight: FontWeight.bold)),
|
||||||
|
const SizedBox(height: 8),
|
||||||
|
LinearProgressIndicator(value: level % 1, minHeight: 8),
|
||||||
|
const SizedBox(height: 4),
|
||||||
|
Text('Level: ${level.toStringAsFixed(2)}'),
|
||||||
|
Text('Correction Points: $correctionsPoints'),
|
||||||
|
Text('Location: $location'),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
|
||||||
|
const SizedBox(height: 16),
|
||||||
|
const TabBar(
|
||||||
|
tabs: [
|
||||||
|
Tab(text: 'Skills'),
|
||||||
|
Tab(text: 'Projects'),
|
||||||
|
],
|
||||||
|
labelColor: Colors.blue,
|
||||||
|
unselectedLabelColor: Colors.grey,
|
||||||
|
),
|
||||||
|
|
||||||
|
Expanded(
|
||||||
|
child: TabBarView(
|
||||||
|
children: [
|
||||||
|
ListView.builder(
|
||||||
|
itemCount: skills.length,
|
||||||
|
itemBuilder: (context, index) {
|
||||||
|
final skill = skills[index] as Map<String, dynamic>;
|
||||||
|
return ListTile(
|
||||||
|
title: Text(skill['name']),
|
||||||
|
trailing: Text(
|
||||||
|
skill['level'].toStringAsFixed(2),
|
||||||
|
style: const TextStyle(
|
||||||
|
fontWeight: FontWeight.bold,
|
||||||
|
fontSize: 18,),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
ListView.builder(
|
||||||
|
itemCount: projects.length,
|
||||||
|
itemBuilder: (context, index) {
|
||||||
|
final project = projects[index] as Map<String, dynamic>;
|
||||||
|
Text project_grade;
|
||||||
|
if (project['final_mark'] == null) {
|
||||||
|
project_grade = const Text(
|
||||||
|
'pending',
|
||||||
|
style: TextStyle(
|
||||||
|
fontWeight: FontWeight.bold,
|
||||||
|
color: Colors.black38,
|
||||||
|
fontSize: 18
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var color = (project['validated?']) ? Colors.green : Colors.red;
|
||||||
|
project_grade = Text(
|
||||||
|
project['final_mark'].toStringAsFixed(2),
|
||||||
|
style: TextStyle(
|
||||||
|
fontWeight: FontWeight.bold,
|
||||||
|
color: color,
|
||||||
|
fontSize: 18
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return ListTile(
|
||||||
|
title: Text(project['project']['name']),
|
||||||
|
trailing: project_grade
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return const Scaffold(
|
||||||
|
body: Center(child: Text("Unexpected error.")),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -57,6 +57,7 @@ flutter:
|
|||||||
assets:
|
assets:
|
||||||
- assets/images/42_logo.png
|
- assets/images/42_logo.png
|
||||||
- assets/images/cluster-photo-00.jpg
|
- assets/images/cluster-photo-00.jpg
|
||||||
|
- .env
|
||||||
# The following line ensures that the Material Icons font is
|
# The following line ensures that the Material Icons font is
|
||||||
# included with your application, so that you can use the icons in
|
# included with your application, so that you can use the icons in
|
||||||
# the material Icons class.
|
# the material Icons class.
|
||||||
|
|||||||
Reference in New Issue
Block a user