Skip to content

Commit

Permalink
Replace the separate service control buttons with a single toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
buttercookie42 committed Dec 15, 2024
1 parent 39285cf commit db8fca3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 28 deletions.
45 changes: 28 additions & 17 deletions app/src/main/java/de/buttercookie/simbadroid/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.google.android.material.button.MaterialButton;

import org.apache.commons.lang3.StringUtils;

import de.buttercookie.simbadroid.databinding.ActivityMainBinding;
Expand All @@ -30,7 +32,7 @@
import de.buttercookie.simbadroid.service.SmbServiceConnection;
import de.buttercookie.simbadroid.service.SmbServiceStatusLiveData;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;

private SmbService mService;
Expand All @@ -57,8 +59,7 @@ protected void onCreate(Bundle savedInstanceState) {

binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
binding.startService.setOnClickListener(this);
binding.stopService.setOnClickListener(this);
binding.toggleService.setOnClickListener(v -> toggleSmbService());

ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
Expand All @@ -67,6 +68,7 @@ protected void onCreate(Bundle savedInstanceState) {
});

SmbServiceStatusLiveData.get().observe(this, status -> {
updateButtonState(status);
if (!status.serviceRunning()) {
binding.serviceStatus.setText(R.string.status_server_off);
} else if (!status.serverRunning()) {
Expand Down Expand Up @@ -100,14 +102,34 @@ protected void onStop() {
mBound = false;
}

private void startSmbService() {
private void updateButtonState(SmbService.Status status) {
final MaterialButton button = binding.toggleService;

if (status.serviceRunning()) {
button.setText(R.string.button_stop_server);
button.setIcon(AppCompatResources.getDrawable(this, R.drawable.ic_stop));
} else {
button.setText(R.string.button_start_server);
button.setIcon(AppCompatResources.getDrawable(this, R.drawable.ic_start));
}
}

private void toggleSmbService() {
if (!mBound) {
Toast.makeText(this,
R.string.toast_error_starting_server,
Toast.LENGTH_SHORT).show();
return;
}

if (!mService.isRunning()) {
startSmbService();
} else {
stopSmbService();
}
}

private void startSmbService() {
if (!mService.isWifiAvailable()) {
Toast.makeText(this,
R.string.toast_error_no_wifi,
Expand All @@ -119,18 +141,7 @@ private void startSmbService() {
}

private void stopSmbService() {
if (mBound) {
mService.stop();
}
}

@Override
public void onClick(View v) {
if (v == binding.startService) {
startSmbService();
} else if (v == binding.stopService) {
stopSmbService();
}
mService.stop();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ public void onDestroy() {
super.onDestroy();
}

public boolean isRunning() {
return mRunning;
}

public boolean isWifiAvailable() {
return mLinkAddress != null;
}
Expand Down
13 changes: 2 additions & 11 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,14 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<Button
android:id="@+id/start_service"
<com.google.android.material.button.MaterialButton
android:id="@+id/toggle_service"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_start_server"
app:icon="@drawable/ic_start"
app:iconPadding="6dp"
app:iconSize="24dp" />

<Button
android:id="@+id/stop_service"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_stop_server"
app:icon="@drawable/ic_stop"
app:iconPadding="6dp"
app:iconSize="24dp" />
</LinearLayout>

<TextView
Expand Down

0 comments on commit db8fca3

Please sign in to comment.