This is a theory question for me just to try and write better code. I am pretty new so hopefully I am using terms like state in the correct way.
How I should be thinking about this problem? I am coding something that sort of works but whatever I do it seems complicated, hard to understand and hard to modify.
I'm playing a video clip with an id, I also want it to be able to dynamically switch to a thumbnail view of that video clip. I want to be able to set this video blank also and shut down any connections that are not needed, I'm trying to do all of this efficiently - not reconnecting to the server when I don't need to etc.
so I have-
connected -> on/off
video id -> a string
mode -> streaming video/thumbnail view/disabled
Hopefully you will see my dilemma with some examples-
Case 1.The user starts from scratch and wants to view a thumbnail of video "a"
-connect to server for "a", send request for thumbnail, turn thumbnail object on.
Case 2.The user starts is viewing a thumbnail of video "a" and wants to start watching a stream of video "b"
-compare strings "a" and "b"- see they are different. hide thumbnail object and clear its buffer. Disconnect from server for "a" and connect to "b" server. start streaming video, turn video object on, request thumbnail from the server so its loaded in memory in case the user wants to switch to thumbnail mode.
Case 3.the user is watching a stream of "b" and wants to view a thumbnail of "b"
-compare strings "b" and "b"- see they are the same and stay connected to the server, hide video object and stop streaming, turn thumbnail object on, send another request to server for the thumbnail...
All these options are doing my head in, I want it to choose the most efficient way to get from one state to another but all the different options are confusing me. I guess Im looking for help about whats the best way to structure this, because what Im doing at the moment seems very confusing, i end up with huge tree of nested ifs of lots of specific cases and then patching up where it goes wrong.
if (connected but used to be disconnected) {
if (mode streaming but used to be thumbnail) {...
if (mode streaming but used to be streaming) {...
if (connected but used to be connected) {....
if (disconnected but used to be connected) {....
if (disconnected but used to be disconnected) {...
it goes on forever..