mobileApplicationsKonectarApp/lib/views/notifications.dart

605 lines
24 KiB
Dart
Raw Normal View History

2024-01-04 07:06:37 +00:00
import 'dart:collection';
import 'dart:convert';
import 'package:pwa_ios/utils/apicall.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:pwa_ios/utils/constants.dart';
import 'package:pwa_ios/utils/util.dart';
import 'package:pwa_ios/widgets/webview_popup.dart';
class NotificationsScreen extends StatefulWidget {
const NotificationsScreen({super.key});
@override
State<NotificationsScreen> createState() => _NotificationsScreenState();
}
class _NotificationsScreenState extends State<NotificationsScreen>
with WidgetsBindingObserver {
final GlobalKey webViewKey = GlobalKey();
ValueNotifier<bool> isLoading = ValueNotifier(true);
InAppWebViewController? webViewController;
late Future<String> _username;
late Future<String> _useremail;
late Future<String> _domain;
late Future<String> _key;
late Future<String> _token;
final Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
late final String name, email, key;
late String token = '';
late bool logout = false;
InAppWebViewSettings sharedSettings = InAppWebViewSettings(
// enable opening windows support
supportMultipleWindows: true,
javaScriptCanOpenWindowsAutomatically: true,
// useful for identifying traffic, e.g. in Google Analytics.
applicationNameForUserAgent: 'My PWA App Name',
// Override the User Agent, otherwise some external APIs, such as Google and Facebook logins, will not work
// because they recognize and block the default WebView User Agent.
userAgent:
'Mozilla/5.0 (Linux; Android 13) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.5304.105 Mobile Safari/537.36',
disableDefaultErrorPage: true,
// enable iOS service worker feature limited to defined App Bound Domains
limitsNavigationsToAppBoundDomains: true);
@override
void initState() {
WidgetsBinding.instance.addObserver(this);
super.initState();
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
// init();
// showDialog(
// context: context,
// builder: (context) {
// return Align(
// alignment: Alignment.center,
// child: UIBox(
// 'Notifications',
// overrides: [
// Override(
// 'd32e1776-1467-571c-8a86-99f564425733',
// builder: (context, node, child, children) {
// return GestureDetector(
// onTap: () {
// debugPrint('Tapped!');
// Navigator.pop(context);
// },
// child: Container(
// color: Colors.blue,
// height: 40,
// child: child,
// ) // You can even use the original child
// );
// },
// ),
// Override(
// '37326013-d2b7-5f86-96da-8764757eefdd',
// builder: (context, node, child, children) {
// return Text('\n\nTest Notifications\n\n\n\n');
// },
// )
// ],
// ));
// },
// );
_username = _prefs.then((SharedPreferences prefs) {
name = prefs.getString('username') ?? "";
return prefs.getString('username') ?? "";
});
_useremail = _prefs.then((SharedPreferences prefs) {
email = prefs.getString('useremail') ?? "";
return prefs.getString('useremail') ?? "";
});
_domain = _prefs.then((SharedPreferences prefs) {
return prefs.getString('domain') ?? "";
});
_key = _prefs.then((SharedPreferences prefs) {
key = prefs.getString('secretkey') ?? "";
return prefs.getString('secretkey') ?? "";
});
_token = _prefs.then((SharedPreferences prefs) {
token = prefs.getString('token') ?? "";
return prefs.getString('token') ?? "";
});
});
}
init() async {
// await ApiCall().listnotifications("ja");
}
@override
void dispose() {
webViewController = null;
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (!kIsWeb) {
if (webViewController != null &&
defaultTargetPlatform == TargetPlatform.android) {
if (state == AppLifecycleState.paused) {
pauseAll();
} else {
resumeAll();
}
}
}
}
void pauseAll() {
if (defaultTargetPlatform == TargetPlatform.android) {
webViewController?.pause();
}
webViewController?.pauseTimers();
}
void resumeAll() {
if (defaultTargetPlatform == TargetPlatform.android) {
webViewController?.resume();
}
webViewController?.resumeTimers();
}
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () async {
// detect Android back button click
final controller = webViewController;
if (controller != null) {
if (await controller.canGoBack()) {
controller.goBack();
return false;
}
}
return true;
},
child: SafeArea(
child: Scaffold(
body: Column(children: <Widget>[
Expanded(
child: Stack(
children: [
FutureBuilder<bool>(
future: isNetworkAvailable(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Container();
}
final bool networkAvailable = snapshot.data ?? false;
// Android-only
final cacheMode = networkAvailable
? CacheMode.LOAD_DEFAULT
: CacheMode.LOAD_CACHE_ELSE_NETWORK;
// iOS-only
final cachePolicy = networkAvailable
? URLRequestCachePolicy.USE_PROTOCOL_CACHE_POLICY
: URLRequestCachePolicy.RETURN_CACHE_DATA_ELSE_LOAD;
final webViewInitialSettings = sharedSettings.copy();
//webViewInitialSettings.cacheMode = cacheMode;
webViewInitialSettings.useShouldOverrideUrlLoading = true;
webViewInitialSettings
.javaScriptCanOpenWindowsAutomatically = true;
webViewInitialSettings.useShouldInterceptRequest = true;
webViewInitialSettings.useShouldInterceptAjaxRequest = true;
return InAppWebView(
key: webViewKey,
// initialData:
shouldInterceptRequest: (controller, request) async {
print("REQUEST URL ");
print(request.url);
return null;
},
initialUrlRequest: URLRequest(
url: WebUri("http://192.168.2.109:8001/forms/new"),
),
// token.isNotEmpty
// ? URLRequest(
// url: WebUri(
// "https://cardio-staging.konectar.io/notifications/"),
// headers: {
// "rows": "10",
// "page": "1",
// "sidx": "name",
// "sord": "desc"
// },
// method: "POST")
// : URLRequest(
// url: kPwaUri,
// headers: {
// "key": key,
// "email": email,
// "name": name,
// "key":
// "\$2a\$08\$XeBs/kLqAESRk/jWyNVsyeCjoOvxEmDT7/TK5xkLn23FJ/.5B5beK",
// // // "email": "scheepu@tikamobile.com",
// // // "name": "scheepu",
// },
// method: "GET"),
2024-01-04 07:06:37 +00:00
//cachePolicy: cachePolicy),
onReceivedServerTrustAuthRequest:
(controller, challenge) async {
return ServerTrustAuthResponse(
action: ServerTrustAuthResponseAction.PROCEED);
},
initialUserScripts: UnmodifiableListView<UserScript>([
UserScript(
source: """
document.getElementById('notifications').addEventListener('click', function(event) {
var randomText = Math.random().toString(36).slice(2, 7);
window.flutter_inappwebview.callHandler('requestDummyNotification', randomText);
});
""",
injectionTime:
UserScriptInjectionTime.AT_DOCUMENT_END)
]),
initialSettings: webViewInitialSettings,
onWebViewCreated: (controller) {
webViewController = controller;
controller.callAsyncJavaScript(
functionBody: "list_notifications_grid");
// message event listener
controller.addJavaScriptHandler(
handlerName: 'list_notifications_grid',
callback: (arguments) {
final String randomText =
arguments.isNotEmpty ? arguments[0] : '';
print("responses notifications");
print(arguments);
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text("some notification")));
},
);
},
shouldOverrideUrlLoading:
(controller, navigationAction) async {
// restrict navigation to target host, open external links in 3rd party apps
final uri = navigationAction.request.url;
if (uri != null &&
navigationAction.isForMainFrame &&
uri.host != kPwaHost &&
await canLaunchUrl(uri)) {
launchUrl(uri);
return NavigationActionPolicy.CANCEL;
}
return NavigationActionPolicy.ALLOW;
},
onLoadStop: (controller, url) async {
await controller.evaluateJavascript(source: """
window.addEventListener("list_notifications_grid", (event) => {
console.log(JSON.stringify(event.detail));
}, false);
""");
final SharedPreferences prefs = await _prefs;
var html = await controller.evaluateJavascript(
source:
"document.getElementById('notificationsList').innerText");
print("html");
print(html);
dynamic response = await controller.evaluateJavascript(
source: 'document.body.innerText');
print(jsonEncode(response));
// String response2 = await controller.evaluateJavascript(
// source: 'notificationsList');
// setState(() {
// isLoading.value = false;
// isLoading.addListener(() {});
// });
if (response.contains('token')) {
String token = response
.split(',')
.last
.split(':')[1]
.replaceAll('}', "")
.replaceAll('"', "");
setState(() {
_token = prefs
.setString('token', token)
.then((bool success) {
return _token;
});
});
print("response");
print(token);
print(
"https://cardio-staging.konectar.io/nested_logins/verify_url/$token/notifications/list_all_notifications");
// await ApiCall().listnotifications(
// "https://cardio-staging.konectar.io/nested_logins/verify_url/$token/notifications/list_all_notifications");
controller.loadUrl(
urlRequest: URLRequest(
url: WebUri(
"https://cardio-staging.konectar.io/nested_logins/verify_url/$token/user_settings/",
),
));
}
await ApiCall()
.listnotifications()
.then((value) => print("see value $value"));
// controller.loadUrl(
// urlRequest: URLRequest(
// url: WebUri(
// "https://cardio-staging.konectar.io/notifications/list_all_notifications",
// ),
// headers: {
// "rows": "10",
// "page": "1",
// "sidx": "name",
// "sord": "desc"
// },
// method: "POST"));
if (await isNetworkAvailable() &&
!(await isPWAInstalled())) {
// if network is available and this is the first timeß
setPWAInstalled();
}
},
onProgressChanged: (controller, progress) {
setState(() {
// loadingProgress = progress / 100;
});
},
shouldInterceptAjaxRequest:
(InAppWebViewController controller,
AjaxRequest ajaxRequest) async {
print("ajax urls");
print(ajaxRequest.url);
// ajaxRequest.headers!.setRequestHeader(
// "Cookie", "list_all_notifications");
return ajaxRequest;
},
onAjaxReadyStateChange:
(InAppWebViewController controller,
AjaxRequest ajaxRequest) async {
print("ajax request");
print(ajaxRequest.url);
print(ajaxRequest.status);
return AjaxRequestAction.PROCEED;
},
onAjaxProgress: (InAppWebViewController controller,
AjaxRequest ajaxRequest) async {
print("ajax response");
print(ajaxRequest.url);
print(ajaxRequest.status);
return AjaxRequestAction.PROCEED;
},
onReceivedError: (controller, request, error) async {
final isForMainFrame = request.isForMainFrame ?? true;
if (isForMainFrame && !(await isNetworkAvailable())) {
if (!(await isPWAInstalled())) {
await controller.loadData(
data: kHTMLErrorPageNotInstalled);
}
}
},
onCreateWindow: (controller, createWindowAction) async {
print("CREATES WINDOW");
showDialog(
context: context,
builder: (context) {
final popupWebViewSettings = sharedSettings.copy();
popupWebViewSettings.supportMultipleWindows = false;
popupWebViewSettings
.javaScriptCanOpenWindowsAutomatically = false;
return WebViewPopup(
createWindowAction: createWindowAction,
popupWebViewSettings: popupWebViewSettings);
},
);
return true;
},
);
},
)
],
),
),
])),
),
);
}
}
/*
Widget buildLocationDropdownWidget(
SectionList sectionItem, InteractionProvider provider) {
if (sectionItem.name == 'Country' && provider.countryList.isNotEmpty) {
print("country list ${provider.countryList}");
List<String> list =
provider.countryList.map((e) => e.countryName).toList();
provider.selectedCountry = list[0];
// dropdownvalue = provider.selectedCountry;
print("country list $list");
return returnCountryDropDown(sectionItem, provider, list);
}
if (sectionItem.name == 'State') {
List<String> list = provider.stateList.map((e) => e.stateName).toList();
provider.selectedState = provider.getStateId(list[0]);
// dropdownvalue = list[0];
return returnStateDropDown(sectionItem, provider, list);
}
if (sectionItem.name == 'City') {
List<String> list = provider.selectedState.isNotEmpty
? provider
.getCity(provider.selectedState)
.map((e) => e.cityName)
.toList()
: provider.cityList.map((e) => e.cityName).toList();
provider.selectedCity = list[0];
// dropdownvalue = provider.selectedCity;
return returnCityDropDown(sectionItem, provider, list);
}
return SizedBox.shrink();
}
Widget returnCountryDropDown(
var sectionItem, InteractionProvider provider, List<String> list) {
return Container(
width: 160,
decoration: BoxDecoration(
border: Border.all(width: 1.0, color: Colors.grey.shade800)),
child: Center(
child: DropdownButton<String>(
// Initial Value
value: dropdownvalue,
underline: const SizedBox.shrink(),
hint: Text('Select'),
// Down Arrow Icon
icon: const Icon(Icons.keyboard_arrow_down),
// Array list of items
items: provider.countryList.map((Country items) {
return DropdownMenuItem(
value: items.countryName,
child: Text(items.countryName),
);
}).toList(),
// After selecting the desired option,it will
// change button value to selected value
onChanged: (String? newValue) {
setState(() {
sectionItem['value'] = newValue!;
if (sectionItem['name'] == 'Country') {
provider.selectedCountry = provider.getCountryId(newValue);
print("see country id : ${provider.selectedCountry}");
}
// if (sectionItem['name'] == 'State') {
// provider.selectedState = newValue;
// }
// if (sectionItem['name'] == 'City') {
// provider.selectedCity = newValue;
// }
});
},
),
),
);
}
Widget returnStateDropDown(
var sectionItem, InteractionProvider provider, List<String> list) {
return Container(
width: 160,
decoration: BoxDecoration(
border: Border.all(width: 1.0, color: Colors.grey.shade800)),
child: Center(
child: DropdownButton<String>(
// Initial Value
value: dropdownvalue,
underline: const SizedBox.shrink(),
hint: Text('Select'),
// Down Arrow Icon
icon: const Icon(Icons.keyboard_arrow_down),
// Array list of items
items: provider.selectedCountry.isNotEmpty
? provider.stateList.map((States items) {
return DropdownMenuItem(
value: items.stateId,
child: Text(items.stateName),
);
}).toList()
: list.map((String items) {
return DropdownMenuItem(
value: items,
child: Text(items),
);
}).toList(),
// After selecting the desired option,it will
// change button value to selected value
onChanged: (String? newValue) {
setState(() {
sectionItem['value'] = newValue!;
if (provider.selectedCountry.isNotEmpty) {
provider.selectedState = provider.getStateId(newValue);
print("see state id : ${provider.selectedState}");
}
// if (sectionItem['name'] == 'State') {
// provider.selectedState = newValue;
// }
// if (sectionItem['name'] == 'City') {
// provider.selectedCity = newValue;
// }
});
},
),
),
);
}
Widget returnCityDropDown(
var sectionItem, InteractionProvider provider, List<String> list) {
return Container(
width: 160,
decoration: BoxDecoration(
border: Border.all(width: 1.0, color: Colors.grey.shade800)),
child: Center(
child: DropdownButton<String>(
// Initial Value
value: dropdownvalue,
underline: const SizedBox.shrink(),
hint: Text('Select'),
// Down Arrow Icon
icon: const Icon(Icons.keyboard_arrow_down),
// Array list of items
items: provider.cityList.map((City items) {
return DropdownMenuItem(
value: items.cityName,
child: Text(items.cityName),
);
}).toList(),
// After selecting the desired option,it will
// change button value to selected value
onChanged: (String? newValue) {
setState(() {
sectionItem['value'] = newValue!;
provider.selectedCity = provider.getCityId(newValue);
print("see city id : ${provider.selectedCity}");
// if (sectionItem['name'] == 'State') {
// provider.selectedState = newValue;
// }
// if (sectionItem['name'] == 'City') {
// provider.selectedCity = newValue;
// }
});
},
),
),
);
}
*/