Skip to content

Commit 8fe3d7b

Browse files
committed
Add presenter
1 parent 8e3238e commit 8fe3d7b

File tree

4 files changed

+69
-24
lines changed

4 files changed

+69
-24
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.hydev.mcpm.client.commands.presenters;
2+
3+
import org.hydev.mcpm.client.export.ExportPluginsResult;
4+
5+
/**
6+
* Interface for presenting the export use case
7+
*/
8+
public interface ExportPresenter {
9+
void present(ExportPluginsResult exportPluginsResult);
10+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.hydev.mcpm.client.display.presenters;
2+
3+
import org.hydev.mcpm.client.commands.presenters.ExportPresenter;
4+
import org.hydev.mcpm.client.export.ExportPluginsResult;
5+
6+
import java.util.function.Consumer;
7+
8+
/**
9+
* ExportPresenter that presents to a log.
10+
*
11+
* @param log The log to present to
12+
*/
13+
public record LogExportPresenter(Consumer<String> log) implements ExportPresenter {
14+
15+
private String getColor(ExportPluginsResult exportPluginsResult) {
16+
return switch (exportPluginsResult.state()) {
17+
case SUCCESS -> "&a";
18+
case FAILED_TO_FETCH_PLUGINS -> "&c";
19+
};
20+
}
21+
22+
private String getMessage(ExportPluginsResult exportPluginsResult) {
23+
return switch ((exportPluginsResult.state())) {
24+
case SUCCESS -> exportPluginsResult.export();
25+
case FAILED_TO_FETCH_PLUGINS -> "Failed to fetch plugins";
26+
};
27+
}
28+
29+
30+
@Override
31+
public void present(ExportPluginsResult exportPluginsResult) {
32+
log.accept(getColor(exportPluginsResult) + getMessage(exportPluginsResult));
33+
}
34+
}

src/main/java/org/hydev/mcpm/client/export/ExportPluginsResult.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Results returned from ExportPluginBoundary.
77
*
88
* @param state The outcome of the export. Must be SUCCESS for other values to be valid.
9+
* @param export A string representing where the export went to.
910
*/
1011
public record ExportPluginsResult(
1112
State state,

src/main/java/org/hydev/mcpm/client/export/ImportResult.java

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
* Class storing results of the
88
*/
99
public class ImportResult {
10-
private State state;
11-
private Map<String, Boolean> installResults;
10+
private final State state;
11+
private final Map<String, Boolean> installResults;
1212

1313
/**
1414
* Result for importing plugins
@@ -17,6 +17,26 @@ public class ImportResult {
1717
*/
1818
public ImportResult(Map<String, Boolean> installResults) {
1919
this.installResults = installResults;
20+
boolean success = true;
21+
boolean fail = false;
22+
23+
/* Code for when import returns ImportResult instead of boolean
24+
Set<Type> good = new HashSet<>();
25+
for (Type type : Type.values()) {
26+
if (type.name().contains("SUCCESS")) // definitely legit
27+
good.add(type);
28+
}
29+
*/
30+
31+
for (var x : installResults.values()) {
32+
success &= x;
33+
fail |= x;
34+
if (!success && fail) { // not a full success nor full failure
35+
state = State.PARTIAL_SUCCESS;
36+
return;
37+
}
38+
}
39+
state = success ? State.SUCCESS : State.FAILURE;
2040
}
2141

2242

@@ -30,32 +50,12 @@ public enum State {
3050
}
3151

3252
/**
33-
* Infer the state from the state of the install results
53+
* Infer the state from the state of the installation results
3454
*
3555
* @return Overall state of the import
3656
*/
3757
public State getState()
3858
{
39-
if (state != null)
40-
return state;
41-
boolean success = true;
42-
boolean fail = false;
43-
44-
/* Code for when import returns ImportResult instead of boolean
45-
Set<Type> good = new HashSet<>();
46-
for (Type type : Type.values()) {
47-
if (type.name().contains("SUCCESS")) // definitely legit
48-
good.add(type);
49-
}
50-
*/
51-
52-
for (var x : installResults.values()) {
53-
success &= x;
54-
fail |= x;
55-
if (!success && fail) // not a full success nor full failure
56-
return state = State.PARTIAL_SUCCESS;
57-
}
58-
59-
return state = success ? State.SUCCESS : State.FAILURE;
59+
return state;
6060
}
6161
}

0 commit comments

Comments
 (0)