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 (#2037)

Enable the [view binding
feature](https://developer.android.com/topic/libraries/view-binding#kts)
and replace explicit view binding with `binding.xyz` in
`DeviceActivity`.
This commit is contained in:
Adam Szewera 2024-01-07 15:17:52 +01:00 committed by GitHub
parent 9b043748bd
commit e5574f0d62
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 80 deletions

View file

@ -35,6 +35,7 @@ android {
buildFeatures { buildFeatures {
dataBinding = true dataBinding = true
viewBinding = true
} }
defaultConfig { defaultConfig {

View file

@ -1,5 +1,12 @@
package com.nutomic.syncthingandroid.activities; package com.nutomic.syncthingandroid.activities;
import static android.text.TextUtils.isEmpty;
import static android.view.View.GONE;
import static android.view.View.VISIBLE;
import static android.view.WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN;
import static com.nutomic.syncthingandroid.service.SyncthingService.State.ACTIVE;
import static com.nutomic.syncthingandroid.util.Compression.METADATA;
import android.app.Activity; import android.app.Activity;
import android.app.Dialog; import android.app.Dialog;
import android.content.Context; import android.content.Context;
@ -7,7 +14,6 @@ import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.Bundle; import android.os.Bundle;
import androidx.core.content.ContextCompat;
import android.text.Editable; import android.text.Editable;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.TextWatcher; import android.text.TextWatcher;
@ -16,13 +22,13 @@ import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.CompoundButton; import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import com.google.android.material.materialswitch.MaterialSwitch; import androidx.core.content.ContextCompat;
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.ActivityDeviceBinding;
import com.nutomic.syncthingandroid.model.Connections; import com.nutomic.syncthingandroid.model.Connections;
import com.nutomic.syncthingandroid.model.Device; import com.nutomic.syncthingandroid.model.Device;
import com.nutomic.syncthingandroid.service.SyncthingService; import com.nutomic.syncthingandroid.service.SyncthingService;
@ -34,13 +40,6 @@ import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import static android.text.TextUtils.isEmpty;
import static android.view.View.GONE;
import static android.view.View.VISIBLE;
import static android.view.WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN;
import static com.nutomic.syncthingandroid.service.SyncthingService.State.ACTIVE;
import static com.nutomic.syncthingandroid.util.Compression.METADATA;
/** /**
* Shows device details and allows changing them. * Shows device details and allows changing them.
*/ */
@ -65,27 +64,7 @@ public class DeviceActivity extends SyncthingActivity implements View.OnClickLis
private Device mDevice; private Device mDevice;
private View mIdContainer; private ActivityDeviceBinding binding;
private EditText mIdView;
private View mQrButton;
private EditText mNameView;
private EditText mAddressesView;
private TextView mCurrentAddressView;
private TextView mCompressionValueView;
private MaterialSwitch mIntroducerView;
private MaterialSwitch mDevicePaused;
private TextView mSyncthingVersionView;
private View mCompressionContainer;
private boolean mIsCreateMode; private boolean mIsCreateMode;
@ -105,7 +84,7 @@ public class DeviceActivity extends SyncthingActivity implements View.OnClickLis
mDeviceNeedsToUpdate = true; mDeviceNeedsToUpdate = true;
mDevice.compression = compression.getValue(DeviceActivity.this); mDevice.compression = compression.getValue(DeviceActivity.this);
mCompressionValueView.setText(compression.getTitle(DeviceActivity.this)); binding.compressionValue.setText(compression.getTitle(DeviceActivity.this));
} }
} }
}; };
@ -160,26 +139,15 @@ public class DeviceActivity extends SyncthingActivity implements View.OnClickLis
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_device); binding = ActivityDeviceBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
mIsCreateMode = getIntent().getBooleanExtra(EXTRA_IS_CREATE, false); mIsCreateMode = getIntent().getBooleanExtra(EXTRA_IS_CREATE, false);
registerOnServiceConnectedListener(this::onServiceConnected); registerOnServiceConnectedListener(this::onServiceConnected);
setTitle(mIsCreateMode ? R.string.add_device : R.string.edit_device); setTitle(mIsCreateMode ? R.string.add_device : R.string.edit_device);
mIdContainer = findViewById(R.id.idContainer); binding.qrButton.setOnClickListener(this);
mIdView = findViewById(R.id.id); binding.compressionContainer.setOnClickListener(this);
mQrButton = findViewById(R.id.qrButton);
mNameView = findViewById(R.id.name);
mAddressesView = findViewById(R.id.addresses);
mCurrentAddressView = findViewById(R.id.currentAddress);
mCompressionContainer = findViewById(R.id.compressionContainer);
mCompressionValueView = findViewById(R.id.compressionValue);
mIntroducerView = findViewById(R.id.introducer);
mDevicePaused = findViewById(R.id.devicePause);
mSyncthingVersionView = findViewById(R.id.syncthingVersion);
mQrButton.setOnClickListener(this);
mCompressionContainer.setOnClickListener(this);
if (savedInstanceState != null){ if (savedInstanceState != null){
if (mDevice == null) { if (mDevice == null) {
@ -221,9 +189,9 @@ public class DeviceActivity extends SyncthingActivity implements View.OnClickLis
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);
} }
mIdView.removeTextChangedListener(mIdTextWatcher); binding.id.removeTextChangedListener(mIdTextWatcher);
mNameView.removeTextChangedListener(mNameTextWatcher); binding.name.removeTextChangedListener(mNameTextWatcher);
mAddressesView.removeTextChangedListener(mAddressesTextWatcher); binding.addresses.removeTextChangedListener(mAddressesTextWatcher);
} }
@Override @Override
@ -270,12 +238,12 @@ public class DeviceActivity extends SyncthingActivity implements View.OnClickLis
* version/address changes. * version/address changes.
*/ */
private void onReceiveConnections(Connections connections) { private void onReceiveConnections(Connections connections) {
boolean viewsExist = mSyncthingVersionView != null && mCurrentAddressView != null; boolean viewsExist = binding.syncthingVersion != null && binding.currentAddress != null;
if (viewsExist && connections.connections.containsKey(mDevice.deviceID)) { if (viewsExist && connections.connections.containsKey(mDevice.deviceID)) {
mCurrentAddressView.setVisibility(VISIBLE); binding.currentAddress.setVisibility(VISIBLE);
mSyncthingVersionView.setVisibility(VISIBLE); binding.syncthingVersion.setVisibility(VISIBLE);
mCurrentAddressView.setText(connections.connections.get(mDevice.deviceID).address); binding.currentAddress.setText(connections.connections.get(mDevice.deviceID).address);
mSyncthingVersionView.setText(connections.connections.get(mDevice.deviceID).clientVersion); binding.syncthingVersion.setText(connections.connections.get(mDevice.deviceID).clientVersion);
} }
} }
@ -307,26 +275,26 @@ public class DeviceActivity extends SyncthingActivity implements View.OnClickLis
} }
private void updateViewsAndSetListeners() { private void updateViewsAndSetListeners() {
mIdView.removeTextChangedListener(mIdTextWatcher); binding.id.removeTextChangedListener(mIdTextWatcher);
mNameView.removeTextChangedListener(mNameTextWatcher); binding.name.removeTextChangedListener(mNameTextWatcher);
mAddressesView.removeTextChangedListener(mAddressesTextWatcher); binding.addresses.removeTextChangedListener(mAddressesTextWatcher);
mIntroducerView.setOnCheckedChangeListener(null); binding.introducer.setOnCheckedChangeListener(null);
mDevicePaused.setOnCheckedChangeListener(null); binding.devicePause.setOnCheckedChangeListener(null);
// Update views // Update views
mIdView.setText(mDevice.deviceID); binding.id.setText(mDevice.deviceID);
mNameView.setText(mDevice.name); binding.name.setText(mDevice.name);
mAddressesView.setText(displayableAddresses()); binding.addresses.setText(displayableAddresses());
mCompressionValueView.setText(Compression.fromValue(this, mDevice.compression).getTitle(this)); binding.compressionValue.setText(Compression.fromValue(this, mDevice.compression).getTitle(this));
mIntroducerView.setChecked(mDevice.introducer); binding.introducer.setChecked(mDevice.introducer);
mDevicePaused.setChecked(mDevice.paused); binding.devicePause.setChecked(mDevice.paused);
// Keep state updated // Keep state updated
mIdView.addTextChangedListener(mIdTextWatcher); binding.id.addTextChangedListener(mIdTextWatcher);
mNameView.addTextChangedListener(mNameTextWatcher); binding.name.addTextChangedListener(mNameTextWatcher);
mAddressesView.addTextChangedListener(mAddressesTextWatcher); binding.addresses.addTextChangedListener(mAddressesTextWatcher);
mIntroducerView.setOnCheckedChangeListener(mCheckedListener); binding.introducer.setOnCheckedChangeListener(mCheckedListener);
mDevicePaused.setOnCheckedChangeListener(mCheckedListener); binding.devicePause.setOnCheckedChangeListener(mCheckedListener);
} }
@Override @Override
@ -399,7 +367,7 @@ public class DeviceActivity extends SyncthingActivity implements View.OnClickLis
String scanResult = intent.getStringExtra(QRScannerActivity.QR_RESULT_ARG); String scanResult = intent.getStringExtra(QRScannerActivity.QR_RESULT_ARG);
if (scanResult != null) { if (scanResult != null) {
mDevice.deviceID = scanResult; mDevice.deviceID = scanResult;
mIdView.setText(mDevice.deviceID); binding.id.setText(mDevice.deviceID);
} }
} }
} }
@ -419,11 +387,11 @@ public class DeviceActivity extends SyncthingActivity implements View.OnClickLis
getWindow().setSoftInputMode(SOFT_INPUT_STATE_ALWAYS_HIDDEN); getWindow().setSoftInputMode(SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Drawable dr = ContextCompat.getDrawable(this, R.drawable.ic_content_copy_24dp); Drawable dr = ContextCompat.getDrawable(this, R.drawable.ic_content_copy_24dp);
mIdView.setCompoundDrawablesWithIntrinsicBounds(null, null, dr, null); binding.id.setCompoundDrawablesWithIntrinsicBounds(null, null, dr, null);
mIdView.setEnabled(false); binding.id.setEnabled(false);
mQrButton.setVisibility(GONE); binding.qrButton.setVisibility(GONE);
mIdContainer.setOnClickListener(this); binding.idContainer.setOnClickListener(this);
} }
/** /**
@ -450,12 +418,12 @@ public class DeviceActivity extends SyncthingActivity implements View.OnClickLis
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (v.equals(mCompressionContainer)) { if (v.equals(binding.compressionContainer)) {
showCompressionDialog(); showCompressionDialog();
} else if (v.equals(mQrButton)){ } else if (v.equals(binding.qrButton)){
Intent qrIntent = QRScannerActivity.intent(this); Intent qrIntent = QRScannerActivity.intent(this);
startActivityForResult(qrIntent, QR_SCAN_REQUEST_CODE); startActivityForResult(qrIntent, QR_SCAN_REQUEST_CODE);
} else if (v.equals(mIdContainer)) { } else if (v.equals(binding.idContainer)) {
Util.copyDeviceId(this, mDevice.deviceID); Util.copyDeviceId(this, mDevice.deviceID);
} }
} }