Skip to content

Commit

Permalink
Upgraded Network Calling Procedure
Browse files Browse the repository at this point in the history
  • Loading branch information
SatyamX64 committed Mar 27, 2021
1 parent 91a03a6 commit 549d737
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 212 deletions.
61 changes: 0 additions & 61 deletions lib/DataBundle/DataBundle.dart

This file was deleted.

114 changes: 31 additions & 83 deletions lib/Network/Network.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import 'package:http/http.dart';
import 'URI.dart';

class NetworkLoader {
final API api;
NetworkLoader(this.api);
Future<bool> checkUsername(String userId) async {

final String userId;
final api = API();
NetworkLoader(this.userId);

Future<bool> checkUsername() async {
try {
final uri = api.userURI().toString() + '/$userId';
Response response = await get(uri, headers: api.headers);
Expand All @@ -20,101 +23,46 @@ class NetworkLoader {
}
}

Future<Map<String, dynamic>> getData(String userId) async {
try {
final uri = api.userURI().toString() + '/$userId';
Response response = await get(uri, headers: api.headers);
Map data = jsonDecode(response.body);
return data;
} catch (e) {
Future<Map> getUserData() async {
try{
final Map map = await getInitialData();
final List<dynamic> repoData = await getSpecificData('repos');
final List<dynamic> starredData = await getSpecificData('starred');
final List<dynamic> followersData = await getSpecificData('followers');
final List<dynamic> followingData = await getSpecificData('following');
final List<dynamic> organisationsData = await getSpecificData('orgs');
map['repos'] = repoData;
map['starred'] = starredData;
map['followers'] = followersData;
map['following'] = followingData;
map['organisations'] = organisationsData;
return map;
}catch(e){
print('ERROR: $e');
return {};
return null;
}

}

Future<List<dynamic>> getFollowers(String userId) async {
Future<Map<String, dynamic>> getInitialData() async {
try {
List temp = [];
for (int page = 1;; page++) {
final uri = api.userURI().toString() +
'/$userId' +
'/followers?page=$page&per_page=100';
Response response = await get(uri, headers: api.headers);
List data = jsonDecode(response.body);
temp.addAll(data);
if (data.length < 100) break;
}
return temp;
final uri = api.userURI().toString() + '/$userId';
Response response = await get(uri, headers: api.headers);
Map data = jsonDecode(response.body);
return data;
} catch (e) {
print('ERROR: $e');
return [];
throw e;
}
}

Future<List<dynamic>> getFollowing(String userId) async {
Future<List<dynamic>> getSpecificData(String key) async {
try {
List temp = [];
for (int page = 1;; page++) {
final uri = api.userURI().toString() +
'/$userId' +
'/following?page=$page&per_page=100';
Response response = await get(uri, headers: api.headers);
List data = jsonDecode(response.body);
temp.addAll(data);
if (data.length < 100) break;
}
return temp;
} catch (e) {
print('ERROR : $e');
return [];
}
}

Future<List<dynamic>> getStarred(String userId) async {
try {
List temp = [];
for (int page = 1;; page++) {
final uri = api.userURI().toString() +
'/$userId' +
'/starred?page=$page&per_page=100';
Response response = await get(uri, headers: api.headers);
List data = jsonDecode(response.body);
temp.addAll(data);
if (data.length < 100) break;
}
return temp;
} catch (e) {
print('ERROR: $e');
return [];
}
}

Future<List<dynamic>> getOrganisations(String userId) async {
try {
List temp = [];
for (int page = 0;; page++) {
final uri = api.userURI().toString() +
'/$userId' +
'/orgs?page=$page&per_page=100';
Response response = await get(uri, headers: api.headers);
List data = jsonDecode(response.body);
temp.addAll(data);
if (data.length < 100) break;
}
return temp;
} catch (e) {
print('ERROR : $e');
return [];
}
}

Future<List<dynamic>> getRepos(String userId) async {
try {
List temp = [];
for (int page = 0;; page++) {
final uri = api.userURI().toString() +
'/$userId' +
'/repos?page=$page&per_page=100';
'/$key?page=$page&per_page=100';
Response response = await get(uri, headers: api.headers);
List data = jsonDecode(response.body);
temp.addAll(data);
Expand Down
5 changes: 5 additions & 0 deletions lib/Network/User.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class User {
final String userId;
final Map map;
User({this.map,this.userId});
}
8 changes: 4 additions & 4 deletions lib/Screens/About.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:flutter/painting.dart';
import 'package:fluttericon/brandico_icons.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:gitoo/Constants.dart';
import 'package:gitoo/DataBundle/DataBundle.dart';
import 'package:gitoo/Network/User.dart';
import 'package:provider/provider.dart';
import 'package:share/share.dart';
import 'package:url_launcher/url_launcher.dart';
Expand Down Expand Up @@ -145,9 +145,9 @@ class About extends StatelessWidget {
Expanded(
child: GestureDetector(
onTap: () {
user.map['login_id'] != null
user.userId != null
? _launchURL(
'https://www.github.com/${user.map['login_id']}')
'https://www.github.com/${user.userId}')
: _showToast('User Not Found');
},
child: Icon(
Expand Down Expand Up @@ -177,7 +177,7 @@ class About extends StatelessWidget {
Expanded(
child: GestureDetector(
onTap: () {
Share.share('http://www.github.com/${user.map['login_id']}');
Share.share('http://www.github.com/${user.userId}');
},
child: Container(
margin: EdgeInsets.symmetric(
Expand Down
50 changes: 26 additions & 24 deletions lib/Screens/DashBoard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import 'package:fluttericon/elusive_icons.dart';
import 'package:fluttericon/mfg_labs_icons.dart';
import 'package:fluttericon/modern_pictograms_icons.dart';
import 'package:gitoo/Constants.dart';
import 'package:gitoo/Network/User.dart';
import 'package:gitoo/Widgets/NeumorphicBox.dart';
import 'package:gitoo/Widgets/CategoryButton.dart';
import 'package:gitoo/DataNotifier/DataNotifier.dart';
import 'package:gitoo/Widgets/ListBuilder.dart';
import 'package:marquee/marquee.dart';
import 'package:provider/provider.dart';

class DashBoard extends StatefulWidget {
@override
Expand All @@ -16,25 +18,25 @@ class DashBoard extends StatefulWidget {

class _DashBoardState extends State<DashBoard> {
int selectedList = 0;
String bio;
checkBio() {
oneLiner(String bio) {
if (bio.contains('\n')) {
bio = bio.replaceAll('\n', ' ');
}
return bio;
}

@override
Widget build(BuildContext context) {
DataNotifier dataNotifier = DataNotifier(context);
final _user = Provider.of<User>(context);
ListBuilder listBuilder = ListBuilder(_user.map);
final size = MediaQuery.of(context).size;
bio = dataNotifier.user.map['bio'] ?? 'Bio Not Available';
checkBio();

List dataLists = [
dataNotifier.showFollowers(),
dataNotifier.showFollowing(),
dataNotifier.showStarred(),
dataNotifier.showRepos(),
dataNotifier.showOrgs(),
listBuilder.showFollowers(),
listBuilder.showFollowing(),
listBuilder.showStarred(),
listBuilder.showRepos(),
listBuilder.showOrgs(),
];

return Stack(
Expand All @@ -51,7 +53,7 @@ class _DashBoardState extends State<DashBoard> {
child: FittedBox(
fit: BoxFit.scaleDown,
child: Text(
'${dataNotifier.user.map['name'] ?? 'Not Available'}',
'${_user.map['name'] ?? 'Not Available'}',
style: kUserNameStyle,
),
),
Expand All @@ -74,7 +76,7 @@ class _DashBoardState extends State<DashBoard> {
child: FittedBox(
fit: BoxFit.scaleDown,
child: Text(
'${dataNotifier.user.map['email'] ?? 'Not Available'}',
'${_user.map['email'] ?? 'Not Available'}',
style: kEmailStyle,
),
),
Expand All @@ -87,7 +89,7 @@ class _DashBoardState extends State<DashBoard> {
child: Row(
children: <Widget>[
CategoryButton(
count: dataNotifier.user.map['followers'],
count: List.from(_user.map['followers']).length,
title: 'Followers',
icon: MfgLabs.users,
iconColor: kGreen,
Expand All @@ -103,7 +105,7 @@ class _DashBoardState extends State<DashBoard> {
),
CategoryButton(
title: 'Following',
count: dataNotifier.user.map['following'],
count: List.from(_user.map['following']).length,
icon: Elusive.group,
iconColor: kGreen,
function: () {
Expand All @@ -118,8 +120,7 @@ class _DashBoardState extends State<DashBoard> {
),
CategoryButton(
title: 'Starred',
count: dataNotifier
.userBigData.map['starred'].length,
count: List.from(_user.map['starred']).length,
icon: Icons.star,
iconColor: kYellow,
function: () {
Expand Down Expand Up @@ -159,8 +160,8 @@ class _DashBoardState extends State<DashBoard> {
CrossAxisAlignment.stretch,
children: <Widget>[
CategoryButton(
count:
dataNotifier.user.map['public_repos'],
count: List.from(_user.map['repos'])
.length,
title: 'Repos',
iconColor: kOrange,
icon: Elusive.github,
Expand All @@ -176,8 +177,8 @@ class _DashBoardState extends State<DashBoard> {
height: size.width * 0.04,
),
CategoryButton(
count: dataNotifier.userBigData
.map['organisations'].length,
count: List.from(_user.map['organisations'])
.length,
iconColor: kOrange,
icon: ModernPictograms.money,
title: 'Orgs',
Expand Down Expand Up @@ -205,7 +206,7 @@ class _DashBoardState extends State<DashBoard> {
child: Container(
alignment: Alignment.center,
child: Marquee(
text: '$bio',
text: '${oneLiner(_user.map['bio'] ?? 'Bio Not Available')}',
style: kInfoStyle,
scrollAxis: Axis.horizontal,
crossAxisAlignment: CrossAxisAlignment.start,
Expand All @@ -224,8 +225,10 @@ class _DashBoardState extends State<DashBoard> {
),
),
),

],
),

Positioned(
top: (MediaQuery.of(context).size.height) / 20,
left: 10,
Expand Down Expand Up @@ -255,8 +258,7 @@ class _DashBoardState extends State<DashBoard> {
child: CircleAvatar(
radius: size.width * 0.11,
backgroundColor: kSecondary,
backgroundImage:
NetworkImage(dataNotifier.user.map['avatar_url']),
backgroundImage: NetworkImage(_user.map['avatar_url']),
),
),
),
Expand Down
Loading

0 comments on commit 549d737

Please sign in to comment.