[bookmarks] Use list of categories ids in java/cpp

Signed-off-by: cyber-toad <the.cyber.toad@proton.me>
This commit is contained in:
cyber-toad 2024-01-22 01:22:53 +01:00 committed by Viktor Govako
parent 1c75b0dac2
commit a535f5256c
8 changed files with 55 additions and 37 deletions

View file

@ -188,20 +188,23 @@ void OnPreparedFileForSharing(JNIEnv * env, BookmarkManager::SharingResult const
static jclass const classBookmarkSharingResult = jni::GetGlobalClassRef(env,
"app/organicmaps/bookmarks/data/BookmarkSharingResult");
// Java signature : BookmarkSharingResult(long categoryId, @Code int code,
// Java signature : BookmarkSharingResult(long[] categoriesIds, @Code int code,
// @NonNull String sharingPath,
// @NonNull String errorString)
static jmethodID const ctorBookmarkSharingResult = jni::GetConstructorID(env,
classBookmarkSharingResult, "(JILjava/lang/String;Ljava/lang/String;)V");
classBookmarkSharingResult, "([JILjava/lang/String;Ljava/lang/String;)V");
jsize const size = static_cast<jsize>(result.m_categoriesIds.size());
jni::ScopedLocalRef<jlongArray> categoriesIds(env, env->NewLongArray(size));
std::vector<jlong> tmp(result.m_categoriesIds.cbegin(), result.m_categoriesIds.cend());
env->SetLongArrayRegion(categoriesIds.get(), 0, size, tmp.data());
jni::TScopedLocalRef const sharingPath(env, jni::ToJavaString(env, result.m_sharingPath));
jni::TScopedLocalRef const errorString(env, jni::ToJavaString(env, result.m_errorString));
jni::TScopedLocalRef const sharingResult(env, env->NewObject(classBookmarkSharingResult,
ctorBookmarkSharingResult, static_cast<jlong>(result.m_categoryId),
ctorBookmarkSharingResult, categoriesIds.get(),
static_cast<jint>(result.m_code), sharingPath.get(), errorString.get()));
env->CallVoidMethod(bookmarkManagerInstance, g_onPreparedFileForSharingMethod,
sharingResult.get());
env->CallVoidMethod(bookmarkManagerInstance, g_onPreparedFileForSharingMethod, sharingResult.get());
jni::HandleJavaException(env);
}
@ -592,10 +595,13 @@ Java_app_organicmaps_bookmarks_data_BookmarkManager_nativeSetAllCategoriesVisibi
}
JNIEXPORT void JNICALL
Java_app_organicmaps_bookmarks_data_BookmarkManager_nativePrepareFileForSharing(
JNIEnv * env, jclass, jlong catId)
Java_app_organicmaps_bookmarks_data_BookmarkManager_nativePrepareFileForSharing(JNIEnv * env, jclass, jlongArray catIds)
{
frm()->GetBookmarkManager().PrepareFileForSharing(static_cast<kml::MarkGroupId>(catId),
auto const size = env->GetArrayLength(catIds);
std::vector<jlong> tmp(size);
env->GetLongArrayRegion(catIds, 0, size, &tmp[0]);
kml::GroupIdCollection catIdsVector(tmp.cbegin(), tmp.cend());
frm()->GetBookmarkManager().PrepareFileForSharing(catIdsVector,
[env](BookmarkManager::SharingResult const & result)
{
OnPreparedFileForSharing(env, result);

View file

@ -14,6 +14,11 @@ import app.organicmaps.util.SharingUtils;
import app.organicmaps.util.log.Logger;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public enum BookmarksSharingHelper
{
INSTANCE;
@ -24,6 +29,12 @@ public enum BookmarksSharingHelper
private ProgressDialog mProgressDialog;
public void prepareBookmarkCategoryForSharing(@NonNull Activity context, long catId)
{
showProgressDialog(context);
BookmarkManager.INSTANCE.prepareCategoriesForSharing(new long[]{catId});
}
private void showProgressDialog(@NonNull Activity context)
{
mProgressDialog = new ProgressDialog(context, R.style.MwmTheme_ProgressDialog);
mProgressDialog.setMessage(context.getString(R.string.please_wait));
@ -31,7 +42,6 @@ public enum BookmarksSharingHelper
mProgressDialog.setIndeterminate(true);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
BookmarkManager.INSTANCE.prepareCategoryForSharing(catId);
}
public void onPreparedFileForSharing(@NonNull FragmentActivity context,
@ -57,8 +67,10 @@ public enum BookmarksSharingHelper
.setMessage(R.string.bookmarks_error_message_share_general)
.setPositiveButton(R.string.ok, null)
.show();
String catName = BookmarkManager.INSTANCE.getCategoryById(result.getCategoryId()).getName();
Logger.e(TAG, "Failed to share bookmark category '" + catName + "', error code: " + result.getCode());
List<String> names = new ArrayList<>();
for (long categoryId : result.getCategoriesIds())
names.add(BookmarkManager.INSTANCE.getCategoryById(categoryId).getName());
Logger.e(TAG, "Failed to share bookmark categories " + names + ", error code: " + result.getCode());
}
default -> throw new AssertionError("Unsupported bookmark sharing code: " + result.getCode());
}

View file

@ -559,9 +559,9 @@ public enum BookmarkManager
nativeSetChildCategoriesVisibility(catId, visible);
}
public void prepareCategoryForSharing(long catId)
public void prepareCategoriesForSharing(long[] catIds)
{
nativePrepareFileForSharing(catId);
nativePrepareFileForSharing(catIds);
}
public void setNotificationsEnabled(boolean enabled)
@ -799,7 +799,7 @@ public enum BookmarkManager
private static native void nativeSetAllCategoriesVisibility(boolean visible);
private static native void nativePrepareFileForSharing(long catId);
private static native void nativePrepareFileForSharing(long[] catIds);
private static native boolean nativeIsCategoryEmpty(long catId);

View file

@ -21,7 +21,7 @@ public class BookmarkSharingResult
public static final int ARCHIVE_ERROR = 2;
public static final int FILE_ERROR = 3;
private final long mCategoryId;
private final long[] mCategoriesIds;
@Code
private final int mCode;
@NonNull
@ -30,19 +30,19 @@ public class BookmarkSharingResult
@SuppressWarnings("unused")
private final String mErrorString;
private BookmarkSharingResult(long categoryId, @Code int code,
public BookmarkSharingResult(long[] categoriesIds, @Code int code,
@NonNull String sharingPath,
@NonNull String errorString)
{
mCategoryId = categoryId;
mCategoriesIds = categoriesIds;
mCode = code;
mSharingPath = sharingPath;
mErrorString = errorString;
}
public long getCategoryId()
public long[] getCategoriesIds()
{
return mCategoryId;
return mCategoriesIds;
}
public int getCode()

View file

@ -570,7 +570,7 @@ static BookmarkManager::SortingType convertSortingTypeToCore(MWMBookmarksSorting
- (void)shareCategory:(MWMMarkGroupID)groupId
{
self.bm.PrepareFileForSharing(groupId, [self](auto sharingResult)
self.bm.PrepareFileForSharing({groupId}, [self](auto sharingResult)
{
MWMBookmarksShareStatus status;
switch (sharingResult.m_code)

View file

@ -87,13 +87,13 @@ BookmarkManager::SharingResult GetFileForSharing(BookmarkManager::KMLDataCollect
auto const categoryId = kmlToShare.second->m_categoryData.m_id;
if (!SaveKmlFileSafe(*kmlToShare.second, filePath, KmlFileType::Text))
return {categoryId, BookmarkManager::SharingResult::Code::FileError, "Bookmarks file does not exist."};
return {{categoryId}, BookmarkManager::SharingResult::Code::FileError, "Bookmarks file does not exist."};
auto const tmpFilePath = base::JoinPath(GetPlatform().TmpDir(), fileName + std::string{kKmzExtension});
if (!CreateZipFromPathDeflatedAndDefaultCompression(filePath, tmpFilePath))
return {categoryId, BookmarkManager::SharingResult::Code::ArchiveError, "Could not create archive."};
return {{categoryId}, BookmarkManager::SharingResult::Code::ArchiveError, "Could not create archive."};
return {categoryId, tmpFilePath};
return {{categoryId}, tmpFilePath};
}
std::string ToString(BookmarkManager::SortingType type)
@ -2596,20 +2596,20 @@ void BookmarkManager::SaveBookmarks(kml::GroupIdCollection const & groupIdCollec
});
}
void BookmarkManager::PrepareFileForSharing(kml::MarkGroupId categoryId, SharingHandler && handler)
void BookmarkManager::PrepareFileForSharing(kml::GroupIdCollection const & categoriesIds, SharingHandler && handler)
{
CHECK_THREAD_CHECKER(m_threadChecker, ());
ASSERT(handler, ());
if (IsCategoryEmpty(categoryId))
if (categoriesIds.size() == 1 && IsCategoryEmpty(categoriesIds.front()))
{
handler(SharingResult(categoryId, SharingResult::Code::EmptyCategory));
handler(SharingResult(categoriesIds, SharingResult::Code::EmptyCategory));
return;
}
auto collection = PrepareToSaveBookmarks({categoryId});
auto collection = PrepareToSaveBookmarks(categoriesIds);
if (!collection || collection->empty())
{
handler(SharingResult(categoryId, SharingResult::Code::FileError));
handler(SharingResult(categoriesIds, SharingResult::Code::FileError));
return;
}

View file

@ -316,31 +316,31 @@ public:
FileError
};
SharingResult(kml::MarkGroupId categoryId, std::string const & sharingPath)
: m_categoryId(categoryId)
SharingResult(kml::GroupIdCollection const & categoriesIds, std::string const & sharingPath)
: m_categoriesIds(categoriesIds)
, m_code(Code::Success)
, m_sharingPath(sharingPath)
{}
SharingResult(kml::MarkGroupId categoryId, Code code)
: m_categoryId(categoryId)
SharingResult(kml::GroupIdCollection const & categoriesIds, Code code)
: m_categoriesIds(categoriesIds)
, m_code(code)
{}
SharingResult(kml::MarkGroupId categoryId, Code code, std::string const & errorString)
: m_categoryId(categoryId)
SharingResult(kml::GroupIdCollection const & categoriesIds, Code code, std::string const & errorString)
: m_categoriesIds(categoriesIds)
, m_code(code)
, m_errorString(errorString)
{}
kml::MarkGroupId m_categoryId;
kml::MarkIdCollection m_categoriesIds;
Code m_code;
std::string m_sharingPath;
std::string m_errorString;
};
using SharingHandler = platform::SafeCallback<void(SharingResult const & result)>;
void PrepareFileForSharing(kml::MarkGroupId categoryId, SharingHandler && handler);
void PrepareFileForSharing(kml::GroupIdCollection const & categoriesIds, SharingHandler && handler);
bool IsCategoryEmpty(kml::MarkGroupId categoryId) const;

View file

@ -170,7 +170,7 @@ void BookmarkDialog::OnExportClick()
if (name.isEmpty())
return;
m_framework.GetBookmarkManager().PrepareFileForSharing(categoryIt->second,
m_framework.GetBookmarkManager().PrepareFileForSharing({categoryIt->second},
[this, name](BookmarkManager::SharingResult const & result)
{
if (result.m_code == BookmarkManager::SharingResult::Code::Success)