You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

204 lines
6.2 KiB

import 'dart:convert';
import 'dart:math';
import 'package:http/http.dart' as http;
import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:flutter/cupertino.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:teso/Classes/API%20Clasess/Post.dart';
import 'package:teso/Pages/PageWidgets/Home/homeTile.dart';
import 'package:teso/Pages/Sub_Pages/homeSub/VideoList.dart';
import 'package:teso/util/consts.dart';
class HomeFeed extends StatefulWidget {
@override
_HomeFeedState createState() => _HomeFeedState();
}
class _HomeFeedState extends State<HomeFeed> {
ScrollController _controller;
// List<PostedAd> trends;
List<Post> show;
int count;
var _future;
List<Post> deadFeed;
RefreshController _refreshController =
RefreshController(initialRefresh: false);
Future<List<Post>> pullAds() async {
try {
SharedPreferences prefs = await SharedPreferences.getInstance();
String token = prefs.getString("tokensTeso");
Map<String, String> requestHeaders = {
'Content-type': 'application/json',
'Authorization': token
};
var register = serverLocation + 'posts/homefeed';
var client =
await http.post(Uri.parse(register), headers: requestHeaders);
if (client.statusCode == 200) {
var posts = jsonDecode(client.body);
setState(() {
this.show = List<Post>.from(
posts.map((model) => Post.fromJSON(model)).toList());
// this.show.sort((b, a) => a.timestamp.compareTo(b.timestamp));
});
await prefs.setString("homefeeds", client.body);
// await fetchImages();
//
return show;
} else {
return null;
}
} catch (e) {
return null;
}
}
void _scrollListener() {
if (_controller.offset >= _controller.position.maxScrollExtent &&
!_controller.position.outOfRange) {
pullAds();
}
}
void _onRefresh() async {
try {
await pullAds();
} catch (e) {
print(e);
}
_refreshController.refreshCompleted();
}
Future<void> watch(advert) async {
List<Post> towatch = <Post>[];
towatch.add(advert);
if (show.length != 0) {
for (int i = 0; (i < show.length) && (i < 10); i++) {
final _random = new Random();
towatch.add(show[_random.nextInt(show.length)]);
}
} else if (show.length == 0) {
for (int i = 0; (i < deadFeed.length) && (i < 10); i++) {
final _random = new Random();
towatch.add(deadFeed[_random.nextInt(show.length)]);
}
}
Post reported = await Navigator.of(context).push(
new PageRouteBuilder(
pageBuilder: (_, __, ___) => new VideoList(
postedAd: towatch,
// posts: towatch,
// user: user,
//friend: addable,
),
),
);
if (reported != null) {
show.remove(reported);
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setString("homefeeds", jsonEncode(show));
print(prefs.getString("homefeed"));
}
}
@override
void initState() {
// WidgetsBinding.instance.addPostFrameCallback((_) => widget.toggle(1));
_controller = ScrollController();
_controller.addListener(_scrollListener);
count = 0;
// pullAds();
SharedPreferences.getInstance().then((value) {
if (value.getString("homefeeds") != null) {
var posts = jsonDecode(value.getString("homefeeds"));
setState(() {
deadFeed = List<Post>.from(
posts.map((model) => Post.fromJSON(model)).toList());
});
}
});
_future = pullAds();
super.initState();
}
@override
void dispose() {
_controller.dispose();
_refreshController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder(
initialData: deadFeed,
future: _future,
builder: (context, snapshot) {
if (show == null &&
deadFeed == null &&
(snapshot.connectionState == ConnectionState.none ||
snapshot.connectionState == ConnectionState.waiting)) {
return Center(
child: CupertinoActivityIndicator(
animating: true,
radius: 15,
),
);
}
if (show == null &&
deadFeed == null &&
snapshot.connectionState == ConnectionState.done) {
return SmartRefresher(
enablePullDown: true,
enablePullUp: false,
header: ClassicHeader(),
controller: _refreshController,
onRefresh: _onRefresh,
child: Container(
child: Center(
child: Text("An error occurred"),
),
),
);
} else {
show = show != null ? show : deadFeed;
return SmartRefresher(
enablePullDown: true,
enablePullUp: false,
header: ClassicHeader(),
controller: _refreshController,
onRefresh: _onRefresh,
child: StaggeredGridView.count(
controller: _controller,
crossAxisCount: 2,
children: List.generate(show.length, (int index) {
return show.elementAt(index).aspect == null ||
show.elementAt(index).aspect == "null"
? Container()
: double.parse(show.elementAt(index).aspect.toString()) <
1
? buildTile(
context, show.elementAt(index), 0.65, watch)
: buildTile(
context, show.elementAt(index), 0.35, watch);
}),
staggeredTiles: List.generate(
show.length,
(int index) {
return StaggeredTile.fit(1);
},
),
),
);
}
},
),
);
}
}