init
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
import 'shop_models.dart';
|
||||
import 'shop_repository.dart';
|
||||
|
||||
enum ShopStatus { initial, loading, loaded, error }
|
||||
|
||||
class ShopState extends Equatable {
|
||||
final ShopStatus status;
|
||||
final ShopData? data;
|
||||
final bool busy; // در حال انجام یک عملیات (خرید/انتخاب)
|
||||
|
||||
const ShopState({this.status = ShopStatus.initial, this.data, this.busy = false});
|
||||
|
||||
ShopState copyWith({ShopStatus? status, ShopData? data, bool? busy}) => ShopState(
|
||||
status: status ?? this.status,
|
||||
data: data ?? this.data,
|
||||
busy: busy ?? this.busy,
|
||||
);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [status, data, busy];
|
||||
}
|
||||
|
||||
class ShopCubit extends Cubit<ShopState> {
|
||||
final ShopRepository _repo;
|
||||
ShopCubit(this._repo) : super(const ShopState());
|
||||
|
||||
Future<void> load() async {
|
||||
emit(state.copyWith(status: ShopStatus.loading));
|
||||
try {
|
||||
emit(state.copyWith(status: ShopStatus.loaded, data: await _repo.getShop()));
|
||||
} catch (_) {
|
||||
emit(state.copyWith(status: ShopStatus.error));
|
||||
}
|
||||
}
|
||||
|
||||
/// یک عملیات را اجرا، فروشگاه را بازخوانی و پیام نتیجه را برمیگرداند.
|
||||
Future<String> _run(Future<void> Function() action, String okMsg) async {
|
||||
if (state.busy) return '';
|
||||
emit(state.copyWith(busy: true));
|
||||
try {
|
||||
await action();
|
||||
final data = await _repo.getShop();
|
||||
emit(state.copyWith(status: ShopStatus.loaded, data: data, busy: false));
|
||||
return okMsg;
|
||||
} on DioException catch (e) {
|
||||
emit(state.copyWith(busy: false));
|
||||
final d = e.response?.data;
|
||||
if (d is Map && d['message'] != null) return d['message'].toString();
|
||||
return 'خطا در ارتباط با سرور';
|
||||
} catch (_) {
|
||||
emit(state.copyWith(busy: false));
|
||||
return 'خطای نامشخص';
|
||||
}
|
||||
}
|
||||
|
||||
Future<String> buyCard(String id) => _run(() => _repo.buyCard(id), 'کارت خریداری شد');
|
||||
|
||||
Future<String> selectCard(String id) =>
|
||||
_run(() => _repo.selectCard(id), 'کارت انتخاب شد');
|
||||
|
||||
Future<String> purchase(String kind, String id) => _run(
|
||||
() => _repo.purchase(
|
||||
store: 'bazaar',
|
||||
kind: kind,
|
||||
productId: id,
|
||||
token: 'dev-$kind-$id-${DateTime.now().millisecondsSinceEpoch}',
|
||||
),
|
||||
'خرید با موفقیت انجام شد',
|
||||
);
|
||||
|
||||
Future<String> claimAd() => _run(
|
||||
() => _repo.adReward('dev-ad-${DateTime.now().millisecondsSinceEpoch}'),
|
||||
'سکه رایگان دریافت شد',
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
// مدلهای کاتالوگ فروشگاه (پاسخ GET /api/shop).
|
||||
|
||||
class CoinPackage {
|
||||
final String id;
|
||||
final String title;
|
||||
final int coins;
|
||||
final int vipDays;
|
||||
final int priceToman;
|
||||
final int bonusPct;
|
||||
|
||||
CoinPackage.fromJson(Map<String, dynamic> j)
|
||||
: id = j['id'] as String,
|
||||
title = j['title'] as String,
|
||||
coins = (j['coins'] ?? 0) as int,
|
||||
vipDays = (j['vip_days'] ?? 0) as int,
|
||||
priceToman = (j['price_toman'] ?? 0) as int,
|
||||
bonusPct = (j['bonus_pct'] ?? 0) as int;
|
||||
}
|
||||
|
||||
class TicketPackage {
|
||||
final String id;
|
||||
final String title;
|
||||
final int tickets;
|
||||
final int priceToman;
|
||||
|
||||
TicketPackage.fromJson(Map<String, dynamic> j)
|
||||
: id = j['id'] as String,
|
||||
title = j['title'] as String,
|
||||
tickets = (j['tickets'] ?? 0) as int,
|
||||
priceToman = (j['price_toman'] ?? 0) as int;
|
||||
}
|
||||
|
||||
class CardSkin {
|
||||
final String id;
|
||||
final String title;
|
||||
final int priceCoins;
|
||||
|
||||
CardSkin.fromJson(Map<String, dynamic> j)
|
||||
: id = j['id'] as String,
|
||||
title = j['title'] as String,
|
||||
priceCoins = (j['price_coins'] ?? 0) as int;
|
||||
}
|
||||
|
||||
class Booster {
|
||||
final String id;
|
||||
final String title;
|
||||
final int multiplier;
|
||||
final int hours;
|
||||
final int priceToman;
|
||||
|
||||
Booster.fromJson(Map<String, dynamic> j)
|
||||
: id = j['id'] as String,
|
||||
title = j['title'] as String,
|
||||
multiplier = (j['multiplier'] ?? 1) as int,
|
||||
hours = (j['hours'] ?? 0) as int,
|
||||
priceToman = (j['price_toman'] ?? 0) as int;
|
||||
}
|
||||
|
||||
/// دادهی کامل فروشگاه: کاتالوگ + کارتهای متعلق به کاربر + کارت انتخابی.
|
||||
class ShopData {
|
||||
final List<CoinPackage> coinPackages;
|
||||
final List<TicketPackage> ticketPackages;
|
||||
final List<CardSkin> cardSkins;
|
||||
final List<Booster> boosters;
|
||||
final List<String> ownedCards;
|
||||
final String selectedCard;
|
||||
|
||||
ShopData({
|
||||
required this.coinPackages,
|
||||
required this.ticketPackages,
|
||||
required this.cardSkins,
|
||||
required this.boosters,
|
||||
required this.ownedCards,
|
||||
required this.selectedCard,
|
||||
});
|
||||
|
||||
factory ShopData.fromJson(Map<String, dynamic> j) {
|
||||
final cat = Map<String, dynamic>.from(j['catalog'] as Map);
|
||||
List<T> parse<T>(String key, T Function(Map<String, dynamic>) f) =>
|
||||
((cat[key] as List?) ?? [])
|
||||
.map((e) => f(Map<String, dynamic>.from(e as Map)))
|
||||
.toList();
|
||||
return ShopData(
|
||||
coinPackages: parse('coin_packages', CoinPackage.fromJson),
|
||||
ticketPackages: parse('ticket_packages', TicketPackage.fromJson),
|
||||
cardSkins: parse('card_skins', CardSkin.fromJson),
|
||||
boosters: parse('boosters', Booster.fromJson),
|
||||
ownedCards: ((j['owned_cards'] as List?) ?? []).map((e) => e as String).toList(),
|
||||
selectedCard: (j['selected_card'] ?? 'simple') as String,
|
||||
);
|
||||
}
|
||||
|
||||
bool owns(String cardId) => cardId == 'simple' || ownedCards.contains(cardId);
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
import '../../core/network/api_client.dart';
|
||||
import 'shop_models.dart';
|
||||
|
||||
/// دسترسی به endpointهای فروشگاه و پاداشها.
|
||||
class ShopRepository {
|
||||
final ApiClient _api;
|
||||
ShopRepository(this._api);
|
||||
|
||||
Future<ShopData> getShop() async {
|
||||
final res = await _api.dio.get('/shop');
|
||||
return ShopData.fromJson(Map<String, dynamic>.from(res.data));
|
||||
}
|
||||
|
||||
Future<void> buyCard(String cardId) =>
|
||||
_api.dio.post('/shop/buy-card', data: {'card_id': cardId});
|
||||
|
||||
Future<void> selectCard(String cardId) =>
|
||||
_api.dio.post('/shop/select-card', data: {'card_id': cardId});
|
||||
|
||||
/// تأیید خرید IAP. در حالت واقعی token از SDK بازار/مایکت میآید؛
|
||||
/// فعلاً توکن توسعهای فرستاده میشود (بکاند در حالت dev هر توکن غیرخالی را میپذیرد).
|
||||
Future<void> purchase({
|
||||
required String store,
|
||||
required String kind,
|
||||
required String productId,
|
||||
required String token,
|
||||
}) =>
|
||||
_api.dio.post('/shop/purchase', data: {
|
||||
'store': store,
|
||||
'kind': kind,
|
||||
'product_id': productId,
|
||||
'token': token,
|
||||
});
|
||||
|
||||
/// سکه رایگان پس از تبلیغ rewarded. token از SDK تپسل میآید (فعلاً توسعهای).
|
||||
Future<int> adReward(String token) async {
|
||||
final res = await _api.dio.post('/rewards/ad', data: {'token': token});
|
||||
return (res.data['amount'] ?? 0) as int;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,384 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
import '../../core/theme/app_theme.dart';
|
||||
import '../lobby/wallet_cubit.dart';
|
||||
import 'shop_cubit.dart';
|
||||
import 'shop_models.dart';
|
||||
|
||||
/// صفحهی فروشگاه با تبهای سکه/بلیط/کارت/تجهیزات.
|
||||
class ShopScreen extends StatelessWidget {
|
||||
const ShopScreen({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return DefaultTabController(
|
||||
length: 4,
|
||||
child: Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('فروشگاه'),
|
||||
actions: [
|
||||
BlocBuilder<WalletCubit, WalletState>(
|
||||
builder: (context, s) => Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(left: 12),
|
||||
child: Row(children: [
|
||||
const Icon(Icons.monetization_on,
|
||||
color: AppColors.gold, size: 18),
|
||||
const SizedBox(width: 4),
|
||||
Text('${s.wallet?.coins ?? 0}',
|
||||
style: const TextStyle(color: AppColors.text)),
|
||||
]),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
bottom: const TabBar(
|
||||
isScrollable: true,
|
||||
labelColor: AppColors.gold,
|
||||
indicatorColor: AppColors.gold,
|
||||
tabs: [
|
||||
Tab(text: 'سکه'),
|
||||
Tab(text: 'بلیط'),
|
||||
Tab(text: 'کارت'),
|
||||
Tab(text: 'تجهیزات'),
|
||||
],
|
||||
),
|
||||
),
|
||||
body: BlocConsumer<ShopCubit, ShopState>(
|
||||
listener: (context, state) {},
|
||||
builder: (context, state) {
|
||||
if (state.status == ShopStatus.loading ||
|
||||
state.status == ShopStatus.initial) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
if (state.status == ShopStatus.error || state.data == null) {
|
||||
return Center(
|
||||
child: Column(mainAxisSize: MainAxisSize.min, children: [
|
||||
const Text('خطا در بارگذاری فروشگاه'),
|
||||
TextButton(
|
||||
onPressed: () => context.read<ShopCubit>().load(),
|
||||
child: const Text('تلاش مجدد'),
|
||||
),
|
||||
]),
|
||||
);
|
||||
}
|
||||
final d = state.data!;
|
||||
return TabBarView(
|
||||
children: [
|
||||
_CoinsTab(packages: d.coinPackages),
|
||||
_TicketsTab(packages: d.ticketPackages),
|
||||
_CardsTab(data: d),
|
||||
_BoostersTab(boosters: d.boosters),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// اجرای یک عملیات فروشگاه و نمایش نتیجه + بازخوانی کیفپول.
|
||||
Future<void> _do(BuildContext context, Future<String> Function() action) async {
|
||||
final msg = await action();
|
||||
if (!context.mounted || msg.isEmpty) return;
|
||||
await context.read<WalletCubit>().load();
|
||||
if (!context.mounted) return;
|
||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(msg)));
|
||||
}
|
||||
|
||||
class _CoinsTab extends StatelessWidget {
|
||||
final List<CoinPackage> packages;
|
||||
const _CoinsTab({required this.packages});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return GridView.count(
|
||||
crossAxisCount: 2,
|
||||
padding: const EdgeInsets.all(12),
|
||||
childAspectRatio: 0.82,
|
||||
mainAxisSpacing: 12,
|
||||
crossAxisSpacing: 12,
|
||||
children: [
|
||||
_FreeCoinCard(
|
||||
onTap: () => _do(context, () => context.read<ShopCubit>().claimAd()),
|
||||
),
|
||||
for (final p in packages)
|
||||
_StoreCard(
|
||||
title: p.title,
|
||||
badge: p.bonusPct > 0 ? '+${p.bonusPct}%' : null,
|
||||
lines: [
|
||||
'${p.coins} سکه',
|
||||
if (p.vipDays > 0) '${p.vipDays} روز VIP',
|
||||
],
|
||||
priceLabel: '${p.priceToman} تومان',
|
||||
icon: Icons.savings,
|
||||
onTap: () =>
|
||||
_do(context, () => context.read<ShopCubit>().purchase('coin', p.id)),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _TicketsTab extends StatelessWidget {
|
||||
final List<TicketPackage> packages;
|
||||
const _TicketsTab({required this.packages});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return GridView.count(
|
||||
crossAxisCount: 2,
|
||||
padding: const EdgeInsets.all(12),
|
||||
childAspectRatio: 0.82,
|
||||
mainAxisSpacing: 12,
|
||||
crossAxisSpacing: 12,
|
||||
children: [
|
||||
for (final p in packages)
|
||||
_StoreCard(
|
||||
title: p.title,
|
||||
lines: ['${p.tickets} بلیط'],
|
||||
priceLabel: '${p.priceToman} تومان',
|
||||
icon: Icons.confirmation_number,
|
||||
onTap: () =>
|
||||
_do(context, () => context.read<ShopCubit>().purchase('ticket', p.id)),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _BoostersTab extends StatelessWidget {
|
||||
final List<Booster> boosters;
|
||||
const _BoostersTab({required this.boosters});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return GridView.count(
|
||||
crossAxisCount: 2,
|
||||
padding: const EdgeInsets.all(12),
|
||||
childAspectRatio: 0.82,
|
||||
mainAxisSpacing: 12,
|
||||
crossAxisSpacing: 12,
|
||||
children: [
|
||||
for (final b in boosters)
|
||||
_StoreCard(
|
||||
title: b.title,
|
||||
lines: ['تجربه ×${b.multiplier}', '${b.hours} ساعت'],
|
||||
priceLabel: '${b.priceToman} تومان',
|
||||
icon: Icons.bolt,
|
||||
onTap: () =>
|
||||
_do(context, () => context.read<ShopCubit>().purchase('booster', b.id)),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _CardsTab extends StatelessWidget {
|
||||
final ShopData data;
|
||||
const _CardsTab({required this.data});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return GridView.count(
|
||||
crossAxisCount: 2,
|
||||
padding: const EdgeInsets.all(12),
|
||||
childAspectRatio: 0.82,
|
||||
mainAxisSpacing: 12,
|
||||
crossAxisSpacing: 12,
|
||||
children: [
|
||||
for (final c in data.cardSkins)
|
||||
_CardSkinCard(
|
||||
skin: c,
|
||||
owned: data.owns(c.id),
|
||||
selected: data.selectedCard == c.id,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _CardSkinCard extends StatelessWidget {
|
||||
final CardSkin skin;
|
||||
final bool owned;
|
||||
final bool selected;
|
||||
const _CardSkinCard(
|
||||
{required this.skin, required this.owned, required this.selected});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final Widget action;
|
||||
if (selected) {
|
||||
action = const _Pill(text: 'انتخاب شده', color: AppColors.goldDark);
|
||||
} else if (owned) {
|
||||
action = _ActionButton(
|
||||
label: 'انتخاب',
|
||||
color: AppColors.green,
|
||||
onTap: () =>
|
||||
_do(context, () => context.read<ShopCubit>().selectCard(skin.id)),
|
||||
);
|
||||
} else {
|
||||
action = _ActionButton(
|
||||
label: '${skin.priceCoins} سکه',
|
||||
color: AppColors.accent,
|
||||
onTap: () => _do(context, () => context.read<ShopCubit>().buyCard(skin.id)),
|
||||
);
|
||||
}
|
||||
return _Panel(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Text(skin.title,
|
||||
textAlign: TextAlign.center,
|
||||
style: const TextStyle(color: AppColors.gold)),
|
||||
const Icon(Icons.style, size: 48, color: AppColors.text),
|
||||
SizedBox(width: double.infinity, child: action),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _FreeCoinCard extends StatelessWidget {
|
||||
final VoidCallback onTap;
|
||||
const _FreeCoinCard({required this.onTap});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return _Panel(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
const Text('سکه رایگان',
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(color: AppColors.gold)),
|
||||
const Icon(Icons.ondemand_video, size: 44, color: AppColors.green),
|
||||
const Text('با دیدن تبلیغ',
|
||||
style: TextStyle(color: Colors.white60, fontSize: 12)),
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
child: _ActionButton(
|
||||
label: 'رایگان', color: AppColors.green, onTap: onTap),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _StoreCard extends StatelessWidget {
|
||||
final String title;
|
||||
final List<String> lines;
|
||||
final String priceLabel;
|
||||
final IconData icon;
|
||||
final String? badge;
|
||||
final VoidCallback onTap;
|
||||
const _StoreCard({
|
||||
required this.title,
|
||||
required this.lines,
|
||||
required this.priceLabel,
|
||||
required this.icon,
|
||||
required this.onTap,
|
||||
this.badge,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Stack(
|
||||
children: [
|
||||
_Panel(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Text(title,
|
||||
textAlign: TextAlign.center,
|
||||
style: const TextStyle(color: AppColors.gold)),
|
||||
Icon(icon, size: 40, color: AppColors.gold),
|
||||
Column(
|
||||
children: [
|
||||
for (final l in lines)
|
||||
Text(l, style: const TextStyle(color: AppColors.text)),
|
||||
],
|
||||
),
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
child: _ActionButton(
|
||||
label: priceLabel, color: AppColors.accent, onTap: onTap),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
if (badge != null)
|
||||
Positioned(
|
||||
top: 4,
|
||||
left: 4,
|
||||
child: _Pill(text: badge!, color: AppColors.green),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _Panel extends StatelessWidget {
|
||||
final Widget child;
|
||||
const _Panel({required this.child});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(10),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.panel,
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
border: Border.all(color: AppColors.goldDark),
|
||||
),
|
||||
child: child,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _ActionButton extends StatelessWidget {
|
||||
final String label;
|
||||
final Color color;
|
||||
final VoidCallback onTap;
|
||||
const _ActionButton(
|
||||
{required this.label, required this.color, required this.onTap});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final busy = context.select((ShopCubit c) => c.state.busy);
|
||||
return ElevatedButton(
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: color,
|
||||
minimumSize: const Size.fromHeight(38),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 6),
|
||||
),
|
||||
onPressed: busy ? null : onTap,
|
||||
child: Text(label,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: const TextStyle(fontSize: 13)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _Pill extends StatelessWidget {
|
||||
final String text;
|
||||
final Color color;
|
||||
const _Pill({required this.text, required this.color});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 6),
|
||||
decoration: BoxDecoration(
|
||||
color: color,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Text(text,
|
||||
style: const TextStyle(color: Colors.white, fontSize: 12)),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user