From c246d1cbebf84876d295a7dcc3740719c474f3e4 Mon Sep 17 00:00:00 2001 From: Vaibhav Date: Fri, 20 Sep 2024 15:50:11 +0530 Subject: [PATCH] First commmit of RD App --- lib/controller/get_place_order_service.dart | 2 +- lib/controller/home_controller.dart | 99 ++++++++++++++- lib/controller/home_service.dart | 49 ++++++-- lib/controller/place_order_controller.dart | 84 ------------- lib/controller/place_order_service.dart | 12 +- lib/controller/product_service.dart | 4 +- lib/models/user_model.dart | 71 ++++++++--- lib/screens/authentication/Profile.dart | 117 ++++++++++++++++++ .../controller/auth_service.dart | 6 +- .../product/product_detail_screen.dart | 13 +- ...oduct_mannual.dart => product_manual.dart} | 0 lib/screens/profile_screen.dart | 99 +++++++++++++++ lib/screens/splash_screen.dart | 21 +++- lib/utils/api_urls.dart | 18 +++ lib/utils/common_api_service.dart | 100 ++++++++------- lib/widgets/my_drawer.dart | 55 ++++---- pubspec.lock | 32 +++-- pubspec.yaml | 1 + 18 files changed, 552 insertions(+), 231 deletions(-) create mode 100644 lib/screens/authentication/Profile.dart rename lib/screens/product/{product_mannual.dart => product_manual.dart} (100%) create mode 100644 lib/screens/profile_screen.dart create mode 100644 lib/utils/api_urls.dart diff --git a/lib/controller/get_place_order_service.dart b/lib/controller/get_place_order_service.dart index 5c10096..ebab3fa 100644 --- a/lib/controller/get_place_order_service.dart +++ b/lib/controller/get_place_order_service.dart @@ -13,7 +13,7 @@ class GetOrderPlacedService { try { while (_hasMoreOrders) { // Construct the API URL with pagination parameters - String url = "/api/get-placed-order-pd?page=$_currentPage"; + String url = "api/rd-place-order?page=$_currentPage"; final response = await commonApiService>( method: "GET", diff --git a/lib/controller/home_controller.dart b/lib/controller/home_controller.dart index 26f243c..3499aaf 100644 --- a/lib/controller/home_controller.dart +++ b/lib/controller/home_controller.dart @@ -6,7 +6,19 @@ import 'package:shared_preferences/shared_preferences.dart'; class HomeController extends GetxController { final HomeService homeService = HomeService(); - var user = Rxn(); + UserModel? user; + + // var userModel = UserModel( + // id: '', + // uniqueId: '', + // name: '', + // email: '', + // phone: '', + // role: '', + // sbu: '', + // createdAt: '', + // updatedAt: '', + // ).obs; // Observable for UserModel @override void onInit() { @@ -14,10 +26,85 @@ class HomeController extends GetxController { super.onInit(); } - Future getUser() async - { - SharedPreferences prefs = await SharedPreferences.getInstance(); - String? token = prefs.getString('token'); - user.value = (await homeService.getUser(token: token)) as UserModel? ; + Future getUser() async { + try { + print("Starting getUser function in controller"); + SharedPreferences prefs = await SharedPreferences.getInstance(); + String? token = prefs.getString('token'); + + print("Token from SharedPreferences: $token"); + + HomeService homeService = HomeService(); + print("Calling homeService.getUser"); + user = await homeService.getUser(token: token); + print("homeService.getUser completed. User: $user"); + + update(); + + if (user != null) { + print("User fetched successfully: $user"); + } else { + print('Failed to fetch user data'); + } + } catch (e) { + print("Error in getUser controller function: ${e.toString()}"); + } } } + + + + + +// import 'package:cheminova/controller/home_service.dart'; +// import 'package:cheminova/models/user_model.dart'; +// import 'package:get/get.dart'; +// import 'package:shared_preferences/shared_preferences.dart'; +// +// import '../notification_service.dart'; +// +// class HomeController extends GetxController { +// final HomeService homeService = HomeService(); +// NotificationServices notificationServices = NotificationServices(); +// +// +// +// var userModel = UserModel(id: '', uniqueId: '', name: '', email: '', phone: '', role: '', sbu: '', createdAt: '', updatedAt: '' +// +// ).obs; // Observable for UserModel +// +// @override +// void onInit() { +// getUser(); +// super.onInit(); +// notificationServices.requestNotificationPermission(); +// notificationServices.getDeviceToken().then((value) { +// print('Device Token: $value'); +// fcmToken(); +// }); +// } +// +// Future fcmToken() async { +// SharedPreferences prefs = await SharedPreferences.getInstance(); +// String? token = prefs.getString('token'); +// final fcmToken = await NotificationServices().getDeviceToken(); +// print('fcmToken: $fcmToken'); +// homeService.fcmToken({"fcmToken": fcmToken}, token!); +// } +// +// Future getUser() async { +// SharedPreferences prefs = await SharedPreferences.getInstance(); +// +// String? token = prefs.getString('token'); +// +// userModel = (await homeService.getUser(token: token)) as dynamic; +// +// +// // if (userModel != null) { +// // if (userModel != null) {ddddd +// // userModel.value = userResponse as UserModel; // Update the userModel with API response +// // update(); // Notify GetX to rebuild widgets using GetBuilder/Obx +// // } +// } +// } +// diff --git a/lib/controller/home_service.dart b/lib/controller/home_service.dart index ba5ed41..173a24f 100644 --- a/lib/controller/home_service.dart +++ b/lib/controller/home_service.dart @@ -1,25 +1,54 @@ import 'package:cheminova/models/user_model.dart'; import 'package:cheminova/utils/common_api_service.dart'; import 'package:cheminova/utils/show_snackbar.dart'; +import '../utils/api_urls.dart'; class HomeService { - Future?> getUser({String? token}) async { + Future getUser({String? token}) async { try { - final response = await commonApiService>( + print("Starting getUser method in HomeService"); + print("Token: $token"); + print("URL: ${ApiUrls.profileUrl}"); + + final response = await commonApiService>( method: "GET", - url: "api/v1/user/details", - fromJson: (json) => json, - additionalHeaders: { // Pass the token here + url: ApiUrls.profileUrl, + additionalHeaders: { 'Authorization': 'Bearer $token', }, + fromJson: (json) => json as Map, ); - return response; + print("Raw API response: $response"); + + if (response == null) { + print("API response is null"); + showSnackbar("Failed to get a response from the server"); + return null; + } + + if (response['success'] != true) { + print("API call was not successful"); + showSnackbar(response['message'] ?? "API call was not successful"); + return null; + } + + if (response['myData'] == null) { + print("myData is null in the response"); + showSnackbar("User data not found in the response"); + return null; + } + + print("Attempting to create UserModel from myData"); + final userModel = UserModel.fromJson(response['myData']); + print("UserModel created successfully: $userModel"); + return userModel; } catch (e) { - showSnackbar(e.toString()); + print("Error in getUser: ${e.toString()}"); + showSnackbar("Error fetching user data: ${e.toString()}"); + return null; } - return null; } - -} +// ... keep the fcmToken method as it was +} \ No newline at end of file diff --git a/lib/controller/place_order_controller.dart b/lib/controller/place_order_controller.dart index a2e56c1..0cfe30e 100644 --- a/lib/controller/place_order_controller.dart +++ b/lib/controller/place_order_controller.dart @@ -43,87 +43,3 @@ class OrderPlacedController extends GetxController { // isLoading.value = false; // } } - - - - - - - - -// import 'package:cheminova/controller/place_order_service.dart'; -// import 'package:cheminova/models/category_model.dart'; -// import 'package:get/get.dart'; -// import 'package:shared_preferences/shared_preferences.dart'; -// import '../models/oder_place_model.dart'; -// import 'cart_controller.dart'; -// -// -// class OrderPlacedController extends GetxController { -// final OrderPlacedService _orderPlacedService = OrderPlacedService(); -// final CartController _cartController = Get.find(); -// Rx placedOrder1 = Rx(null); -// RxBool isLoading = false.obs; -// -// // Method to place an order with provided details -// Future placeOrder() async { -// SharedPreferences prefs = await SharedPreferences.getInstance(); -// String? token = prefs.getString('token'); -// -// // Prepare order details -// final order = PlacedOrderModel( -// paymentMode: 'cheque', -// shipTo: '456, Park Street, Kolkata, West Bengal - 700016', -// billTo: '456, Park Street, Kolkata, West Bengal - 700016', -// subtotal: 4000, -// gstTotal: 18, -// grandTotal: 4720, -// orderItems: [ -// OrderItem( -// id: "66cc7869f02b935094127a27", -// sku: "BJD", -// name: "Zycor 60m.l", -// price: 4000, -// gst: 18, -// hsnCode: 200, -// description: "", -// productStatus: "Active", -// addedBy: "ChemiNova", -// image: [], -// createdAt: DateTime.parse("2024-08-26T12:43:21.103Z"), -// updatedAt: DateTime.parse("2024-08-26T12:43:21.103Z"), -// count: 1, category:Category(id: "66cc7868f02b935094127a21", categoryName: "Insectiside") , -// brand: Brand(id: "66cc77fbf02b9350941279f5", brandName: "Old"), v: 0, -// ), -// ], -// ); -// -// try { -// isLoading.value = true; -// PlacedOrderModel? result = await _orderPlacedService.placeOrder(order, token); -// if (result != null) { -// placedOrder1.value = result; -// Get.snackbar('Success', 'Order placed successfully.'); -// // _cartController.clearCart(); // Clear cart items after order -// } else { -// Get.snackbar('Error', 'Failed to place order.'); -// } -// } catch (e) { -// Get.snackbar('Error', e.toString()); -// } finally { -// isLoading.value = false; -// } -// } -// -// String getShippingAddress() { -// return placedOrder1.value?.shipTo ?? 'No shipping address'; -// } -// -// String getBillingAddress() { -// return placedOrder1.value?.billTo ?? 'No billing address'; -// } -// -// String getPaymentMode() { -// return placedOrder1.value?.paymentMode ?? 'No payment mode'; -// } -// } diff --git a/lib/controller/place_order_service.dart b/lib/controller/place_order_service.dart index 79141cb..483b60f 100644 --- a/lib/controller/place_order_service.dart +++ b/lib/controller/place_order_service.dart @@ -14,7 +14,8 @@ class OrderPlacedService { //try { // logger.w("orderjson ${jsonEncode(orderDetails.toJson())}"); final response = await dio.post( - 'https://api.cnapp.co.in/api/order-place', // Ensure this is your correct endpoint + 'https://api.cnapp.co.in/api/rd-place-order', + // Ensure this is your correct endpoint data: jsonEncode(orderDetails.toJson()), options: Options( headers: { @@ -23,19 +24,10 @@ class OrderPlacedService { }, ), ); - //logger.w("Status code,${response.statusCode}"); if (response.statusCode != 201) { throw Exception('Failed to place order'); } } - // on DioException catch (e) { - // print("DioException: ${e.message}"); - // throw Exception('Failed to place order: ${e.message}'); - // } - // catch (e) { - // print("General Exception: ${e.toString()}"); - // throw Exception('Failed to place order: ${e.toString()}'); - // } } diff --git a/lib/controller/product_service.dart b/lib/controller/product_service.dart index b9796cb..123942c 100644 --- a/lib/controller/product_service.dart +++ b/lib/controller/product_service.dart @@ -7,9 +7,9 @@ class ProductService { try { String url; if (category != null && category.isNotEmpty) { - url = "/api/product/getAll/user?page=$page&category=$category"; + url = "/api/product/getAll/user/?page=$page&category=$category"; } else { - url = "/api/product/getAll/user?page=$page"; // URL without category filter + url = "/api/product/getAll/user/?page=$page"; // URL without category filter } final response = await commonApiService>( diff --git a/lib/models/user_model.dart b/lib/models/user_model.dart index b079da9..6c5f627 100644 --- a/lib/models/user_model.dart +++ b/lib/models/user_model.dart @@ -1,43 +1,76 @@ class UserModel { - String id; - String name; - String uniqueId; - String email; - bool isVerified; + final String id; + final String uniqueId; + final String name; + final String email; + final String mobileNumber; + final String designation; + final String userType; + final String? principalDistributer; + final String? addedBy; + final String? kyc; + final String? fcmToken; + final String createdAt; + final String updatedAt; + final String? mappedSC; UserModel({ required this.id, - required this.name, required this.uniqueId, + required this.name, required this.email, - required this.isVerified, + required this.mobileNumber, + required this.designation, + required this.userType, + this.principalDistributer, + this.addedBy, + this.kyc, + this.fcmToken, + required this.createdAt, + required this.updatedAt, + this.mappedSC, }); - // Factory constructor to create an instance of UserModel from a JSON map factory UserModel.fromJson(Map json) { return UserModel( - id: json['_id'] ??"", - name: json['name'] ??"Sarita", - uniqueId: json['uniqueId'] ??"1234", - email: json['email'] ??"", - isVerified: json['isVerified'] as bool? ??false, + id: json['_id'], + uniqueId: json['uniqueId'], + name: json['name'], + email: json['email'], + mobileNumber: json['mobile_number'], + designation: json['designation'], + userType: json['userType'], + principalDistributer: json['principal_distributer'], + addedBy: json['addedBy'], + kyc: json['kyc'], + fcmToken: json['fcm_token'], + createdAt: json['createdAt'], + updatedAt: json['updatedAt'], + mappedSC: json['mappedSC'], ); } - // Method to convert an instance of UserModel to a JSON map Map toJson() { return { '_id': id, - 'name': name, 'uniqueId': uniqueId, + 'name': name, 'email': email, - 'isVerified': isVerified, + 'mobile_number': mobileNumber, + 'designation': designation, + 'userType': userType, + 'principal_distributer': principalDistributer, + 'addedBy': addedBy, + 'kyc': kyc, + 'fcm_token': fcmToken, + 'createdAt': createdAt, + 'updatedAt': updatedAt, + 'mappedSC': mappedSC, }; } - // Override toString() to provide a readable output @override String toString() { - return 'UserModel{id: $id, name: $name, uniqueId: $uniqueId, email: $email, isVerified: $isVerified}'; + return 'UserModel{id: $id, uniqueId: $uniqueId, name: $name, email: $email, mobileNumber: $mobileNumber, designation: $designation, userType: $userType, principalDistributer: $principalDistributer, addedBy: $addedBy, kyc: $kyc, fcmToken: $fcmToken, createdAt: $createdAt, updatedAt: $updatedAt, mappedSC: $mappedSC}'; } -} +} \ No newline at end of file diff --git a/lib/screens/authentication/Profile.dart b/lib/screens/authentication/Profile.dart new file mode 100644 index 0000000..82f4588 --- /dev/null +++ b/lib/screens/authentication/Profile.dart @@ -0,0 +1,117 @@ +import 'package:cheminova/controller/home_controller.dart'; +import 'package:cheminova/widgets/my_drawer.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_svg/svg.dart'; +import 'package:get/get.dart'; + + +import '../../widgets/comman_background.dart'; +import '../../widgets/common_appbar.dart'; + +class ProfileScreen extends StatefulWidget { + // String? name; + // final String uniqueId; + // final String email; + // final String mobileNumber; + // final String designation; + + const ProfileScreen({ + Key? key, + + }) : super(key: key); + + @override + State createState() => _ProfileScreenState(); +} + +class _ProfileScreenState extends State { + + final homecontroller = Get.put(HomeController()); + @override + Widget build(BuildContext context) { + final user = homecontroller!.user; + return Stack( + children: [ + CommonBackground( + isFullWidth: true, + child: Scaffold( + drawer: MyDrawer(), + backgroundColor: Colors.transparent, + appBar: CommonAppBar( + title: const Text('Profile'), + backgroundColor: Colors.transparent, + elevation: 0, + actions: [ + IconButton( + onPressed: () { + Navigator.pop(context); + }, + icon: SvgPicture.asset( + 'assets/svg/back_arrow.svg', + ), + padding: const EdgeInsets.only(right: 20), + ), + ], + ), + body: SingleChildScrollView( + child: Column( + children: [ + Container( + padding: const EdgeInsets.all(20.0) + .copyWith(top: 15, bottom: 30), + margin: const EdgeInsets.symmetric( + horizontal: 30.0, vertical: 20.0), + decoration: BoxDecoration( + border: Border.all(color: Colors.white), + color: const Color(0xffB4D1E5).withOpacity(0.9), + borderRadius: BorderRadius.circular(26.0), + ), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + const SizedBox(height: 20), + _buildProfileItem('Name', user!.name), + _buildProfileItem('ID', user.uniqueId), + _buildProfileItem('Email ID', user.email), + // _buildProfileItem('Mobile Number', user.phone), + // _buildProfileItem('Designation', user.role), + ], + ), + ), + ], + ), + ), + ), + ), + ], + ); + } + + Widget _buildProfileItem(String label, String value) { + return Padding( + padding: const EdgeInsets.symmetric(vertical: 10), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + label, + style: const TextStyle( + fontSize: 16, + fontWeight: FontWeight.bold, + color: Color(0xff004791), + ), + ), + const SizedBox(height: 5), + Text( + value, + style: const TextStyle( + fontSize: 18, + color: Colors.black, + ), + ), + const Divider(color: Colors.grey), + ], + ), + ); + } +} diff --git a/lib/screens/authentication/controller/auth_service.dart b/lib/screens/authentication/controller/auth_service.dart index 8d20668..ec85a5e 100644 --- a/lib/screens/authentication/controller/auth_service.dart +++ b/lib/screens/authentication/controller/auth_service.dart @@ -5,7 +5,7 @@ class AuthService { Future?> login(Map data) async { try { final response = await commonApiService>( - url: '/api/v1/user/login/', + url: '/api/rd-login', method: 'POST', body: data, fromJson: (json) => json, // Simply return the JSON map as is @@ -21,7 +21,7 @@ class AuthService { Future?> forgotPassword(Map data) async { try { final response = await commonApiService>( - url: '/api/v1/user/password/forgot', + url: '/api/forgot-password', method: 'POST', body: data, fromJson: (json) => json, // Simply return the JSON map as is @@ -37,7 +37,7 @@ class AuthService { Future?> changePassword(Map data, {required String token}) async { try { final response = await commonApiService>( - url: '/api/v1/user/password/update', + url: '/api/rd-password/update', method: 'PUT', body: data, fromJson: (json) => json, // Simply return the JSON map as is diff --git a/lib/screens/product/product_detail_screen.dart b/lib/screens/product/product_detail_screen.dart index d58c752..17a0441 100644 --- a/lib/screens/product/product_detail_screen.dart +++ b/lib/screens/product/product_detail_screen.dart @@ -1,16 +1,13 @@ import 'package:cheminova/models/product_model.dart'; import 'package:cheminova/models/product_model1.dart'; -import 'package:cheminova/screens/order/checkout_screen.dart'; import 'package:cheminova/utils/show_snackbar.dart'; import 'package:cheminova/widgets/my_drawer.dart'; -import 'package:cheminova/widgets/product_card.dart'; import 'package:flutter/material.dart'; import 'package:flutter_svg/svg.dart'; import 'package:get/get.dart'; import 'package:google_fonts/google_fonts.dart'; import '../../controller/cart_controller.dart'; -import 'cart_screen.dart'; class ProductDetailScreen extends StatefulWidget { Product? productModel; @@ -119,7 +116,7 @@ class _ProductDetailScreenState extends State { child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ - Text("Product Name ",style: TextStyle(fontWeight: FontWeight.bold,fontSize: 16),), + const Text("Product Name ",style: TextStyle(fontWeight: FontWeight.bold,fontSize: 16),), Text( capitalizeFirstLetter(widget.productModel!.name), style: GoogleFonts.roboto( @@ -136,7 +133,7 @@ class _ProductDetailScreenState extends State { child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ - Text("Price ",style: TextStyle(fontSize: 16,fontWeight: FontWeight.bold),), + const Text("Price ",style: TextStyle(fontSize: 16,fontWeight: FontWeight.bold),), Text( "₹ ${widget.productModel!.price.toString()}", style: GoogleFonts.roboto( @@ -153,9 +150,9 @@ class _ProductDetailScreenState extends State { child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ - Text("Category ",style: TextStyle(fontWeight: FontWeight.bold,fontSize: 16),), + const Text("Category ",style: TextStyle(fontWeight: FontWeight.bold,fontSize: 16),), Text( - capitalizeFirstLetter(widget.productModel!.category!.categoryName), + capitalizeFirstLetter(widget.productModel!.category.categoryName), style: GoogleFonts.roboto( fontSize: 16, fontWeight: FontWeight.w400, @@ -170,7 +167,7 @@ class _ProductDetailScreenState extends State { padding: const EdgeInsets.symmetric(horizontal: 8), child: Row( children: [ - Text("Description",style: TextStyle(fontWeight: FontWeight.bold,fontSize: 16)), + const Text("Description",style: TextStyle(fontWeight: FontWeight.bold,fontSize: 16)), Text( capitalizeFirstLetter(widget.productModel!.description), style: GoogleFonts.roboto( diff --git a/lib/screens/product/product_mannual.dart b/lib/screens/product/product_manual.dart similarity index 100% rename from lib/screens/product/product_mannual.dart rename to lib/screens/product/product_manual.dart diff --git a/lib/screens/profile_screen.dart b/lib/screens/profile_screen.dart new file mode 100644 index 0000000..282e9bc --- /dev/null +++ b/lib/screens/profile_screen.dart @@ -0,0 +1,99 @@ +// import 'package:cheminova/controller/home_controller.dart'; +// import 'package:cheminova/widgets/my_drawer.dart'; +// import 'package:flutter/cupertino.dart'; +// import 'package:flutter/material.dart'; +// +// import '../widgets/comman_background.dart'; +// import '../widgets/common_appbar.dart'; +// +// class ProfileScreen extends StatelessWidget { +// const ProfileScreen({Key? key}) : super(key: key); +// +// @override +// Widget build(BuildContext context) { +// return Stack( +// children: [ +// CommonBackground( +// isFullWidth: true, +// child: Scaffold( +// drawer: const MyDrawer(), +// backgroundColor: Colors.transparent, +// appBar: CommonAppBar( +// title: const Text('Profile'), +// backgroundColor: Colors.transparent, +// elevation: 0, +// actions: [ +// IconButton( +// onPressed: () { +// Navigator.pop(context); +// }, +// icon: Image.asset('assets/Back_attendance.png'), +// padding: const EdgeInsets.only(right: 20), +// ), +// ], +// ), +// body: Consumer( +// builder: (context, profileProvider, child) { +// final profileData = profileProvider.profileResponse?.myData!; +// return SingleChildScrollView( +// child: Column( +// children: [ +// Container( +// padding: const EdgeInsets.all(20.0).copyWith(top: 15, bottom: 30), +// margin: const EdgeInsets.symmetric(horizontal: 30.0, vertical: 20.0), +// decoration: BoxDecoration( +// border: Border.all(color: Colors.white), +// color: const Color(0xffB4D1E5).withOpacity(0.9), +// borderRadius: BorderRadius.circular(26.0), +// ), +// child: Column( +// crossAxisAlignment: CrossAxisAlignment.start, +// children: [ +// SizedBox(height: 20), +// _buildProfileItem('Name', profileData?.name ?? ''), +// _buildProfileItem('ID', profileData?.uniqueId ?? ''), +// _buildProfileItem('Email ID', profileData?.email ?? ''), +// _buildProfileItem('Mobile Number', profileData?.mobileNumber ?? ''), +// _buildProfileItem('Designation', profileData?.designation ?? ''), +// ], +// ), +// ), +// ], +// ), +// ); +// }, +// ), +// ), +// ), +// ], +// ); +// } +// +// Widget _buildProfileItem(String label, String value) { +// return Padding( +// padding: const EdgeInsets.symmetric(vertical: 10), +// child: Column( +// crossAxisAlignment: CrossAxisAlignment.start, +// children: [ +// Text( +// label, +// style: const TextStyle( +// fontSize: 16, +// fontWeight: FontWeight.bold, +// color: Color(0xff004791), +// ), +// ), +// const SizedBox(height: 5), +// Text( +// value, +// style: const TextStyle( +// fontSize: 18, +// color: Colors.black, +// ), +// ), +// const Divider(color: Colors.grey), +// ], +// ), +// ); +// } +// } \ No newline at end of file diff --git a/lib/screens/splash_screen.dart b/lib/screens/splash_screen.dart index c4ff951..97d47ff 100644 --- a/lib/screens/splash_screen.dart +++ b/lib/screens/splash_screen.dart @@ -1,9 +1,11 @@ import 'dart:async'; import 'package:cheminova/screens/authentication/login_screen.dart'; +import 'package:cheminova/screens/home_screen.dart'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:google_fonts/google_fonts.dart'; +import 'package:shared_preferences/shared_preferences.dart'; class SplashScreen extends StatefulWidget { const SplashScreen({super.key}); @@ -15,12 +17,25 @@ class SplashScreen extends StatefulWidget { class _SplashScreenState extends State { @override void initState() { - super.initState(); - Timer(const Duration(seconds: 2), () { - Get.offAll(() => const LoginScreen()); + Timer(const Duration(seconds: 3), () { + checkLogin(); }); + super.initState(); + } + checkLogin() async { + SharedPreferences prefs = await SharedPreferences.getInstance(); + String? token = prefs.getString('token'); + if (token != null) { + Get.offAll(() => const HomeScreen()); + } else { + Get.offAll(() => const LoginScreen()); + } + } + + + @override Widget build(BuildContext context) { return Scaffold( diff --git a/lib/utils/api_urls.dart b/lib/utils/api_urls.dart new file mode 100644 index 0000000..336c6bf --- /dev/null +++ b/lib/utils/api_urls.dart @@ -0,0 +1,18 @@ +class ApiUrls { + // static const String baseUrl = 'https://cheminova-api-2.onrender.com/api/'; + static const String baseUrl = 'https://api.cnapp.co.in'; + static const String loginUrl = '/api/v1/user/login/'; + static const String profileUrl = '/api/rd-get-me'; + static const String forgetPasswordUrl = '/api/v1/user/password/forgot'; + static const String changePasswordUrl = '/api/v1/user/password/update'; + static const String fcmUrl = '/api/v1/user/fcm-token'; + static const String getCategoryUrl = '/api/category/getCategories'; + static const String getProductUrl = '/api/category/getCategories'; + static const String getProductManualUrl = '/api/productmanual/getall'; + static const String getKycUrl = '/api/kyc/getAll'; + static const String getNotificationUrl = '/api/get-notification-pd'; + static const String getPlacedOrderUrl ='/api/get-placed-order-pd'; + static const String getSinglePlacedOrderUrl ='/api/get-single-placed-order-pd'; + static const String placedOrderUrl ='${baseUrl}/api/order-place'; + +} diff --git a/lib/utils/common_api_service.dart b/lib/utils/common_api_service.dart index caa8abe..cf2fc74 100644 --- a/lib/utils/common_api_service.dart +++ b/lib/utils/common_api_service.dart @@ -1,93 +1,97 @@ import 'dart:io'; - import 'package:cheminova/utils/constants.dart'; import 'package:cheminova/utils/show_snackbar.dart'; import 'package:dio/dio.dart'; import 'package:shared_preferences/shared_preferences.dart'; +import 'package:pretty_dio_logger/pretty_dio_logger.dart'; Future commonApiService({ required String url, required String method, Map body = const {}, File? imageFile, - bool isformData = true, + bool isFormData = false, Map? additionalHeaders, required BodyType Function(Map) fromJson, }) async { try { - Dio dio = Dio(); - final Response response; + final dio = Dio(); - print("body : $body"); + // Add PrettyDioLogger + dio.interceptors.add(PrettyDioLogger( + requestHeader: true, + requestBody: true, + responseBody: true, + responseHeader: false, + error: true, + compact: true, + maxWidth: 90, + )); - // Fetch the token from SharedPreferences - SharedPreferences prefs = await SharedPreferences.getInstance(); - String? token = prefs.getString('token'); + final prefs = await SharedPreferences.getInstance(); + final token = prefs.getString('token'); - Map headers = { - 'Content-Type': isformData ? 'multipart/form-data' : 'application/json', + final headers = { + 'Content-Type': isFormData ? 'multipart/form-data' : 'application/json', + if (token != null) 'Authorization': 'Bearer $token', + ...?additionalHeaders, }; - if (token != null) { - headers['Authorization'] = 'Bearer $token'; - } + final options = Options(headers: headers); - if (additionalHeaders != null) { - headers.addAll(additionalHeaders); - } - Options options = Options(headers: headers); + dynamic requestData = isFormData ? FormData.fromMap(body) : body; - FormData formData = FormData.fromMap(body); - - if (imageFile != null) { - String fileName = imageFile.path.split('/').last; - formData.files.add(MapEntry( + if (imageFile != null && isFormData) { + final fileName = imageFile.path.split('/').last; + (requestData as FormData).files.add(MapEntry( "image", await MultipartFile.fromFile(imageFile.path, filename: fileName), )); } - if (method == "POST") { - response = await dio.post("$baseUrl$url", - data: isformData ? formData : body, options: options); - } else if (method == "PUT") { - response = await dio.put("$baseUrl$url", - data: isformData ? formData : body, options: options); - } else if (method == "DELETE") { - response = await dio.delete("$baseUrl$url", options: options); - } else if (method == "PATCH") { - response = await dio.patch("$baseUrl$url", - data: isformData ? formData : body, options: options); - } else { - response = await dio.get("$baseUrl$url", options: options); + final Response response; + switch (method.toUpperCase()) { + case "POST": + response = await dio.post("$baseUrl$url", data: requestData, options: options); + break; + case "PUT": + response = await dio.put("$baseUrl$url", data: requestData, options: options); + break; + case "DELETE": + response = await dio.delete("$baseUrl$url", options: options); + break; + case "PATCH": + response = await dio.patch("$baseUrl$url", data: requestData, options: options); + break; + case "GET": + response = await dio.get("$baseUrl$url", options: options); + break; + default: + throw Exception("Unsupported HTTP method: $method"); } - print("response of $url : $response"); - - if (url == "/api/v1/user/login/" && - response.data['token'] != null) { - prefs.setString('token', response.data['token']); + if (url == "/api/rd-login" && response.data['token'] != null) { + await prefs.setString('token', response.data['token']); } - if (url == "/api/territorymanager/my-profile") { + if (url == "/api/rd-get-me" && response.data['myData'] != null) { return fromJson(response.data['myData']); } return fromJson(response.data); } on DioException catch (e) { - print("dio exception $url ${e.response?.data}}"); - print("dio exception details: ${e.message} ${e.response?.statusCode}"); String errorMessage = "An error occurred"; - if (e.response?.data is Map) { errorMessage = e.response?.data['message'] ?? errorMessage; } else if (e.response?.data is String) { errorMessage = e.response?.data; } - //showSnackbar(errorMessage); + showSnackbar(errorMessage); + return null; } catch (e) { - print("exception $url $e"); + print("Unexpected error for $url: $e"); + showSnackbar("An unexpected error occurred"); + return null; } - return null; -} +} \ No newline at end of file diff --git a/lib/widgets/my_drawer.dart b/lib/widgets/my_drawer.dart index 7dcdc48..d000a08 100644 --- a/lib/widgets/my_drawer.dart +++ b/lib/widgets/my_drawer.dart @@ -5,6 +5,8 @@ import 'package:cheminova/screens/home_screen.dart'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; +import '../screens/authentication/Profile.dart'; + class MyDrawer extends StatefulWidget { const MyDrawer({super.key}); @@ -22,34 +24,37 @@ class _MyDrawerState extends State { children: [ SizedBox( height: 150, - child: Obx( - (){ - return DrawerHeader( + child:( + DrawerHeader( decoration: const BoxDecoration( color: Colors.black87, ), - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - mainAxisAlignment: MainAxisAlignment.start, - children: [ - Text( - homeController.user.value?.name?? "username", - style: const TextStyle( - color: Colors.white, - fontSize: 18, - ), - ), - Text( - homeController.user.value?.uniqueId?? 'Employee ID', - style: const TextStyle( - color: Colors.white, - fontSize: 20, - ), - ), - ], + child: GetBuilder( + init: homeController, + builder: (controller) { + return Column( + crossAxisAlignment: CrossAxisAlignment.start, + mainAxisAlignment: MainAxisAlignment.start, + children: [ + Text( + controller.user?.name?? "username", + style: const TextStyle( + color: Colors.white, + fontSize: 18, + ), + ), + Text( + controller.user?.uniqueId?? 'Employee ID', + style: const TextStyle( + color: Colors.white, + fontSize: 20, + ), + ), + ], + ); + } ), - ); - }, + ) ), ), ListTile( @@ -61,7 +66,7 @@ class _MyDrawerState extends State { leading: const Icon(Icons.account_circle), title: const Text('Profile'), onTap: () { - Navigator.pop(context); + Get.to(ProfileScreen()); }, ), ListTile( diff --git a/pubspec.lock b/pubspec.lock index 80a9be6..e143114 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -500,18 +500,18 @@ packages: dependency: transitive description: name: leak_tracker - sha256: "7f0df31977cb2c0b88585095d168e689669a2cc9b97c309665e3386f3e9d341a" + sha256: "3f87a60e8c63aecc975dda1ceedbc8f24de75f09e4856ea27daf8958f2f0ce05" url: "https://pub.dev" source: hosted - version: "10.0.4" + version: "10.0.5" leak_tracker_flutter_testing: dependency: transitive description: name: leak_tracker_flutter_testing - sha256: "06e98f569d004c1315b991ded39924b21af84cf14cc94791b8aea337d25b57f8" + sha256: "932549fb305594d82d7183ecd9fa93463e9914e1b67cacc34bc40906594a1806" url: "https://pub.dev" source: hosted - version: "3.0.3" + version: "3.0.5" leak_tracker_testing: dependency: transitive description: @@ -548,18 +548,18 @@ packages: dependency: transitive description: name: material_color_utilities - sha256: "0e0a020085b65b6083975e499759762399b4475f766c21668c4ecca34ea74e5a" + sha256: f7142bb1154231d7ea5f96bc7bde4bda2a0945d2806bb11670e30b850d56bdec url: "https://pub.dev" source: hosted - version: "0.8.0" + version: "0.11.1" meta: dependency: transitive description: name: meta - sha256: "7687075e408b093f36e6bbf6c91878cc0d4cd10f409506f7bc996f68220b9136" + sha256: bdb68674043280c3428e9ec998512fb681678676b3c54e773629ffe74419f8c7 url: "https://pub.dev" source: hosted - version: "1.12.0" + version: "1.15.0" mime: dependency: transitive description: @@ -664,6 +664,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.8" + pretty_dio_logger: + dependency: "direct main" + description: + name: pretty_dio_logger + sha256: "36f2101299786d567869493e2f5731de61ce130faa14679473b26905a92b6407" + url: "https://pub.dev" + source: hosted + version: "1.4.0" shared_preferences: dependency: "direct main" description: @@ -841,10 +849,10 @@ packages: dependency: transitive description: name: test_api - sha256: "9955ae474176f7ac8ee4e989dadfb411a58c30415bcfb648fa04b2b8a03afa7f" + sha256: "5b8a98dafc4d5c4c9c72d8b31ab2b23fc13422348d2997120294d3bac86b4ddb" url: "https://pub.dev" source: hosted - version: "0.7.0" + version: "0.7.2" timezone: dependency: transitive description: @@ -969,10 +977,10 @@ packages: dependency: transitive description: name: vm_service - sha256: "3923c89304b715fb1eb6423f017651664a03bf5f4b29983627c4da791f74a4ec" + sha256: f652077d0bdf60abe4c1f6377448e8655008eef28f128bc023f7b5e8dfeb48fc url: "https://pub.dev" source: hosted - version: "14.2.1" + version: "14.2.4" web: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 623e98d..4e6ca72 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -54,6 +54,7 @@ dependencies: flutter_local_notifications: ^17.2.1+2 firebase_crashlytics: ^4.0.4 firebase_analytics: ^11.2.1 + pretty_dio_logger: ^1.3.1 dev_dependencies: flutter_test: