mirror of
https://github.com/TimeCrafters/TimeCraftersConfigurationTool.git
synced 2025-12-15 05:02:33 +00:00
Stubbed search
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
package org.timecrafters.TimeCraftersConfigurationTool.library;
|
||||
|
||||
import org.timecrafters.TimeCraftersConfigurationTool.backend.config.Action;
|
||||
import org.timecrafters.TimeCraftersConfigurationTool.backend.config.Group;
|
||||
import org.timecrafters.TimeCraftersConfigurationTool.backend.config.Variable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class SearchResult {
|
||||
public ArrayList<Group> groups = new ArrayList<>();
|
||||
public ArrayList<Action> actions = new ArrayList<>();
|
||||
public ArrayList<Variable> variables = new ArrayList<>();
|
||||
|
||||
public ArrayList<Group> groupPresets = new ArrayList<>();
|
||||
public ArrayList<Action> actionPresets = new ArrayList<>();
|
||||
public ArrayList<Variable> variablesFromPresets = new ArrayList<>();
|
||||
}
|
||||
@@ -4,29 +4,299 @@ import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ImageButton;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.lifecycle.Observer;
|
||||
import androidx.lifecycle.ViewModelProviders;
|
||||
|
||||
import org.timecrafters.TimeCraftersConfigurationTool.R;
|
||||
import org.timecrafters.TimeCraftersConfigurationTool.backend.Backend;
|
||||
import org.timecrafters.TimeCraftersConfigurationTool.backend.Config;
|
||||
import org.timecrafters.TimeCraftersConfigurationTool.backend.config.Action;
|
||||
import org.timecrafters.TimeCraftersConfigurationTool.backend.config.Group;
|
||||
import org.timecrafters.TimeCraftersConfigurationTool.backend.config.Variable;
|
||||
import org.timecrafters.TimeCraftersConfigurationTool.library.SearchResult;
|
||||
import org.timecrafters.TimeCraftersConfigurationTool.library.TimeCraftersFragment;
|
||||
|
||||
public class SearchFragment extends TimeCraftersFragment {
|
||||
final static String TAG = "SearchFragment";
|
||||
|
||||
private SearchViewModel searchViewModel;
|
||||
Config config;
|
||||
|
||||
LinearLayout searchResultsContainer;
|
||||
EditText searchQuery;
|
||||
ImageButton searchButton;
|
||||
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
this.config = Backend.instance().getConfig();
|
||||
|
||||
public View onCreateView(@NonNull LayoutInflater inflater,
|
||||
ViewGroup container, Bundle savedInstanceState) {
|
||||
searchViewModel =
|
||||
ViewModelProviders.of(this).get(SearchViewModel.class);
|
||||
View root = inflater.inflate(R.layout.fragment_search, container, false);
|
||||
searchViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
|
||||
this.searchQuery = root.findViewById(R.id.search_query);
|
||||
this.searchButton = root.findViewById(R.id.search);
|
||||
this.searchResultsContainer = root.findViewById(R.id.search_results);
|
||||
|
||||
searchButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onChanged(@Nullable String s) {
|
||||
public void onClick(View v) {
|
||||
if (Backend.instance().getConfig() != null) {
|
||||
performSearch();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
private void performSearch() {
|
||||
final String query = searchQuery.getText().toString().toLowerCase();
|
||||
|
||||
if (query.length() == 0) {
|
||||
searchResultsContainer.removeAllViews();
|
||||
return;
|
||||
}
|
||||
|
||||
SearchResult searchResult = search(query);
|
||||
|
||||
showSearchResults(searchResult);
|
||||
}
|
||||
|
||||
private void showSearchResults(SearchResult searchResult) {
|
||||
searchResultsContainer.removeAllViews();
|
||||
int i = 0;
|
||||
|
||||
// GROUPS
|
||||
if (searchResult.groups.size() > 0) {
|
||||
final View view = View.inflate(getContext(), R.layout.fragment_part_search_result_header, null);
|
||||
final TextView section = view.findViewById(R.id.section);
|
||||
|
||||
section.setText(R.string.groups);
|
||||
searchResultsContainer.addView(view);
|
||||
}
|
||||
|
||||
i = 0;
|
||||
for (Group group : searchResult.groups) {
|
||||
final View view = View.inflate(getContext(), R.layout.fragment_part_search_result, null);
|
||||
final Button button = view.findViewById(R.id.name);
|
||||
button.setText(group.name);
|
||||
|
||||
if (i % 2 == 0) { // even
|
||||
view.setBackgroundColor(getResources().getColor(R.color.list_even));
|
||||
} else {
|
||||
view.setBackgroundColor(getResources().getColor(R.color.list_odd));
|
||||
}
|
||||
|
||||
searchResultsContainer.addView(view);
|
||||
i++;
|
||||
}
|
||||
|
||||
// ACTIONS
|
||||
if (searchResult.actions.size() > 0) {
|
||||
final View view = View.inflate(getContext(), R.layout.fragment_part_search_result_header, null);
|
||||
final TextView section = view.findViewById(R.id.section);
|
||||
|
||||
section.setText(R.string.actions);
|
||||
searchResultsContainer.addView(view);
|
||||
}
|
||||
|
||||
i = 0;
|
||||
for (Action action : searchResult.actions) {
|
||||
final View view = View.inflate(getContext(), R.layout.fragment_part_search_result, null);
|
||||
final Button button = view.findViewById(R.id.name);
|
||||
button.setText(action.name);
|
||||
|
||||
if (i % 2 == 0) { // even
|
||||
view.setBackgroundColor(getResources().getColor(R.color.list_even));
|
||||
} else {
|
||||
view.setBackgroundColor(getResources().getColor(R.color.list_odd));
|
||||
}
|
||||
|
||||
searchResultsContainer.addView(view);
|
||||
i++;
|
||||
}
|
||||
|
||||
// VARIABLES
|
||||
if (searchResult.variables.size() > 0) {
|
||||
final View view = View.inflate(getContext(), R.layout.fragment_part_search_result_header, null);
|
||||
final TextView section = view.findViewById(R.id.section);
|
||||
|
||||
section.setText(R.string.variables);
|
||||
searchResultsContainer.addView(view);
|
||||
}
|
||||
|
||||
i = 0;
|
||||
for (Variable variable : searchResult.variables) {
|
||||
final View view = View.inflate(getContext(), R.layout.fragment_part_search_result, null);
|
||||
final Button button = view.findViewById(R.id.name);
|
||||
button.setText(variable.name + " [" + variable.value().toString() + "]");
|
||||
|
||||
if (i % 2 == 0) { // even
|
||||
view.setBackgroundColor(getResources().getColor(R.color.list_even));
|
||||
} else {
|
||||
view.setBackgroundColor(getResources().getColor(R.color.list_odd));
|
||||
}
|
||||
|
||||
searchResultsContainer.addView(view);
|
||||
i++;
|
||||
}
|
||||
|
||||
// PRESET GROUPS
|
||||
if (searchResult.groupPresets.size() > 0) {
|
||||
final View view = View.inflate(getContext(), R.layout.fragment_part_search_result_header, null);
|
||||
final TextView section = view.findViewById(R.id.section);
|
||||
|
||||
section.setText("Presets - Groups");
|
||||
searchResultsContainer.addView(view);
|
||||
}
|
||||
|
||||
i = 0;
|
||||
for (Group group : searchResult.groupPresets) {
|
||||
final View view = View.inflate(getContext(), R.layout.fragment_part_search_result, null);
|
||||
final Button button = view.findViewById(R.id.name);
|
||||
button.setText(group.name);
|
||||
|
||||
if (i % 2 == 0) { // even
|
||||
view.setBackgroundColor(getResources().getColor(R.color.list_even));
|
||||
} else {
|
||||
view.setBackgroundColor(getResources().getColor(R.color.list_odd));
|
||||
}
|
||||
|
||||
searchResultsContainer.addView(view);
|
||||
i++;
|
||||
}
|
||||
|
||||
// PRESET ACTIONS
|
||||
if (searchResult.actions.size() > 0) {
|
||||
final View view = View.inflate(getContext(), R.layout.fragment_part_search_result_header, null);
|
||||
final TextView section = view.findViewById(R.id.section);
|
||||
|
||||
section.setText("Presets - Actions");
|
||||
searchResultsContainer.addView(view);
|
||||
}
|
||||
|
||||
i = 0;
|
||||
for (Action action : searchResult.actionPresets) {
|
||||
final View view = View.inflate(getContext(), R.layout.fragment_part_search_result, null);
|
||||
final Button button = view.findViewById(R.id.name);
|
||||
button.setText(action.name);
|
||||
|
||||
if (i % 2 == 0) { // even
|
||||
view.setBackgroundColor(getResources().getColor(R.color.list_even));
|
||||
} else {
|
||||
view.setBackgroundColor(getResources().getColor(R.color.list_odd));
|
||||
}
|
||||
|
||||
searchResultsContainer.addView(view);
|
||||
i++;
|
||||
}
|
||||
|
||||
// VARIABLES FROM PRESETS
|
||||
if (searchResult.variablesFromPresets.size() > 0) {
|
||||
final View view = View.inflate(getContext(), R.layout.fragment_part_search_result_header, null);
|
||||
final TextView section = view.findViewById(R.id.section);
|
||||
|
||||
section.setText("Presets - Variables");
|
||||
searchResultsContainer.addView(view);
|
||||
}
|
||||
|
||||
i = 0;
|
||||
for (Variable variable : searchResult.variablesFromPresets) {
|
||||
final View view = View.inflate(getContext(), R.layout.fragment_part_search_result, null);
|
||||
final Button button = view.findViewById(R.id.name);
|
||||
button.setText(variable.name + " [" + variable.value().toString() + "]");
|
||||
|
||||
if (i % 2 == 0) { // even
|
||||
view.setBackgroundColor(getResources().getColor(R.color.list_even));
|
||||
} else {
|
||||
view.setBackgroundColor(getResources().getColor(R.color.list_odd));
|
||||
}
|
||||
|
||||
searchResultsContainer.addView(view);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
private SearchResult search(String query) {
|
||||
SearchResult searchResult = new SearchResult();
|
||||
|
||||
searchGroups(query, searchResult);
|
||||
searchActions(query, searchResult);
|
||||
searchVariables(query, searchResult);
|
||||
searchPresets(query, searchResult);
|
||||
|
||||
return searchResult;
|
||||
}
|
||||
|
||||
private void searchGroups(String query, SearchResult searchResult) {
|
||||
for (Group group : config.getGroups()) {
|
||||
if (group.name.toLowerCase().contains(query)) {
|
||||
searchResult.groups.add(group);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void searchActions(String query, SearchResult searchResult) {
|
||||
for (Group group : config.getGroups()) {
|
||||
for (Action action : group.getActions()) {
|
||||
if (action.name.toLowerCase().contains(query)) {
|
||||
searchResult.actions.add(action);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void searchVariables(String query, SearchResult searchResult) {
|
||||
for (Group group : config.getGroups()) {
|
||||
for (Action action : group.getActions()) {
|
||||
for (Variable variable : action.getVariables()) {
|
||||
if (variable.name.toLowerCase().contains(query)) {
|
||||
searchResult.variables.add(variable);
|
||||
}
|
||||
if (variable.value().toString().toLowerCase().contains(query)) {
|
||||
searchResult.variables.add(variable);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void searchPresets(String query, SearchResult searchResult) {
|
||||
for (Group group : config.getPresets().getGroups()) {
|
||||
if (group.name.toLowerCase().contains(query)) {
|
||||
searchResult.groupPresets.add(group);
|
||||
}
|
||||
}
|
||||
|
||||
for (Action action : config.getPresets().getActions()) {
|
||||
if (action.name.toLowerCase().contains(query)) {
|
||||
searchResult.actionPresets.add(action);
|
||||
}
|
||||
}
|
||||
|
||||
for (Group group : config.getPresets().getGroups()) {
|
||||
for (Action action : group.getActions()) {
|
||||
for (Variable variable : action.getVariables()) {
|
||||
if (variable.name.toLowerCase().contains(query)) {
|
||||
searchResult.variablesFromPresets.add(variable);
|
||||
}
|
||||
if (variable.value().toString().toLowerCase().contains(query)) {
|
||||
searchResult.variablesFromPresets.add(variable);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (Action action : config.getPresets().getActions()) {
|
||||
for (Variable variable : action.getVariables()) {
|
||||
if (variable.name.toLowerCase().contains(query)) {
|
||||
searchResult.variablesFromPresets.add(variable);
|
||||
}
|
||||
if (variable.value().toString().toLowerCase().contains(query)) {
|
||||
searchResult.variablesFromPresets.add(variable);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package org.timecrafters.TimeCraftersConfigurationTool.ui.search;
|
||||
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
import androidx.lifecycle.ViewModel;
|
||||
|
||||
public class SearchViewModel extends ViewModel {
|
||||
|
||||
private MutableLiveData<String> mText;
|
||||
|
||||
public SearchViewModel() {
|
||||
mText = new MutableLiveData<>();
|
||||
mText.setValue("This is search fragment");
|
||||
}
|
||||
|
||||
public LiveData<String> getText() {
|
||||
return mText;
|
||||
}
|
||||
}
|
||||
22
app/src/main/res/layout/fragment_part_search_result.xml
Normal file
22
app/src/main/res/layout/fragment_part_search_result.xml
Normal file
@@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/container"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent">
|
||||
|
||||
<Button
|
||||
android:id="@+id/name"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Button" />
|
||||
</LinearLayout>
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="@dimen/button_margin_top"
|
||||
android:layout_marginBottom="@dimen/button_margin_bottom">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/section"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="center"
|
||||
android:text="TextView"
|
||||
android:textColor="#000"
|
||||
android:textSize="18sp"
|
||||
android:textStyle="bold"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
@@ -45,4 +45,5 @@
|
||||
<string name="save_as_preset">Save as preset</string>
|
||||
<string name="add_from_preset">Add from preset</string>
|
||||
<string name="add_variable">Add Variable</string>
|
||||
<string name="variables">Variables</string>
|
||||
</resources>
|
||||
Reference in New Issue
Block a user