Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/patch-beta-002'
Browse files Browse the repository at this point in the history
# Conflicts:
#	jomodoro-timer.iml
  • Loading branch information
tsathis committed Sep 16, 2020
2 parents b3552cb + 372620d commit 57cf109
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@

import javafx.stage.Stage;

import java.util.Optional;

public abstract class Controller
{
ControllerManager controllerManager;
public Optional<ControllerManager> getControllerManager()
{
return Optional.ofNullable(controllerManager);
}

private ControllerManager controllerManager;
Stage stage;

public void setControllerManager( ControllerManager controllerManager )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,26 @@

import java.util.EnumMap;
import java.util.Map;
import java.util.Optional;

public class ControllerManager
{
private Map<View,Controller> controllersMap;

public Controller getController( View view )
private <T extends Controller> T findController( Class<T> controllerType )
{
if( controllersMap == null ) return null;
if( !controllersMap.containsKey( view )) return null;
return controllersMap.get( view );

for (Map.Entry<View, Controller> entry : controllersMap.entrySet()) {
if(controllerType.isInstance( entry.getValue())){
return (T) entry.getValue();
}
}
return null;
}

public <T extends Controller> Optional<T> getController( Class<T> controllerType){
return Optional.ofNullable( findController( controllerType ) );
}

public void registerController( View view, Controller controller, Stage stage )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ public class FullScreenController extends Controller
@FXML
void btnCloseOnAction( ActionEvent event )
{
//todo
// getStage().close();
controllerManager.showView( ControllerManager.View.MAIN );
getControllerManager().ifPresent( controllerManager -> controllerManager.showView( ControllerManager.View.MAIN ) );
}

@FXML
Expand Down Expand Up @@ -91,18 +89,18 @@ void btnStartOnAction( ActionEvent event )

private void startTimer()
{
final MainController mainController = ( MainController ) controllerManager.getController(
ControllerManager.View.MAIN );
mainController.setDefaultTimerDuration( Integer.parseInt( txtTime.getText() ) * 60 );
mainController.setBreakTimerDuration( Integer.parseInt( txtBreak.getText() ) * 60 );
mainController.resetTimer();
mainController.startTimer( );
mainController.setTagLabel( txtLabel.getText() );

//todo
// getStage().close();
mainController.setView( MainController.MainControllerViews.MAIN );
controllerManager.showView( ControllerManager.View.MAIN );
getControllerManager()
.flatMap( controllerManager -> controllerManager.getController( MainController.class ) )
.ifPresent( mainController ->
{
mainController.setDefaultTimerDuration( Integer.parseInt( txtTime.getText() ) * 60 );
mainController.setBreakTimerDuration( Integer.parseInt( txtBreak.getText() ) * 60 );
mainController.resetTimer();
mainController.startTimer();
mainController.setTagLabel( txtLabel.getText() );
mainController.setView( MainController.MainControllerViews.MAIN );
} );
getControllerManager().ifPresent( controllerManager -> controllerManager.showView( ControllerManager.View.MAIN ));
}

private void changeDurationValue( TextField textField, int i )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,17 @@

import java.util.Timer;

import static com.github.tharindusathis.jomodoro.controller.ControllerManager.View;
import static com.github.tharindusathis.jomodoro.controller.FullScreenController.FullscreenControllerView;

/**
* Main controller class
*
* @author tharindusathis
*/
public class MainController extends Controller
{
public State getCurrentState()
{
return currentState;
}

public void setCurrentState( State currentState )
{
this.currentState = currentState;
}

final static int FLASH_NOTIFY_REPEAT_INTERVAL = 60;
State currentState = State.INIT;
Timer timer;
int defaultTimerDuration = 25 * 60;
Expand Down Expand Up @@ -86,22 +80,14 @@ void addMinute()
}
}

private FullScreenController getFullScreenController()
public State getCurrentState()
{
if( fullScreenController == null )
{
if( controllerManager == null )
{
return null;
}
else
{
fullScreenController = ( FullScreenController ) controllerManager.getController(
ControllerManager.View.FULLSCREEN );
return fullScreenController;
}
}
return fullScreenController;
return currentState;
}

public void setCurrentState( State currentState )
{
this.currentState = currentState;
}

int getMinutes()
Expand All @@ -124,7 +110,8 @@ void handleBtnAdd( ActionEvent event )
void handleBtnBreak( ActionEvent event )
{
setTimerToBreakTime();
getFullScreenController().setView( FullScreenController.FullscreenControllerView.START );
getControllerManager().flatMap( ctrlMgr -> ctrlMgr.getController( FullScreenController.class ) )
.ifPresent( controller -> controller.setView( FullscreenControllerView.START ) );
}

@FXML
Expand Down Expand Up @@ -157,9 +144,14 @@ void handleBtnReset( ActionEvent event )
@FXML
void handleBtnSettings( ActionEvent event )
{

getFullScreenController().setView( FullScreenController.FullscreenControllerView.START );
controllerManager.showView( ControllerManager.View.FULLSCREEN );
getControllerManager().ifPresent(
ctrlMgr -> ctrlMgr.getController( FullScreenController.class )
.ifPresent( controller ->
{
controller.setView( FullscreenControllerView.START );
ctrlMgr.showView( View.FULLSCREEN );
} )
);
}

@FXML
Expand All @@ -183,17 +175,26 @@ else if( currentState.isRunning() )
void handleBtnTimerPlay( ActionEvent event )
{
resetTimer();
getFullScreenController().setView( FullScreenController.FullscreenControllerView.START );
controllerManager.showView( ControllerManager.View.FULLSCREEN );
getControllerManager().ifPresent( ctrlMgr -> ctrlMgr.getController( FullScreenController.class )
.ifPresent( controller ->
{
controller.setView( FullscreenControllerView.START );
ctrlMgr.showView( View.FULLSCREEN );
} ) );
}

@FXML
void handleBtnTimerPlayBreak( ActionEvent event )
{
setTimerToBreakTime();
startTimer();
getFullScreenController().setView( FullScreenController.FullscreenControllerView.BREAK );
controllerManager.showView( ControllerManager.View.FULLSCREEN );

getControllerManager().ifPresent( ctrlMgr -> ctrlMgr.getController( FullScreenController.class )
.ifPresent( controller ->
{
controller.setView( FullscreenControllerView.BREAK );
ctrlMgr.showView( View.FULLSCREEN );
} ) );
}

@FXML
Expand Down Expand Up @@ -309,7 +310,6 @@ else if( remainingSeconds > 0 )
mainWindowInvisibleBorder.setOnMouseEntered( onMouseLeaveMainView );
mainWindowInvisibleBorder.setOnMouseExited( onMouseLeaveMainView );

getFullScreenController();
resetTimer();
}

Expand Down Expand Up @@ -370,18 +370,26 @@ else if( String.valueOf( getMinutes() ).length() <= 3 )
formattedTime = "999+";
}
System.out.println( "Set time to: " + formattedTime );

lblTimer.setText( formattedTime );
if( getFullScreenController() != null )
{
getFullScreenController().getLblTimer().setText( formattedTime );
getFullScreenController().updateBreakProgressBar( ( double ) remainingSeconds / breakTimerDuration );
}

getControllerManager().flatMap( ctrlMgr -> ctrlMgr.getController( FullScreenController.class ) )
.ifPresent( ctrlr ->
{
ctrlr.getLblTimer().setText( formattedTime );
ctrlr.updateBreakProgressBar(
( double ) remainingSeconds / breakTimerDuration );
} );

lblTimerSmall.setText( formattedTime );
if( secs == 0 )
{
playSound();
( ( NotifyFlashScreenController ) controllerManager.getController(
ControllerManager.View.NOTIFY_FLASH ) ).flashRepeatedly(60);

getControllerManager()
.flatMap( ctrlMgr -> ctrlMgr.getController( NotifyFlashScreenController.class ) )
.ifPresent( controller -> controller.flashRepeatedly( FLASH_NOTIFY_REPEAT_INTERVAL ) )
;

setView( MainControllerViews.TIMER_STOP );
if( currentState == State.BREAK_RUNNING )
Expand Down Expand Up @@ -491,8 +499,11 @@ else if( btnCtrlCtrlView.isHover() || gridPaneCtrlBtnArea.isHover() )

public void startTimer()
{
( ( NotifyFlashScreenController ) controllerManager.getController(
ControllerManager.View.NOTIFY_FLASH ) ).flashReset();
getControllerManager()
.flatMap( ctrlMgr -> ctrlMgr.getController( NotifyFlashScreenController.class ) )
.ifPresent( NotifyFlashScreenController::flashReset )
;


if( currentState == State.BREAK_STOP )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public void flash()

public void flashRepeatedly( int seconds )
{
flashReset();
timer = new Timer();
timer.scheduleAtFixedRate( new TimerTask()
{
@Override
Expand All @@ -42,6 +44,7 @@ public void flashReset()
if( timer != null )
{
timer.purge();
timer.cancel();
}
}

Expand Down

0 comments on commit 57cf109

Please sign in to comment.