Skip to content

Commit 9e33403

Browse files
committed
Created Presenter for the ListAll usecase
Instead of logging directly in the Controller, the implementation of the presenter allows for logging to take place in the ListPresenter instead (as it should). Implementation Details: ListController now instantiates a new ListPresenter based on the log parameter, which performs the logging task with error codes, etc. ListResult is a record class storing the result of a call to ListAll in the controller. ListResultPresenter offers an interface for the ListPresenter's displayResult method, as pursuant to the Dependency Inversion Principle.
1 parent 0a1a600 commit 9e33403

File tree

7 files changed

+113
-23
lines changed

7 files changed

+113
-23
lines changed

src/main/java/org/hydev/mcpm/client/commands/controllers/ListController.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package org.hydev.mcpm.client.commands.controllers;
22

3+
import org.hydev.mcpm.client.display.presenters.ListPresenter;
34
import org.hydev.mcpm.client.list.ListAllBoundary;
5+
import org.hydev.mcpm.client.models.PluginYml;
46
import org.hydev.mcpm.client.updater.CheckForUpdatesBoundary;
57
import org.hydev.mcpm.client.list.ListType;
8+
import org.hydev.mcpm.client.list.ListResult;
9+
import org.hydev.mcpm.client.commands.presenters.ListResultPresenter;
610

711
import java.util.List;
812
import java.util.function.Consumer;
@@ -18,6 +22,7 @@ public class ListController {
1822
private final ListAllBoundary listAllBoundary;
1923
private final CheckForUpdatesBoundary checkForUpdatesBoundary;
2024

25+
2126
/**
2227
* Constructor for ListAllController.
2328
*
@@ -26,16 +31,16 @@ public class ListController {
2631
public ListController(ListAllBoundary listAllBoundary, CheckForUpdatesBoundary checkForUpdatesBoundary) {
2732
this.listAllBoundary = listAllBoundary;
2833
this.checkForUpdatesBoundary = checkForUpdatesBoundary;
34+
2935
}
3036

3137
/**
3238
* Executes the ListAll use case.
3339
*
3440
* @param parameter The parameter for the ListAll use case.
35-
* @param log Logger
3641
*/
3742
public void listAll(String parameter, Consumer<String> log) {
38-
43+
ListPresenter listPresenter = new ListPresenter(log);
3944
ListType listType;
4045
switch (parameter.toLowerCase()) {
4146
case "all":
@@ -51,17 +56,19 @@ public void listAll(String parameter, Consumer<String> log) {
5156
listType = ListType.OUTDATED;
5257
break;
5358
default:
54-
log.accept("Invalid parameter. Please use 'all', 'manual', 'automatic', or 'outdated'.");
59+
ListResult queryResult = new ListResult(null, ListResult.Type.SEARCH_INVALID_INPUT);
60+
listPresenter.displayResult(queryResult);
5561
return;
5662
}
5763

58-
var list = listAllBoundary.listAll(listType);
59-
60-
// Tabulate result
61-
var table = tabulate(
62-
list.stream().map(p -> List.of("&a" + p.name(), "&e" + p.getFirstAuthor(), p.version())).toList(),
63-
List.of(":Name", "Author", "Version:"));
64+
try {
65+
var list = listAllBoundary.listAll(listType);
66+
ListResult queryResult = new ListResult(list, ListResult.Type.SUCCESS_RETRIEVING_LOCAL_AND_UPDATABLE);
67+
listPresenter.displayResult(queryResult);
68+
} catch (Exception e) {
69+
ListResult queryResult = new ListResult(null, ListResult.Type.SEARCH_FAILED_TO_FETCH_INSTALLED);
70+
listPresenter.displayResult(queryResult);
71+
}
6472

65-
log.accept(table);
6673
}
6774
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.hydev.mcpm.client.commands.presenters;
2+
3+
import org.hydev.mcpm.client.installer.InstallResult;
4+
import org.hydev.mcpm.client.list.ListResult;
5+
6+
public interface ListResultPresenter {
7+
/**
8+
* Display the associated output to the console
9+
*
10+
* @param listResult result of the list command
11+
*/
12+
void displayResult(ListResult listResult);
13+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.hydev.mcpm.client.display.presenters;
2+
3+
import org.hydev.mcpm.client.commands.presenters.ListResultPresenter;
4+
import org.hydev.mcpm.client.list.ListResult;
5+
6+
import java.util.List;
7+
import java.util.function.Consumer;
8+
9+
import static org.hydev.mcpm.client.display.presenters.Table.tabulate;
10+
11+
public class ListPresenter implements ListResultPresenter {
12+
private final Consumer<String> log;
13+
14+
/**
15+
* Instantiate Install Presenter
16+
*
17+
* @param log log string to the console
18+
*/
19+
20+
public ListPresenter(Consumer<String> log) {
21+
this.log = log;
22+
}
23+
24+
@Override
25+
public void displayResult(ListResult listResult) {
26+
var list = listResult.queryResult();
27+
if (listResult.type() == ListResult.Type.SEARCH_INVALID_INPUT) {
28+
log.accept(listResult.type().reason() + "\n" + "Invalid input. Please enter one of the following: " +
29+
"all, manual, automatic, outdated");
30+
} else if (listResult.type() == ListResult.Type.SEARCH_FAILED_TO_FETCH_INSTALLED) {
31+
log.accept(listResult.type().reason() + "\n" + "Unable to fetch result.");
32+
} else {
33+
var table = tabulate(
34+
list.stream().map(p -> List.of("&a" + p.name(), "&e" + p.getFirstAuthor(), p.version())).toList(),
35+
List.of(":Name", "Author", "Version:"));
36+
this.log.accept(listResult.type().reason() + "\n" + table);
37+
}
38+
}
39+
}

src/main/java/org/hydev/mcpm/client/list/ListAllInteractor.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,15 @@
77
import java.util.List;
88

99
/**
10-
* Implementation to the ListAll functionality
11-
*
12-
* @author Kevin (https://github.com/kchprog)
13-
* @since 2022-11-20
10+
* Implementation for the ListAll functionality
1411
*/
1512
public record ListAllInteractor(PluginTracker pluginTracker, CheckForUpdatesBoundary checkForUpdatesBoundary) implements ListAllBoundary {
1613
/**
1714
* listAllInteractor interacts with the LocalPluginTracker to get the list of
1815
* plugins, according to a specified
1916
* parameter
2017
*
18+
*
2119
* @param parameter The parameter for the ListAll use case. 'All' denotes a
2220
* request to list all manually
2321
* installed plugins, 'manual' denotes a request to list all
@@ -40,8 +38,8 @@ public List<PluginYml> listAll(ListType parameter) {
4038
return installed.stream().filter(it -> !manual.contains(it.name())).toList();
4139

4240
case OUTDATED:
43-
ListUpdateableBoundary listUpdateableBoundary = new ListUpdateableHelper();
44-
var outdated = listUpdateableBoundary.listUpdateable(pluginTracker, checkForUpdatesBoundary);
41+
ListUpdatableBoundary listUpdatableBoundary = new ListUpdatableHelper();
42+
var outdated = listUpdatableBoundary.listUpdatable(pluginTracker, checkForUpdatesBoundary);
4543
return installed.stream().filter(it -> outdated.contains(it.name())).toList();
4644

4745
default:
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.hydev.mcpm.client.list;
2+
3+
4+
import org.hydev.mcpm.client.models.PluginYml;
5+
6+
import java.util.List;
7+
8+
9+
/**
10+
* Exception during installation of a plugin
11+
*/
12+
public record ListResult(List<PluginYml> queryResult, Type type) {
13+
/**
14+
* Type of list failure
15+
*/
16+
public enum Type {
17+
SEARCH_INVALID_INPUT("Invalid search input"),
18+
SEARCH_FAILED_TO_FETCH_INSTALLED("Failed to fetch the local files"),
19+
SUCCESS_RETRIEVING_LOCAL_BUT_FAIL_UPDATABLE("Successfully retrieved local files," +
20+
" but failed to retrieve updatable files"),
21+
SUCCESS_RETRIEVING_LOCAL_AND_UPDATABLE("Successfully retrieved local files");
22+
23+
private final String reason;
24+
25+
Type(String reason) {
26+
this.reason = reason;
27+
}
28+
29+
public String reason() {
30+
return reason;
31+
}
32+
}
33+
}

src/main/java/org/hydev/mcpm/client/list/ListUpdateableBoundary.java renamed to src/main/java/org/hydev/mcpm/client/list/ListUpdatableBoundary.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/**
88
* Defines an interface for obtaining a list of plugin names that may be updated
99
*/
10-
public interface ListUpdateableBoundary {
11-
ArrayList<String> listUpdateable(PluginTracker superPluginTracker,
12-
CheckForUpdatesBoundary checkForUpdatesBoundary);
10+
public interface ListUpdatableBoundary {
11+
ArrayList<String> listUpdatable(PluginTracker superPluginTracker,
12+
CheckForUpdatesBoundary checkForUpdatesBoundary);
1313
}

src/main/java/org/hydev/mcpm/client/list/ListUpdateableHelper.java renamed to src/main/java/org/hydev/mcpm/client/list/ListUpdatableHelper.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
import java.util.OptionalLong;
1919

2020
/**
21-
* A helper class for ListUpdateableBoundary.
21+
* A helper class for ListUpdatableBoundary.
2222
*/
23-
public class ListUpdateableHelper implements ListUpdateableBoundary {
23+
public class ListUpdatableHelper implements ListUpdatableBoundary {
2424

2525
/**
2626
* Returns a list of plugins names that belong to outdated plugins
@@ -29,7 +29,7 @@ public class ListUpdateableHelper implements ListUpdateableBoundary {
2929
* @param checkForUpdatesBoundary The boundary to check for updates
3030
* @return A list of plugins that are outdated.
3131
*/
32-
public ArrayList<String> listUpdateable(
32+
public ArrayList<String> listUpdatable(
3333
PluginTracker localPluginTracker, CheckForUpdatesBoundary checkForUpdatesBoundary) {
3434

3535
ArrayList<PluginVersionState> temp = new ArrayList<>();
@@ -56,7 +56,7 @@ public ArrayList<String> listUpdateable(
5656

5757
// Read the list of installedModels and create a CheckForUpdatesInput object
5858
// with state equal
59-
// to the list of PluginTrackerModels's version
59+
// to the list of PluginTrackerModels' version
6060

6161
if (rawResult.state() == CheckForUpdatesResult.State.SUCCESS) {
6262

0 commit comments

Comments
 (0)