generated from htl-leonding-college/asciidoctor-convert-template
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bf1ee86
commit a7daaa1
Showing
9 changed files
with
177 additions
and
35 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
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
19 changes: 19 additions & 0 deletions
19
labs/TodoAndroid/app/src/main/java/at/htl/todo/model/ModelStore.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,19 @@ | ||
package at.htl.todo.model; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Singleton; | ||
|
||
import at.htl.todo.util.store.Store; | ||
|
||
/** This is our Storage area for <a href="https://redux.js.org/understanding/thinking-in-redux/three-principles">single source of truth</a> {@link Model}. */ | ||
@Singleton | ||
public class ModelStore extends Store<Model> { | ||
@Inject | ||
ModelStore() { | ||
super(Model.class, new Model()); | ||
} | ||
public void setTodos(Todo[] todos) { | ||
apply(model -> model.todos = todos); | ||
} | ||
} | ||
|
7 changes: 0 additions & 7 deletions
7
labs/TodoAndroid/app/src/main/java/at/htl/todo/model/Store.java
This file was deleted.
Oops, something went wrong.
50 changes: 27 additions & 23 deletions
50
labs/TodoAndroid/app/src/main/java/at/htl/todo/model/TodoService.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 |
---|---|---|
@@ -1,30 +1,34 @@ | ||
package at.htl.todo.model; | ||
|
||
import org.eclipse.microprofile.config.Config; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Singleton; | ||
|
||
//@Singleton | ||
//public class TodoService { | ||
// | ||
// | ||
// public static final String JSON_PLACEHOLDER_BASE_URL_SETTING = "json.placeholder.baseurl"; | ||
// public final String baseUrl; | ||
// public final ToDoClient toDoClient; | ||
// public final ModelStore store; | ||
// | ||
// @Inject | ||
// ToDoService(Config config, RestApiClientBuilder builder, ModelStore store) { | ||
// this.baseUrl = config.getValue(JSON_PLACEHOLDER_BASE_URL_SETTING, String.class); | ||
// toDoClient = builder.build(ToDoClient.class, baseUrl); | ||
// this.store = store; | ||
// } | ||
// | ||
// public void getAll() { | ||
// CompletableFuture | ||
// .supplyAsync(() -> toDoClient.all()) | ||
// .thenAccept(store::setTodos); | ||
// } | ||
// | ||
//} | ||
import at.htl.todo.util.resteasy.RestApiClientBuilder; | ||
|
||
@Singleton | ||
public class TodoService { | ||
|
||
public static final String JSON_PLACEHOLDER_BASE_URL_SETTING = "json.placeholder.baseurl"; | ||
public final String baseUrl; | ||
public final TodoClient todoClient; | ||
public final ModelStore modelStore; | ||
|
||
@Inject // Hilt verlangt Constructor Injection | ||
TodoService(Config config, RestApiClientBuilder builder, ModelStore modelStore) { | ||
this.baseUrl = config.getValue(JSON_PLACEHOLDER_BASE_URL_SETTING, String.class); | ||
todoClient = builder.build(TodoClient.class, baseUrl); | ||
this.modelStore = modelStore; | ||
} | ||
|
||
public void getAll() { | ||
CompletableFuture | ||
.supplyAsync(() -> todoClient.all()) | ||
.thenAccept(modelStore::setTodos); | ||
//todoClient.all(); // NetworkOnMainThreadException | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
labs/TodoAndroid/app/src/main/java/at/htl/todo/util/immer/Immer.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,36 @@ | ||
package at.htl.todo.util.immer; | ||
|
||
import java.util.function.Consumer; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Singleton; | ||
|
||
import at.htl.todo.util.mapper.Mapper; | ||
|
||
|
||
/** Immer simplifies handling immutable data structures. | ||
* @see <a>https://immerjs.github.io/immer/</a> | ||
* @see <a>https://redux.js.org/understanding/thinking-in-redux/motivation</a> | ||
* | ||
* @param <T> The type of the baseState | ||
*/ | ||
@Singleton | ||
public class Immer<T> { | ||
public final Mapper<T> mapper; | ||
|
||
@Inject | ||
public Immer(Class<? extends T> type) { | ||
mapper = new Mapper<T>(type); | ||
} | ||
|
||
/** | ||
* @param readonlyState the readonly readonlyState | ||
* @param recipe the callback function that modifies the cloned state | ||
* @return | ||
*/ | ||
public T produce(final T readonlyState, Consumer<T> recipe) { | ||
var nextState = mapper.clone(readonlyState); | ||
recipe.accept(nextState); | ||
return nextState; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
labs/TodoAndroid/app/src/main/java/at/htl/todo/util/mapper/Mapper.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,51 @@ | ||
package at.htl.todo.util.mapper; | ||
|
||
import com.fasterxml.jackson.annotation.JsonAutoDetect; | ||
import com.fasterxml.jackson.annotation.PropertyAccessor; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
|
||
import java.io.IOException; | ||
|
||
/** A Mapper that maps types to their json representation and back. | ||
* ... plus a convenient deep-clone function | ||
* @param <T> the Class that is mapped | ||
*/ | ||
public class Mapper<T> { | ||
private Class<? extends T> clazz; | ||
private ObjectMapper mapper; | ||
|
||
public Mapper(Class<? extends T> clazz) { | ||
this.clazz = clazz; | ||
mapper = new ObjectMapper() | ||
.configure(SerializationFeature.INDENT_OUTPUT, true) | ||
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); | ||
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); // records | ||
} | ||
public String toResource(T model) { | ||
try { | ||
return mapper.writeValueAsString(model); | ||
} catch (JsonProcessingException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
public T fromResource(String json) { | ||
T model = null; | ||
try { | ||
model = mapper.readValue(json.getBytes(), clazz); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
return model; | ||
} | ||
/** deep clone an object by converting it to its json representation and back. | ||
* | ||
* @param thing the thing to clone, unchanged | ||
* @return the deeply cloned thing | ||
*/ | ||
public T clone(final T thing) { | ||
return fromResource(toResource(thing)); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
labs/TodoAndroid/app/src/main/java/at/htl/todo/util/store/Store.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,24 @@ | ||
package at.htl.todo.util.store; | ||
|
||
import java.util.concurrent.CompletionException; | ||
import java.util.function.Consumer; | ||
|
||
import at.htl.todo.util.immer.Immer; | ||
import io.reactivex.rxjava3.subjects.BehaviorSubject; | ||
|
||
public class Store<T> { | ||
public final BehaviorSubject<T> pipe; | ||
public final Immer<T> immer; | ||
|
||
protected Store(Class<? extends T> type, T initialState) { | ||
try { | ||
pipe = BehaviorSubject.createDefault(initialState); | ||
immer = new Immer<T>(type); | ||
} catch (Exception e) { | ||
throw new CompletionException(e); | ||
} | ||
} | ||
public void apply(Consumer<T> recipe) { | ||
pipe.onNext(immer.produce(pipe.getValue(), recipe)); | ||
} | ||
} |
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