Skip to content

Commit

Permalink
Use view binding in more activities (#2052)
Browse files Browse the repository at this point in the history
# Description
Use view binding in more activities:
* QRScannerActivity
* ShareActivity
* PullOrderDialogActivity

Rename also a couple of xml layout files from `fragment_xyz` to
`activity_xyz` as they are being used inside activities.
  • Loading branch information
adamszewe authored Feb 10, 2024
1 parent 03f7b8c commit d415716
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;
import com.nutomic.syncthingandroid.R;

import com.nutomic.syncthingandroid.databinding.ActivityPullorderDialogBinding;
import java.util.Arrays;
import java.util.List;

Expand All @@ -19,6 +16,8 @@ public class PullOrderDialogActivity extends ThemedAppCompatActivity {

private String selectedType;

private ActivityPullorderDialogBinding binding;

private static final List<String> mTypes = Arrays.asList(
"random",
"alphabetic",
Expand All @@ -31,7 +30,8 @@ public class PullOrderDialogActivity extends ThemedAppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_pullorder_dialog);
binding = ActivityPullorderDialogBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
if (savedInstanceState == null) {
selectedType = getIntent().getStringExtra(EXTRA_PULL_ORDER);
}
Expand All @@ -40,8 +40,7 @@ protected void onCreate(Bundle savedInstanceState) {
}

private void initiateFinishBtn() {
Button finishBtn = findViewById(R.id.finish_btn);
finishBtn.setOnClickListener(v -> {
binding.finishBtn.setOnClickListener(v -> {
saveConfiguration();
finish();
});
Expand All @@ -54,9 +53,8 @@ private void saveConfiguration() {
}

private void initiateSpinner() {
Spinner pullOrderTypeSpinner = findViewById(R.id.pullOrderTypeSpinner);
pullOrderTypeSpinner.setSelection(mTypes.indexOf(selectedType));
pullOrderTypeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
binding.pullOrderTypeSpinner.setSelection(mTypes.indexOf(selectedType));
binding.pullOrderTypeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position != mTypes.indexOf(selectedType)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,15 @@
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.google.zxing.ResultPoint;
import com.journeyapps.barcodescanner.BarcodeCallback;
import com.journeyapps.barcodescanner.BarcodeResult;
import com.journeyapps.barcodescanner.DecoratedBarcodeView;
import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.databinding.ActivityQrScannerBinding;

import java.util.List;

Expand All @@ -33,17 +29,16 @@ static Intent intent(Context context) {

private final int RC_HANDLE_CAMERA_PERM = 888;

private DecoratedBarcodeView barcodeView;
private ActivityQrScannerBinding binding;

// region === Activity Lifecycle ===
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_qr_scanner);

this.barcodeView = findViewById(R.id.bar_code_scanner_view);
binding = ActivityQrScannerBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());

findViewById(R.id.cancel_button).setOnClickListener(view -> {
binding.cancelButton.setOnClickListener(view -> {
finishScanning();
});

Expand Down Expand Up @@ -98,12 +93,12 @@ private void checkPermissionAndStartScanner() {
}

private void startScanner() {
this.barcodeView.resume();
this.barcodeView.decodeSingle(this);
binding.barCodeScannerView.resume();
binding.barCodeScannerView.decodeSingle(this);
}

private void finishScanning() {
this.barcodeView.pause();
binding.barCodeScannerView.pause();
finish();
}
// endregion
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.nutomic.syncthingandroid.activities;

import static android.view.WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN;

import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.Intent;
Expand All @@ -15,14 +17,13 @@
import android.webkit.MimeTypeMap;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.google.common.io.Files;
import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.databinding.ActivityShareBinding;
import com.nutomic.syncthingandroid.model.Folder;
import com.nutomic.syncthingandroid.service.SyncthingService;
import com.nutomic.syncthingandroid.util.Util;
Expand All @@ -39,8 +40,6 @@
import java.util.List;
import java.util.Map;

import static android.view.WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN;

/**
* Shares incoming files to syncthing folders.
* <p>
Expand All @@ -59,6 +58,8 @@ public class ShareActivity extends StateDialogActivity

private Spinner mFoldersSpinner;

private ActivityShareBinding binding;

@Override
public void onServiceStateChange(SyncthingService.State currentState) {
if (currentState != SyncthingService.State.ACTIVE || getApi() == null)
Expand All @@ -81,9 +82,8 @@ public void onServiceStateChange(SyncthingService.State currentState) {
this, android.R.layout.simple_spinner_item, folders);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner sItems = findViewById(R.id.folders);
sItems.setAdapter(adapter);
sItems.setSelection(folderIndex);
binding.folders.setAdapter(adapter);
binding.folders.setSelection(folderIndex);
}

@Override
Expand All @@ -102,18 +102,13 @@ protected void onPostCreate(Bundle savedInstanceState) {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_share);
binding = ActivityShareBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());

getWindow().setSoftInputMode(SOFT_INPUT_STATE_ALWAYS_HIDDEN);

registerOnServiceConnectedListener(this);

Button mShareButton = findViewById(R.id.share_button);
Button mCancelButton = findViewById(R.id.cancel_button);
Button browseButton = findViewById(R.id.browse_button);
EditText mShareName = findViewById(R.id.name);
TextView mShareTitle = findViewById(R.id.namesTitle);

mSubDirectoryTextView = findViewById(R.id.sub_directory_Textview);
mFoldersSpinner = findViewById(R.id.folders);

Expand Down Expand Up @@ -143,16 +138,16 @@ protected void onCreate(Bundle savedInstanceState) {
files.put(sourceUri, displayName);
}

mShareName.setText(TextUtils.join("\n", files.values()));
binding.name.setText(TextUtils.join("\n", files.values()));
if (files.size() > 1) {
mShareName.setFocusable(false);
mShareName.setKeyListener(null);
binding.name.setFocusable(false);
binding.name.setKeyListener(null);
}
mShareTitle.setText(getResources().getQuantityString(R.plurals.file_name_title,
binding.namesTitle.setText(getResources().getQuantityString(R.plurals.file_name_title,
files.size() > 1 ? 2 : 1));
mShareButton.setOnClickListener(view -> {
binding.shareButton.setOnClickListener(view -> {
if (files.size() == 1)
files.entrySet().iterator().next().setValue(mShareName.getText().toString());
files.entrySet().iterator().next().setValue(binding.name.getText().toString());
Folder folder = (Folder) mFoldersSpinner.getSelectedItem();
File directory = new File(folder.path, getSavedSubDirectory());
CopyFilesTask mCopyFilesTask = new CopyFilesTask(this, files, folder, directory);
Expand All @@ -171,15 +166,15 @@ public void onNothingSelected(AdapterView<?> parent) {
}
});

browseButton.setOnClickListener(view -> {
binding.browseButton.setOnClickListener(view -> {
Folder folder = (Folder) mFoldersSpinner.getSelectedItem();
File initialDirectory = new File(folder.path, getSavedSubDirectory());
startActivityForResult(FolderPickerActivity.createIntent(getApplicationContext(),
initialDirectory.getAbsolutePath(), folder.path),
FolderPickerActivity.DIRECTORY_REQUEST_CODE);
});

mCancelButton.setOnClickListener(view -> finish());
binding.cancelButton.setOnClickListener(view -> finish());
mSubDirectoryTextView.setText(getSavedSubDirectory());
}

Expand Down

0 comments on commit d415716

Please sign in to comment.