upstock.dart
Read-only136 lines • 4492 bytesApp StoreGoogle Play
/**
 * @id upstock
 * @title Upstock — Gamified Stock Trading & Investment Platform
 * @description A modern stock trading application integrating gamification to enhance user engagement.
 * @date Feb 2025 – Aug 2025
 * @company Bolt Holdings Talent
 * @role Mobile Developer
 * @tags Dart, Flutter, Rive Animations, Real-time Order Book, Gamification, WebSockets
 * @summary Modern stock trading application integrating real-time Order Book, gamified User Quests, and high-performance Rive animations.
 * @playStore https://play.google.com/store/apps/details?id=com.upstock.invest
 * @appStore https://apps.apple.com/app/upstock-trade-invest/id6479590623
 * @order 4
 */
/// Project: Upstock (Trade & Invest)
/// Organization: Associated with Bolt Holdings Talent
/// Duration: Feb 2025 – Aug 2025
/// Focus: High-Frequency Order Book, Gamification Quests, Interactive Rive Motion Design

import 'dart:async';

// ============================================================================
// 1. REAL-TIME ORDER BOOK MODULE (LEVEL 2 MARKET DATA)
// ============================================================================

class PriceLevel {
  final double price;
  final int volume;
  final int orderCount;

  const PriceLevel({
    required this.price,
    required this.volume,
    required this.orderCount,
  });
}

class OrderBookSnapshot {
  final String symbol;
  final List<PriceLevel> bids;
  final List<PriceLevel> asks;
  final DateTime timestamp;

  const OrderBookSnapshot({
    required this.symbol,
    required this.bids,
    required this.asks,
    required this.timestamp,
  });

  /// Computes cumulative volume depth for visual order book ladder rendering
  double calculateDepthPercentage(int currentVolume, int maxVolume) {
    if (maxVolume == 0) return 0.0;
    return (currentVolume / maxVolume).clamp(0.0, 1.0);
  }
}

/// High-throughput Order Book stream provider buffering WebSocket updates
/// to prevent UI thread frame drops during extreme market volatility.
class OrderBookManager {
  final StreamController<OrderBookSnapshot> _orderBookStream =
      StreamController<OrderBookSnapshot>.broadcast();

  Stream<OrderBookSnapshot> get stream => _orderBookStream.stream;

  void handleMarketDataTick(String symbol, Map<String, dynamic> rawPacket) {
    // Throttled 60fps state dispatch for fluid market depth animation
  }
}

// ============================================================================
// 2. USER QUEST & TASK GAMIFICATION ENGINE
// ============================================================================

enum QuestType { firstTrade, dailyStreak, portfolioDiversification, volumeMilestone }

class UserQuest {
  final String id;
  final String title;
  final String description;
  final QuestType type;
  final int currentProgress;
  final int targetGoal;
  final int rewardXp;
  final bool isClaimed;

  const UserQuest({
    required this.id,
    required this.title,
    required this.description,
    required this.type,
    required this.currentProgress,
    required this.targetGoal,
    required this.rewardXp,
    required this.isClaimed,
  });

  bool get isCompleted => currentProgress >= targetGoal;
}

class GamificationEngine {
  final List<UserQuest> _activeQuests = [];

  void onTradeExecuted({required String symbol, required double amount}) {
    // Updates quest progress, rewards user badges, and fires trigger animations
  }
}

// ============================================================================
// 3. RIVE ANIMATION STATE MACHINE INTEGRATION
// ============================================================================

abstract class RiveController {
  void fireTrigger(String triggerName);
  void setNumberInput(String inputName, double value);
}

/// Manages dynamic Rive animations responding seamlessly to user interactions
/// such as celebration confetti, mascot reactions, and level-up rewards.
class UpstockRiveAnimationManager {
  RiveController? _mascotStateMachine;

  void bindStateMachine(RiveController controller) {
    _mascotStateMachine = controller;
  }

  /// Triggers gamified visual effect on successful trade execution
  void playTradeCelebration(double orderValue) {
    _mascotStateMachine?.setNumberInput('tradeValue', orderValue);
    _mascotStateMachine?.fireTrigger('onTradeSuccess');
  }

  /// Plays dynamic reaction when market price hits user target alert
  void playPriceAlertNotification() {
    _mascotStateMachine?.fireTrigger('onAlertTriggered');
  }
}