From 62b3483a6d468d83a576bf0ab5d98b32ffe17f4a Mon Sep 17 00:00:00 2001 From: vyPal Date: Wed, 20 Sep 2023 13:52:44 +0200 Subject: [PATCH] Replace baseUrl with remoteconfig --- lib/home.dart | 15 ++++++++++++--- lib/icanteen_setup.dart | 3 ++- lib/load.dart | 3 ++- lib/main.dart | 15 +++++++++++++++ lib/message.dart | 3 ++- lib/messages.dart | 3 ++- lib/timetable.dart | 3 ++- 7 files changed, 37 insertions(+), 8 deletions(-) diff --git a/lib/home.dart b/lib/home.dart index 712518e..27574b5 100644 --- a/lib/home.dart +++ b/lib/home.dart @@ -6,6 +6,7 @@ import 'package:dio_http_cache/dio_http_cache.dart'; import 'package:eduapge2/icanteen_setup.dart'; import 'package:eduapge2/message.dart'; import 'package:eduapge2/messages.dart'; +import 'package:firebase_remote_config/firebase_remote_config.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_session_manager/flutter_session_manager.dart'; @@ -144,7 +145,7 @@ final _shorebirdCodePush = ShorebirdCodePush(); class HomePageState extends State { final GlobalKey scaffoldKey = GlobalKey(); late SharedPreferences sharedPreferences; - String baseUrl = "https://lobster-app-z6jfk.ondigitalocean.app/api"; + String baseUrl = FirebaseRemoteConfig.instance.getString("baseUrl"); late Response response; Dio dio = Dio(); @@ -306,8 +307,16 @@ class HomePageState extends State { final buildName = packageInfo.version; try { - final response = await dio.get( - 'https://api.github.com/repos/DislikesSchool/EduPage2/releases/latest'); + final response = await dio + .get( + 'https://api.github.com/repos/DislikesSchool/EduPage2/releases/latest') + .catchError((r) { + return Response( + requestOptions: RequestOptions(path: r.path), statusCode: 500); + }); + if (response.statusCode == 500) { + return; + } final responseData = response.data; // Extract the tag_name from the response JSON and remove the "v" prefix if present diff --git a/lib/icanteen_setup.dart b/lib/icanteen_setup.dart index da5db9b..9539c76 100644 --- a/lib/icanteen_setup.dart +++ b/lib/icanteen_setup.dart @@ -1,4 +1,5 @@ import 'package:dio/dio.dart'; +import 'package:firebase_remote_config/firebase_remote_config.dart'; import 'package:flutter/material.dart'; import 'package:flutter_session_manager/flutter_session_manager.dart'; import 'package:shared_preferences/shared_preferences.dart'; @@ -22,7 +23,7 @@ class ICanteenSetupScreenState extends State { Dio dio = Dio(); - String baseUrl = "https://lobster-app-z6jfk.ondigitalocean.app/api"; + String baseUrl = FirebaseRemoteConfig.instance.getString("baseUrl"); AppLocalizations? local; diff --git a/lib/load.dart b/lib/load.dart index 854359e..f5a632b 100644 --- a/lib/load.dart +++ b/lib/load.dart @@ -4,6 +4,7 @@ import 'package:dio/dio.dart'; import 'package:dio_http_cache/dio_http_cache.dart'; import 'package:eduapge2/login.dart'; import 'package:firebase_performance/firebase_performance.dart'; +import 'package:firebase_remote_config/firebase_remote_config.dart'; import 'package:flutter/material.dart'; import 'package:flutter_session_manager/flutter_session_manager.dart'; import 'package:shared_preferences/shared_preferences.dart'; @@ -33,7 +34,7 @@ class LoadingScreenState extends State { double progress = 0.0; String loaderText = "Loading..."; - String baseUrl = "https://lobster-app-z6jfk.ondigitalocean.app/api"; + String baseUrl = FirebaseRemoteConfig.instance.getString("baseUrl"); late AppLocalizations? local; diff --git a/lib/main.dart b/lib/main.dart index 2af3248..f86b637 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -6,6 +6,7 @@ import 'package:eduapge2/load.dart'; import 'package:eduapge2/messages.dart'; import 'package:eduapge2/timetable.dart'; import 'package:firebase_analytics/firebase_analytics.dart'; +import 'package:firebase_remote_config/firebase_remote_config.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_session_manager/flutter_session_manager.dart'; @@ -113,7 +114,21 @@ class PageBaseState extends State { super.setState(fn); } + initRemoteConfig() async { + final remoteConfig = FirebaseRemoteConfig.instance; + await remoteConfig.setConfigSettings(RemoteConfigSettings( + fetchTimeout: const Duration(minutes: 1), + minimumFetchInterval: const Duration(hours: 1), + )); + await remoteConfig.setDefaults(const { + "baseUrl": "https://lobster-app-z6jfk.ondigitalocean.app", + }); + await remoteConfig.fetchAndActivate(); + baseUrl = remoteConfig.getString("baseUrl"); + } + getMsgs() async { + await initRemoteConfig(); var msgs = await sessionManager.get('messages'); var ic = await sessionManager.get('iCanteenEnabled'); if (ic == true) { diff --git a/lib/message.dart b/lib/message.dart index 7d430f2..1c7a201 100644 --- a/lib/message.dart +++ b/lib/message.dart @@ -1,3 +1,4 @@ +import 'package:firebase_remote_config/firebase_remote_config.dart'; import 'package:flutter/material.dart'; import 'package:flutter_cached_pdfview/flutter_cached_pdfview.dart'; import 'package:flutter_linkify/flutter_linkify.dart'; @@ -24,7 +25,7 @@ class MessagePage extends StatefulWidget { class MessagePageState extends State { late SessionManager sessionManager; late SharedPreferences sharedPreferences; - String baseUrl = "https://lobster-app-z6jfk.ondigitalocean.app/api"; + String baseUrl = FirebaseRemoteConfig.instance.getString("baseUrl"); bool loading = true; Dio dio = Dio(); diff --git a/lib/messages.dart b/lib/messages.dart index 20e1d11..c82e8da 100644 --- a/lib/messages.dart +++ b/lib/messages.dart @@ -3,6 +3,7 @@ import 'dart:convert'; import 'package:dio/dio.dart'; import 'package:dio_http_cache/dio_http_cache.dart'; import 'package:eduapge2/message.dart'; +import 'package:firebase_remote_config/firebase_remote_config.dart'; import 'package:flutter/material.dart'; import 'package:flutter_session_manager/flutter_session_manager.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; @@ -64,7 +65,7 @@ class TimeTablePageState extends State { SharedPreferences sp = await SharedPreferences.getInstance(); if (sp.getBool('quickstart') ?? false) { String token = sp.getString("token")!; - String baseUrl = "https://lobster-app-z6jfk.ondigitalocean.app/api"; + String baseUrl = FirebaseRemoteConfig.instance.getString("baseUrl"); Dio dio = Dio(); Response response = await dio.get( "$baseUrl/messages", diff --git a/lib/timetable.dart b/lib/timetable.dart index d231d8f..a7c2f3c 100644 --- a/lib/timetable.dart +++ b/lib/timetable.dart @@ -1,5 +1,6 @@ import 'dart:convert'; +import 'package:firebase_remote_config/firebase_remote_config.dart'; import 'package:flutter/material.dart'; import 'package:dio/dio.dart'; import 'package:dio_http_cache/dio_http_cache.dart'; @@ -18,7 +19,7 @@ class TimeTablePage extends StatefulWidget { } class TimeTablePageState extends State { - String baseUrl = "https://lobster-app-z6jfk.ondigitalocean.app/api"; + String baseUrl = FirebaseRemoteConfig.instance.getString("baseUrl"); TimeTableData tt = TimeTableData(DateTime.now(), [ TimeTableClass("2", "ZAE", "STJI", "8:55", "9:40", "U32", 0, {}),