|
| 1 | +import 'dart:convert'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | +import 'package:flutter_app/widgets/app_drawer.dart'; |
| 4 | +import 'package:flutter_app/widgets/urlListTile.dart'; |
| 5 | +import 'package:shared_preferences/shared_preferences.dart'; |
| 6 | +import 'package:http/http.dart' as http; |
| 7 | + |
| 8 | +class UrlsScreen extends StatefulWidget { |
| 9 | + const UrlsScreen({super.key}); |
| 10 | + |
| 11 | + @override |
| 12 | + State<UrlsScreen> createState() => _UrlsScreenState(); |
| 13 | +} |
| 14 | + |
| 15 | +class _UrlsScreenState extends State<UrlsScreen> { |
| 16 | + @override |
| 17 | + void initState() { |
| 18 | + super.initState(); |
| 19 | + _checkToken(); |
| 20 | + } |
| 21 | + |
| 22 | + Future<String?> _getToken() async { |
| 23 | + final prefs = await SharedPreferences.getInstance(); |
| 24 | + return prefs.getString('JWT_TOKEN'); |
| 25 | + } |
| 26 | + |
| 27 | + void _checkToken() async { |
| 28 | + final token = await _getToken(); |
| 29 | + |
| 30 | + if (token == null || token.isEmpty) { |
| 31 | + if (mounted) { |
| 32 | + Navigator.pushReplacementNamed(context, '/login'); |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + @override |
| 38 | + Widget build(BuildContext context) { |
| 39 | + return Scaffold( |
| 40 | + appBar: AppBar(title: const Text("Urls"), centerTitle: true), |
| 41 | + drawer: const AppDrawer(), |
| 42 | + body: Padding( |
| 43 | + padding: const EdgeInsets.all(20), |
| 44 | + child: Column( |
| 45 | + crossAxisAlignment: CrossAxisAlignment.stretch, |
| 46 | + children: [ |
| 47 | + const SizedBox(height: 20), |
| 48 | + const Text( |
| 49 | + "See your short URLs below", |
| 50 | + style: TextStyle(fontSize: 18, fontWeight: FontWeight.w500), |
| 51 | + textAlign: TextAlign.center, |
| 52 | + ), |
| 53 | + const SizedBox(height: 20), |
| 54 | + |
| 55 | + Expanded(child: UrlListSection()), |
| 56 | + ], |
| 57 | + ), |
| 58 | + ), |
| 59 | + ); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | + |
| 64 | +class UrlListSection extends StatelessWidget { |
| 65 | + const UrlListSection({super.key}); |
| 66 | + |
| 67 | + Future<List<dynamic>> fetchUrls() async { |
| 68 | + final prefs = await SharedPreferences.getInstance(); |
| 69 | + final token = prefs.getString('JWT_TOKEN'); |
| 70 | + |
| 71 | + if (token == null || token.isEmpty) { |
| 72 | + return []; |
| 73 | + } |
| 74 | + |
| 75 | + try { |
| 76 | + final response = await http.get( |
| 77 | + Uri.parse('http://localhost:9000/api/userUrls/'), |
| 78 | + headers: { |
| 79 | + 'Content-Type': 'application/json', |
| 80 | + 'Authorization': 'Bearer $token', |
| 81 | + }, |
| 82 | + ); |
| 83 | + |
| 84 | + if (response.statusCode == 200 || response.statusCode == 304) { |
| 85 | + final data = jsonDecode(response.body); |
| 86 | + return data; |
| 87 | + } else { |
| 88 | + throw Exception("Failed to fetch URLs (${response.statusCode})"); |
| 89 | + } |
| 90 | + } catch (e) { |
| 91 | + debugPrint("Error fetching URLs: $e"); |
| 92 | + throw Exception("Network error occurred"); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + @override |
| 97 | + Widget build(BuildContext context) { |
| 98 | + return FutureBuilder<List<dynamic>>( |
| 99 | + future: fetchUrls(), |
| 100 | + builder: (context, snapshot) { |
| 101 | + if (snapshot.connectionState == ConnectionState.waiting) { |
| 102 | + return const Center(child: CircularProgressIndicator()); |
| 103 | + } |
| 104 | + else if (snapshot.hasError) { |
| 105 | + return Center(child: Text("Error: ${snapshot.error}")); |
| 106 | + } else if (!snapshot.hasData || snapshot.data!.isEmpty) { |
| 107 | + return const Center(child: Text("No URLs found")); |
| 108 | + } |
| 109 | + |
| 110 | + final urls = snapshot.data!; |
| 111 | + return ListView.builder( |
| 112 | + itemCount: urls.length, |
| 113 | + itemBuilder: (context, index) { |
| 114 | + return UrlListTile(url: urls[index]); |
| 115 | + }, |
| 116 | + ); |
| 117 | + }, |
| 118 | + ); |
| 119 | + } |
| 120 | +} |
0 commit comments