Refactor ViewLoader for proper injection

ViewLoader is now modeled as a stateless singleton and injected into all
components (usually controllers) that need it. This is as opposed to the
prior situation in which a ViewLoader was instatiated every time view
loading was required. This was an understerstandable approach, given
that FXMLLoader (which ViewLoader wraps) assumes the same
construction-per-use approach, but it is nevertheless problematic.

 - Return Item tuple from ViewLoader#load.

   This avoids the need to call ViewLoader#load followed by individual
   calls to get the view and then the controller, as this requires
   mutable state to be held in the ViewLoader (which is now a shared
   singleton injected throughout the application). The previous approach
   is (a) confusing and (b) could create problems in a multithreaded
   environment. While (b) is unlikely in our case, the new approach is
   still clearer anyway.

 - Refactor ViewLoader#load to accept URL vs FxmlResource.

   This decouples the ViewLoader abstraction away from the
   Navigation.Item / FxmlResource abstraction completely.
This commit is contained in:
Chris Beams 2014-11-15 23:18:02 +01:00
parent cc75aec8f0
commit a4d3fab462
No known key found for this signature in database
GPG key ID: 3D214F8F5BC5ED73
21 changed files with 215 additions and 219 deletions

View file

@ -19,16 +19,19 @@ package io.bitsquare.app.gui;
import io.bitsquare.BitsquareException;
import io.bitsquare.app.BitsquareEnvironment;
import io.bitsquare.gui.GuiceControllerFactory;
import io.bitsquare.gui.Navigation;
import io.bitsquare.gui.ViewLoader;
import com.google.inject.Guice;
import com.google.inject.Injector;
import java.net.MalformedURLException;
import java.net.URL;
import javafx.application.Application;
import javafx.stage.Stage;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
@ -59,27 +62,24 @@ public class ViewLoaderTests {
Thread.sleep(10);
}
private GuiceControllerFactory controllerFactory;
@Before
public void setUp() {
OptionParser parser = new OptionParser();
BitsquareEnvironment env = new BitsquareEnvironment(parser.parse(new String[]{}));
Injector injector = Guice.createInjector(new BitsquareAppModule(env, TestApp.primaryStage));
ViewLoader.setInjector(injector);
}
@After
public void tearDown() {
ViewLoader.setInjector(null);
controllerFactory = injector.getInstance(GuiceControllerFactory.class);
controllerFactory.setInjector(injector);
}
@Test(expected = BitsquareException.class)
public void loadingBogusFxmlResourceShouldThrow() {
new ViewLoader(() -> "a bogus fxml resource", false).load();
public void loadingBogusFxmlResourceShouldThrow() throws MalformedURLException {
new ViewLoader(controllerFactory).load(Navigation.Item.BOGUS.getFxmlUrl(), false);
}
@Test
public void loadingValidFxmlResourceShouldNotThrow() {
new ViewLoader(Navigation.Item.ACCOUNT, false).load();
new ViewLoader(controllerFactory).load(Navigation.Item.ACCOUNT.getFxmlUrl(), false);
}
}