Skip to content

Commit b5cac13

Browse files
authored
Merge pull request #226 from amitamrutiya2210/issue-197-backend-connection
fix: RSSfeed get added without backend connection
2 parents 69d1b15 + 4880084 commit b5cac13

File tree

2 files changed

+162
-94
lines changed

2 files changed

+162
-94
lines changed

lib/Api/client_api.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,33 @@ class ClientApi {
118118
print(e.toString());
119119
}
120120
}
121+
122+
static Future<bool> checkClientOnline(BuildContext context) async {
123+
try {
124+
String url = Provider.of<ApiProvider>(context, listen: false).baseUrl +
125+
ApiProvider.getClientSettingsUrl;
126+
print('---CHECK CLIENT ONLINE---');
127+
print(url);
128+
Response response;
129+
Dio dio = new Dio();
130+
//Headers
131+
dio.options.headers['Accept'] = "application/json";
132+
dio.options.headers['Content-Type'] = "application/json";
133+
dio.options.headers['Connection'] = "keep-alive";
134+
dio.options.headers['Cookie'] =
135+
Provider.of<UserDetailProvider>(context, listen: false).token;
136+
response = await dio.get(
137+
url,
138+
);
139+
if (response.statusCode == 200) {
140+
return true;
141+
} else {
142+
return false;
143+
}
144+
} catch (e) {
145+
print('--ERROR--');
146+
print(e.toString());
147+
return false;
148+
}
149+
}
121150
}

lib/Components/RSSFeedHomePage.dart

Lines changed: 133 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'package:clipboard/clipboard.dart';
22
import 'package:contained_tab_bar_view/contained_tab_bar_view.dart';
3+
import 'package:flood_mobile/Api/client_api.dart';
34
import 'package:flood_mobile/Api/feeds_contents_api.dart';
45
import 'package:flood_mobile/Api/update_feed_api.dart';
56
import 'package:flood_mobile/Model/single_feed_and_response_model.dart';
@@ -693,60 +694,79 @@ class _RSSFeedHomePageState extends State<RSSFeedHomePage>
693694
width: 150,
694695
height: 50,
695696
child: ElevatedButton(
696-
onPressed: () {
697-
if (_feedformKey.currentState!
698-
.validate() ==
699-
true) {
700-
setState(() {
701-
if (isUpdateFeedSelected ==
702-
false) {
703-
FeedsApi.addFeeds(
704-
type: "feed",
705-
id: "43",
706-
label: labelController
707-
.text,
708-
feedurl:
709-
urlController.text,
710-
interval: int.parse(
711-
intervalController
712-
.text),
713-
count: 0,
714-
context: context,
715-
);
716-
}
717-
final addFeedSnackbar =
718-
addFloodSnackBar(
719-
SnackbarType
720-
.information,
721-
'New Feed added successfully',
722-
'Dismiss');
723-
724-
ScaffoldMessenger.of(
725-
context)
726-
.showSnackBar(
727-
addFeedSnackbar);
728-
FeedsApi
729-
.listAllFeedsAndRules(
730-
context: context);
731-
clearFeedsFields();
732-
if (isUpdateFeedSelected) {
733-
UpdateFeedApi.updateFeed(
697+
onPressed: () async {
698+
if (await ClientApi
699+
.checkClientOnline(
700+
context)) {
701+
if (_feedformKey.currentState!
702+
.validate() ==
703+
true) {
704+
setState(() {
705+
if (isUpdateFeedSelected ==
706+
false) {
707+
FeedsApi.addFeeds(
734708
type: "feed",
735709
id: updateFeedId,
736710
label: labelController
737711
.text,
738712
feedurl: urlController
739713
.text,
740-
context: context,
741714
interval: int.parse(
742715
intervalController
743716
.text),
744-
count: 1);
717+
count: 0,
718+
context: context,
719+
);
720+
}
721+
final addFeedSnackbar =
722+
addFloodSnackBar(
723+
SnackbarType
724+
.information,
725+
'New Feed added successfully',
726+
'Dismiss');
727+
728+
ScaffoldMessenger.of(
729+
context)
730+
.showSnackBar(
731+
addFeedSnackbar);
745732
FeedsApi
746733
.listAllFeedsAndRules(
747734
context: context);
748-
}
749-
});
735+
clearFeedsFields();
736+
if (isUpdateFeedSelected) {
737+
UpdateFeedApi.updateFeed(
738+
type: "feed",
739+
id: updateFeedId,
740+
label:
741+
labelController
742+
.text,
743+
feedurl:
744+
urlController
745+
.text,
746+
context: context,
747+
interval: int.parse(
748+
intervalController
749+
.text),
750+
count: 1);
751+
FeedsApi
752+
.listAllFeedsAndRules(
753+
context:
754+
context);
755+
}
756+
});
757+
}
758+
} else {
759+
final connectionCheckSnackbar =
760+
addFloodSnackBar(
761+
SnackbarType.caution,
762+
'Please check your backend connection',
763+
'Dismiss');
764+
765+
ScaffoldMessenger.of(context)
766+
.clearSnackBars();
767+
ScaffoldMessenger.of(context)
768+
.showSnackBar(
769+
connectionCheckSnackbar);
750770
}
751771
},
752772
style: ElevatedButton.styleFrom(
@@ -2209,59 +2229,78 @@ class _RSSFeedHomePageState extends State<RSSFeedHomePage>
22092229
width: 150,
22102230
height: 50,
22112231
child: ElevatedButton(
2212-
onPressed: () {
2213-
if (_rulesformKey.currentState!
2214-
.validate() ==
2215-
true) {
2216-
setState(() {
2217-
isNewDownloadRules = true;
2218-
RulesApi.addRules(
2219-
type: "rule",
2220-
label:
2221-
labelRulesController
2222-
.text,
2223-
feedIDs: [
2224-
feedidgetter(
2225-
applicableFeedSelected
2226-
.toString(),
2227-
model
2228-
.rssFeedsList)
2229-
.toString()
2230-
],
2231-
field: "test",
2232-
matchpattern:
2233-
matchpatternController
2234-
.text,
2235-
excludepattern:
2236-
excludepatternController
2237-
.text,
2238-
destination:
2239-
destinationController
2240-
.text,
2241-
tags: [
2242-
tagsController.text
2243-
],
2244-
startOnLoad: startOnLoad,
2245-
isBasePath: useAsBasePath,
2246-
count: 0,
2247-
context: context,
2248-
);
2249-
final addRuleSnackbar =
2250-
addFloodSnackBar(
2251-
SnackbarType
2252-
.information,
2253-
'New Rule added successfully',
2254-
'Dismiss');
2232+
onPressed: () async {
2233+
if (await ClientApi
2234+
.checkClientOnline(
2235+
context)) {
2236+
if (_rulesformKey
2237+
.currentState!
2238+
.validate() ==
2239+
true) {
2240+
setState(() {
2241+
isNewDownloadRules = true;
2242+
RulesApi.addRules(
2243+
type: "rule",
2244+
label:
2245+
labelRulesController
2246+
.text,
2247+
feedIDs: [
2248+
feedidgetter(
2249+
applicableFeedSelected
2250+
.toString(),
2251+
model
2252+
.rssFeedsList)
2253+
.toString()
2254+
],
2255+
field: "test",
2256+
matchpattern:
2257+
matchpatternController
2258+
.text,
2259+
excludepattern:
2260+
excludepatternController
2261+
.text,
2262+
destination:
2263+
destinationController
2264+
.text,
2265+
tags: [
2266+
tagsController.text
2267+
],
2268+
startOnLoad:
2269+
startOnLoad,
2270+
isBasePath:
2271+
useAsBasePath,
2272+
count: 0,
2273+
context: context,
2274+
);
2275+
final addRuleSnackbar =
2276+
addFloodSnackBar(
2277+
SnackbarType
2278+
.information,
2279+
'New Rule added successfully',
2280+
'Dismiss');
22552281

2256-
ScaffoldMessenger.of(
2257-
context)
2258-
.showSnackBar(
2259-
addRuleSnackbar);
2260-
FeedsApi
2261-
.listAllFeedsAndRules(
2262-
context: context);
2263-
clearDownloadRulesFields();
2264-
});
2282+
ScaffoldMessenger.of(
2283+
context)
2284+
.showSnackBar(
2285+
addRuleSnackbar);
2286+
FeedsApi
2287+
.listAllFeedsAndRules(
2288+
context: context);
2289+
clearDownloadRulesFields();
2290+
});
2291+
}
2292+
} else {
2293+
final connectionCheckSnackbar =
2294+
addFloodSnackBar(
2295+
SnackbarType.caution,
2296+
'Please check your backend connection',
2297+
'Dismiss');
2298+
2299+
ScaffoldMessenger.of(context)
2300+
.clearSnackBars();
2301+
ScaffoldMessenger.of(context)
2302+
.showSnackBar(
2303+
connectionCheckSnackbar);
22652304
}
22662305
},
22672306
style: ElevatedButton.styleFrom(

0 commit comments

Comments
 (0)