diff --git a/lib/main.dart b/lib/main.dart index 2bfebdbcfd253029604cd7a6166c851cdc791400..27641fa2557077798a8b8bdb79d9d9c43242290b 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,9 +1,17 @@ +import 'dart:convert'; + import 'package:english_words/english_words.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; +import 'package:http/http.dart' as http; void main() { - runApp(MyApp()); + runApp( + ChangeNotifierProvider( + create: (context) => MyAppState(), + child: const MyApp(), + ), + ); } class MyApp extends StatelessWidget { @@ -11,17 +19,28 @@ class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { - return ChangeNotifierProvider( - create: (context) => MyAppState(), - child: MaterialApp( + var appstate=context.watch<MyAppState>(); + return MaterialApp( title: 'Namer App', theme: ThemeData( useMaterial3: true, colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepOrange), ), - home: MyHomePage(), - ), - ); + home: FutureBuilder<List<WordPair>>( + future: appstate.futureFavorites, + builder: (context, snapshot) { + if (snapshot.connectionState == ConnectionState.waiting) { + return const Center(child: CircularProgressIndicator()); + } else if (snapshot.hasError) { + return Center(child: Text('Error : ${snapshot.error}'),); + } else if (snapshot.hasData) { + return MyHomePage(); + } else { + return Center(child: Text('No Favorites yet')); + } + } + ), + ); } } @@ -32,15 +51,49 @@ class MyAppState extends ChangeNotifier { notifyListeners(); } var favorites = <WordPair>[]; + late Future<List<WordPair>> futureFavorites; + + MyAppState() { + futureFavorites = fetchFavorites(); + } + + Future<List<WordPair>> fetchFavorites() async { + final response=await http.get( + Uri.parse('https://wonbin3977.web.ajousw.kr/likes')); + List<WordPair> list = []; + var jsonData = jsonDecode(response.body); - void toggleFavorite() { - if (favorites.contains(current)) { + for(Map<String,dynamic> obj in jsonData) { + List<String> res=obj["id"].split('_'); + list.add(WordPair(res[0],res[1])); + } + favorites = [...list]; + notifyListeners(); + return list; + } + + Future<void> toggleFavorite() async { + var uri ="https:wonbin3977.web.ajousw.kr/likes/${current.first}_${current.second}"; + if(favorites.contains(current)) { + final http.Response resp=await http.delete(Uri.parse(uri)); + if(resp.statusCode!=200) { + return; + } favorites.remove(current); - } else { - favorites.add(current); + favorites=[...favorites]; + } else{ + final http.Response resp=await http.post( + Uri.parse(uri), + ); + if(resp.statusCode!=200) { + return; + } + favorites=[...favorites,current]; } + print(favorites); notifyListeners(); } + } // ... diff --git a/pubspec.lock b/pubspec.lock index 39660c34ae54fd4cd8e8348cdb1510e518226105..5d2d9e5d3a082cc15a260841eb7db6c37f8257a8 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -75,6 +75,22 @@ packages: description: flutter source: sdk version: "0.0.0" + http: + dependency: "direct main" + description: + name: http + sha256: "2c11f3f94c687ee9bad77c171151672986360b2b001d109814ee7140b2cf261b" + url: "https://pub.dev" + source: hosted + version: "1.4.0" + http_parser: + dependency: transitive + description: + name: http_parser + sha256: "178d74305e7866013777bab2c3d8726205dc5a4dd935297175b19a23a2e66571" + url: "https://pub.dev" + source: hosted + version: "4.1.2" leak_tracker: dependency: transitive description: @@ -208,6 +224,14 @@ packages: url: "https://pub.dev" source: hosted version: "0.7.4" + typed_data: + dependency: transitive + description: + name: typed_data + sha256: f9049c039ebfeb4cf7a7104a675823cd72dba8297f264b6637062516699fa006 + url: "https://pub.dev" + source: hosted + version: "1.4.0" vector_math: dependency: transitive description: @@ -224,6 +248,14 @@ packages: url: "https://pub.dev" source: hosted version: "14.3.1" + web: + dependency: transitive + description: + name: web + sha256: "868d88a33d8a87b18ffc05f9f030ba328ffefba92d6c127917a2ba740f9cfe4a" + url: "https://pub.dev" + source: hosted + version: "1.1.1" sdks: dart: ">=3.7.0-0 <4.0.0" flutter: ">=3.18.0-18.0.pre.54" diff --git a/pubspec.yaml b/pubspec.yaml index dd55bfb5191354b7065f613a6acd98bdc7064634..64097e923aad756a6f0be5ae9651d02c53ea0c45 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -14,6 +14,7 @@ dependencies: english_words: ^4.0.0 provider: ^6.0.0 + http: ^1.4.0 dev_dependencies: flutter_test: