KRTirtho / spotube

🎧 Open source Spotify client that doesn't require Premium nor uses Electron! Available for both desktop & mobile!
https://spotube.krtirtho.dev/
Other
31.25k stars 1.3k forks source link

refactorying #2011

Open s-b-repo opened 1 month ago

s-b-repo commented 1 month ago

Key Improvements:

Platform-Specific Setup Initialization:
    The Android-specific setup logic is isolated in _initPlatformSpecificSetup, making it easier to maintain and reason about.
    The Android session setup is encapsulated within _setAndroidAudioSession.

Player Event Subscriptions:
    All player event listeners are organized in _listenToPlayerEvents, improving clarity.
    This keeps the constructor cleaner and allows for better management of event streams.

Resource Management:
    Subscriptions and stream controllers are properly cleaned up in dispose() to avoid memory leaks.
    StreamController.broadcast() ensures that multiple listeners can listen to the streams without issues.

Separation of Concerns:
    setShuffle now has clearer functionality by adding a small delay before reordering the playlist to ensure shuffle logic is applied correctly.

Improved Readability:
    Method names and class structure follow a clean and logical order, which improves readability and maintainability.

These refactorings ensure better organization of platform-specific logic, effective resource management, and cleaner stream management

Key Improvements:

Playback Control: The methods related to play, pause, resume, stop, and seek are now simpler and reusable.
Volume and Speed: Methods like setVolume and setSpeed now follow assertions and checks for safer control.
Playlist Management: Common logic (like checking index bounds) is centralized, which reduces code duplication and improves clarity.
Helper Methods: currentSource, nextSource, and previousSource have been simplified using helper functions that avoid redundant code.
Modular Structure: The class now cleanly separates different responsibilities: playback control, playlist management, and device handling.

Here is the refactored version of your SpotubeAudioPlayer class. Improvements include:

Code Reusability: Reused some parts like volume assertions and better method organization.
Simplified Playlist Management: Moved repetitive logic for playlist management into helper methods.
Consistency: Ensured consistency with naming and behavior across the player methods.

Key Improvements:

SpotubeMediaFactory: Handles the logic of creating SpotubeMedia instances, allowing for easier scalability and reducing repetitive code.

Dependency Injection (DI): CustomPlayer is injected into the AudioPlayerInterface, improving testability and modularity.

Helper Methods: Functions like getNetworkAddress() and getUriForTrack() simplify and centralize repeated logic, improving maintainability.

Playback Control Methods: Added play(), pause(), stop(), and seek() methods for better playback control with error handling.

PlaybackStateManager: Manages the state-related properties (isPlaying, duration, etc.), keeping the AudioPlayerInterface cleaner and more focused on playback control.

Advantages:

Separation of Concerns: The code is now better structured with clear separation between media management (SpotubeMedia), playback state management (PlaybackStateManager), and playback controls (AudioPlayerInterface).

Extensibility: The code is more scalable with the factory pattern, making it easy to add new track types or other media sources.

Testability: With dependency injection, you can easily mock the CustomPlayer and test the logic of AudioPlayerInterface independently.

Clean Code: Centralized logic and helper methods reduce code duplication, improving readability and maintainability.

This refactored version of the code adheres to SOLID principles and provides a flexible, scalable, and clean architecture.

s-b-repo commented 1 month ago

please test it im not that great with dart