diff --git a/component/scanner/src/main/java/one/nem/lacerta/component/scanner/ScannerManagerActivity.java b/component/scanner/src/main/java/one/nem/lacerta/component/scanner/ScannerManagerActivity.java index f55484a1..3603ff12 100644 --- a/component/scanner/src/main/java/one/nem/lacerta/component/scanner/ScannerManagerActivity.java +++ b/component/scanner/src/main/java/one/nem/lacerta/component/scanner/ScannerManagerActivity.java @@ -1,5 +1,6 @@ package one.nem.lacerta.component.scanner; +import android.app.ProgressDialog; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.BitmapDrawable; @@ -22,6 +23,7 @@ import com.websitebeaver.documentscanner.DocumentScanner; import java.util.ArrayList; import java.util.Objects; +import java.util.concurrent.CompletableFuture; import javax.inject.Inject; @@ -152,21 +154,34 @@ public class ScannerManagerActivity extends AppCompatActivity { private void saveNewDocument() { logger.debug(TAG, "saveNewDocument"); + // Deprecatedだが、中断機能が存在しないので操作をブロックする目的で(意図的に)使用 + ProgressDialog dialog = new ProgressDialog(this); + dialog.setMessage("保存中..."); // TODO-rca: テキストをリソースに移動 + dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); + dialog.setCancelable(false); + dialog.show(); DocumentMeta documentMeta = new DocumentMeta("Untitled"); // TODO-rca: デフォルトタイトルを指定できるようにする - DocumentDetail documentDetail = document.createDocument(documentMeta); - Bitmap[] bitmaps = new Bitmap[this.croppedImages.size()]; - this.croppedImages.toArray(bitmaps); - try { - documentProcessorFactory.create(documentDetail).addNewPagesToLast(bitmaps); - Toast.makeText(this, "Saved.", Toast.LENGTH_SHORT).show(); - lacertaVcsFactory.create(documentDetail.getMeta().getId()).generateRevisionAtCurrent("Initial commit"); - lacertaVcsFactory.create(documentDetail.getMeta().getId()).printLog(); // Debug - lacertaVcsFactory.create(documentDetail.getMeta().getId()).printRev(); // Debug - finish(); - } catch (Exception e) { - logger.error(TAG, "Error: " + e.getMessage()); - logger.e_code("9dff2a28-20e8-4ccd-9d04-f0c7646faa6a"); - } + document.createDocument(documentMeta).thenAccept((documentDetail1) -> { + Bitmap[] bitmaps = new Bitmap[this.croppedImages.size()]; + this.croppedImages.toArray(bitmaps); + addPagesToDocumentDetail(documentDetail1, bitmaps).thenRun(() -> { + dialog.dismiss(); + finish(); + }); + }); + + } + + private CompletableFuture addPagesToDocumentDetail(DocumentDetail documentDetail, Bitmap[] bitmaps) { + return CompletableFuture.runAsync(() -> { + try { + documentProcessorFactory.create(documentDetail).addNewPagesToLast(bitmaps); + lacertaVcsFactory.create(documentDetail.getMeta().getId()).generateRevisionAtCurrent("Initial commit"); + } catch (Exception e) { + logger.error(TAG, "Error: " + e.getMessage()); + logger.e_code("9dff2a28-20e8-4ccd-9d04-f0c7646faa6a"); + } + }); } private void insertToExistDocument() { diff --git a/data/src/main/java/one/nem/lacerta/data/Document.java b/data/src/main/java/one/nem/lacerta/data/Document.java index 145a5905..0904c3a2 100644 --- a/data/src/main/java/one/nem/lacerta/data/Document.java +++ b/data/src/main/java/one/nem/lacerta/data/Document.java @@ -2,6 +2,7 @@ package one.nem.lacerta.data; import java.lang.reflect.Array; import java.util.ArrayList; +import java.util.concurrent.CompletableFuture; import one.nem.lacerta.model.document.DocumentMeta; import one.nem.lacerta.model.document.DocumentDetail; @@ -13,13 +14,13 @@ import one.nem.lacerta.model.document.tag.DocumentTag; */ public interface Document { - DocumentDetail createDocument(DocumentMeta meta); + CompletableFuture createDocument(DocumentMeta meta); - DocumentDetail createDocument(); + CompletableFuture createDocument(); - void deleteDocument(String documentId); + CompletableFuture deleteDocument(String documentId); - void updateDocument(DocumentMeta meta, DocumentDetail detail); + CompletableFuture updateDocument(DocumentMeta meta, DocumentDetail detail); - DocumentDetail getDocument(String documentId); + CompletableFuture getDocument(String documentId); } diff --git a/data/src/main/java/one/nem/lacerta/data/LacertaLibrary.java b/data/src/main/java/one/nem/lacerta/data/LacertaLibrary.java index eeb8546b..cb19b425 100644 --- a/data/src/main/java/one/nem/lacerta/data/LacertaLibrary.java +++ b/data/src/main/java/one/nem/lacerta/data/LacertaLibrary.java @@ -1,6 +1,7 @@ package one.nem.lacerta.data; import java.util.ArrayList; +import java.util.concurrent.CompletableFuture; import one.nem.lacerta.model.ListItem; import one.nem.lacerta.model.ListItemType; @@ -11,15 +12,13 @@ import one.nem.lacerta.model.document.DocumentDetail; public interface LacertaLibrary { // Get History - ArrayList getRecentDocument(int limit); - ArrayList getRecentDocument(int limit, int offset); + CompletableFuture> getRecentDocument(int limit); + CompletableFuture> getRecentDocument(int limit, int offset); // Get Library page - LibraryItemPage getLibraryPage(int limit); - LibraryItemPage getLibraryPage(int limit, int offset); - LibraryItemPage getLibraryPage(String pageId, int limit); - LibraryItemPage getLibraryPage(String pageId, int limit, int offset); + CompletableFuture getLibraryPage(int limit); + CompletableFuture getLibraryPage(int limit, int offset); + CompletableFuture getLibraryPage(String pageId, int limit); + CompletableFuture getLibraryPage(String pageId, int limit, int offset); - // GetDocument - DocumentDetail getDocumentDetailById(String id); // TODO-rca: Documentに統合する } diff --git a/data/src/main/java/one/nem/lacerta/data/impl/DocumentImpl.java b/data/src/main/java/one/nem/lacerta/data/impl/DocumentImpl.java index 1253d0a4..0fec6757 100644 --- a/data/src/main/java/one/nem/lacerta/data/impl/DocumentImpl.java +++ b/data/src/main/java/one/nem/lacerta/data/impl/DocumentImpl.java @@ -1,8 +1,10 @@ package one.nem.lacerta.data.impl; +import java.io.IOException; import java.util.ArrayList; import java.util.Date; import java.util.UUID; +import java.util.concurrent.CompletableFuture; // Hilt import javax.inject.Inject; @@ -16,13 +18,19 @@ import one.nem.lacerta.model.document.DocumentMeta; import one.nem.lacerta.model.document.DocumentDetail; // Lacerta/source +import one.nem.lacerta.model.document.internal.XmlMetaPageModel; +import one.nem.lacerta.model.document.page.Page; import one.nem.lacerta.source.database.LacertaDatabase; import one.nem.lacerta.source.database.entity.DocumentEntity; // Lacerta/utils +import one.nem.lacerta.source.file.FileManager; +import one.nem.lacerta.source.file.factory.FileManagerFactory; import one.nem.lacerta.utils.LacertaLogger; // Lacerta/vcs +import one.nem.lacerta.utils.XmlMetaParser; +import one.nem.lacerta.utils.repository.DeviceInfoUtils; import one.nem.lacerta.vcs.LacertaVcs; import one.nem.lacerta.vcs.factory.LacertaVcsFactory; @@ -40,6 +48,14 @@ public class DocumentImpl implements Document { @Inject LacertaVcsFactory vcsFactory; + @Inject + FileManagerFactory fileManagerFactory; + + @Inject + DeviceInfoUtils deviceInfoUtils; + + @Inject + XmlMetaParser xmlMetaParser; @Inject public DocumentImpl() { @@ -49,35 +65,34 @@ public class DocumentImpl implements Document { @Override - public DocumentDetail createDocument(DocumentMeta meta) { - DocumentDetail detail = new DocumentDetail(); - detail.setMeta(meta); - detail.setPages(new ArrayList<>()); + public CompletableFuture createDocument(DocumentMeta meta) { + return CompletableFuture.supplyAsync(() -> { + DocumentDetail detail = new DocumentDetail(); + detail.setMeta(meta); + detail.setPages(new ArrayList<>()); + // Create DocumentEntity + DocumentEntity documentEntity = new DocumentEntity(); + documentEntity.id = meta.getId(); + documentEntity.title = meta.getTitle(); + documentEntity.author = meta.getAuthor(); + documentEntity.defaultBranch = meta.getDefaultBranch(); + documentEntity.updatedAt = meta.getUpdatedAt(); + documentEntity.createdAt = meta.getCreatedAt(); + documentEntity.publicPath = meta.getPath().getStringPath(); + documentEntity.tagIds = meta.getTagIds(); - // TODO-rca: UIスレッドから追い出す + database.documentDao().insert(documentEntity); - // Create DocumentEntity - DocumentEntity documentEntity = new DocumentEntity(); - documentEntity.id = meta.getId(); - documentEntity.title = meta.getTitle(); - documentEntity.author = meta.getAuthor(); - documentEntity.defaultBranch = meta.getDefaultBranch(); - documentEntity.updatedAt = meta.getUpdatedAt(); - documentEntity.createdAt = meta.getCreatedAt(); - documentEntity.publicPath = meta.getPath().getStringPath(); - documentEntity.tagIds = meta.getTagIds(); + // Vcs + LacertaVcs vcs = vcsFactory.create(meta.getId()); + vcs.createDocument(meta.getId()); - database.documentDao().insert(documentEntity); - - // Vcs - LacertaVcs vcs = vcsFactory.create(meta.getId()); - vcs.createDocument(meta.getId()); - - return detail; + return detail; + }); } @Override - public DocumentDetail createDocument() { + public CompletableFuture createDocument() { DocumentMeta meta = new DocumentMeta(); meta.setId(UUID.randomUUID().toString()); meta.setTitle("New Document"); @@ -91,17 +106,83 @@ public class DocumentImpl implements Document { } @Override - public void deleteDocument(String documentId) { - + public CompletableFuture deleteDocument(String documentId) { + return CompletableFuture.supplyAsync(() -> { + return null; + }); } @Override - public void updateDocument(DocumentMeta meta, DocumentDetail detail) { - + public CompletableFuture updateDocument(DocumentMeta meta, DocumentDetail detail) { + return CompletableFuture.supplyAsync(() -> { + return null; + }); } @Override - public DocumentDetail getDocument(String documentId) { - return null; + public CompletableFuture getDocument(String documentId) { + return CompletableFuture.supplyAsync(() -> { + DocumentEntity documentEntity = database.documentDao().findById(documentId); + if (documentEntity == null) { + throw new IllegalArgumentException("documentId is not found"); + } + DocumentMeta meta = new DocumentMeta(); + meta.setId(documentEntity.id); + meta.setTitle(documentEntity.title); + meta.setAuthor(documentEntity.author); + meta.setDefaultBranch(documentEntity.defaultBranch); + meta.setUpdatedAt(documentEntity.updatedAt); + meta.setCreatedAt(documentEntity.createdAt); + meta.setPath(new PublicPath().resolve(documentEntity.publicPath)); + meta.setTags(new ArrayList<>()); // TODO-rca: タグを取得する + + DocumentDetail detail = new DocumentDetail(); + + getPagesByXmlMeta(documentId).thenCompose(xmlMetaPageModels -> getPagesByXmlMetaPageModel(documentId, xmlMetaPageModels)).thenAccept(pages -> { + detail.setMeta(meta); + detail.setPages(pages); + }); + return detail; + }); + } + + private CompletableFuture> getPagesByXmlMeta(String documentId) { + return CompletableFuture.supplyAsync(() -> { + FileManager fileManager = fileManagerFactory.create(deviceInfoUtils.getExternalStorageDirectory()); + try { + return xmlMetaParser.deserialize(fileManager.resolve(documentId).loadXml("meta.xml")).getPages(); + } catch (IOException e) { + logger.error(TAG, "DocumentMeta parse error"); + logger.trace(TAG, e.getMessage()); + logger.e_code("db18c04a-1625-4871-9e4e-918d805568ac"); + } + return null; + }); + } + + private CompletableFuture> getPagesByXmlMetaPageModel(String documentId, ArrayList xmlMetaPageModels) { + return CompletableFuture.supplyAsync(() -> { + ArrayList pages = new ArrayList<>(); + FileManager fileManager; + try { + fileManager = fileManagerFactory.create(deviceInfoUtils.getExternalStorageDirectory()).resolve(documentId); + } catch (IOException e) { + logger.error(TAG, "FileManager resolve error"); + logger.trace(TAG, e.getMessage()); + logger.e_code("2235e8ee-4177-46f8-af6b-084381b202e6"); + return null; + } + for (XmlMetaPageModel xmlMetaPageModel : xmlMetaPageModels) { + try { + pages.add(new Page(xmlMetaPageModel.getFilename(), fileManager.loadBitmap(xmlMetaPageModel.getFilename()))); + } catch (IOException e) { + logger.error(TAG, "Bitmap decode error"); + logger.trace(TAG, e.getMessage()); + logger.e_code("3bba8c8f-90fb-4a24-a726-7ea201929f8b"); + return null; + } + } + return pages; + }); } } diff --git a/data/src/main/java/one/nem/lacerta/data/impl/LacertaLibraryImpl.java b/data/src/main/java/one/nem/lacerta/data/impl/LacertaLibraryImpl.java index 01a7209f..e9ecee78 100644 --- a/data/src/main/java/one/nem/lacerta/data/impl/LacertaLibraryImpl.java +++ b/data/src/main/java/one/nem/lacerta/data/impl/LacertaLibraryImpl.java @@ -2,7 +2,9 @@ package one.nem.lacerta.data.impl; import java.text.DateFormat; import java.util.ArrayList; +import java.util.Collection; import java.util.List; +import java.util.concurrent.CompletableFuture; import javax.inject.Inject; @@ -18,55 +20,71 @@ import one.nem.lacerta.utils.LacertaLogger; public class LacertaLibraryImpl implements LacertaLibrary { - @Inject LacertaLogger logger; - - @Inject LacertaDatabase database; - @Override - public ArrayList getRecentDocument(int limit) { - List documentEntities = database.documentDao().getRecentDocument(limit); - - ArrayList listItems = new ArrayList<>(); - for (DocumentEntity documentEntity : documentEntities) { - ListItem listItem = new ListItem(); - listItem.setItemType(ListItemType.ITEM_TYPE_DOCUMENT); - listItem.setTitle(documentEntity.title); - listItem.setDescription(DateFormat.getDateInstance().format(documentEntity.updatedAt)); - listItem.setItemId(documentEntity.id); - listItems.add(listItem); - } - return listItems; + @Inject + public LacertaLibraryImpl(LacertaLogger logger, LacertaDatabase database) { + this.logger = logger; + this.database = database; } @Override - public ArrayList getRecentDocument(int limit, int offset) { + public CompletableFuture> getRecentDocument(int limit) { + return CompletableFuture.supplyAsync(() -> { + // 5秒フリーズさせる + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + List documentEntities = database.documentDao().getRecentDocument(limit); + + ArrayList listItems = new ArrayList<>(); + for (DocumentEntity documentEntity : documentEntities) { + ListItem listItem = new ListItem(); + listItem.setItemType(ListItemType.ITEM_TYPE_DOCUMENT); + listItem.setTitle(documentEntity.title); + listItem.setDescription(DateFormat.getDateInstance().format(documentEntity.updatedAt)); + listItem.setItemId(documentEntity.id); + listItems.add(listItem); + } + + return listItems; + }); + } + + @Override + public CompletableFuture> getRecentDocument(int limit, int offset) { return null; // TODO-rca: Implement } @Override - public LibraryItemPage getLibraryPage(int limit) { - return null; + public CompletableFuture getLibraryPage(int limit) { + return CompletableFuture.supplyAsync(() -> { + return null; + }); } @Override - public LibraryItemPage getLibraryPage(int limit, int offset) { - return null; + public CompletableFuture getLibraryPage(int limit, int offset) { + return CompletableFuture.supplyAsync(() -> { + return null; + }); } @Override - public LibraryItemPage getLibraryPage(String pageId, int limit) { - return null; + public CompletableFuture getLibraryPage(String pageId, int limit) { + return CompletableFuture.supplyAsync(() -> { + return null; + }); } @Override - public LibraryItemPage getLibraryPage(String pageId, int limit, int offset) { - return null; - } - - @Override - public DocumentDetail getDocumentDetailById(String id) { - return null; + public CompletableFuture getLibraryPage(String pageId, int limit, int offset) { + return CompletableFuture.supplyAsync(() -> { + return null; + }); } } diff --git a/data/src/main/java/one/nem/lacerta/data/impl/LacertaLibraryStubImpl.java b/data/src/main/java/one/nem/lacerta/data/impl/LacertaLibraryStubImpl.java index df5aec9a..bebea642 100644 --- a/data/src/main/java/one/nem/lacerta/data/impl/LacertaLibraryStubImpl.java +++ b/data/src/main/java/one/nem/lacerta/data/impl/LacertaLibraryStubImpl.java @@ -1,219 +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 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 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 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 ArrayList getRecentDocument(int limit) { - return getRecentDocumentPage(limit).getListItems(); - } - - /** - * 履歴ページを取得する - * @param limit 取得するアイテム数 - * @param offset 取得するアイテムのオフセット - * @return ページオブジェクト - */ - @Override - public ArrayList getRecentDocument(int limit, int offset) { - return getRecentDocumentPage(limit).getListItems(); - } - - /** - * ライブラリページを取得する - * @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); - } -} +//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 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 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 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 ArrayList getRecentDocument(int limit) { +// return getRecentDocumentPage(limit).getListItems(); +// } +// +// /** +// * 履歴ページを取得する +// * @param limit 取得するアイテム数 +// * @param offset 取得するアイテムのオフセット +// * @return ページオブジェクト +// */ +// @Override +// public ArrayList getRecentDocument(int limit, int offset) { +// return getRecentDocumentPage(limit).getListItems(); +// } +// +// /** +// * ライブラリページを取得する +// * @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); +// } +//} diff --git a/data/src/main/java/one/nem/lacerta/data/impl/LacertaSearchStubImpl.java b/data/src/main/java/one/nem/lacerta/data/impl/LacertaSearchStubImpl.java index b6076cfd..e0ba38a7 100644 --- a/data/src/main/java/one/nem/lacerta/data/impl/LacertaSearchStubImpl.java +++ b/data/src/main/java/one/nem/lacerta/data/impl/LacertaSearchStubImpl.java @@ -30,9 +30,10 @@ public class LacertaSearchStubImpl implements LacertaSearch { */ @Override public ArrayList autoSearch(String query, int limit) { - logger.debug("SearchStub", "autoSearch"); - logger.debug("SearchStub", "query: " + query); - return library.getLibraryPage(limit).getListItems(); +// logger.debug("SearchStub", "autoSearch"); +// logger.debug("SearchStub", "query: " + query); +// return library.getLibraryPage(limit).getListItems(); + return null; } /** @@ -44,8 +45,9 @@ public class LacertaSearchStubImpl implements LacertaSearch { */ @Override public ArrayList autoSearch(String query, int limit, int offset) { - logger.debug("SearchStub", "autoSearch"); - logger.debug("SearchStub", "query: " + query); - return library.getLibraryPage(limit, offset).getListItems(); +// logger.debug("SearchStub", "autoSearch"); +// logger.debug("SearchStub", "query: " + query); +// return library.getLibraryPage(limit, offset).getListItems(); + return null; } } diff --git a/data/src/main/java/one/nem/lacerta/data/module/LacertaLibraryModule.java b/data/src/main/java/one/nem/lacerta/data/module/LacertaLibraryModule.java index 6c98e653..abf17b27 100644 --- a/data/src/main/java/one/nem/lacerta/data/module/LacertaLibraryModule.java +++ b/data/src/main/java/one/nem/lacerta/data/module/LacertaLibraryModule.java @@ -7,7 +7,7 @@ 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.data.impl.LacertaLibraryImpl; import one.nem.lacerta.utils.LacertaLogger; @Module @@ -16,6 +16,6 @@ import one.nem.lacerta.utils.LacertaLogger; abstract public class LacertaLibraryModule { @Binds - public abstract LacertaLibrary bindLacertaLibrary(LacertaLibraryStubImpl impl); + public abstract LacertaLibrary bindLacertaLibrary(LacertaLibraryImpl impl); } diff --git a/feature/debug/src/main/java/one/nem/lacerta/feature/debug/DebugMenuLibraryItemListPageFragment.java b/feature/debug/src/main/java/one/nem/lacerta/feature/debug/DebugMenuLibraryItemListPageFragment.java index 35b9d263..cb5ae57e 100644 --- a/feature/debug/src/main/java/one/nem/lacerta/feature/debug/DebugMenuLibraryItemListPageFragment.java +++ b/feature/debug/src/main/java/one/nem/lacerta/feature/debug/DebugMenuLibraryItemListPageFragment.java @@ -57,7 +57,8 @@ public class DebugMenuLibraryItemListPageFragment extends Fragment { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_debug_menu_library_item_list_page, container, false); - ArrayList listItems = lacertaLibrary.getRecentDocument(10); +// ArrayList listItems = lacertaLibrary.getRecentDocument(10); + ArrayList listItems = new ArrayList<>(); for (ListItem listItem : listItems) { System.out.println(listItem.getTitle()); } diff --git a/feature/home/src/main/java/one/nem/lacerta/feature/home/HomeTopFragment.java b/feature/home/src/main/java/one/nem/lacerta/feature/home/HomeTopFragment.java index 46a452d5..1510b2ac 100644 --- a/feature/home/src/main/java/one/nem/lacerta/feature/home/HomeTopFragment.java +++ b/feature/home/src/main/java/one/nem/lacerta/feature/home/HomeTopFragment.java @@ -23,6 +23,7 @@ import com.google.android.material.appbar.CollapsingToolbarLayout; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.CompletableFuture; import javax.inject.Inject; @@ -67,18 +68,6 @@ public class HomeTopFragment extends Fragment { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_home_top, container, false); - ArrayList recentDocument = lacertaLibrary.getRecentDocument(10); - - Log.d("docs", Integer.toString(recentDocument.size())); - - RecyclerView recyclerView = view.findViewById(R.id.home_item_recycler_view); - - recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); - - ListItemAdapter listItemAdapter = new ListItemAdapter(recentDocument); - - recyclerView.setAdapter(listItemAdapter); - return view; } @@ -86,6 +75,20 @@ public class HomeTopFragment extends Fragment { public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); + RecyclerView recyclerView = view.findViewById(R.id.home_item_recycler_view); + + + ListItemAdapter listItemAdapter = new ListItemAdapter(); + recyclerView.setAdapter(listItemAdapter); + recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); + + lacertaLibrary.getRecentDocument(10).thenAccept(listItems -> { + listItemAdapter.setListItems(listItems); + getActivity().runOnUiThread(() -> { + listItemAdapter.notifyItemRangeInserted(0, listItems.size()); + }); + }); + CollapsingToolbarLayout collapsingToolbarLayout = view.findViewById(R.id.collapsing_toolbar); Toolbar toolbar = view.findViewById(R.id.toolbar); diff --git a/feature/home/src/main/java/one/nem/lacerta/feature/home/ListItemAdapter.java b/feature/home/src/main/java/one/nem/lacerta/feature/home/ListItemAdapter.java index 9d2fef13..2ffa70e9 100644 --- a/feature/home/src/main/java/one/nem/lacerta/feature/home/ListItemAdapter.java +++ b/feature/home/src/main/java/one/nem/lacerta/feature/home/ListItemAdapter.java @@ -23,6 +23,14 @@ public class ListItemAdapter extends RecyclerView.Adapter(); + } + + public void setListItems(ArrayList listItems) { + this.listItems = listItems; + } + @NonNull @Override public ListItemAdapter.ListItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {