Merge pull request #117 from lacerta-doc/tag/add

タグ管理画面の追加
This commit is contained in:
ろむねこ 2024-01-26 15:41:49 +09:00 committed by GitHub
commit 5e5e98f0b6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 167 additions and 9 deletions

View File

@ -0,0 +1,87 @@
package one.nem.lacerta.component.common;
import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import androidx.fragment.app.DialogFragment;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
public class LacertaCreateTagDialog extends DialogFragment {
private String title;
private String message;
private String positiveButtonText;
private String negativeButtonText;
private LacertaCreateTagDialogListener listener;
// Setter
public LacertaCreateTagDialog setListener(LacertaCreateTagDialogListener listener) {
this.listener = listener;
return this;
}
public LacertaCreateTagDialog setTitle(String title) {
this.title = title;
return this;
}
public LacertaCreateTagDialog setMessage(String message) {
this.message = message;
return this;
}
public LacertaCreateTagDialog setPositiveButtonText(String positiveButtonText) {
this.positiveButtonText = positiveButtonText;
return this;
}
public LacertaCreateTagDialog setNegativeButtonText(String negativeButtonText) {
this.negativeButtonText = negativeButtonText;
return this;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(getActivity());
LayoutInflater inflater = requireActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.lacerta_dialog_create_tag, null);
// TextEdit
EditText tag_name_edit_text = view.findViewById(R.id.tag_name_edit_text);
com.google.android.material.textfield.TextInputLayout tag_name_text_input_layout = view.findViewById(R.id.tag_name_text_input_layout);
tag_name_text_input_layout.setHint("タグの名前");
EditText tag_color_edit_text = view.findViewById(R.id.tag_color_edit_text);
com.google.android.material.textfield.TextInputLayout tag_color_text_input_layout = view.findViewById(R.id.tag_color_text_input_layout);
tag_color_text_input_layout.setHint("タグの色(カラーコード)");
builder.setTitle(this.title == null ? "Create new tag" : this.title);
// Button
builder.setPositiveButton(positiveButtonText == null ? "OK" : positiveButtonText, (dialog, which) -> {
String tag_name = tag_name_edit_text.getText().toString();
String tag_color = tag_color_edit_text.getText().toString();
if (listener != null) {
listener.onPositiveClick(tag_name, tag_color);
}
});
builder.setNegativeButton(negativeButtonText == null ? "Cancel" : negativeButtonText, (dialog, which) -> {
if (listener != null) {
listener.onNegativeClick();
}
});
builder.setView(view);
return builder.create();
}
}

View File

@ -0,0 +1,8 @@
package one.nem.lacerta.component.common;
public interface LacertaCreateTagDialogListener {
void onPositiveClick(String tag_name, String tag_color);
void onNegativeClick();
}

View File

@ -4,12 +4,33 @@
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tag_name_text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:paddingHorizontal="16dp"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/tag_name_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tag_color_text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:paddingHorizontal="16dp"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<!-- todo: add color picker -->
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/tag_color_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>

View File

@ -9,6 +9,7 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
@ -244,6 +245,9 @@ public class LacertaLibraryImpl implements LacertaLibrary {
public CompletableFuture<Void> createTag(DocumentTag tag) {
return CompletableFuture.supplyAsync(() -> {
TagEntity tagEntity = convertDocumentTagToTagEntity(tag);
if (Objects.isNull(tagEntity.id) || tagEntity.id.isEmpty()) {
tagEntity.id = UUID.randomUUID().toString();
}
database.tagDao().insert(tagEntity);
logger.debug("LacertaLibraryImpl", "Database Query: Inserted TagEntity (" + tag.getId() + ")");
return null;

View File

@ -46,6 +46,8 @@ dependencies {
// shared
implementation project(':shared:ui')
implementation project(':component:common')
implementation project(':data')
implementation project(':model')

View File

@ -16,7 +16,10 @@ import android.widget.Toast;
import javax.inject.Inject;
import dagger.hilt.android.AndroidEntryPoint;
import one.nem.lacerta.component.common.LacertaCreateTagDialog;
import one.nem.lacerta.component.common.LacertaCreateTagDialogListener;
import one.nem.lacerta.data.LacertaLibrary;
import one.nem.lacerta.model.document.tag.DocumentTag;
/**
* A simple {@link Fragment} subclass.
@ -29,6 +32,10 @@ public class SettingTagManageFragment extends Fragment {
@Inject
LacertaLibrary lacertaLibrary;
private RecyclerView recyclerView;
private TagListItemAdapter adapter;
public SettingTagManageFragment() {
// Required empty public constructor
}
@ -62,19 +69,29 @@ public class SettingTagManageFragment extends Fragment {
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
RecyclerView recyclerView = view.findViewById(R.id.tag_item_recycler_view);
TagListItemAdapter adapter = new TagListItemAdapter((tagId, tagName, tagColor) -> {
this.recyclerView = view.findViewById(R.id.tag_item_recycler_view);
this.adapter = new TagListItemAdapter((tagId, tagName, tagColor) -> {
Toast.makeText(getContext(), "Tag Clicked", Toast.LENGTH_SHORT).show();
});
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
lacertaLibrary.getTagList().thenAccept(documentTags -> {
adapter.setDocumentTags(documentTags);
adapter.notifyDataSetChanged();
});
updateTagList();
}
/**
* タグリストを更新する
*/
private void updateTagList() {
lacertaLibrary.getTagList().thenAccept(documentTags -> {
int currentTagCount = this.adapter.getItemCount();
this.adapter.setDocumentTags(documentTags);
if (currentTagCount != this.adapter.getItemCount()) {
this.adapter.notifyItemRangeRemoved(0, currentTagCount);
this.adapter.notifyItemRangeInserted(0, this.adapter.getItemCount());
}
});
}
/**
@ -102,6 +119,25 @@ public class SettingTagManageFragment extends Fragment {
toolbar.setOnMenuItemClickListener(item -> {
if (item.getItemId() == R.id.setting_tag_manage_menu_add) {
Toast.makeText(getContext(), "Add Clicked", Toast.LENGTH_SHORT).show();
LacertaCreateTagDialog dialog = new LacertaCreateTagDialog();
dialog.setListener(new LacertaCreateTagDialogListener() {
@Override
public void onPositiveClick(String tagName, String tagColor) {
Toast.makeText(getContext(), "Positive Clicked", Toast.LENGTH_SHORT).show();
DocumentTag newTag = new DocumentTag();
newTag.setName(tagName);
newTag.setColor(tagColor);
lacertaLibrary.createTag(newTag).join();
updateTagList();
}
@Override
public void onNegativeClick() {
Toast.makeText(getContext(), "Negative Clicked", Toast.LENGTH_SHORT).show();
updateTagList();
}
});
dialog.show(getParentFragmentManager(), "create_tag_dialog");
return true;
} else {
return false;