forked from organicmaps/organicmaps
[Android] Added bookmarks async loading in bookmark activity
This commit is contained in:
parent
fa6278c65d
commit
de99faf3dc
5 changed files with 152 additions and 9 deletions
44
android/res/layout/fragment_bookmark_categories.xml
Normal file
44
android/res/layout/fragment_bookmark_categories.xml
Normal file
|
@ -0,0 +1,44 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<FrameLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
style="@style/MwmWidget.FrameLayout"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
<include layout="@layout/recycler_default"/>
|
||||
|
||||
<com.mapswithme.maps.widget.PlaceholderView
|
||||
android:id="@+id/placeholder"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:paddingLeft="@dimen/margin_double_and_half"
|
||||
android:paddingRight="@dimen/margin_double_and_half"
|
||||
android:paddingTop="@dimen/placeholder_margin_top"
|
||||
android:visibility="gone"
|
||||
tools:visibility="visible"/>
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/placeholder_loading"
|
||||
android:orientation="vertical"
|
||||
android:paddingLeft="@dimen/margin_double_and_half"
|
||||
android:paddingRight="@dimen/margin_double_and_half"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:gravity="center"
|
||||
android:visibility="gone"
|
||||
tools:visibility="visible">
|
||||
<ProgressBar
|
||||
style="@style/Widget.AppCompat.ProgressBar"
|
||||
android:background="@null"
|
||||
android:layout_gravity="center"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"/>
|
||||
<TextView
|
||||
android:gravity="center"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingTop="@dimen/margin_half"
|
||||
android:textAppearance="@style/MwmTextAppearance.Body3"
|
||||
android:text="@string/load_kmz_title"/>
|
||||
</LinearLayout>
|
||||
</FrameLayout>
|
|
@ -19,20 +19,24 @@ import com.mapswithme.maps.widget.PlaceholderView;
|
|||
import com.mapswithme.maps.widget.recycler.RecyclerClickListener;
|
||||
import com.mapswithme.maps.widget.recycler.RecyclerLongClickListener;
|
||||
import com.mapswithme.util.BottomSheetHelper;
|
||||
import com.mapswithme.util.UiUtils;
|
||||
import com.mapswithme.util.sharing.SharingHelper;
|
||||
|
||||
public class BookmarkCategoriesFragment extends BaseMwmRecyclerFragment
|
||||
implements EditTextDialogFragment.OnTextSaveListener,
|
||||
MenuItem.OnMenuItemClickListener,
|
||||
RecyclerClickListener,
|
||||
RecyclerLongClickListener
|
||||
RecyclerLongClickListener,
|
||||
BookmarkManager.BookmarksLoadingListener
|
||||
{
|
||||
private int mSelectedPosition;
|
||||
@Nullable
|
||||
private View mLoadingPlaceholder;
|
||||
|
||||
@Override
|
||||
protected @LayoutRes int getLayoutRes()
|
||||
{
|
||||
return R.layout.fragment_search_base;
|
||||
return R.layout.fragment_bookmark_categories;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -55,6 +59,8 @@ public class BookmarkCategoriesFragment extends BaseMwmRecyclerFragment
|
|||
{
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
|
||||
mLoadingPlaceholder = view.findViewById(R.id.placeholder_loading);
|
||||
|
||||
if (getAdapter() != null)
|
||||
{
|
||||
getAdapter().setOnClickListener(this);
|
||||
|
@ -73,13 +79,43 @@ public class BookmarkCategoriesFragment extends BaseMwmRecyclerFragment
|
|||
private void updateResultsPlaceholder()
|
||||
{
|
||||
if (getAdapter() != null)
|
||||
showPlaceholder(getAdapter().getItemCount() == 0);
|
||||
{
|
||||
boolean showLoadingPlaceholder = BookmarkManager.nativeIsAsyncBookmarksLoadingInProgress();
|
||||
showPlaceholder(!showLoadingPlaceholder && getAdapter().getItemCount() == 0);
|
||||
}
|
||||
}
|
||||
|
||||
private void updateLoadingPlaceholder()
|
||||
{
|
||||
if (mLoadingPlaceholder != null)
|
||||
{
|
||||
boolean showLoadingPlaceholder = BookmarkManager.nativeIsAsyncBookmarksLoadingInProgress();
|
||||
if (getAdapter() != null && getAdapter().getItemCount() != 0)
|
||||
showLoadingPlaceholder = false;
|
||||
|
||||
UiUtils.showIf(showLoadingPlaceholder, mLoadingPlaceholder);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart()
|
||||
{
|
||||
super.onStart();
|
||||
BookmarkManager.INSTANCE.addListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop()
|
||||
{
|
||||
super.onStop();
|
||||
BookmarkManager.INSTANCE.removeListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume()
|
||||
{
|
||||
super.onResume();
|
||||
updateLoadingPlaceholder();
|
||||
if (getAdapter() != null)
|
||||
getAdapter().notifyDataSetChanged();
|
||||
}
|
||||
|
@ -158,6 +194,29 @@ public class BookmarkCategoriesFragment extends BaseMwmRecyclerFragment
|
|||
@Override
|
||||
protected void setupPlaceholder(@NonNull PlaceholderView placeholder)
|
||||
{
|
||||
placeholder.setContent(R.drawable.img_bookmarks, R.string.bookmarks_empty_title, R.string.bookmarks_usage_hint);
|
||||
placeholder.setContent(R.drawable.img_bookmarks, R.string.bookmarks_empty_title,
|
||||
R.string.bookmarks_usage_hint);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBookmarksLoadingStarted()
|
||||
{
|
||||
updateLoadingPlaceholder();
|
||||
updateResultsPlaceholder();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBookmarksLoadingFinished()
|
||||
{
|
||||
updateLoadingPlaceholder();
|
||||
updateResultsPlaceholder();
|
||||
if (getAdapter() != null)
|
||||
getAdapter().notifyDataSetChanged();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBookmarksFileLoaded(boolean success)
|
||||
{
|
||||
// Do nothing here.
|
||||
}
|
||||
}
|
||||
|
|
|
@ -158,6 +158,7 @@ df::UserLineMark const * BookmarkCategory::GetUserLineMark(size_t index) const
|
|||
|
||||
void BookmarkCategory::ClearTracks()
|
||||
{
|
||||
SetDirty();
|
||||
m_tracks.clear();
|
||||
}
|
||||
|
||||
|
@ -177,6 +178,7 @@ std::vector<std::unique_ptr<Track>> BookmarkCategory::StealTracks()
|
|||
|
||||
void BookmarkCategory::AppendTracks(std::vector<std::unique_ptr<Track>> && tracks)
|
||||
{
|
||||
SetDirty();
|
||||
std::move(tracks.begin(), tracks.end(), std::back_inserter(m_tracks));
|
||||
}
|
||||
|
||||
|
@ -861,7 +863,9 @@ bool BookmarkCategory::SaveToKMLFile()
|
|||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
m_file = GenerateUniqueFileName(GetPlatform().SettingsDir(), name);
|
||||
}
|
||||
|
||||
std::string const fileTmp = m_file + ".tmp";
|
||||
|
||||
|
|
|
@ -137,6 +137,7 @@ void BookmarkManager::ClearCategories()
|
|||
void BookmarkManager::LoadBookmarks()
|
||||
{
|
||||
ClearCategories();
|
||||
m_loadBookmarksFinished = false;
|
||||
|
||||
NotifyAboutStartAsyncLoading();
|
||||
GetPlatform().RunTask(Platform::Thread::File, [this]()
|
||||
|
@ -163,6 +164,17 @@ void BookmarkManager::LoadBookmarks()
|
|||
|
||||
void BookmarkManager::LoadBookmark(std::string const & filePath, bool isTemporaryFile)
|
||||
{
|
||||
if (!m_loadBookmarksFinished || m_asyncLoadingInProgress)
|
||||
{
|
||||
m_bookmarkLoadingQueue.emplace_back(filePath, isTemporaryFile);
|
||||
return;
|
||||
}
|
||||
LoadBookmarkRoutine(filePath, isTemporaryFile);
|
||||
}
|
||||
|
||||
void BookmarkManager::LoadBookmarkRoutine(std::string const & filePath, bool isTemporaryFile)
|
||||
{
|
||||
ASSERT(!m_asyncLoadingInProgress, ());
|
||||
NotifyAboutStartAsyncLoading();
|
||||
GetPlatform().RunTask(Platform::Thread::File, [this, filePath, isTemporaryFile]()
|
||||
{
|
||||
|
@ -198,7 +210,7 @@ void BookmarkManager::NotifyAboutStartAsyncLoading()
|
|||
|
||||
GetPlatform().RunTask(Platform::Thread::Gui, [this]()
|
||||
{
|
||||
m_asyncLoadingCounter++;
|
||||
m_asyncLoadingInProgress = true;
|
||||
if (m_asyncLoadingCallbacks.m_onStarted != nullptr)
|
||||
m_asyncLoadingCallbacks.m_onStarted();
|
||||
});
|
||||
|
@ -211,12 +223,20 @@ void BookmarkManager::NotifyAboutFinishAsyncLoading(std::shared_ptr<CategoriesCo
|
|||
|
||||
GetPlatform().RunTask(Platform::Thread::Gui, [this, collection]()
|
||||
{
|
||||
m_asyncLoadingInProgress = false;
|
||||
m_loadBookmarksFinished = true;
|
||||
if (!collection->empty())
|
||||
MergeCategories(std::move(*collection));
|
||||
|
||||
m_asyncLoadingCounter--;
|
||||
if (m_asyncLoadingCounter == 0 && m_asyncLoadingCallbacks.m_onFinished != nullptr)
|
||||
if (m_asyncLoadingCallbacks.m_onFinished != nullptr)
|
||||
m_asyncLoadingCallbacks.m_onFinished();
|
||||
|
||||
if (!m_bookmarkLoadingQueue.empty())
|
||||
{
|
||||
LoadBookmarkRoutine(m_bookmarkLoadingQueue.front().m_filename,
|
||||
m_bookmarkLoadingQueue.front().m_isTemporaryFile);
|
||||
m_bookmarkLoadingQueue.pop_front();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -522,6 +542,9 @@ void BookmarkManager::MergeCategories(CategoriesCollection && newCategories)
|
|||
category->AppendTracks((*it)->StealTracks());
|
||||
category->SaveToKMLFile();
|
||||
|
||||
// Delete file since it has been merged.
|
||||
my::DeleteFileX((*it)->GetFileName());
|
||||
|
||||
newCategories.erase(it);
|
||||
}
|
||||
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
|
||||
#include <atomic>
|
||||
#include <functional>
|
||||
#include <list>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
@ -91,7 +92,7 @@ public:
|
|||
std::unique_ptr<MyPositionMarkPoint> & MyPositionMark();
|
||||
std::unique_ptr<MyPositionMarkPoint> const & MyPositionMark() const;
|
||||
|
||||
bool IsAsyncLoadingInProgress() const { return m_asyncLoadingCounter != 0; }
|
||||
bool IsAsyncLoadingInProgress() const { return m_asyncLoadingInProgress; }
|
||||
|
||||
private:
|
||||
UserMarkContainer const * FindUserMarksContainer(UserMark::Type type) const;
|
||||
|
@ -104,11 +105,13 @@ private:
|
|||
void NotifyAboutFinishAsyncLoading(std::shared_ptr<CategoriesCollection> && collection);
|
||||
boost::optional<std::string> GetKMLPath(std::string const & filePath);
|
||||
void NotifyAboutFile(bool success, std::string const & filePath, bool isTemporaryFile);
|
||||
void LoadBookmarkRoutine(std::string const & filePath, bool isTemporaryFile);
|
||||
|
||||
GetStringsBundleFn m_getStringsBundle;
|
||||
df::DrapeEngineSafePtr m_drapeEngine;
|
||||
AsyncLoadingCallbacks m_asyncLoadingCallbacks;
|
||||
std::atomic<bool> m_needTeardown;
|
||||
bool m_loadBookmarksFinished = false;
|
||||
|
||||
ScreenBase m_viewport;
|
||||
|
||||
|
@ -120,7 +123,17 @@ private:
|
|||
std::unique_ptr<StaticMarkPoint> m_selectionMark;
|
||||
std::unique_ptr<MyPositionMarkPoint> m_myPositionMark;
|
||||
|
||||
size_t m_asyncLoadingCounter = 0;
|
||||
bool m_asyncLoadingInProgress = false;
|
||||
struct BookmarkLoaderInfo
|
||||
{
|
||||
std::string m_filename;
|
||||
bool m_isTemporaryFile = false;
|
||||
BookmarkLoaderInfo() {}
|
||||
BookmarkLoaderInfo(std::string const & filename, bool isTemporaryFile)
|
||||
: m_filename(filename), m_isTemporaryFile(isTemporaryFile)
|
||||
{}
|
||||
};
|
||||
std::list<BookmarkLoaderInfo> m_bookmarkLoadingQueue;
|
||||
|
||||
DISALLOW_COPY_AND_MOVE(BookmarkManager);
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue