1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-11-22 12:21:15 +00:00

Use view binding in FolderActivity

This commit is contained in:
Adam Szewera 2024-01-09 21:23:56 +01:00 committed by Simon Frei
parent 43d8ebfeca
commit 9a5c500c11

View file

@ -27,6 +27,7 @@ import android.widget.Toast;
import com.google.android.material.materialswitch.MaterialSwitch; import com.google.android.material.materialswitch.MaterialSwitch;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.nutomic.syncthingandroid.R; import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.databinding.FragmentFolderBinding;
import com.nutomic.syncthingandroid.model.Device; import com.nutomic.syncthingandroid.model.Device;
import com.nutomic.syncthingandroid.model.Folder; import com.nutomic.syncthingandroid.model.Folder;
import com.nutomic.syncthingandroid.service.Constants; import com.nutomic.syncthingandroid.service.Constants;
@ -85,20 +86,7 @@ public class FolderActivity extends SyncthingActivity
// Indicates the result of the write test to mFolder.path on dialog init or after a path change. // Indicates the result of the write test to mFolder.path on dialog init or after a path change.
Boolean mCanWriteToPath = false; Boolean mCanWriteToPath = false;
private EditText mLabelView; private FragmentFolderBinding binding;
private EditText mIdView;
private TextView mPathView;
private TextView mAccessExplanationView;
private TextView mFolderTypeView;
private TextView mFolderTypeDescriptionView;
private MaterialSwitch mFolderFileWatcher;
private MaterialSwitch mFolderPaused;
private ViewGroup mDevicesContainer;
private TextView mPullOrderTypeView;
private TextView mPullOrderDescriptionView;
private TextView mVersioningDescriptionView;
private TextView mVersioningTypeView;
private TextView mEditIgnores;
private boolean mIsCreateMode; private boolean mIsCreateMode;
private boolean mFolderNeedsToUpdate = false; private boolean mFolderNeedsToUpdate = false;
@ -111,9 +99,9 @@ public class FolderActivity extends SyncthingActivity
private final TextWatcher mTextWatcher = new TextWatcherAdapter() { private final TextWatcher mTextWatcher = new TextWatcherAdapter() {
@Override @Override
public void afterTextChanged(Editable s) { public void afterTextChanged(Editable s) {
mFolder.label = mLabelView.getText().toString(); mFolder.label = binding.label.getText().toString();
mFolder.id = mIdView.getText().toString(); mFolder.id = binding.id.getText().toString();
// mPathView must not be handled here as it's handled by {@link onActivityResult} // binding.directoryTextView must not be handled here as it's handled by {@link onActivityResult}
mFolderNeedsToUpdate = true; mFolderNeedsToUpdate = true;
} }
}; };
@ -147,33 +135,19 @@ public class FolderActivity extends SyncthingActivity
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_folder); binding = FragmentFolderBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
mIsCreateMode = getIntent().getBooleanExtra(EXTRA_IS_CREATE, false); mIsCreateMode = getIntent().getBooleanExtra(EXTRA_IS_CREATE, false);
setTitle(mIsCreateMode ? R.string.create_folder : R.string.edit_folder); setTitle(mIsCreateMode ? R.string.create_folder : R.string.edit_folder);
registerOnServiceConnectedListener(this); registerOnServiceConnectedListener(this);
mLabelView = findViewById(R.id.label); binding.directoryTextView.setOnClickListener(view -> onPathViewClick());
mIdView = findViewById(R.id.id);
mPathView = findViewById(R.id.directoryTextView);
mAccessExplanationView = findViewById(R.id.accessExplanationView);
mFolderTypeView = findViewById(R.id.folderType);
mFolderTypeDescriptionView = findViewById(R.id.folderTypeDescription);
mFolderFileWatcher = findViewById(R.id.fileWatcher);
mFolderPaused = findViewById(R.id.folderPause);
mPullOrderTypeView = findViewById(R.id.pullOrderType);
mPullOrderDescriptionView = findViewById(R.id.pullOrderDescription);
mVersioningDescriptionView = findViewById(R.id.versioningDescription);
mVersioningTypeView = findViewById(R.id.versioningType);
mDevicesContainer = findViewById(R.id.devicesContainer);
mEditIgnores = findViewById(R.id.edit_ignores);
mPathView.setOnClickListener(view -> onPathViewClick());
findViewById(R.id.folderTypeContainer).setOnClickListener(v -> showFolderTypeDialog()); findViewById(R.id.folderTypeContainer).setOnClickListener(v -> showFolderTypeDialog());
findViewById(R.id.pullOrderContainer).setOnClickListener(v -> showPullOrderDialog()); findViewById(R.id.pullOrderContainer).setOnClickListener(v -> showPullOrderDialog());
findViewById(R.id.versioningContainer).setOnClickListener(v -> showVersioningDialog()); findViewById(R.id.versioningContainer).setOnClickListener(v -> showVersioningDialog());
mEditIgnores.setOnClickListener(v -> editIgnores()); binding.editIgnores.setOnClickListener(v -> editIgnores());
if (mIsCreateMode) { if (mIsCreateMode) {
if (savedInstanceState != null) { if (savedInstanceState != null) {
@ -186,15 +160,15 @@ public class FolderActivity extends SyncthingActivity
initFolder(); initFolder();
} }
// Open keyboard on label view in edit mode. // Open keyboard on label view in edit mode.
mLabelView.requestFocus(); binding.label.requestFocus();
mEditIgnores.setEnabled(false); binding.editIgnores.setEnabled(false);
} }
else { else {
// Prepare edit mode. // Prepare edit mode.
mIdView.clearFocus(); binding.id.clearFocus();
mIdView.setFocusable(false); binding.id.setFocusable(false);
mIdView.setEnabled(false); binding.id.setEnabled(false);
mPathView.setEnabled(false); binding.directoryTextView.setEnabled(false);
} }
if (savedInstanceState != null){ if (savedInstanceState != null){
@ -211,7 +185,7 @@ public class FolderActivity extends SyncthingActivity
} }
/** /**
* Invoked after user clicked on the {@link mPathView} label. * Invoked after user clicked on the directoryTextView label.
*/ */
@SuppressLint("InlinedAPI") @SuppressLint("InlinedAPI")
private void onPathViewClick() { private void onPathViewClick() {
@ -312,8 +286,8 @@ public class FolderActivity extends SyncthingActivity
syncthingService.getNotificationHandler().cancelConsentNotification(getIntent().getIntExtra(EXTRA_NOTIFICATION_ID, 0)); syncthingService.getNotificationHandler().cancelConsentNotification(getIntent().getIntExtra(EXTRA_NOTIFICATION_ID, 0));
syncthingService.unregisterOnServiceStateChangeListener(this::onServiceStateChange); syncthingService.unregisterOnServiceStateChangeListener(this::onServiceStateChange);
} }
mLabelView.removeTextChangedListener(mTextWatcher); binding.label.removeTextChangedListener(mTextWatcher);
mIdView.removeTextChangedListener(mTextWatcher); binding.id.removeTextChangedListener(mTextWatcher);
} }
@Override @Override
@ -395,22 +369,22 @@ public class FolderActivity extends SyncthingActivity
} }
private void updateViewsAndSetListeners() { private void updateViewsAndSetListeners() {
mLabelView.removeTextChangedListener(mTextWatcher); binding.label.removeTextChangedListener(mTextWatcher);
mIdView.removeTextChangedListener(mTextWatcher); binding.id.removeTextChangedListener(mTextWatcher);
mFolderFileWatcher.setOnCheckedChangeListener(null); binding.fileWatcher.setOnCheckedChangeListener(null);
mFolderPaused.setOnCheckedChangeListener(null); binding.folderPause.setOnCheckedChangeListener(null);
// Update views // Update views
mLabelView.setText(mFolder.label); binding.label.setText(mFolder.label);
mIdView.setText(mFolder.id); binding.id.setText(mFolder.id);
updateFolderTypeDescription(); updateFolderTypeDescription();
updatePullOrderDescription(); updatePullOrderDescription();
updateVersioningDescription(); updateVersioningDescription();
mFolderFileWatcher.setChecked(mFolder.fsWatcherEnabled); binding.fileWatcher.setChecked(mFolder.fsWatcherEnabled);
mFolderPaused.setChecked(mFolder.paused); binding.folderPause.setChecked(mFolder.paused);
List<Device> devicesList = getApi().getDevices(false); List<Device> devicesList = getApi().getDevices(false);
mDevicesContainer.removeAllViews(); binding.devicesContainer.removeAllViews();
if (devicesList.isEmpty()) { if (devicesList.isEmpty()) {
addEmptyDeviceListView(); addEmptyDeviceListView();
} else { } else {
@ -420,10 +394,10 @@ public class FolderActivity extends SyncthingActivity
} }
// Keep state updated // Keep state updated
mLabelView.addTextChangedListener(mTextWatcher); binding.label.addTextChangedListener(mTextWatcher);
mIdView.addTextChangedListener(mTextWatcher); binding.id.addTextChangedListener(mTextWatcher);
mFolderFileWatcher.setOnCheckedChangeListener(mCheckedListener); binding.fileWatcher.setOnCheckedChangeListener(mCheckedListener);
mFolderPaused.setOnCheckedChangeListener(mCheckedListener); binding.folderPause.setOnCheckedChangeListener(mCheckedListener);
} }
@Override @Override
@ -552,7 +526,7 @@ public class FolderActivity extends SyncthingActivity
* Prerequisite: mFolder.path must be non-empty * Prerequisite: mFolder.path must be non-empty
*/ */
private void checkWriteAndUpdateUI() { private void checkWriteAndUpdateUI() {
mPathView.setText(mFolder.path); binding.directoryTextView.setText(mFolder.path);
if (TextUtils.isEmpty(mFolder.path)) { if (TextUtils.isEmpty(mFolder.path)) {
return; return;
} }
@ -564,9 +538,9 @@ public class FolderActivity extends SyncthingActivity
*/ */
mCanWriteToPath = Util.nativeBinaryCanWriteToPath(FolderActivity.this, mFolder.path); mCanWriteToPath = Util.nativeBinaryCanWriteToPath(FolderActivity.this, mFolder.path);
if (mCanWriteToPath) { if (mCanWriteToPath) {
mAccessExplanationView.setText(R.string.folder_path_readwrite); binding.accessExplanationView.setText(R.string.folder_path_readwrite);
mFolderTypeView.setEnabled(true); binding.folderType.setEnabled(true);
mEditIgnores.setEnabled(true); binding.editIgnores.setEnabled(true);
if (mIsCreateMode) { if (mIsCreateMode) {
/** /**
* Suggest folder type FOLDER_TYPE_SEND_RECEIVE for folders to be created * Suggest folder type FOLDER_TYPE_SEND_RECEIVE for folders to be created
@ -579,9 +553,9 @@ public class FolderActivity extends SyncthingActivity
} }
} else { } else {
// Force "sendonly" folder. // Force "sendonly" folder.
mAccessExplanationView.setText(R.string.folder_path_readonly); binding.accessExplanationView.setText(R.string.folder_path_readonly);
mFolderTypeView.setEnabled(false); binding.folderType.setEnabled(false);
mEditIgnores.setEnabled(false); binding.editIgnores.setEnabled(false);
mFolder.type = Constants.FOLDER_TYPE_SEND_ONLY; mFolder.type = Constants.FOLDER_TYPE_SEND_ONLY;
updateFolderTypeDescription(); updateFolderTypeDescription();
} }
@ -626,15 +600,15 @@ public class FolderActivity extends SyncthingActivity
int contentInset = getResources().getDimensionPixelOffset(R.dimen.abc_action_bar_content_inset_material); int contentInset = getResources().getDimensionPixelOffset(R.dimen.abc_action_bar_content_inset_material);
setMarginStart(params, dividerInset); setMarginStart(params, dividerInset);
setMarginEnd(params, contentInset); setMarginEnd(params, contentInset);
TextView emptyView = new TextView(mDevicesContainer.getContext()); TextView emptyView = new TextView(binding.devicesContainer.getContext());
emptyView.setGravity(CENTER_VERTICAL); emptyView.setGravity(CENTER_VERTICAL);
emptyView.setText(R.string.devices_list_empty); emptyView.setText(R.string.devices_list_empty);
mDevicesContainer.addView(emptyView, params); binding.devicesContainer.addView(emptyView, params);
} }
private void addDeviceViewAndSetListener(Device device, LayoutInflater inflater) { private void addDeviceViewAndSetListener(Device device, LayoutInflater inflater) {
inflater.inflate(R.layout.item_device_form, mDevicesContainer); inflater.inflate(R.layout.item_device_form, binding.devicesContainer);
MaterialSwitch deviceView = (MaterialSwitch) mDevicesContainer.getChildAt(mDevicesContainer.getChildCount()-1); MaterialSwitch deviceView = (MaterialSwitch) binding.devicesContainer.getChildAt(binding.devicesContainer.getChildCount()-1);
deviceView.setOnCheckedChangeListener(null); deviceView.setOnCheckedChangeListener(null);
deviceView.setChecked(mFolder.getDevice(device.deviceID) != null); deviceView.setChecked(mFolder.getDevice(device.deviceID) != null);
deviceView.setText(device.getDisplayName()); deviceView.setText(device.getDisplayName());
@ -721,8 +695,8 @@ public class FolderActivity extends SyncthingActivity
} }
private void setFolderTypeDescription(String type, String description) { private void setFolderTypeDescription(String type, String description) {
mFolderTypeView.setText(type); binding.folderType.setText(type);
mFolderTypeDescriptionView.setText(description); binding.folderTypeDescription.setText(description);
} }
private void updatePullOrderDescription() { private void updatePullOrderDescription() {
@ -765,8 +739,8 @@ public class FolderActivity extends SyncthingActivity
} }
private void setPullOrderDescription(String type, String description) { private void setPullOrderDescription(String type, String description) {
mPullOrderTypeView.setText(type); binding.pullOrderType.setText(type);
mPullOrderDescriptionView.setText(description); binding.pullOrderDescription.setText(description);
} }
private void updateVersioningDescription() { private void updateVersioningDescription() {
@ -801,7 +775,7 @@ public class FolderActivity extends SyncthingActivity
} }
private void setVersioningDescription(String type, String description) { private void setVersioningDescription(String type, String description) {
mVersioningTypeView.setText(type); binding.versioningType.setText(type);
mVersioningDescriptionView.setText(description); binding.versioningDescription.setText(description);
} }
} }