Files
AppCardsStarter_Modelo/lib/videos/bloc/youtube_controller.dart
T
2021-09-22 10:59:59 -03:00

98 lines
3.0 KiB
Dart

import 'package:comunika/index/index_controller.dart';
import 'package:comunika/videos/api/youtube_api.dart';
import 'package:comunika/videos/api/youtube_classes.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart' show DateFormat;
class YouTubeController extends GetxController {
YouTubeApi _api = new YouTubeApi();
final IndexController _ic = Get.find();
var results = new YouTubeSearchResponse(
errorCode: 0,
errorMessage: '',
items: [],
).obs;
var itens = <YouTubeItems>[].obs;
bool atualizar = false;
var errorCode = 0.obs;
String? errorMessage = '';
var loading = true.obs;
final f = DateFormat('dd/MM/yyyy HH:mm:ss');
@override
void onInit() {
super.onInit();
// _controller = AnimationController(
// duration: const Duration(milliseconds: 650),
// vsync: this,
// )..forward();
// animation = Tween<Offset>(
// begin: const Offset(-0.5, 0.0),
// end: const Offset(0.0, 0.0),
// ).animate(CurvedAnimation(
// parent: _controller,
// curve: Curves.easeInCubic,
// ));
this.atualizar = true;
this.itens = new RxList<YouTubeItems>();
this.getChannelVideosBloc();
}
Future<void> getChannelVideosBloc({String textoBusca = '-'}) async {
//this.loading.value = true;
if (results.value.errorCode == null ||
atualizar ||
results.value.errorCode != 0) {
results.value = new YouTubeSearchResponse();
this.itens = new RxList<YouTubeItems>();
results.value = await this._api.getChannelVideos();
results.value.items = results.value.items!
.where(
(element) => GetUtils.isNullOrBlank(element.id!.videoId) == false)
.toList();
this.itens.addAll(results.value.items!);
this.errorCode.value = results.value.errorCode!;
this.errorMessage = results.value.errorMessage;
this.atualizar = false;
} else if (results.value.nextPageToken != null &&
results.value.nextPageToken!.isNotEmpty) {
var more = await this
._api
.getChannelVideos(nextPageToken: results.value.nextPageToken);
more.items = more.items!
.where(
(element) => GetUtils.isNullOrBlank(element.id!.videoId) == false)
.toList();
this.errorCode.value = more.errorCode!;
this.errorMessage = more.errorMessage;
this.itens.addAll(more.items!);
results.value.nextPageToken = more.nextPageToken ?? '';
results.value.items!.addAll(more.items!);
}
if (this.errorCode.value == -5) {
this._ic.tabsWithoutVideos();
}
this.loading.value = false;
}
Future<void> refresh() async {
this.loading.value = true;
this.atualizar = true;
results.value = YouTubeSearchResponse(
errorCode: 0,
errorMessage: '',
items: [],
);
this.getChannelVideosBloc();
}
String getDateFormatada(String dt) {
String r = '';
try {
r = this.f.format(DateTime.parse(dt));
} catch (e) {}
return r;
}
}