Skip to content

Commit a1e029e

Browse files
authored
Merge pull request #94 from CSC207-2022F-UofT/search-presenter
Search Presenter
2 parents 49dcea4 + 24b06eb commit a1e029e

14 files changed

+90
-38
lines changed

src/main/java/org/hydev/mcpm/client/arguments/CommandsFactory.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.hydev.mcpm.client.commands.controllers.UninstallController;
3030
import org.hydev.mcpm.client.commands.controllers.UnloadController;
3131
import org.hydev.mcpm.client.commands.controllers.UpdateController;
32+
import org.hydev.mcpm.client.display.presenters.SearchPresenter;
3233
import org.hydev.mcpm.client.local.LocalPluginTracker;
3334
import org.hydev.mcpm.client.updater.CheckForUpdatesInteractor;
3435
import org.hydev.mcpm.client.list.ListAllInteractor;
@@ -86,7 +87,7 @@ public static List<CommandParser> baseParsers(boolean isMinecraft) {
8687
// Controllers
8788
var exportPluginsController = new ExportPluginsController(new ExportInteractor(superTracker));
8889
var listController = new ListController(new ListAllInteractor(superTracker), updateChecker);
89-
var searchController = new SearchPackagesController(searcher, pager);
90+
var searchController = new SearchPackagesController(searcher);
9091
var mirrorController = new MirrorController(mirror);
9192
var infoController = new InfoController(superTracker);
9293

@@ -95,6 +96,8 @@ public static List<CommandParser> baseParsers(boolean isMinecraft) {
9596
var updateController = new UpdateController(updater);
9697
var refreshController = new RefreshController(fetcher, listener, mirror);
9798

99+
var searchPresenter = new SearchPresenter(pager);
100+
98101
/*
99102
* Add general parsers to this list!
100103
* All versions of MCPM will have access to these parsers.
@@ -103,7 +106,7 @@ public static List<CommandParser> baseParsers(boolean isMinecraft) {
103106
return List.of(
104107
new ExportPluginsParser(exportPluginsController),
105108
new ListParser(listController),
106-
new SearchParser(searchController),
109+
new SearchParser(searchController, searchPresenter),
107110
new MirrorParser(mirrorController),
108111
new InfoParser(infoController),
109112
new InstallParser(installController),

src/main/java/org/hydev/mcpm/client/arguments/parsers/SearchParser.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import net.sourceforge.argparse4j.inf.Namespace;
55
import net.sourceforge.argparse4j.inf.Subparser;
66
import org.hydev.mcpm.client.commands.controllers.SearchPackagesController;
7+
import org.hydev.mcpm.client.commands.presenters.SearchResultPresenter;
8+
import org.hydev.mcpm.client.display.presenters.SearchPresenter;
79

810
import java.util.List;
911
import java.util.function.Consumer;
@@ -13,7 +15,8 @@
1315
* When the user runs the search command, the program prompts the user to specify the type of
1416
* search and the search text.
1517
*/
16-
public record SearchParser(SearchPackagesController controller) implements CommandParser {
18+
public record SearchParser(SearchPackagesController controller,
19+
SearchResultPresenter presenter) implements CommandParser {
1720
@Override
1821
public String name() {
1922
return "search";
@@ -51,6 +54,8 @@ public void run(Namespace details, Consumer<String> log) {
5154

5255
// Call searchPackages
5356
List<String> t = details.getList("text");
54-
controller.searchPackages(type, String.join(" ", t), !details.getBoolean("noCache"), log);
57+
var result = controller.searchPackages(type,
58+
String.join(" ", t), !details.getBoolean("noCache"));
59+
presenter.displayResult(result, log);
5560
}
5661
}

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

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.hydev.mcpm.client.models.PluginModel;
55
import org.hydev.mcpm.client.search.SearchPackagesBoundary;
66
import org.hydev.mcpm.client.search.SearchPackagesInput;
7+
import org.hydev.mcpm.client.search.SearchPackagesResult;
78
import org.hydev.mcpm.client.search.SearchPackagesType;
89

910
import javax.annotation.Nullable;
@@ -14,43 +15,23 @@
1415
/**
1516
* Handles the user input for a search.
1617
*
17-
* @author Jerry
1818
*/
19-
public record SearchPackagesController(SearchPackagesBoundary searcher, @Nullable PageBoundary pageController) {
19+
public record SearchPackagesController(SearchPackagesBoundary searcher) {
2020

2121
/**
2222
* Load plugins and output status to log.
2323
*
2424
* @param type String that specifies the type of search.
2525
* @param text String that specifies the search text.
2626
* @param noCache Specifies whether to use local cache or not.
27-
* @param log Callback for status for log events.
27+
*
28+
* @return The search result.
2829
*/
29-
public void searchPackages(String type, String text, boolean noCache, Consumer<String> log) {
30+
public SearchPackagesResult searchPackages(String type, String text, boolean noCache) {
3031
SearchPackagesInput inp = new SearchPackagesInput(
3132
SearchPackagesType.valueOf("BY_" + type.toUpperCase()),
3233
text,
3334
noCache);
34-
var result = searcher.search(inp);
35-
log.accept("Search State: " + result.state().toString());
36-
37-
// Print the plugins found
38-
var list = result.plugins().stream()
39-
.map(PluginModel::getLatestPluginVersion)
40-
.filter(Optional::isPresent)
41-
.map(it -> it.get().meta())
42-
.toList();
43-
44-
var table = new Table(List.of(":Name", "Author", "Version:"),
45-
list.stream().map(p -> List.of("&a" + p.name(), "&e" + p.getFirstAuthor(), p.version())).toList());
46-
47-
// Pagination
48-
if (pageController != null) {
49-
pageController.store(table);
50-
log.accept(pageController.formatPage(1));
51-
}
52-
else {
53-
log.accept(table.toString());
54-
}
35+
return searcher.search(inp);
5536
}
5637
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.hydev.mcpm.client.commands.presenters;
2+
3+
import org.hydev.mcpm.client.search.SearchPackagesResult;
4+
5+
import java.util.function.Consumer;
6+
7+
/**
8+
* Interface for presenting the search result.
9+
*
10+
*/
11+
public interface SearchResultPresenter {
12+
/**
13+
* Display the string to the console.
14+
*
15+
* @param searchResult results of the search.
16+
* @param log log Callback for status for log events.
17+
*/
18+
void displayResult(SearchPackagesResult searchResult, Consumer<String> log);
19+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.hydev.mcpm.client.display.presenters;
2+
3+
import org.hydev.mcpm.client.commands.controllers.PageBoundary;
4+
import org.hydev.mcpm.client.commands.presenters.SearchResultPresenter;
5+
import org.hydev.mcpm.client.models.PluginModel;
6+
import org.hydev.mcpm.client.search.SearchPackagesResult;
7+
8+
import javax.annotation.Nullable;
9+
import java.util.List;
10+
import java.util.Optional;
11+
import java.util.function.Consumer;
12+
13+
/**
14+
* Implementation to the SearchResultPresenter interface, displaying the result of the search command.
15+
*/
16+
public class SearchPresenter implements SearchResultPresenter {
17+
@Nullable
18+
PageBoundary pageController;
19+
20+
/**
21+
* Instantiate Install Presenter
22+
*
23+
* @param pageController controller for pagination.
24+
*/
25+
26+
public SearchPresenter(@org.jetbrains.annotations.Nullable PageBoundary pageController) {
27+
this.pageController = pageController;
28+
}
29+
30+
@Override
31+
public void displayResult(SearchPackagesResult result, Consumer<String> log) {
32+
log.accept("Search State: " + result.state().toString());
33+
34+
// Print the plugins found
35+
var list = result.plugins().stream()
36+
.map(PluginModel::getLatestPluginVersion)
37+
.filter(Optional::isPresent)
38+
.map(it -> it.get().meta())
39+
.toList();
40+
41+
var table = new Table(List.of(":Name", "Author", "Version:"),
42+
list.stream().map(p -> List.of("&a" + p.name(), "&e" + p.getFirstAuthor(), p.version())).toList());
43+
44+
// Pagination
45+
if (pageController != null) {
46+
pageController.store(table);
47+
log.accept(pageController.formatPage(1));
48+
}
49+
else {
50+
log.accept(table.toString());
51+
}
52+
}
53+
}

src/main/java/org/hydev/mcpm/client/search/SearchInteractor.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
/**
99
* Handles searching within the database.
1010
*
11-
* @author Jerry Zhu (<a href="https://github.com/jerryzhu509">...</a>)
1211
*/
1312
public class SearchInteractor implements SearchPackagesBoundary {
1413
private final DatabaseFetcher fetcher;

src/main/java/org/hydev/mcpm/client/search/SearchPackagesBoundary.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
/**
44
* Interface for searching plugins.
55
*
6-
* @author Jerry Zhu (<a href="https://github.com/jerryzhu509">...</a>)
76
*/
87
public interface SearchPackagesBoundary {
98
/**

src/main/java/org/hydev/mcpm/client/search/SearchPackagesInput.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
* @param searchStr String containing the name, keyword, or command to search by.
88
* @param noCache If true, the ListPackagesBoundary will re-download the database before performing the request.
99
*
10-
* @author Jerry Zhu (<a href="https://github.com/jerryzhu509">...</a>)
1110
*/
1211
public record SearchPackagesInput(SearchPackagesType type, String searchStr, boolean noCache) {
1312

src/main/java/org/hydev/mcpm/client/search/SearchPackagesResult.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
* @param state The outcome of the request. Must be Success for the other values to be valid.
1111
* @param plugins The list of plugins corresponding to the search.
1212
*
13-
* @author Jerry Zhu (<a href="https://github.com/jerryzhu509">...</a>)
1413
*/
1514
public record SearchPackagesResult(SearchPackagesResult.State state, List<PluginModel> plugins) {
1615
/**

src/main/java/org/hydev/mcpm/client/search/Searcher.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
/**
99
* Interface for searchers that return Maps based on the list of plugins.
1010
*
11-
* @author Jerry Zhu (<a href="https://github.com/jerryzhu509">...</a>)
1211
*/
1312
public interface Searcher {
1413
/**

0 commit comments

Comments
 (0)