From 8e22882faa438226a38345926a601976eec3883d Mon Sep 17 00:00:00 2001 From: isra el Date: Sun, 27 Mar 2022 12:35:36 +0300 Subject: [PATCH] enable/disable gateway service via switch done --- .../vernu/sms/activities/MainActivity.java | 58 ++++++++++++++++--- 1 file changed, 51 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/vernu/sms/activities/MainActivity.java b/app/src/main/java/com/vernu/sms/activities/MainActivity.java index c5d3974..df3beb3 100644 --- a/app/src/main/java/com/vernu/sms/activities/MainActivity.java +++ b/app/src/main/java/com/vernu/sms/activities/MainActivity.java @@ -13,6 +13,7 @@ import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; +import android.widget.CompoundButton; import android.widget.EditText; import android.widget.Switch; import android.widget.Toast; @@ -36,6 +37,8 @@ import retrofit2.converter.gson.GsonConverterFactory; public class MainActivity extends AppCompatActivity { private Context mContext; + private Retrofit retrofit; + private GatewayApiService gatewayApiService; private Switch gatewaySwitch; private EditText gatewayKeyEditText, fcmTokenEditText; @@ -48,6 +51,11 @@ public class MainActivity extends AppCompatActivity { super.onCreate(savedInstanceState); mContext = getApplicationContext(); + retrofit = new Retrofit.Builder() + .baseUrl("https://vernu-sms.herokuapp.com/api/v1/") + .addConverterFactory(GsonConverterFactory.create()) + .build(); + gatewayApiService = retrofit.create(GatewayApiService.class); setContentView(R.layout.activity_main); gatewaySwitch = findViewById(R.id.gatewaySwitch); @@ -68,8 +76,50 @@ public class MainActivity extends AppCompatActivity { }); } + gatewayKeyEditText.setText(SharedPreferenceHelper.getSharedPreferenceString(mContext, "GATEWAY_KEY", "")); + gatewaySwitch.setChecked(SharedPreferenceHelper.getSharedPreferenceBoolean(mContext, "GATEWAY_ENABLED", false)); + gatewaySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton compoundButton, boolean isCheked) { + View view = compoundButton.getRootView(); + compoundButton.setEnabled(false); + String key = gatewayKeyEditText.getText().toString(); + + + UpdateDeviceInputDTO updateDeviceInput = new UpdateDeviceInputDTO(); + updateDeviceInput.setEnabled(isCheked); + + Call apiCall = gatewayApiService.updateFCMToken(key, updateDeviceInput); + apiCall.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + + if (response.isSuccessful()) { + SharedPreferenceHelper.setSharedPreferenceBoolean(mContext, "GATEWAY_ENABLED", isCheked); + Snackbar.make(view, "DONE :)", Snackbar.LENGTH_LONG).show(); + + } else { + Snackbar.make(view, response.message(), Snackbar.LENGTH_LONG).show(); + } + + compoundButton.setEnabled(true); + } + + @Override + public void onFailure(Call call, Throwable t) { + Snackbar.make(view, "An error occured :(", Snackbar.LENGTH_LONG).show(); + Log.d("ERR", t.toString()); + compoundButton.setEnabled(true); + + } + }); + + + } + }); + updateKeyButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { @@ -89,12 +139,6 @@ public class MainActivity extends AppCompatActivity { } String token = task.getResult(); fcmTokenEditText.setText(token); - Retrofit retrofit = new Retrofit.Builder() - .baseUrl("https://vernu-sms.herokuapp.com/api/v1/") - .addConverterFactory(GsonConverterFactory.create()) - .build(); - - GatewayApiService service = retrofit.create(GatewayApiService.class); UpdateDeviceInputDTO updateDeviceInput = new UpdateDeviceInputDTO(); @@ -106,7 +150,7 @@ public class MainActivity extends AppCompatActivity { updateDeviceInput.setOs(Build.VERSION.BASE_OS); - Call apiCall = service.updateFCMToken(newKey, updateDeviceInput); + Call apiCall = gatewayApiService.updateFCMToken(newKey, updateDeviceInput); apiCall.enqueue(new Callback() { @Override public void onResponse(Call call, Response response) {