-
Notifications
You must be signed in to change notification settings - Fork 5
Link TFConnect news to threefold_io RSS #625
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d184738
Add news screen from the xlm endpoint && add endpoint in flagsmith se…
AlaaElattar c219fb8
use T&C url from flagsmith server
AlaaElattar fb38aa7
Merge branch 'development_cleanup' into development_cleanup_news
AlaaElattar 111cbcb
handle pr comments
AlaaElattar c27c0a4
remove space in loading articles text
AlaaElattar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,280 @@ | ||
| import 'dart:convert'; | ||
| import 'package:flutter/material.dart'; | ||
| import 'package:flutter_widget_from_html/flutter_widget_from_html.dart'; | ||
| import 'package:http/http.dart' as http; | ||
| import 'package:threebotlogin/helpers/globals.dart'; | ||
| import 'package:threebotlogin/widgets/layout_drawer.dart'; | ||
| import 'package:xml2json/xml2json.dart'; | ||
| import 'package:url_launcher/url_launcher.dart'; | ||
| import 'package:timeago/timeago.dart' as timeago; | ||
|
|
||
| class NewsScreen extends StatefulWidget { | ||
| const NewsScreen({super.key}); | ||
|
|
||
| @override | ||
| State<NewsScreen> createState() => _NewsScreenState(); | ||
| } | ||
|
|
||
| class _NewsScreenState extends State<NewsScreen> { | ||
| final Xml2Json xml2json = Xml2Json(); | ||
| List allArticles = []; | ||
| List visibleArticles = []; | ||
| bool isLoading = false; | ||
| bool isInitialLoading = true; | ||
| int articlesPerPage = 5; | ||
| int currentPage = 0; | ||
| final ScrollController _scrollController = ScrollController(); | ||
| final newsUrl = Globals().newsUrl; | ||
|
|
||
| Future<void> getArticles() async { | ||
| setState(() { | ||
| isLoading = true; | ||
| if (currentPage == 0) { | ||
| isInitialLoading = true; | ||
| } | ||
| }); | ||
|
|
||
| final url = Uri.parse(newsUrl); | ||
| final response = await http.get(url); | ||
|
|
||
| xml2json.parse(response.body); | ||
|
|
||
| var jsondata = xml2json.toGData(); | ||
| var data = json.decode(jsondata); | ||
|
|
||
| var allEntries = data['feed']['entry'] ?? []; | ||
|
|
||
| setState(() { | ||
| allArticles = allEntries; | ||
| loadMoreArticles(); | ||
|
|
||
| isLoading = false; | ||
| if (currentPage > 0) { | ||
| isInitialLoading = false; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| void loadMoreArticles() { | ||
| final startIndex = currentPage * articlesPerPage; | ||
| final endIndex = startIndex + articlesPerPage; | ||
|
|
||
| if (startIndex < allArticles.length) { | ||
| setState(() { | ||
| isLoading = true; | ||
| visibleArticles.addAll( | ||
| allArticles.sublist( | ||
| startIndex, endIndex.clamp(0, allArticles.length)), | ||
| ); | ||
| currentPage++; | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| @override | ||
| void initState() { | ||
| super.initState(); | ||
| getArticles(); | ||
| _scrollController.addListener(() { | ||
| if (_scrollController.position.pixels == | ||
| _scrollController.position.maxScrollExtent) { | ||
| loadMoreArticles(); | ||
| } else { | ||
| setState(() { | ||
| isLoading = false; | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| @override | ||
| void dispose() { | ||
| _scrollController.dispose(); | ||
| super.dispose(); | ||
| } | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) { | ||
| return LayoutDrawer( | ||
| titleText: 'News', | ||
| content: Column( | ||
| children: [ | ||
| Expanded( | ||
| child: ListView.builder( | ||
| controller: _scrollController, | ||
| itemCount: visibleArticles.length + (isLoading ? 1 : 0), | ||
| itemBuilder: (context, index) { | ||
| if (index == visibleArticles.length && isInitialLoading) { | ||
| return Column( | ||
| mainAxisAlignment: MainAxisAlignment.center, | ||
| crossAxisAlignment: CrossAxisAlignment.center, | ||
| children: [ | ||
| Padding( | ||
| padding: EdgeInsets.only( | ||
| bottom: 4, | ||
| top: MediaQuery.of(context).size.height * 0.4), | ||
| child: const CircularProgressIndicator(), | ||
| ), | ||
| Text( | ||
| 'Loading Articles...', | ||
| style: Theme.of(context) | ||
| .textTheme | ||
| .titleMedium! | ||
| .copyWith( | ||
| color: Theme.of(context).colorScheme.onSurface, | ||
| ), | ||
| ), | ||
| ], | ||
| ); | ||
| } | ||
| var entry = visibleArticles[index]; | ||
|
|
||
| var title = entry['title']?['\$t'] ?? 'No Title'; | ||
| var content = entry['content']?['\$t'] ?? 'No Content'; | ||
| var link = entry['link'] is List | ||
| ? entry['link'].first['href'] | ||
| : entry['link']['href']; | ||
|
|
||
| var publishedDateStr = entry['published']?['\$t'] ?? ''; | ||
| DateTime publishedDate; | ||
| try { | ||
| publishedDate = DateTime.parse(publishedDateStr).toLocal(); | ||
| } catch (e) { | ||
| publishedDate = DateTime.now(); | ||
| } | ||
|
|
||
| String formattedDate = timeago.format(publishedDate); | ||
|
|
||
| content = cleanHtmlContent(content); | ||
|
|
||
| return Padding( | ||
| padding: | ||
| const EdgeInsets.symmetric(horizontal: 8, vertical: 4.0), | ||
| child: Card( | ||
| elevation: 4.0, | ||
| child: Padding( | ||
| padding: const EdgeInsets.all(16), | ||
| child: Column( | ||
| crossAxisAlignment: CrossAxisAlignment.start, | ||
| children: [ | ||
| Row( | ||
| children: [ | ||
| Image.asset( | ||
| 'assets/tft_icon.png', | ||
| color: Theme.of(context).colorScheme.onSurface, | ||
| height: 20, | ||
| width: 20, | ||
| ), | ||
| const SizedBox(width: 2), | ||
| RichText( | ||
| text: TextSpan( | ||
| children: [ | ||
| TextSpan( | ||
| text: 'THREEFOLD - ', | ||
| style: Theme.of(context) | ||
| .textTheme | ||
| .bodySmall! | ||
| .copyWith( | ||
| color: Theme.of(context) | ||
| .colorScheme | ||
| .onSurface, | ||
| )), | ||
| TextSpan( | ||
| text: formattedDate, | ||
| style: Theme.of(context) | ||
| .textTheme | ||
| .bodySmall! | ||
| .copyWith( | ||
| color: Theme.of(context) | ||
| .colorScheme | ||
| .onSurface, | ||
| )), | ||
| ], | ||
| ), | ||
| ), | ||
| ], | ||
| ), | ||
| const SizedBox( | ||
| height: 3, | ||
| ), | ||
| Text( | ||
| title, | ||
| style: Theme.of(context) | ||
| .textTheme | ||
| .titleLarge! | ||
| .copyWith( | ||
| fontWeight: FontWeight.bold, | ||
| color: | ||
| Theme.of(context).colorScheme.onSurface, | ||
| ), | ||
| ), | ||
| const SizedBox(height: 5), | ||
| HtmlWidget( | ||
| content.length > 200 | ||
| ? '${content.substring(0, 200)}...' | ||
| : content, | ||
| textStyle: TextStyle( | ||
| color: Theme.of(context).colorScheme.onSurface, | ||
| ), | ||
| onTapUrl: (url) { | ||
| if (url.isNotEmpty) { | ||
| _launchURL(url); | ||
| return true; | ||
| } | ||
| return false; | ||
| }, | ||
| ), | ||
| const SizedBox(height: 4), | ||
| Row( | ||
| mainAxisAlignment: MainAxisAlignment.end, | ||
| children: [ | ||
| TextButton( | ||
| onPressed: () async { | ||
| _launchURL(link); | ||
| }, | ||
| child: Text( | ||
| 'Read more', | ||
| style: Theme.of(context) | ||
| .textTheme | ||
| .bodyLarge! | ||
| .copyWith( | ||
| color: Theme.of(context) | ||
| .colorScheme | ||
| .primary, | ||
| ), | ||
| ), | ||
| ), | ||
| ], | ||
| ), | ||
| ], | ||
| ), | ||
| ), | ||
| ), | ||
| ); | ||
| }, | ||
| ), | ||
| ), | ||
| if (isLoading && !isInitialLoading) | ||
| const Center( | ||
| child: Padding( | ||
| padding: EdgeInsets.all(8.0), | ||
| child: CircularProgressIndicator(), | ||
| ), | ||
| ), | ||
| ], | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| void _launchURL(String url) async { | ||
| if (await canLaunchUrl(Uri.parse(url))) { | ||
| await launchUrl(Uri.parse(url)); | ||
| } else { | ||
| throw 'Could not launch $url'; | ||
| } | ||
| } | ||
|
|
||
| String cleanHtmlContent(String content) { | ||
| return content.replaceAll(r'\\n', ''); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.