Skip to content

Commit

Permalink
Optimizing code, removing useless code
Browse files Browse the repository at this point in the history
  • Loading branch information
Ankur Kedia committed Dec 12, 2017
1 parent c1de19e commit 54d625d
Show file tree
Hide file tree
Showing 9 changed files with 223 additions and 185 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# newsapp
# News Buzz

A new Flutter project.
News App created using Flutter.

## Getting Started

For help getting started with Flutter, view our online
[documentation](http://flutter.io/).
###features
Custom news feed based on selected sources
Search for topics
Adding articles to bookmarks
Sharing articles
43 changes: 11 additions & 32 deletions lib/CategoriesScreen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:firebase_database/firebase_database.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
import './SourcesScreen.dart' as SourcesScreen;
import './globalStore.dart' as globalStore;
import './categoriesList.dart' as categoriesList;

class CategoriesScreen extends StatefulWidget {
CategoriesScreen({Key key}) : super(key: key);
Expand All @@ -17,46 +18,24 @@ class CategoriesScreen extends StatefulWidget {
}

class _CategoriesScreenState extends State<CategoriesScreen> {
DataSnapshot snapshot;
var sources;
bool change = false;
final FlutterWebviewPlugin flutterWebviewPlugin = new FlutterWebviewPlugin();

Future getData() async {
var libSources = await http.get(
Uri.encodeFull('https://newsapi.org/v2/sources?language=en'),
headers: {
"Accept": "application/json",
"X-Api-Key": "ab31ce4a49814a27bbb16dd5c5c06608"
});

var snap = await globalStore.articleSourcesDatabaseReference.once();
this.setState(() {
sources = JSON.decode(libSources.body);
snapshot = snap;
});
return "Success!";
}

@override
void initState() {
super.initState();
this.getData();
}

@override
Widget build(BuildContext context) {
return new Scaffold(
backgroundColor: Colors.grey[200],
body: globalStore.categories == null
body: categoriesList.list == null
? const Center(
child: const CupertinoActivityIndicator(),
)
: new GridView.builder(
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3, mainAxisSpacing: 25.0),
padding: const EdgeInsets.all(10.0),
itemCount: globalStore.categories.length,
itemCount: categoriesList.list.length,
itemBuilder: (BuildContext context, int index) {
return new GridTile(
footer: new Row(
Expand All @@ -67,7 +46,7 @@ class _CategoriesScreenState extends State<CategoriesScreen> {
height: 16.0,
width: 100.0,
child: new Text(
globalStore.categories[index]["name"],
categoriesList.list[index]["name"],
maxLines: 2,
textAlign: TextAlign.center,
overflow: TextOverflow.ellipsis,
Expand All @@ -77,7 +56,6 @@ class _CategoriesScreenState extends State<CategoriesScreen> {
]),
child: new Container(
height: 500.0,
padding: const EdgeInsets.only(bottom: 5.0),
child: new GestureDetector(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.center,
Expand All @@ -95,13 +73,14 @@ class _CategoriesScreenState extends State<CategoriesScreen> {
backgroundColor: Colors.white,
radius: 40.0,
child: new Icon(
globalStore.categories[index]
categoriesList.list[index]
["icon"],
size: 50.0,
color: Colors.blueGrey),
size: 40.0,
color: categoriesList.list[index]
["color"]),
),
padding: const EdgeInsets.only(
left: 10.0, top: 10.0, right: 10.0),
left: 10.0, right: 10.0),
),
),
],
Expand All @@ -116,9 +95,9 @@ class _CategoriesScreenState extends State<CategoriesScreen> {
context,
new MaterialPageRoute(
builder: (_) => new SourcesScreen.SourcesScreen(
sourceId: globalStore.categories[index]
sourceId: categoriesList.list[index]
['id'],
sourceName: globalStore.categories[index]
sourceName: categoriesList.list[index]
["name"],
isCategory: true,
)));
Expand Down
6 changes: 2 additions & 4 deletions lib/SearchScreen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,9 @@ class _SearchScreenState extends State<SearchScreen> {
if (value != null) {
value.forEach((k, v) {
if (v['url'].compareTo(article['url']) == 0) flag = 1;
return;
});
if (flag == 1)
return true;
else
return false;
if (flag == 1) return true;
}
}
return false;
Expand Down
64 changes: 64 additions & 0 deletions lib/categoriesList.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import 'package:flutter/material.dart';

List list = [
{
"id": "business",
"name": "Business",
"icon": Icons.work,
"color": Colors.teal
},
{
"id": "technology",
"name": "Technology",
"icon": Icons.smartphone,
"color": Colors.grey[600]
},
{
"id": "science-and-nature",
"name": "Science and Nature",
"icon": Icons.nature_people,
"color": Colors.green[600]
},
{
"id": "sport",
"name": "Sports",
"icon": Icons.directions_bike,
"color": Colors.deepOrange
},
{
"id": "gaming",
"name": "Gaming",
"icon": Icons.videogame_asset,
"color": Colors.orange
},
{
"id": "general",
"name": "General",
"icon": Icons.people,
"color": Colors.cyan
},
{
"id": "entertainment",
"name": "Entertainment",
"icon": Icons.local_movies,
"color": Colors.purple
},
{
"id": "health-and-medical",
"name": "Health and Medical",
"icon": Icons.local_hospital,
"color": Colors.red
},
{
"id": "music",
"name": "Music",
"icon": Icons.music_note,
"color": Colors.amber
},
{
"id": "politics",
"name": "Politics",
"icon": Icons.assistant_photo,
"color": Colors.blueGrey
},
];
24 changes: 0 additions & 24 deletions lib/globalStore.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
library news.globals;

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:firebase_analytics/firebase_analytics.dart';
import 'package:firebase_database/firebase_database.dart';
Expand Down Expand Up @@ -39,24 +36,3 @@ Future<Null> _ensureLoggedIn() async {
}

var logIn = _ensureLoggedIn();

List categories = [
{"id": "business", "name": "Business", "icon": Icons.work},
{"id": "technology", "name": "Technology", "icon": Icons.smartphone},
{
"id": "science-and-nature",
"name": "Science and Nature",
"icon": Icons.nature_people
},
{"id": "sport", "name": "Sports", "icon": Icons.directions_bike},
{"id": "entertainment", "name": "Entertainment", "icon": Icons.local_movies},
{"id": "gaming", "name": "Gaming", "icon": Icons.videogame_asset},
{"id": "general", "name": "General", "icon": Icons.people},
{
"id": "health-and-medical",
"name": "Health and Medical",
"icon": Icons.local_hospital
},
{"id": "music", "name": "Music", "icon": Icons.music_note},
{"id": "politics", "name": "Politics", "icon": Icons.assistant_photo}
];
51 changes: 27 additions & 24 deletions lib/homeScreen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ class HomeScreen extends StatefulWidget {

class _HomeScreenState extends State<HomeScreen> {
var data;
var user;
bool change = false;
var newsSelection = "bbc-news";
var newsSelection = "techcrunch";
DataSnapshot snapshot;
var snapSources;
TimeAgo ta = new TimeAgo();
Expand All @@ -44,9 +42,15 @@ class _HomeScreenState extends State<HomeScreen> {
"Accept": "application/json",
"X-Api-Key": "ab31ce4a49814a27bbb16dd5c5c06608"
});

var localData = JSON.decode(response.body);
if (localData != null && localData["articles"] != null) {
localData["articles"].sort((a, b) =>
a["publishedAt"] != null && b["publishedAt"] != null
? b["publishedAt"].compareTo(a["publishedAt"])
: null);
}
this.setState(() {
data = JSON.decode(response.body);
data = localData;
snapshot = snap;
});
return "Success!";
Expand All @@ -60,7 +64,7 @@ class _HomeScreenState extends State<HomeScreen> {
value.forEach((k, v) {
if (v['url'].compareTo(article['url']) == 0) {
flag = 1;
return true;
return;
}
});
if (flag == 1) return true;
Expand Down Expand Up @@ -105,26 +109,29 @@ class _HomeScreenState extends State<HomeScreen> {
pushArticle(article);
}
this.getData();
this.setState(() {
change = true;
});
}

_onRemoveSource(id) {
_onRemoveSource(id, name) {
if (snapSources != null) {
snapSources.value.forEach((key, source) {
if (source['id'].compareTo(id) == 0) {
Scaffold.of(context).showSnackBar(new SnackBar(
content: new Text('News source removed'),
content: new Text('Are you sure you want to remove $name?'),
backgroundColor: Colors.grey[600],
duration: new Duration(seconds: 3),
action: new SnackBarAction(
label: 'Yes',
onPressed: () {
globalStore.articleSourcesDatabaseReference
.child(key)
.remove();
Scaffold.of(context).showSnackBar(
new SnackBar(content: new Text('$name removed')));
}),
));
globalStore.articleSourcesDatabaseReference.child(key).remove();
}
});
this.getData();
this.setState(() {
change = true;
});
}
}

Expand All @@ -150,7 +157,6 @@ class _HomeScreenState extends State<HomeScreen> {

Column buildButtonColumn(IconData icon) {
Color color = Theme.of(context).primaryColor;

return new Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
Expand All @@ -162,12 +168,6 @@ class _HomeScreenState extends State<HomeScreen> {

@override
Widget build(BuildContext context) {
if (data != null && data["articles"] != null) {
data["articles"].sort((a, b) =>
a["publishedAt"] != null && b["publishedAt"] != null
? b["publishedAt"].compareTo(a["publishedAt"])
: null);
}
return new Scaffold(
backgroundColor: Colors.grey[200],
body: new GestureDetector(
Expand Down Expand Up @@ -288,8 +288,11 @@ class _HomeScreenState extends State<HomeScreen> {
child: buildButtonColumn(
Icons.not_interested)),
onTap: () {
_onRemoveSource(data["articles"]
[index]["source"]["id"]);
_onRemoveSource(
data["articles"][index]["source"]
["id"],
data["articles"][index]["source"]
["name"]);
},
),
],
Expand Down
11 changes: 5 additions & 6 deletions lib/libraryScreen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,12 @@ class _LibraryScreenState extends State<LibraryScreen> {
new SizedBox(
child: new Container(
child: new CircleAvatar(
backgroundColor: Colors.white,
backgroundColor: Colors.transparent,
backgroundImage: new NetworkImage(
"https://icons.better-idea.org/icon?url=" +
sources['sources'][index]
['url'] +
"&size=120",
),
"https://icons.better-idea.org/icon?url=" +
sources['sources'][index]
['url'] +
"&size=120"),
radius: 40.0,
),
padding: const EdgeInsets.only(
Expand Down
Loading

0 comments on commit 54d625d

Please sign in to comment.