Merge pull request #53 from lacerta-doc/data/add_library_item_stub

LacertaLibraryインターフェースにスタブを追加
This commit is contained in:
ろむねこ 2024-01-13 14:02:05 +09:00 committed by GitHub
commit a9b76161b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 465 additions and 2 deletions

View File

@ -41,6 +41,9 @@ dependencies {
// JGit // JGit
implementation 'org.eclipse.jgit:org.eclipse.jgit:6.8.0.202311291450-r' implementation 'org.eclipse.jgit:org.eclipse.jgit:6.8.0.202311291450-r'
// Java Faker
implementation 'com.github.javafaker:javafaker:1.0.2'
// Room // Room
implementation libs.androidx.room.runtime implementation libs.androidx.room.runtime
annotationProcessor libs.androidx.room.compiler annotationProcessor libs.androidx.room.compiler

View File

@ -1,4 +1,42 @@
package one.nem.lacerta.data.impl; package one.nem.lacerta.data.impl;
public class LacertaLibraryImpl { import one.nem.lacerta.data.LacertaLibrary;
import one.nem.lacerta.model.LibraryItemPage;
import one.nem.lacerta.model.document.DocumentDetail;
public class LacertaLibraryImpl implements LacertaLibrary {
@Override
public LibraryItemPage getRecentDocument(int limit) {
return null;
}
@Override
public LibraryItemPage getRecentDocument(int limit, int offset) {
return null;
}
@Override
public LibraryItemPage getLibraryPage(int limit) {
return null;
}
@Override
public LibraryItemPage getLibraryPage(int limit, int offset) {
return null;
}
@Override
public LibraryItemPage getLibraryPage(String pageId, int limit) {
return null;
}
@Override
public LibraryItemPage getLibraryPage(String pageId, int limit, int offset) {
return null;
}
@Override
public DocumentDetail getDocumentDetailById(String id) {
return null;
}
} }

View File

@ -0,0 +1,219 @@
package one.nem.lacerta.data.impl;
import one.nem.lacerta.data.LacertaLibrary;
import one.nem.lacerta.model.LibraryItemPage;
import one.nem.lacerta.model.ListItem;
import one.nem.lacerta.model.ListItemType;
import one.nem.lacerta.model.document.DocumentDetail;
import one.nem.lacerta.model.document.DocumentMeta;
import one.nem.lacerta.model.document.path.DocumentPath;
import one.nem.lacerta.utils.LacertaLogger;
import com.github.javafaker.DateAndTime;
import com.github.javafaker.Faker;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Objects;
import java.util.UUID;
import javax.inject.Inject;
/**
* LacertaLibraryのスタブ実装
*/
public class LacertaLibraryStubImpl implements LacertaLibrary {
LacertaLogger logger;
Faker faker;
@Inject
public LacertaLibraryStubImpl(LacertaLogger logger) {
faker = new Faker(); // Init Faker
this.logger = logger;
logger.debug("LibraryStub", "Initialized");
}
// Internal Methods
// Generate Stub Data
private LibraryItemPage generateStubLibraryItemPage(int limit, String pageId) {
logger.debug("LibraryStub", "generateStubLibraryItemPage");
ArrayList<ListItem> listItems = new ArrayList<>();
int itemTotal = faker.number().numberBetween(1, limit); // 実際に返却するアイテム数を決定
int folderTotal;
// フォルダ数の抽選
if (itemTotal > 4) {
folderTotal = faker.number().numberBetween(1, itemTotal - 2);
}
else {
if (itemTotal > 2) {
folderTotal = 1;
}
else { // ドキュメント数がゼロにならないように
folderTotal = 0;
}
}
int documentTotal = itemTotal - folderTotal; // ドキュメント数を決定
logger.debug("LibraryStub", "itemTotal: " + itemTotal);
logger.debug("LibraryStub", "folderTotal: " + folderTotal);
logger.debug("LibraryStub", "documentTotal: " + documentTotal);
// フォルダを生成
for (int i = 0; i < folderTotal; i++) {
listItems.add(generateStubListItem(ListItemType.ITEM_TYPE_FOLDER));
}
// ドキュメントを生成
for (int i = 0; i < documentTotal; i++) {
listItems.add(generateStubListItem(ListItemType.ITEM_TYPE_DOCUMENT));
}
LibraryItemPage libraryItemPage = new LibraryItemPage();
libraryItemPage.setListItems(listItems);
if (pageId == null) {
libraryItemPage.setPageId(UUID.randomUUID().toString());
} else {
libraryItemPage.setPageId(pageId);
}
libraryItemPage.setPageTitle("FakePage" + faker.number().digits(3));
return libraryItemPage;
}
private ListItem generateStubListItem(ListItemType itemType) {
if (itemType == ListItemType.ITEM_TYPE_FOLDER) {
ListItem listItem = new ListItem();
listItem.setTitle("FakeFolder" + faker.number().digits(3));
listItem.setDescription("Updated at " + DateFormat.getDateTimeInstance().format(faker.date().birthday()));
listItem.setItemType(ListItemType.ITEM_TYPE_FOLDER);
listItem.setItemId(UUID.randomUUID().toString());
return listItem;
} else if (itemType == ListItemType.ITEM_TYPE_DOCUMENT) {
ListItem listItem = new ListItem();
listItem.setTitle("FakeDocument" + faker.book().title());
listItem.setDescription("Updated at " + DateFormat.getDateTimeInstance().format(faker.date().birthday()));
listItem.setItemType(ListItemType.ITEM_TYPE_DOCUMENT);
listItem.setItemId(UUID.randomUUID().toString());
return listItem;
} else {
return null;
}
}
private LibraryItemPage getRecentDocumentPage(int limit) {
int itemTotal = faker.number().numberBetween(1, limit);
ArrayList<ListItem> listItems = new ArrayList<>();
for (int i = 0; i < itemTotal; i++) {
listItems.add(generateStubListItem(ListItemType.ITEM_TYPE_DOCUMENT));
}
// DescriptionからDateを抽出して新しい順にソート
listItems.sort((a, b) -> {
String aDate = a.getDescription().substring(11);
String bDate = b.getDescription().substring(11);
return bDate.compareTo(aDate);
});
LibraryItemPage libraryItemPage = new LibraryItemPage();
libraryItemPage.setListItems(listItems);
libraryItemPage.setPageId(UUID.randomUUID().toString());
libraryItemPage.setPageTitle("RecentDocument");
return libraryItemPage;
}
private DocumentDetail generateStubDocumentDetail(String id) throws IllegalArgumentException {
if (Objects.isNull(id)) {
throw new IllegalArgumentException("id is null");
}
DocumentMeta documentMeta = new DocumentMeta();
documentMeta.setId(id);
documentMeta.setTitle("FakeDocument" + faker.book().title());
documentMeta.setCreatedAt(faker.date().birthday());
documentMeta.setUpdatedAt(faker.date().birthday()); // TODO-rca: 更新日のほうが古くなることがあるのでなんとかする
ArrayList<String> tagIds = new ArrayList<>();
DocumentDetail documentDetail = new DocumentDetail();
documentDetail.setMeta(documentMeta);
documentDetail.setPath(null); // TODO-rca: なんとかする
documentDetail.setAuthor(faker.name().fullName());
documentDetail.setRepository(null); // TODO-rca: なんとかする
return documentDetail;
}
/**
* 履歴ページを取得する
* @param limit 取得するアイテム数
* @return ページオブジェクト
*/
@Override
public LibraryItemPage getRecentDocument(int limit) {
return getRecentDocumentPage(limit);
}
/**
* 履歴ページを取得する
* @param limit 取得するアイテム数
* @param offset 取得するアイテムのオフセット
* @return ページオブジェクト
*/
@Override
public LibraryItemPage getRecentDocument(int limit, int offset) {
return getRecentDocumentPage(limit);
}
/**
* ライブラリページを取得する
* @param limit 取得するアイテム数
* @return ページオブジェクト
*/
@Override
public LibraryItemPage getLibraryPage(int limit) {
return generateStubLibraryItemPage(limit, null);
}
/**
* ライブラリページを取得する
* @param limit 取得するアイテム数
* @param offset 取得するアイテムのオフセット
* @return ページオブジェクト
*/
@Override
public LibraryItemPage getLibraryPage(int limit, int offset) {
return generateStubLibraryItemPage(limit, null);
}
/**
* ライブラリページを取得する
* @param pageId ページID
* @param limit 取得するアイテム数
* @return ページオブジェクト
*/
@Override
public LibraryItemPage getLibraryPage(String pageId, int limit) {
return generateStubLibraryItemPage(limit, pageId);
}
/**
* ライブラリページを取得する
* @param pageId ページID
* @param limit 取得するアイテム数
* @param offset 取得するアイテムのオフセット
* @return ページオブジェクト
*/
@Override
public LibraryItemPage getLibraryPage(String pageId, int limit, int offset) {
return generateStubLibraryItemPage(limit, pageId);
}
/**
* ドキュメント詳細を取得する
* @param id ドキュメントID
* @return ドキュメント詳細オブジェクト
*/
@Override
public DocumentDetail getDocumentDetailById(String id) throws IllegalArgumentException {
return generateStubDocumentDetail(id);
}
}

View File

@ -1,4 +1,21 @@
package one.nem.lacerta.data.module; package one.nem.lacerta.data.module;
public class LacertaLibraryModule { import dagger.Binds;
import dagger.Module;
import dagger.hilt.InstallIn;
import dagger.hilt.android.components.FragmentComponent;
import dagger.hilt.migration.DisableInstallInCheck;
import one.nem.lacerta.data.LacertaLibrary;
import one.nem.lacerta.data.impl.LacertaLibraryStubImpl;
import one.nem.lacerta.utils.LacertaLogger;
@Module
// Fragmentにinstall
@InstallIn(FragmentComponent.class)
abstract public class LacertaLibraryModule {
@Binds
public abstract LacertaLibrary bindLacertaLibrary(LacertaLibraryStubImpl impl);
} }

View File

@ -0,0 +1,115 @@
package one.nem.lacerta.feature.debug;
import android.os.Bundle;
import androidx.annotation.AnimatorRes;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import javax.inject.Inject;
import dagger.hilt.android.AndroidEntryPoint;
import one.nem.lacerta.data.LacertaLibrary;
import one.nem.lacerta.model.LibraryItemPage;
import one.nem.lacerta.model.document.DocumentDetail;
import one.nem.lacerta.model.ListItem;
import one.nem.lacerta.model.ListItemType;
/**
* A simple {@link Fragment} subclass.
* Use the {@link DebugMenuLibraryItemListPageFragment#newInstance} factory method to
* create an instance of this fragment.
*/
@AndroidEntryPoint
public class DebugMenuLibraryItemListPageFragment extends Fragment {
@Inject
LacertaLibrary lacertaLibrary;
public DebugMenuLibraryItemListPageFragment() {
// Required empty public constructor
}
public static DebugMenuLibraryItemListPageFragment newInstance() {
DebugMenuLibraryItemListPageFragment fragment = new DebugMenuLibraryItemListPageFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_debug_menu_library_item_list_page, container, false);
LibraryItemPage libraryItemPage = lacertaLibrary.getRecentDocument(10);
for (ListItem listItem : libraryItemPage.getListItems()) {
System.out.println(listItem.getTitle());
}
RecyclerView recyclerView = view.findViewById(R.id.item_recycler_view);
recyclerView.setAdapter(new ItemAdapter(libraryItemPage.getListItems()));
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
return view;
}
private class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ItemViewHolder> {
private ArrayList<ListItem> listItems;
public ItemAdapter(ArrayList<ListItem> listItems) {
this.listItems = listItems;
}
@Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_debug_menu_document, parent, false);
return new ItemViewHolder(view);
}
@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
ListItem listItem = listItems.get(position);
holder.document_list_item_title.setText(listItem.getTitle());
holder.document_list_item_description.setText(listItem.getDescription());
holder.document_list_item_updated_at.setText(listItem.getItemType().toString());
}
@Override
public int getItemCount() {
return listItems.size();
}
public class ItemViewHolder extends RecyclerView.ViewHolder {
TextView document_list_item_title;
TextView document_list_item_description;
TextView document_list_item_updated_at;
public ItemViewHolder(View itemView) {
super(itemView);
document_list_item_title = itemView.findViewById(R.id.document_list_item_title);
document_list_item_description = itemView.findViewById(R.id.document_list_item_description);
document_list_item_updated_at = itemView.findViewById(R.id.document_list_item_updated_at);
}
}
}
}

View File

@ -50,6 +50,7 @@ public class DebugMenuTopFragment extends Fragment {
debugMenuListItems.add(new DebugMenuListItem("Document Tester", "placeholder", R.id.action_debugMenuTopFragment_to_debugMenuDocumentTesterTopFragment, true)); debugMenuListItems.add(new DebugMenuListItem("Document Tester", "placeholder", R.id.action_debugMenuTopFragment_to_debugMenuDocumentTesterTopFragment, true));
debugMenuListItems.add(new DebugMenuListItem("Scanner", "placeholder", R.id.action_debugMenuTopFragment_to_scannerDataManagerStubFragment, true)); debugMenuListItems.add(new DebugMenuListItem("Scanner", "placeholder", R.id.action_debugMenuTopFragment_to_scannerDataManagerStubFragment, true));
debugMenuListItems.add(new DebugMenuListItem("Document List", "placeholder", R.id.action_debugMenuTopFragment_to_debugMenuLibraryItemListPageFragment, true));
DebugMenuListItemAdapter adapter = new DebugMenuListItemAdapter(debugMenuListItems); DebugMenuListItemAdapter adapter = new DebugMenuListItemAdapter(debugMenuListItems);
recyclerView.setAdapter(adapter); recyclerView.setAdapter(adapter);

View File

@ -0,0 +1,17 @@
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DebugMenuLibraryItemListPageFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/item_recycler_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -16,6 +16,9 @@
<action <action
android:id="@+id/action_debugMenuTopFragment_to_scannerDataManagerStubFragment" android:id="@+id/action_debugMenuTopFragment_to_scannerDataManagerStubFragment"
app:destination="@id/scannerDataManagerStubFragment" /> app:destination="@id/scannerDataManagerStubFragment" />
<action
android:id="@+id/action_debugMenuTopFragment_to_debugMenuLibraryItemListPageFragment"
app:destination="@id/debugMenuLibraryItemListPageFragment" />
</fragment> </fragment>
<fragment <fragment
android:id="@+id/debugMenuDocumentTesterTopFragment" android:id="@+id/debugMenuDocumentTesterTopFragment"
@ -43,4 +46,9 @@
android:id="@+id/scannerDataManagerStubFragment" android:id="@+id/scannerDataManagerStubFragment"
android:name="one.nem.lacerta.component.scanner.ScannerDataManagerStubFragment" android:name="one.nem.lacerta.component.scanner.ScannerDataManagerStubFragment"
android:label="ScannerDataManagerStubFragment" /> android:label="ScannerDataManagerStubFragment" />
<fragment
android:id="@+id/debugMenuLibraryItemListPageFragment"
android:name="one.nem.lacerta.feature.debug.DebugMenuLibraryItemListPageFragment"
android:label="fragment_debug_menu_library_item_list_page"
tools:layout="@layout/fragment_debug_menu_library_item_list_page" />
</navigation> </navigation>

View File

@ -8,12 +8,20 @@ public class LibraryItemPage {
String pageId; String pageId;
ArrayList<ListItem> listItems; ArrayList<ListItem> listItems;
// Constructor
public LibraryItemPage(String pageTitle, String pageId, ArrayList<ListItem> listItems) { public LibraryItemPage(String pageTitle, String pageId, ArrayList<ListItem> listItems) {
this.pageTitle = pageTitle; this.pageTitle = pageTitle;
this.pageId = pageId; this.pageId = pageId;
this.listItems = listItems; this.listItems = listItems;
} }
public LibraryItemPage() {
// Empty constructor
}
// Getter
public String getPageTitle() { public String getPageTitle() {
return pageTitle; return pageTitle;
} }
@ -25,4 +33,19 @@ public class LibraryItemPage {
public ArrayList<ListItem> getListItems() { public ArrayList<ListItem> getListItems() {
return listItems; return listItems;
} }
// Setter
public void setPageTitle(String pageTitle) {
this.pageTitle = pageTitle;
}
public void setPageId(String pageId) {
this.pageId = pageId;
}
public void setListItems(ArrayList<ListItem> listItems) {
this.listItems = listItems;
}
} }

View File

@ -20,6 +20,10 @@ public class ListItem {
this.itemId = itemId; this.itemId = itemId;
} }
public ListItem() {
// Empty constructor
}
// Getter // Getter
public String getTitle() { public String getTitle() {
@ -38,4 +42,22 @@ public class ListItem {
return itemId; return itemId;
} }
// Setter
public void setTitle(String title) {
this.title = title;
}
public void setDescription(String description) {
this.description = description;
}
public void setItemType(ListItemType itemType) {
this.itemType = itemType;
}
public void setItemId(String itemId) {
this.itemId = itemId;
}
} }