This commit is contained in:
r-ca 2024-01-23 00:36:26 +09:00
parent 37a18704e3
commit 1105c17fca
No known key found for this signature in database
GPG Key ID: 6A72911AC73464A9
3 changed files with 91 additions and 8 deletions

View File

@ -5,6 +5,7 @@ import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.navigation.NavController;
import androidx.navigation.fragment.NavHostFragment;
import androidx.navigation.ui.NavigationUI;
@ -110,4 +111,47 @@ public class MainActivity extends AppCompatActivity implements FragmentNavigatio
.commit();
}
}
@Override
public void navigateToFragment(Fragment fragment, boolean addToBackStack, boolean clearBackStack) {
if (clearBackStack) {
getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
if (addToBackStack) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.nav_host_fragment, fragment)
.addToBackStack(null)
.commit();
} else {
getSupportFragmentManager().beginTransaction()
.replace(R.id.nav_host_fragment, fragment)
.commit();
}
}
public void navigateToFragmentAlternate(Fragment fragment, boolean addToBackStack) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// get the current fragment
Fragment currentFragment = getSupportFragmentManager().getPrimaryNavigationFragment();
// hide the current fragment
if (currentFragment != null) {
transaction.hide(currentFragment);
}
// Add the new fragment
transaction.add(R.id.nav_host_fragment, fragment);
// Add the transaction to the back stack if needed
if (addToBackStack) {
transaction.addToBackStack(null);
}
// Commit the transaction
transaction.commit();
// Update the primary navigation fragment
getSupportFragmentManager().beginTransaction().setPrimaryNavigationFragment(fragment).commit();
}
}

View File

@ -81,6 +81,16 @@ public class LibraryPageFragment extends Fragment {
return fragment;
}
public static LibraryPageFragment newInstance(String folderId) { // Back action
LibraryPageFragment fragment = new LibraryPageFragment();
Bundle args = new Bundle();
args.putString("folderId", folderId);
args.putString("title", null);
args.putString("publicPath", null);
fragment.setArguments(args);
return fragment;
}
public static LibraryPageFragment newInstance() {
LibraryPageFragment fragment = new LibraryPageFragment();
Bundle args = new Bundle();
@ -127,11 +137,13 @@ public class LibraryPageFragment extends Fragment {
super.onViewCreated(view, savedInstanceState);
if (getArguments() != null) {
this.folderId = getArguments().getString("folderId");
this.title = getArguments().getString("title");
this.parentId = getArguments().getString("parentId");
// Log
logger.debug("LibraryTopFragment", "Args: folderId: " + folderId + ", title: " + title + ", parentId: " + parentId);
this.folderId = getArguments().getString("folderId"); // Required
if (getArguments().getString("title") == null && getArguments().getString("publicPath") == null) {
this.libraryItemPage = new LibraryItemPage();
} else {
this.title = getArguments().getString("title");
this.parentId = getArguments().getString("publicPath");
}
} else {
logger.debug("LibraryTopFragment", "getArguments() is null(maybe root)");
this.libraryItemPage = new LibraryItemPage();
@ -150,7 +162,7 @@ public class LibraryPageFragment extends Fragment {
// 画面遷移
FragmentNavigation fragmentNavigation = (FragmentNavigation) getActivity();
// folderId: 推移先で表示するフォルダのID, folderName: 推移先で表示するフォルダの名前, parentId: このフラグメントで表示しているフォルダのID(推移先の親)
fragmentNavigation.navigateToFragment(LibraryPageFragment.newInstance(folderId, folderName, libraryItemPage != null ? libraryItemPage.getParentId() : null));
fragmentNavigation.navigateToFragment(LibraryPageFragment.newInstance(folderId, folderName, libraryItemPage != null ? libraryItemPage.getParentId() : null), false);
}
@Override
@ -165,6 +177,16 @@ public class LibraryPageFragment extends Fragment {
// Get library page and update RecyclerView items
lacertaLibrary.getLibraryPage(this.folderId, 10).thenAccept(libraryItemPage -> {
this.libraryItemPage = libraryItemPage;
if (this.parentId == null) {
this.parentId = libraryItemPage.getParentId();
}
if (this.title == null) {
this.title = libraryItemPage.getPageTitle();
// Toolbar init again
toolbarSetup(view.findViewById(R.id.library_toolbar), this.folderId != null, this.title != null ? this.title : "ライブラリ");
}
logger.debug("LibraryTopFragment", "Item selected! Total item page: " + this.libraryItemPage.getListItems().size());
getActivity().runOnUiThread(() -> { // TODO-rca: 実行条件を考える
listItemAdapter.notifyItemRangeRemoved(0, this.libraryItemPage.getListItems().size() - 1);
@ -228,8 +250,8 @@ public class LibraryPageFragment extends Fragment {
if (showBackButton) {
toolbar.setNavigationIcon(one.nem.lacerta.shared.ui.R.drawable.arrow_back_24px);
toolbar.setNavigationOnClickListener(v -> {
this.libraryItemPage = lacertaLibrary.getLibraryPage(this.libraryItemPage.getParentId(), 10).join();
getParentFragmentManager().popBackStack();
//this.libraryItemPage = lacertaLibrary.getLibraryPage(this.libraryItemPage.getParentId(), 10).join();
processBack();
});
} else {
toolbar.setNavigationIcon(null);
@ -247,4 +269,17 @@ public class LibraryPageFragment extends Fragment {
});
}
/**
* Backボタンが押された時の処理
*/
private void processBack() {
if (this.parentId != null) {
FragmentNavigation fragmentNavigation = (FragmentNavigation) getActivity();
fragmentNavigation.navigateToFragment(LibraryPageFragment.newInstance(this.parentId));
} else { // Root
FragmentNavigation fragmentNavigation = (FragmentNavigation) getActivity();
fragmentNavigation.navigateToFragment(LibraryPageFragment.newInstance());
}
}
}

View File

@ -6,4 +6,8 @@ public interface FragmentNavigation {
void navigateToFragment(Fragment fragment);
void navigateToFragment(Fragment fragment, boolean addToBackStack);
void navigateToFragment(Fragment fragment, boolean addToBackStack, boolean clearBackStack);
void navigateToFragmentAlternate(Fragment fragment, boolean addToBackStack);
}