[android] Do not duplicate StorageItems in StoragePathAdapter

Signed-off-by: Konstantin Pastbin <konstantin.pastbin@gmail.com>
This commit is contained in:
Konstantin Pastbin 2022-05-12 21:02:35 +03:00 committed by Alexander Borsuk
parent fd75bc869c
commit e8da7ee19f
4 changed files with 26 additions and 30 deletions

View file

@ -6,11 +6,11 @@ package com.mapswithme.maps.settings;
public class StorageItem
{
// Path to the root of writable directory.
public final String mPath;
private final String mPath;
// Free size.
public final long mFreeSize;
private final long mFreeSize;
StorageItem(String path, long size)
public StorageItem(String path, long size)
{
mPath = path;
mFreeSize = size;
@ -26,7 +26,7 @@ public class StorageItem
StorageItem other = (StorageItem) o;
// Storage equal is considered equal, either its path OR size equals to another one's.
// Size of storage free space can change dynamically, so that hack provides us with better results identifying the same storages.
return mFreeSize == other.mFreeSize || mPath.equals(other.mPath);
return mFreeSize == other.getFreeSize() || mPath.equals(other.getFullPath());
}
@Override

View file

@ -15,27 +15,26 @@ import java.util.List;
class StoragePathAdapter extends BaseAdapter
{
private final LayoutInflater mInflater;
private final List<StorageItem> mItems = new ArrayList<>();
private int mCurrentStorageIndex = -1;
private final StoragePathManager mPathManager;
private final Activity mActivity;
private long mSizeNeeded;
public StoragePathAdapter(@NonNull Activity context)
public StoragePathAdapter(StoragePathManager pathManager, Activity activity)
{
mInflater = context.getLayoutInflater();
mPathManager = pathManager;
mActivity = activity;
}
@Override
public int getCount()
{
return (mItems == null ? 0 : mItems.size());
return (mPathManager.getStorageItems() == null ? 0 : mPathManager.getStorageItems().size());
}
@Override
public StorageItem getItem(int position)
{
return mItems.get(position);
return mPathManager.getStorageItems().get(position);
}
@Override
@ -48,29 +47,25 @@ class StoragePathAdapter extends BaseAdapter
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
convertView = mInflater.inflate(R.layout.item_storage, parent, false);
convertView = mActivity.getLayoutInflater().inflate(R.layout.item_storage, parent, false);
StorageItem item = mItems.get(position);
StorageItem item = mPathManager.getStorageItems().get(position);
CheckedTextView checkedView = (CheckedTextView) convertView;
checkedView.setText(item.mPath + ": " + StoragePathFragment.getSizeString(item.mFreeSize));
checkedView.setChecked(position == mCurrentStorageIndex);
checkedView.setEnabled(position == mCurrentStorageIndex || isStorageBigEnough(position));
checkedView.setText(item.getFullPath() + ": " + StoragePathFragment.getSizeString(item.getFreeSize()));
checkedView.setChecked(position == mPathManager.getCurrentStorageIndex());
checkedView.setEnabled(position == mPathManager.getCurrentStorageIndex() || isStorageBigEnough(position));
return convertView;
}
public void update(List<StorageItem> items, int currentItemIndex, long dirSize)
public void update(long dirSize)
{
mSizeNeeded = dirSize;
mItems.clear();
mItems.addAll(items);
mCurrentStorageIndex = currentItemIndex;
notifyDataSetChanged();
}
public boolean isStorageBigEnough(int index)
{
return mItems.get(index).mFreeSize >= mSizeNeeded;
return mPathManager.getStorageItems().get(index).getFreeSize() >= mSizeNeeded;
}
}

View file

@ -45,7 +45,7 @@ public class StoragePathFragment extends BaseSettingsFragment
{
View root = super.onCreateView(inflater, container, savedInstanceState);
mPathManager = new StoragePathManager(requireActivity());
mAdapter = new StoragePathAdapter(requireActivity());
mAdapter = new StoragePathAdapter(mPathManager, requireActivity());
mHeader = root.findViewById(R.id.header);
mList = root.findViewById(R.id.list);
@ -89,7 +89,7 @@ public class StoragePathFragment extends BaseSettingsFragment
{
long dirSize = getWritableDirSize();
mHeader.setText(getString(R.string.maps) + ": " + getSizeString(dirSize));
mAdapter.update(mPathManager.getStorageItems(), mPathManager.getCurrentStorageIndex(), dirSize);
mAdapter.update(dirSize);
}
/**

View file

@ -294,19 +294,20 @@ public class StoragePathManager
for (StorageItem item : items)
{
if (containsMapData(item.mPath))
String path = item.getFullPath();
if (containsMapData(path))
{
LOGGER.i(TAG, "Found maps files at " + item.mPath);
return item.mPath;
LOGGER.i(TAG, "Found maps files at " + path);
return path;
}
else
{
LOGGER.i(TAG, "No maps files found at " + item.mPath);
LOGGER.i(TAG, "No maps files found at " + path);
}
}
// Use the first item by default.
final String defaultDir = items.get(0).mPath;
final String defaultDir = items.get(0).getFullPath();
LOGGER.i(TAG, "Using default directory: " + defaultDir);
return defaultDir;
}