[android][bookmarks] Add sorting bookmarks and tracks by name

Signed-off-by: Simon Klüber <48260138+sklueber@users.noreply.github.com>
This commit is contained in:
Simon Klüber 2024-01-13 17:04:26 -06:00 committed by Roman Tsisyk
parent e40bbcf554
commit cd5923ed69
5 changed files with 78 additions and 4 deletions

View file

@ -74,6 +74,8 @@ public class ChooseBookmarksSortingTypeFragment extends BaseMwmDialogFragment
return R.id.sort_by_distance;
case BookmarkManager.SORT_BY_TIME:
return R.id.sort_by_time;
case BookmarkManager.SORT_BY_NAME:
return R.id.sort_by_name;
}
}
return R.id.sort_by_default;
@ -88,7 +90,7 @@ public class ChooseBookmarksSortingTypeFragment extends BaseMwmDialogFragment
if (args == null)
throw new AssertionError("Arguments of choose sorting type view can't be null.");
UiUtils.hide(view, R.id.sort_by_type, R.id.sort_by_distance, R.id.sort_by_time);
UiUtils.hide(view, R.id.sort_by_type, R.id.sort_by_distance, R.id.sort_by_time, R.id.sort_by_name);
@BookmarkManager.SortingType
int[] availableSortingTypes = args.getIntArray(EXTRA_SORTING_TYPES);
@ -151,5 +153,7 @@ public class ChooseBookmarksSortingTypeFragment extends BaseMwmDialogFragment
setSortingType(BookmarkManager.SORT_BY_DISTANCE);
else if (id == R.id.sort_by_time)
setSortingType(BookmarkManager.SORT_BY_TIME);
else if (id == R.id.sort_by_name)
setSortingType(BookmarkManager.SORT_BY_NAME);
}
}

View file

@ -63,12 +63,13 @@ public enum BookmarkManager
public static final int CLOUD_NOT_ENOUGH_DISK_SPACE = 2;
@Retention(RetentionPolicy.SOURCE)
@IntDef({ SORT_BY_TYPE, SORT_BY_DISTANCE, SORT_BY_TIME })
@IntDef({ SORT_BY_TYPE, SORT_BY_DISTANCE, SORT_BY_TIME, SORT_BY_NAME })
public @interface SortingType {}
public static final int SORT_BY_TYPE = 0;
public static final int SORT_BY_DISTANCE = 1;
public static final int SORT_BY_TIME = 2;
public static final int SORT_BY_NAME = 3;
// These values have to match the values of kml::CompilationType from kml/types.hpp
public static final int CATEGORY = 0;

View file

@ -34,6 +34,15 @@
android:checked="true"
android:text="@string/by_default"/>
<RadioButton
style="?fontSubtitle1"
android:id="@+id/sort_by_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/margin_half_double_plus"
android:checked="false"
android:text="@string/by_name"/>
<RadioButton
style="?fontSubtitle1"
android:id="@+id/sort_by_type"

View file

@ -188,6 +188,7 @@ std::string ToString(BookmarkManager::SortingType type)
case BookmarkManager::SortingType::ByTime: return "ByTime";
case BookmarkManager::SortingType::ByType: return "ByType";
case BookmarkManager::SortingType::ByDistance: return "ByDistance";
case BookmarkManager::SortingType::ByName: return "ByName";
}
UNREACHABLE();
}
@ -200,6 +201,8 @@ bool GetSortingType(std::string const & typeStr, BookmarkManager::SortingType &
type = BookmarkManager::SortingType::ByType;
else if (typeStr == ToString(BookmarkManager::SortingType::ByDistance))
type = BookmarkManager::SortingType::ByDistance;
else if (typeStr == ToString(BookmarkManager::SortingType::ByName))
type = BookmarkManager::SortingType::ByName;
else
return false;
return true;
@ -638,7 +641,8 @@ std::vector<BookmarkManager::SortingType> BookmarkManager::GetAvailableSortingTy
sortingTypes.push_back(SortingType::ByDistance);
if (byTimeChecked)
sortingTypes.push_back(SortingType::ByTime);
// Sorting by name should always be possible.
sortingTypes.push_back(SortingType::ByName);
return sortingTypes;
}
@ -685,6 +689,12 @@ std::string BookmarkManager::GetTracksSortedBlockName()
return platform::GetLocalizedString("tracks_title");
}
// static
std::string BookmarkManager::GetBookmarksSortedBlockName()
{
return platform::GetLocalizedString("bookmarks");
}
// static
std::string BookmarkManager::GetOthersSortedBlockName()
{
@ -1129,6 +1139,16 @@ void BookmarkManager::SortTracksByTime(std::vector<SortTrackData> & tracks)
});
}
// static
void BookmarkManager::SortTracksByName(std::vector<SortTrackData> & tracks)
{
std::sort(tracks.begin(), tracks.end(),
[](SortTrackData const & lbm, SortTrackData const & rbm)
{
return lbm.m_name < rbm.m_name;
});
}
void BookmarkManager::SortByDistance(std::vector<SortBookmarkData> const & bookmarksForSort,
std::vector<SortTrackData> const & tracksForSort,
m2::PointD const & myPosition,
@ -1325,6 +1345,33 @@ void BookmarkManager::SortByType(std::vector<SortBookmarkData> const & bookmarks
}
}
void BookmarkManager::SortByName(std::vector<SortBookmarkData> const & bookmarksForSort,
std::vector<SortTrackData> const & tracksForSort,
SortedBlocksCollection & sortedBlocks)
{
std::vector<SortTrackData> sortedTracks = tracksForSort;
SortTracksByName(sortedTracks);
AddTracksSortedBlock(sortedTracks, sortedBlocks);
std::vector<SortBookmarkData const *> sortedMarks;
sortedMarks.reserve(bookmarksForSort.size());
for (auto const & mark : bookmarksForSort)
sortedMarks.push_back(&mark);
std::sort(sortedMarks.begin(), sortedMarks.end(),
[](SortBookmarkData const * lbm, SortBookmarkData const * rbm)
{
return lbm->m_name < rbm->m_name;
});
// Put all bookmarks into one block
SortedBlock bookmarkBlock;
bookmarkBlock.m_blockName = GetBookmarksSortedBlockName();
for (auto mark : sortedMarks)
bookmarkBlock.m_markIds.push_back(mark->m_id);
sortedBlocks.push_back(bookmarkBlock);
}
void BookmarkManager::GetSortedCategoryImpl(SortParams const & params,
std::vector<SortBookmarkData> const & bookmarksForSort,
std::vector<SortTrackData> const & tracksForSort,
@ -1342,6 +1389,9 @@ void BookmarkManager::GetSortedCategoryImpl(SortParams const & params,
case SortingType::ByType:
SortByType(bookmarksForSort, tracksForSort, sortedBlocks);
return;
case SortingType::ByName:
SortByName(bookmarksForSort, tracksForSort, sortedBlocks);
return;
}
UNREACHABLE();
}

View file

@ -222,7 +222,8 @@ public:
{
ByType,
ByDistance,
ByTime
ByTime,
ByName
};
struct SortedBlock
@ -370,6 +371,7 @@ public:
void CreateCategories(KMLDataCollection && dataCollection, bool autoSave = false);
static std::string GetTracksSortedBlockName();
static std::string GetBookmarksSortedBlockName();
static std::string GetOthersSortedBlockName();
static std::string GetNearMeSortedBlockName();
enum class SortedByTimeBlockType : uint32_t
@ -636,6 +638,7 @@ private:
{
SortBookmarkData(kml::BookmarkData const & bmData, search::ReverseGeocoder::RegionAddress const & address)
: m_id(bmData.m_id)
, m_name(GetPreferredBookmarkName(bmData))
, m_point(bmData.m_point)
, m_type(GetBookmarkBaseType(bmData.m_featureTypes))
, m_timestamp(bmData.m_timestamp)
@ -643,6 +646,7 @@ private:
{}
kml::MarkId m_id;
std::string m_name;
m2::PointD m_point;
BookmarkBaseType m_type;
kml::Timestamp m_timestamp;
@ -653,10 +657,12 @@ private:
{
explicit SortTrackData(kml::TrackData const & trackData)
: m_id(trackData.m_id)
, m_name(GetPreferredBookmarkStr(trackData.m_name))
, m_timestamp(trackData.m_timestamp)
{}
kml::TrackId m_id;
std::string m_name;
kml::Timestamp m_timestamp;
};
@ -674,6 +680,9 @@ private:
static void SortByType(std::vector<SortBookmarkData> const & bookmarksForSort,
std::vector<SortTrackData> const & tracksForSort,
SortedBlocksCollection & sortedBlocks);
static void SortByName(std::vector<SortBookmarkData> const & bookmarksForSort,
std::vector<SortTrackData> const & tracksForSort,
SortedBlocksCollection & sortedBlocks);
using AddressesCollection = std::vector<std::pair<kml::MarkId, search::ReverseGeocoder::RegionAddress>>;
void PrepareBookmarksAddresses(std::vector<SortBookmarkData> & bookmarksForSort, AddressesCollection & newAddresses);
@ -681,6 +690,7 @@ private:
void SetBookmarksAddresses(AddressesCollection const & addresses);
static void AddTracksSortedBlock(std::vector<SortTrackData> const & sortedTracks, SortedBlocksCollection & sortedBlocks);
static void SortTracksByTime(std::vector<SortTrackData> & tracks);
static void SortTracksByName(std::vector<SortTrackData> & tracks);
kml::MarkId GetTrackSelectionMarkId(kml::TrackId trackId) const;
int GetTrackSelectionMarkMinZoom(kml::TrackId trackId) const;