import 'package:circle_app/util/util.dart'; import 'package:flutter/cupertino.dart'; import 'package:get/get.dart'; import '../../network/api.dart'; import '../../network/dio_manager.dart'; import 'state.dart'; class CircleLogic extends GetxController { PageController pageController = PageController(initialPage: 1, viewportFraction: 0.8); InterestsBean circle = InterestsBean(lists: []); final CircleState state = CircleState(); int page = 1; @override void onInit() async { super.onInit(); var data = await DioManager.instance .get(url: Api.getCircleInterests, params: {"page": page}); var bean = BaseResponse.fromJson( data, (data) => InterestsBean.fromJson(data)); circle = bean.data!; update(); } loadMore() async { page = page++; var data = await DioManager.instance .get(url: Api.getCircleInterests, params: {"page": page}); var bean = BaseResponse.fromJson( data, (data) => InterestsBean.fromJson(data)); circle = bean.data!; update(); } outCircle(String interest_id, bool isStatus) async { var data = await DioManager.instance.post( url: Api.outCrrcle + interest_id + "/join", params: {"status": isStatus ? "0" : "1"}); var bean = BaseResponse.fromJson(data, (data) => data); if (bean.code == 200) { circle.lists.forEach((element) { if (element.id.toString() == interest_id.toString()) { element.isJoin = !isStatus; } }); } showToast(bean.msg); } getCircleIndexId() { return circle.lists[state.index].id; } } class InterestsBean { List lists; InterestsBean({required this.lists}); factory InterestsBean.fromJson(Map json) { List listsJson = json['lists']; List lists = listsJson.map((e) => Circle.fromJson(e)).toList(); return InterestsBean(lists: lists); } } class Circle { int id; String image; String title; String intro; double amount; double oldAmount; String lastCalloutTime; bool isJoin; dynamic isLike; int viewTotal; int joinTotal; List lastJoinUsers; Circle({ required this.id, required this.image, required this.title, required this.intro, required this.amount, required this.oldAmount, required this.lastCalloutTime, required this.isJoin, required this.isLike, required this.viewTotal, required this.joinTotal, required this.lastJoinUsers, }); factory Circle.fromJson(Map json) { List lastJoinUsersJson = json['last_join_users']; List lastJoinUsers = lastJoinUsersJson.map((e) => JoinUser.fromJson(e)).toList(); return Circle( id: json['id'], image: json['image'], title: json['title'], intro: json['intro'], amount: json['amount'].toDouble(), oldAmount: json['old_amount'].toDouble(), lastCalloutTime: json['last_callout_time'], isJoin: json['is_join'], isLike: json['is_like'], viewTotal: json['view_total'], joinTotal: json['join_total'], lastJoinUsers: lastJoinUsers, ); } } class JoinUser { int id; String avatar; String nickname; JoinUser({ required this.id, required this.avatar, required this.nickname, }); factory JoinUser.fromJson(Map json) { return JoinUser( id: json['id'], avatar: json['avatar'], nickname: json['nickname'], ); } } class LastJoinUser { int id; String avatar; String nickname; LastJoinUser({ required this.id, required this.avatar, required this.nickname, }); factory LastJoinUser.fromJson(Map json) { return LastJoinUser( id: json['id'], avatar: json['avatar'], nickname: json['nickname'], ); } }