Browse Source

fix: contact center hilang setelah refresh web

Yulian 3 weeks ago
parent
commit
c188da9f49

+ 1 - 1
lib/src/layouts/auth/login.dart

@@ -60,7 +60,7 @@ class _LoginPageState extends State<LoginPage> {
       companyName = lics['companyName']??'';
       serialNumber = lics['serialNumber']??'';
       companyLogo = lics['logo']??'';
-      if(U.isCompatibleWith(VersionKey.multiBahasa)){
+      if(await U.isCompatibleWith(VersionKey.multiBahasa)){
         lang = lics['_validLang'] ?? [];
       } else if(1698720817 <= lics['serverVersion']){
         lang = lics['languages'] != null ? lics['languages'].split(',') : [];

+ 1 - 1
lib/src/layouts/functions/account.dart

@@ -39,7 +39,7 @@ class AccountFunction{
     }
     try{
       var p;
-      if(U.isCompatibleWith(VersionKey.multiBahasa)){
+      if(await U.isCompatibleWith(VersionKey.multiBahasa)){
         p = {'userId': user['userId'], 'language': 'ID', '_language': code.toUpperCase()};
       } else {
         p = {'userId': user['userId'], 'language': code.toUpperCase()};

+ 1 - 1
lib/src/layouts/functions/home.dart

@@ -305,7 +305,7 @@ class HomeFunction{
       print(e.toString());
     }
 
-    if(!U.isCompatibleWith(VersionKey.multiBahasa)) return;
+    if(await U.isCompatibleWith(VersionKey.multiBahasa) == false) return;
     try{
       String url = '/api/messages/search/myForum';
       var res = await apiAuthProvider.getData(url, null, context);

+ 1 - 1
lib/src/layouts/functions/message.dart

@@ -29,7 +29,7 @@ class MessageFunction{
   }
 
   getDataMessages(BuildContext context) async {
-    if(U.isCompatibleWith(VersionKey.multiBahasa)) getDataForum(context);
+    if(await U.isCompatibleWith(VersionKey.multiBahasa)) getDataForum(context);
     String url = '/api/messages/search/myMessages';
     var val = await CacheMan.readData(url);
     if (val != null) {

+ 5 - 5
lib/src/layouts/mobile/menu_account.dart

@@ -57,15 +57,15 @@ class _MobAccountPageState extends State<MobAccountPage> {
 
   setToggle() async{
     var license = await U.getLicense();
+    if(await U.isCompatibleWith(VersionKey.multiBahasa)){
+      lang = license['_validLang'] ?? [];
+    } else {
+      lang = license['languages'] != null ? license['languages'].split(',') : [];
+    }
     setState(() {
       dnd = Provider.of<UserModule>(context, listen: false).dndStatus();
       serDis = U.servantDisplay();
       autoTranslate = U.autoTranslate();
-      if(U.isCompatibleWith(VersionKey.multiBahasa)){
-        lang = license['_validLang'] ?? [];
-      } else {
-        lang = license['languages'] != null ? license['languages'].split(',') : [];
-      }
     });
   }
 

+ 34 - 7
lib/src/layouts/mobile/menu_home.dart

@@ -55,13 +55,17 @@ class _MobHomePageState extends State<MobHomePage> with WidgetsBindingObserver {
       });
     });
     WidgetsBinding.instance.addObserver(this);
-    if(U.isCompatibleWith(VersionKey.multiBahasa)) {
-      homeFunc.getContactCenter(context);
-    }
+    checkCompatibility();
     // TODO: implement initState
     super.initState();
   }
 
+  checkCompatibility() async{
+    if(await U.isCompatibleWith(VersionKey.multiBahasa)) {
+      homeFunc.getContactCenter(context);
+    }
+  }
+
   @override
   void dispose() {
     WidgetsBinding.instance.removeObserver(this);
@@ -90,10 +94,33 @@ class _MobHomePageState extends State<MobHomePage> with WidgetsBindingObserver {
                       Row(
                         children: [
                           //TODO: tambah pengecekan server version disini
-                          U.isCompatibleWith(VersionKey.multiBahasa) && Provider.of<ServiceModule>(context, listen: false).contactCenter().toString() != ''  ? GestureDetector(
-                            onTap: () => launchUrl(Uri.parse(Provider.of<ServiceModule>(context, listen: false).contactCenter().toString())),
-                            child: U.iconsax('phone-number', color: Colors.white, size: 26.0)
-                          ):SizedBox(),
+                          FutureBuilder<bool>(
+                            future: U.isCompatibleWith(VersionKey.multiBahasa),
+                            builder: (context, snapshot) {
+                              if (!snapshot.hasData) {
+                                return SizedBox(); // or a loading spinner
+                              }
+
+                              final isCompatible = snapshot.data!;
+                              final hasContactCenter = Provider.of<ServiceModule>(context, listen: false)
+                                  .contactCenter()
+                                  .toString()
+                                  .isNotEmpty;
+
+                              if (isCompatible && hasContactCenter) {
+                                return GestureDetector(
+                                    onTap: () => launchUrl(Uri.parse(Provider.of<ServiceModule>(context, listen: false).contactCenter().toString())),
+                                    child: U.iconsax('phone-number', color: Colors.white, size: 26.0)
+                                ); // replace with the widget you want to show
+                              } else {
+                                return SizedBox(); // or nothing
+                              }
+                            },
+                          ),
+                          // U.isCompatibleWith(VersionKey.multiBahasa) && Provider.of<ServiceModule>(context, listen: false).contactCenter().toString() != ''  ? GestureDetector(
+                          //   onTap: () => launchUrl(Uri.parse(Provider.of<ServiceModule>(context, listen: false).contactCenter().toString())),
+                          //   child: U.iconsax('phone-number', color: Colors.white, size: 26.0)
+                          // ):SizedBox(),
                           SizedBox(width: 8),
                           GestureDetector(
                             child: Stack(

+ 8 - 1
lib/src/layouts/mobile/message_list.dart

@@ -289,7 +289,14 @@ class _MobMessageListPageState extends State<MobMessageListPage> {
     return Scaffold(
       backgroundColor: Colors.white,
       appBar: appBarTemplate(context: context, title: 'message'.tr()),
-      body: U.isCompatibleWith(VersionKey.multiBahasa) ? bodyForum() : chat(),
+      body: FutureBuilder(future: U.isCompatibleWith(VersionKey.multiBahasa), builder: (context, snapshot){
+        if (!snapshot.hasData) {
+          return chat(); // or a loading spinner
+        } else {
+          return bodyForum();
+        }
+      }),
+      // U.isCompatibleWith(VersionKey.multiBahasa) ? bodyForum() : chat(),
       floatingActionButton: Provider.of<MessageModule>(context).user().isNotEmpty &&
           Provider.of<MessageModule>(context).user()['canSendMessage'] &&
           Provider.of<MessageModule>(context).activeTab() == 0 ? FloatingActionButton(

+ 5 - 5
lib/src/layouts/web/menu_account.dart

@@ -56,15 +56,15 @@ class _WebAccountPageState extends State<WebAccountPage> {
 
   setToggle() async{
     var license = await U.getLicense();
+    if(await U.isCompatibleWith(VersionKey.multiBahasa)){
+      lang = license['_validLang'] ?? [];
+    } else {
+      lang = license['languages'] != null ? license['languages'].split(',') : [];
+    }
     setState(() {
       dnd = Provider.of<UserModule>(context, listen: false).dndStatus();
       serDis = U.servantDisplay();
       autoTranslate = U.autoTranslate();
-      if(U.isCompatibleWith(VersionKey.multiBahasa)){
-        lang = license['_validLang'] ?? [];
-      } else {
-        lang = license['languages'] != null ? license['languages'].split(',') : [];
-      }
     });
   }
 

+ 39 - 11
lib/src/layouts/web/menu_home.dart

@@ -51,14 +51,19 @@ class _WebHomePageState extends State<WebHomePage> {
         }
       });
     });
-    if(U.isCompatibleWith(VersionKey.multiBahasa)) {
-      homeFunc.getContactCenter(context);
-    }
+    checkCompatibility();
     // TODO: implement initState
     super.initState();
   }
   bool _timeLimit = false;
 
+  checkCompatibility() async{
+    var isCompatible = await U.isCompatibleWith(VersionKey.multiBahasa);
+    if(isCompatible) {
+      homeFunc.getContactCenter(context);
+    }
+  }
+
   @override
   Widget build(BuildContext context) {
     return Provider.of<UserModule>(context).user().isNotEmpty?Scaffold(
@@ -100,14 +105,37 @@ class _WebHomePageState extends State<WebHomePage> {
                             ),
                           ),
                           SizedBox(width: 30),
-                          U.isCompatibleWith(VersionKey.multiBahasa) && Provider.of<ServiceModule>(context, listen: false).contactCenter().toString() != ''  ? Container(
-                            padding: EdgeInsets.all(8),
-                            decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.all(Radius.circular(50))),
-                            child: GestureDetector(
-                                onTap: () => launchUrl(Uri.parse(Provider.of<ServiceModule>(context, listen: false).contactCenter().toString())),
-                                child: U.iconsax('phone-number', color: primaryColor, size: 26.0)
-                            ),
-                          ):SizedBox(),
+                          FutureBuilder(future: U.isCompatibleWith(VersionKey.multiBahasa), builder: (context, snapshot){
+                            if(!snapshot.hasData){
+                              return SizedBox();
+                            }
+                            final isCompatible = snapshot.data!;
+                            final hasContactCenter = Provider.of<ServiceModule>(context, listen: false)
+                                .contactCenter()
+                                .toString()
+                                .isNotEmpty;
+
+                            if (isCompatible && hasContactCenter) {
+                              return Container(
+                                padding: EdgeInsets.all(8),
+                                decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.all(Radius.circular(50))),
+                                child: GestureDetector(
+                                    onTap: () => launchUrl(Uri.parse(Provider.of<ServiceModule>(context, listen: false).contactCenter().toString())),
+                                    child: U.iconsax('phone-number', color: primaryColor, size: 26.0)
+                                ),
+                              ); // replace with the widget you want to show
+                            } else {
+                              return SizedBox(); // or nothing
+                            }
+                          }),
+                          // U.isCompatibleWith(VersionKey.multiBahasa) && Provider.of<ServiceModule>(context, listen: false).contactCenter().toString() != ''  ? Container(
+                          //   padding: EdgeInsets.all(8),
+                          //   decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.all(Radius.circular(50))),
+                          //   child: GestureDetector(
+                          //       onTap: () => launchUrl(Uri.parse(Provider.of<ServiceModule>(context, listen: false).contactCenter().toString())),
+                          //       child: U.iconsax('phone-number', color: primaryColor, size: 26.0)
+                          //   ),
+                          // ):SizedBox(),
                           SizedBox(width: 10),
                           GestureDetector(
                             child: Container(

+ 8 - 1
lib/src/layouts/web/message_list.dart

@@ -1087,7 +1087,14 @@ class _WebMessageListPageState extends State<WebMessageListPage> {
             ),
           ),
           divider(),
-          U.isCompatibleWith(VersionKey.multiBahasa) ? tabForum() : SizedBox(),
+          FutureBuilder(future: U.isCompatibleWith(VersionKey.multiBahasa), builder: (context, snapshot){
+            if(!snapshot.hasData){
+              return SizedBox();
+            } else {
+              return tabForum();
+            }
+          }),
+          // U.isCompatibleWith(VersionKey.multiBahasa) ? tabForum() : SizedBox(),
           Expanded(
             child: Container(
               width: double.infinity, height: double.infinity,

+ 17 - 2
lib/src/utils/U.dart

@@ -349,10 +349,25 @@ class U {
     return version <= _serverVersion;
   }
 
-  static isCompatibleWith(VersionKey key){
-    return C.get(key.name.toString()) <= _serverVersion;
+  static final Map<String, Future<bool>> _compatibilityCache = {};
+  static Future<bool> isCompatibleWith(VersionKey key) {
+    final keyName = key.name.toString();
+
+    if (_compatibilityCache.containsKey(keyName)) {
+      return _compatibilityCache[keyName]!;
+    }
+
+    final future = _loadAndCompare(keyName);
+    _compatibilityCache[keyName] = future;
+    return future;
   }
 
+  static Future<bool> _loadAndCompare(String keyName) async {
+    final value = await C.get(keyName);
+    return value <= _serverVersion;
+  }
+
+
   static int retServerVersion(){
     return _serverVersion;
   }