오윤석

setting page api 연결

......@@ -23,6 +23,7 @@
</intent-filter>
</activity>
<activity android:name=".ui.MainActivity" />
<activity android:name=".ui.SettingActivity" />
</application>
</manifest>
\ No newline at end of file
......
......@@ -126,4 +126,36 @@ public class Api {
}
});
}
static public void getSetting(final Callback callback) {
callApi("GET", "/api/setting", null, new Callback() {
@Override
public void callbackMethod(Object obj) {
ApiResult apiResult = (ApiResult) obj;
if(apiResult.isSuccess()) {
JsonObject resp = (JsonObject) apiResult.getData();
callback.callbackMethod(new Setting(resp.get("recording").getAsBoolean()));
} else {
callback.callbackMethod(null);
}
}
});
}
static public void setSetting(Setting setting, final Callback callback) {
JsonObject params = new JsonObject();
params.addProperty("recording", setting.getRecording());
callApi("PUT", "/api/setting", params, new Callback() {
@Override
public void callbackMethod(Object obj) {
ApiResult apiResult = (ApiResult) obj;
if(apiResult.isSuccess()) {
callback.callbackMethod(true);
} else {
callback.callbackMethod(null);
}
}
});
}
}
......
package com.sunnni.smartdoorlock.api;
public class Setting {
private boolean recording;
public Setting(boolean recording) {
this.recording = recording;
}
public boolean getRecording() {
return recording;
}
}
......@@ -2,15 +2,26 @@ package com.sunnni.smartdoorlock.ui;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.sunnni.smartdoorlock.R;
public class MainActivity extends AppCompatActivity {
private Button btnSetting;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSetting = (Button) findViewById(R.id.btn_setting);
btnSetting.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, SettingActivity.class));
}
});
}
}
......
package com.sunnni.smartdoorlock.ui;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;
import com.sunnni.smartdoorlock.R;
import com.sunnni.smartdoorlock.api.Api;
import com.sunnni.smartdoorlock.api.Setting;
import androidx.appcompat.app.AppCompatActivity;
public class SettingActivity extends AppCompatActivity {
private Switch swcRecording;
private Button btnLogout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
btnLogout = (Button) findViewById(R.id.btn_logout);
btnLogout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences pref = getSharedPreferences("gateway", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.remove("accessToken");
editor.commit();
startActivity(new Intent(SettingActivity.this, SplashActivity.class));
}
});
swcRecording = (Switch) findViewById(R.id.swc_recording);
swcRecording.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
Api.setSetting(new Setting(true), new Api.Callback() {
@Override
public void callbackMethod(Object obj) {
if(obj == null) {
Toast.makeText(getApplicationContext(),"연결 상태가 불안정합니다.",Toast.LENGTH_SHORT).show();
}
}
});
}
});
Api.getSetting(new Api.Callback() {
@Override
public void callbackMethod(Object obj) {
Setting setting = (Setting) obj;
if(setting == null) {
Toast.makeText(getApplicationContext(),"연결 상태가 불안정합니다.",Toast.LENGTH_SHORT).show();
return;
}
swcRecording.setChecked(setting.getRecording());
}
});
}
}
......@@ -15,4 +15,12 @@
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_setting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="btn_setting"
tools:layout_editor_absoluteX="166dp"
tools:layout_editor_absoluteY="399dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
......
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn_logout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginTop="9dp"
android:text="btn_logout"
app:layout_constraintStart_toStartOf="@+id/swc_recording"
app:layout_constraintTop_toBottomOf="@+id/swc_recording" />
<Switch
android:id="@+id/swc_recording"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="117dp"
android:layout_marginTop="151dp"
android:text="swc_recording"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file