-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from mvp4g/development
Changes for 1.0.1
- Loading branch information
Showing
44 changed files
with
1,624 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
### Place Service | ||
#### Description | ||
Mvp4g2 instantiates a Place Service to easily manage history based on History converter. | ||
|
||
History converters have two goals: | ||
|
||
* convert event parameters to a string (to add it to the token) and/or store them (in cookie for example) when an event | ||
is stored in browser history ([see this section for more information](#Associate_an_History_Converter_to_an_event)). | ||
|
||
* convert, when history changes, a token to an event, retrieve information to build its parameters (thanks to the token, | ||
database, cookie...) and then fires the converted event to event bus. This conversion is done by convertFromToken | ||
method. | ||
|
||
This is how Mvp4g2 stores an event to the browser history: | ||
|
||
![Place Service](https://github.com/mvp4g/mvp4g2/blob/master/etc/uml/place_service.png) | ||
|
||
This is how Mvp4g retrieves an event from browser history: | ||
|
||
![Place Service Reverse](https://github.com/mvp4g/mvp4g2/blob/master/etc/uml/place_service_reverse.png) | ||
|
||
The token stored in the browser history will be built the following way: event name (or history name, if one is enter | ||
inside the event bus) + "?" + value returned by the handling method of the event. This is the default implementation of | ||
mvp4g2. | ||
|
||
Any event can be stored in history. All you have to do is to associate a history converter to an event. | ||
|
||
If you need to store event information when the event is stored in the browser history, you can do this thanks to the convertToToken conversion method. | ||
|
||
If you need to retrieve event information when browser history changes, you can do this in the convertFromToken method of the history converter. | ||
|
||
#### Create a History Converter | ||
To create a history converter, you have to: | ||
|
||
* create a class that implements HistoryConverter | ||
* have a constructor with no parameter | ||
* annotate your class with @History | ||
* have your history converter implement the event to token conversion method ([see this section for more information](#Associate_an_History_Converter_to_an_event)). | ||
``` | ||
@History | ||
public class CompanyHistoryConverter | ||
implements HistoryConverter<CompanyEventBus> {...} | ||
``` | ||
The @History annotation has also an attribute type. The event to token method that you will have to define for the event will depend on the type ([see this section for more information](#Associate_an_History_Converter_to_an_event)). | ||
|
||
#### <a name="Associate_an_History_Converter_to_an_event">Associate a History Converter to an event</a> | ||
To add a history converter to an event, you need to specify the history converter attribute of the @Event annotation that annotates the method of your event. | ||
``` | ||
@Event(..., historyConverter = CompanyHistoryConverter.class) | ||
public void goToCompany(long id); | ||
``` | ||
By defining the history converter class, Mvp4g2 will be able to find the instance of history converter class and | ||
associate it with the event. | ||
|
||
Mvp4g2 generates instances of history converter as singleton so for one class, it generates only one instance, which means that if for several events, the same history converter class is associated, then the events will share the same instance of the converter. | ||
|
||
When a history converter is associated to an event, it needs to implement the conversion method of this event. The method to define will depend on the history converter type: | ||
|
||
* NONE: parameters won't be converted, only the event's name will be stored in browser's history. | ||
* DEFAULT: the history converter needs to define the handling method of the event but this method must return a String. The returned String will be added to the token and stored in browser's history. | ||
|
||
For example for the previous event, you need to define the following method in your history converter: | ||
``` | ||
public String onGoToCompany(long id); | ||
``` | ||
* SIMPLE: the history converter needs to define one convertToToken method for each event that has a different parameters signature. This convertToToken method must return String and must have the same parameters as the event to convert plus a first String parameter. Mvp4g will use this first String parameter to pass the event's name. The returned String will added to the token and stored in browser's history. | ||
|
||
For example, if you have the following event bus: | ||
``` | ||
public interface OneEventBus ... { | ||
@Event(..., historyConverter=OneHistoryConverter.class) | ||
void event1(int i); | ||
@Event(..., historyConverter=OneHistoryConverter.class) | ||
void event2(int i); | ||
@Event(..., historyConverter=OneHistoryConverter.class) | ||
void event3(int i, String s); | ||
} | ||
``` | ||
you would need to define this history converter with 2 convertToToken methods: | ||
``` | ||
public class OneHistoryConverter... { | ||
public String convertToToken(String eventType, int i){ | ||
//called by event1 and event2 | ||
... | ||
} | ||
public String convertToToken(String eventType, int i, String s){ | ||
//called by event 3 | ||
... | ||
} | ||
} | ||
``` | ||
You can define the history converter type thanks to the "type" attribute of the @History annotation: | ||
``` | ||
@History(type = HistoryConverterType.SIMPLE) | ||
public class OneHistoryConverter implements HistoryConverter { ... } | ||
``` | ||
By default, the type attribute is equals to **DEFAULT**. | ||
|
||
#### Init and NotFound events | ||
When dealing with history, two particular cases can happen: | ||
|
||
* token stored in history is null or empty (ie equals to ""). | ||
* token is incorrect (for example, user tried to modify the url and event type stored in the token is not correct). | ||
|
||
For both of these cases, Mvp4g lets you define events that can be fired if they happen. You can annotate the method defining an event in your event bus with: | ||
|
||
* @InitHistory, to manage the case when the token is empty | ||
* @NotFoundHistory, to manage the case when the token is incorrect. | ||
``` | ||
@InitHistory | ||
@Event(handlers = { RootTemplatePresenter.class, TopBarPresenter.class }) | ||
public void init(); | ||
@NotFoundHistory | ||
@Event(handlers = RootTemplatePresenter.class) | ||
public void notFound(); | ||
``` | ||
@InitHistory must be set if you have events with history converters. @NotFoundHistory is always optional. If you have events with history converters and you haven't set the @NotFoundHistory, then the event annotated with @InitHistory will be fired in case the token is incorrect. | ||
|
||
**No object can be fired with event(s) annotated with @InitHistory or @NotFoundHistory.** | ||
|
||
#### Clear History Token | ||
For some event, you may want to delete history token stored in the URL. In order to do so, you just have to associate your event to a particular HistoryConverter provided by the framework, ClearHistory. | ||
``` | ||
@Event(handlers = MainPresenter.class, historyConverter=ClearHistory.class) | ||
public void clearHistory(); | ||
``` | ||
#### History on start | ||
When you start your application, you may want to fire the current history state in order to convert any token that could be stored in the URL. | ||
|
||
In order to do so, you have to set the attribute historyOnStart of the @EventBus annotation of your event bus to true. By default this parameter is false. | ||
``` | ||
@Events(...historyOnStart = true) | ||
public interface MainEventBus extends EventBusWithLookup {...} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ndlerNotInBindAndHandlersAttribute/EventTestHandlerNotInBindAndHandlersAttributeImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...tTestEventBusWithOneStartAnnotation/StartEventTestEventBusWithOneStartAnnotationImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...lerAnnotation/EventBusEventhandlerWithHanderlsAttributeAndEventHandlerAnnotationImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
mvp4g2/etc/backUp/test/java/com/github/mvp4g/junit/test/core/PropertyFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright (c) 2018 - Frank Hossfeld | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
* | ||
*/ | ||
|
||
package com.github.mvp4g.junit.test.core; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Arrays; | ||
import java.util.Properties; | ||
|
||
import com.github.mvp4g.mvp4g2.core.history.annotation.History; | ||
import com.github.mvp4g.mvp4g2.core.internal.history.HistoryMetaData; | ||
import org.junit.Assert; | ||
|
||
public class PropertyFactory { | ||
|
||
|
||
private static HistoryMetaData getHistoryMetaData(String requestedHistoryConverter, | ||
String uri) { | ||
if (requestedHistoryConverter == null) { | ||
return null; | ||
} | ||
final HistoryMetaData[] historyMetaData = {null}; | ||
ClassLoader loader = Thread.currentThread() | ||
.getContextClassLoader(); | ||
Properties historyMetaDataProps = new Properties(); | ||
try (InputStream resourceStream = loader.getResourceAsStream(uri)) { | ||
historyMetaDataProps.load(resourceStream); | ||
} catch (IOException e) { | ||
Assert.fail("Resource >>" + uri + "<< not found!"); | ||
} | ||
String[] historyConverters = historyMetaDataProps.getProperty("historyConverters") | ||
.split(","); | ||
Arrays.stream(historyConverters) | ||
.filter(s -> requestedHistoryConverter.equals(s)) | ||
.forEach(s -> { | ||
History.HistoryConverterType type; | ||
String historyConverterType = historyMetaDataProps.getProperty(s + ".historyConverterType"); | ||
switch (historyConverterType) { | ||
case "DEFAULT": | ||
type = History.HistoryConverterType.DEFAULT; | ||
break; | ||
case "SIMPLE": | ||
type = History.HistoryConverterType.SIMPLE; | ||
break; | ||
default: | ||
type = History.HistoryConverterType.NONE; | ||
break; | ||
} | ||
historyMetaData[0] = new HistoryMetaData(historyMetaDataProps.getProperty(s + ".historyConverter"), | ||
type) { | ||
@Override | ||
public String getHistoryConverterClassName() { | ||
return super.getHistoryConverterClassName(); | ||
} | ||
}; | ||
}); | ||
return historyMetaData.length == 0 ? null : historyMetaData[0]; | ||
} | ||
} |
Oops, something went wrong.