1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2025-01-11 04:25:53 +00:00

Use data binding for folder items

This commit is contained in:
Felix Ableitner 2016-12-17 23:26:06 +09:00
parent 9670547e1b
commit e0412ec51b
3 changed files with 76 additions and 77 deletions

View file

@ -34,6 +34,7 @@ android {
compileSdkVersion 25 compileSdkVersion 25
buildToolsVersion "25.0.2" buildToolsVersion "25.0.2"
buildTypes.debug.applicationIdSuffix ".debug" buildTypes.debug.applicationIdSuffix ".debug"
dataBinding.enabled = true
defaultConfig { defaultConfig {
applicationId "com.nutomic.syncthingandroid" applicationId "com.nutomic.syncthingandroid"

View file

@ -1,6 +1,7 @@
package com.nutomic.syncthingandroid.views; package com.nutomic.syncthingandroid.views;
import android.content.Context; import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
import android.text.TextUtils; import android.text.TextUtils;
@ -12,6 +13,7 @@ import android.widget.TextView;
import com.nutomic.syncthingandroid.BuildConfig; import com.nutomic.syncthingandroid.BuildConfig;
import com.nutomic.syncthingandroid.R; import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.databinding.ItemFolderListBinding;
import com.nutomic.syncthingandroid.model.Folder; import com.nutomic.syncthingandroid.model.Folder;
import com.nutomic.syncthingandroid.model.Model; import com.nutomic.syncthingandroid.model.Model;
import com.nutomic.syncthingandroid.service.RestApi; import com.nutomic.syncthingandroid.service.RestApi;
@ -28,51 +30,43 @@ import static android.view.View.VISIBLE;
public class FoldersAdapter extends ArrayAdapter<Folder> { public class FoldersAdapter extends ArrayAdapter<Folder> {
private final HashMap<String, Model> mModels = new HashMap<>(); private final HashMap<String, Model> mModels = new HashMap<>();
private final LayoutInflater mInflater;
public FoldersAdapter(Context context) { public FoldersAdapter(Context context) {
super(context, R.layout.item_folder_list); super(context, 0);
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
} }
@Override @Override
@NonNull @NonNull
public View getView(int position, View convertView, @NonNull ViewGroup parent) { public View getView(int position, View convertView, @NonNull ViewGroup parent) {
if (convertView == null) ItemFolderListBinding binding = (convertView == null)
convertView = mInflater.inflate(R.layout.item_folder_list, parent, false); ? DataBindingUtil.inflate(LayoutInflater.from(getContext()), R.layout.item_folder_list, parent, false)
: DataBindingUtil.bind(convertView);
TextView label = (TextView) convertView.findViewById(R.id.label);
TextView state = (TextView) convertView.findViewById(R.id.state);
TextView directory = (TextView) convertView.findViewById(R.id.directory);
TextView items = (TextView) convertView.findViewById(R.id.items);
TextView size = (TextView) convertView.findViewById(R.id.size);
TextView invalid = (TextView) convertView.findViewById(R.id.invalid);
Folder folder = getItem(position); Folder folder = getItem(position);
Model model = mModels.get(folder.id); Model model = mModels.get(folder.id);
label.setText(TextUtils.isEmpty(folder.label) ? folder.id : folder.label); binding.label.setText(TextUtils.isEmpty(folder.label) ? folder.id : folder.label);
state.setTextColor(ContextCompat.getColor(getContext(), R.color.text_green)); binding.state.setTextColor(ContextCompat.getColor(getContext(), R.color.text_green));
directory.setText(folder.path); binding.directory.setText(folder.path);
if (model != null) { if (model != null) {
int percentage = (model.globalBytes != 0) int percentage = (model.globalBytes != 0)
? Math.round(100 * model.inSyncBytes / model.globalBytes) ? Math.round(100 * model.inSyncBytes / model.globalBytes)
: 100; : 100;
state.setText(getLocalizedState(getContext(), model.state, percentage)); binding.state.setText(getLocalizedState(getContext(), model.state, percentage));
items.setVisibility(VISIBLE); binding.items.setVisibility(VISIBLE);
items.setText(getContext() binding.items.setText(getContext()
.getString(R.string.files, model.inSyncFiles, model.globalFiles)); .getString(R.string.files, model.inSyncFiles, model.globalFiles));
size.setVisibility(VISIBLE); binding.size.setVisibility(VISIBLE);
size.setText(getContext().getString(R.string.folder_size_format, binding.size.setText(getContext().getString(R.string.folder_size_format,
Util.readableFileSize(getContext(), model.inSyncBytes), Util.readableFileSize(getContext(), model.inSyncBytes),
Util.readableFileSize(getContext(), model.globalBytes))); Util.readableFileSize(getContext(), model.globalBytes)));
setTextOrHide(invalid, model.invalid); setTextOrHide(binding.invalid, model.invalid);
} else { } else {
items.setVisibility(GONE); binding.items.setVisibility(GONE);
size.setVisibility(GONE); binding.size.setVisibility(GONE);
setTextOrHide(invalid, folder.invalid); setTextOrHide(binding.invalid, folder.invalid);
} }
return convertView; return binding.getRoot();
} }
/** /**

View file

@ -1,6 +1,8 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" <layout xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:paddingBottom="8dp" android:paddingBottom="8dp"
@ -60,3 +62,5 @@
android:textColor="@color/text_red" /> android:textColor="@color/text_red" />
</RelativeLayout> </RelativeLayout>
</layout>