diff --git a/WRSD2/app/src/main/java/com/example/wrsd/AddDeviceActivity.java b/WRSD2/app/src/main/java/com/example/wrsd/AddDeviceActivity.java index 916e8da..f4a2e81 100644 --- a/WRSD2/app/src/main/java/com/example/wrsd/AddDeviceActivity.java +++ b/WRSD2/app/src/main/java/com/example/wrsd/AddDeviceActivity.java @@ -2,13 +2,21 @@ package com.example.wrsd; import androidx.appcompat.app.AppCompatActivity; +import android.annotation.SuppressLint; import android.content.Intent; import android.os.Bundle; import android.view.View; +import android.widget.ArrayAdapter; import android.widget.EditText; +import android.widget.Spinner; + +import java.util.List; public class AddDeviceActivity extends AppCompatActivity { + static String[] deviceTypes = new String[]{"Rolety", "Oświetlenie", "Ogrzewanie"}; + + Spinner deviceTypeDropdown; EditText editTextTextName; @Override protected void onCreate(Bundle savedInstanceState) { @@ -16,6 +24,12 @@ public class AddDeviceActivity extends AppCompatActivity { setContentView(R.layout.activity_add_device); editTextTextName = (EditText) findViewById(R.id.editTextTextName); + deviceTypeDropdown = (Spinner) findViewById(R.id.deviceTypeDropdown); + + ArrayAdapter adapter = new ArrayAdapter<>(AddDeviceActivity.this, android.R.layout.simple_spinner_item, deviceTypes); + + adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + deviceTypeDropdown.setAdapter(adapter); } @@ -30,8 +44,22 @@ public class AddDeviceActivity extends AppCompatActivity { public void DeviceName(View view){ String editTextName = editTextTextName.getText().toString().trim(); + + if (editTextName.isEmpty()) { + editTextTextName.setError("Podaj nazwę"); + editTextTextName.requestFocus(); + return; + } + if (editTextTextName.getText().toString().contains(" ")){ + editTextTextName.setError("Nazwa nie może zawierać spacji"); + editTextTextName.requestFocus(); + return; + } + + String deviceType = deviceTypeDropdown.getSelectedItem().toString(); Intent intent = new Intent(); intent.putExtra("name", editTextName); + intent.putExtra("type", deviceType); setResult(RESULT_OK, intent); finish(); } diff --git a/WRSD2/app/src/main/java/com/example/wrsd/BlindsDevice.java b/WRSD2/app/src/main/java/com/example/wrsd/BlindsDevice.java new file mode 100644 index 0000000..15bf996 --- /dev/null +++ b/WRSD2/app/src/main/java/com/example/wrsd/BlindsDevice.java @@ -0,0 +1,25 @@ +package com.example.wrsd; + +public class BlindsDevice extends Device{ + + public boolean state; + + public BlindsDevice(String id, String name) { + super(id, name, DeviceType.BLINDS); + state = false; + } + + public boolean isState() { + return state; + } + + public void setState(boolean state) { + this.state = state; + } + + @Override + public String toString() { + String stateString = state? "1" : "0"; + return deviceType.toString() + name + "?" + stateString; + } +} diff --git a/WRSD2/app/src/main/java/com/example/wrsd/BulbDevice.java b/WRSD2/app/src/main/java/com/example/wrsd/BulbDevice.java new file mode 100644 index 0000000..1a05252 --- /dev/null +++ b/WRSD2/app/src/main/java/com/example/wrsd/BulbDevice.java @@ -0,0 +1,37 @@ +package com.example.wrsd; + +public class BulbDevice extends Device{ + + public boolean state; + + public int brightness; + + public BulbDevice(String id, String name) { + super(id, name, DeviceType.BULB); + this.state = false; + this.brightness = 000; + } + + public boolean isState() { + return state; + } + + public void setState(boolean state) { + this.state = state; + } + + public int getBrightness() { + return brightness; + } + + public void setBrightness(int brightness) { + this.brightness = brightness; + } + + @Override + public String toString() { + String stateString = state? "1" : "0"; + String brightnessString = Integer.toString(brightness); + return deviceType.toString() + name + "?" + stateString + "?" + brightnessString; + } +} diff --git a/WRSD2/app/src/main/java/com/example/wrsd/Device.java b/WRSD2/app/src/main/java/com/example/wrsd/Device.java index 77ad2ef..2a1daf5 100644 --- a/WRSD2/app/src/main/java/com/example/wrsd/Device.java +++ b/WRSD2/app/src/main/java/com/example/wrsd/Device.java @@ -4,10 +4,12 @@ public class Device { String id; String name; + DeviceType deviceType; - public Device(String id, String name) { + public Device(String id, String name, DeviceType deviceType) { this.id = id; this.name = name; + this.deviceType = deviceType; } public String getId() { @@ -25,4 +27,9 @@ public class Device { public void setName(String name) { this.name = name; } + + @Override + public String toString() { + return deviceType.toString() + id; + } } diff --git a/WRSD2/app/src/main/java/com/example/wrsd/DeviceSettings.java b/WRSD2/app/src/main/java/com/example/wrsd/DeviceSettings.java index 1701bef..813ed96 100644 --- a/WRSD2/app/src/main/java/com/example/wrsd/DeviceSettings.java +++ b/WRSD2/app/src/main/java/com/example/wrsd/DeviceSettings.java @@ -4,7 +4,11 @@ import static com.example.wrsd.RoomActivity_1.setWindowFlag; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; +import androidx.core.app.NotificationCompat; +import androidx.core.app.NotificationManagerCompat; +import android.app.NotificationChannel; +import android.app.NotificationManager; import android.content.Intent; import android.graphics.Color; import android.os.Build; @@ -12,6 +16,9 @@ import android.os.Bundle; import android.view.View; import android.view.WindowManager; import android.widget.Button; +import android.widget.CompoundButton; +import android.widget.SeekBar; +import android.widget.Switch; import android.widget.TextView; import android.widget.Toast; @@ -24,7 +31,12 @@ import com.google.firebase.firestore.FirebaseFirestore; public class DeviceSettings extends AppCompatActivity { private Button deleteButton; - private String device, userEmail, room; + private TextView progressLabel, roomText; + private Switch stateSwitch; + private SeekBar progressBar; + private String device, userEmail, type, room, code, state; + private Integer techNotification = 0; + private String onOff, tempTextView; @Override protected void onCreate(Bundle savedInstanceState) { @@ -39,29 +51,223 @@ public class DeviceSettings extends AppCompatActivity { super.onCreate(savedInstanceState); setContentView(R.layout.activity_device_settings); + progressLabel = (TextView) findViewById(R.id.progressLabel); + stateSwitch = (Switch) findViewById(R.id.state); + progressBar = (SeekBar) findViewById(R.id.progress); + roomText = (TextView) findViewById(R.id.textView4); + + stateSwitch.setChecked(true); + + progressLabel.setVisibility(View.INVISIBLE); + stateSwitch.setVisibility(View.INVISIBLE); + progressBar.setVisibility(View.INVISIBLE); + Bundle extras = getIntent().getExtras(); if (extras != null) { device = extras.getString("device"); userEmail = extras.getString("email"); room = extras.getString("room"); + type = extras.getString("type"); + code = extras.getString("code"); +// state = extras.getString("state"); + roomText.setText(room); //The key argument here must match that used in the other activity + } +// if(state.equals("1")){ +// stateSwitch.setChecked(true); +// }else{ +// stateSwitch.setChecked(false); +// } + deleteButton = (Button) findViewById(R.id.deleteButton); FirebaseFirestore rootRef = FirebaseFirestore.getInstance(); + stateSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + if(isChecked==true){ + onOff = "włączone"; + }else{ + onOff = "wyłączone"; + } + } + }); + + if (type.equals("Blinds")) { + if(onOff == "włączone"){ + stateSwitch.setText("Opuść rolety"); + + }else{ + stateSwitch.setText("Podnieś rolety"); + } + stateSwitch.setVisibility(View.VISIBLE); + } else if (type.equals("Bulb")) { + if(onOff == "włączone"){ + stateSwitch.setText("Wyłącz oświetlenie"); + + }else{ + stateSwitch.setText("Włącz oświetlenie"); + } + progressLabel.setText("Jasność"); + tempTextView = "Jasność"; + stateSwitch.setVisibility(View.VISIBLE); + progressLabel.setVisibility(View.VISIBLE); + progressBar.setVisibility(View.VISIBLE); + } else if (type.equals("Heater")) { + if(onOff == "włączone"){ + stateSwitch.setText("Wyłącz ogrzewanie"); + + }else{ + stateSwitch.setText("Włącz ogrzewanie"); + } + tempTextView = "Temperatura"; + progressLabel.setText("Temperatura"); + stateSwitch.setVisibility(View.VISIBLE); + progressLabel.setVisibility(View.VISIBLE); + progressBar.setVisibility(View.VISIBLE); + } + deleteButton.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ DocumentReference docRef = rootRef.collection("users").document(userEmail).collection("rooms").document(room); - docRef.update("Urzadzenia", FieldValue.arrayRemove(device)); + docRef.update("Urzadzenia", FieldValue.arrayRemove(code)); + Toast.makeText(getApplicationContext(), "Usunięto " + device, Toast.LENGTH_LONG).show(); + + Intent intent = new Intent(); + intent.putExtra("name", "!"); + intent.putExtra("type", "!"); + setResult(RESULT_OK, intent); finish(); } }); + + progressBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { + int progress = progressBar.getProgress(); + + boolean started = false; //use this variable to see whether the user clicked the right place + + + @Override + public void onProgressChanged(SeekBar seekBar, int progressValue, boolean fromUser) { + if(!started){ //check to see if user clicks the right place + //if the user clicks within a specific threshold + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){ + NotificationChannel channel = new NotificationChannel("Moje powiadomienie", "Moje powiadomienie", NotificationManager.IMPORTANCE_DEFAULT ); + NotificationManager manager = getSystemService(NotificationManager.class); + manager.createNotificationChannel(channel); + + } + + NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), "Moje powiadomienie"); + builder.setContentTitle("WRSD Home"); + builder.setContentText(tempTextView + " w urządzeniu " + device + " w pokoju " + room + " została zmieniona."); + builder.setStyle( new + NotificationCompat.BigTextStyle().bigText(tempTextView + " w urządzeniu " + device + " w pokoju: " + room + " została zmieniona.")); + builder.setSmallIcon(R.drawable.wrsdlogo); + builder.setAutoCancel(false); + + NotificationManagerCompat managerCompat = NotificationManagerCompat.from(getApplicationContext()); + managerCompat.notify(techNotification,builder.build()); + techNotification++; + } + + if(started) { + + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){ + NotificationChannel channel = new NotificationChannel("Moje powiadomienie", "Moje powiadomienie", NotificationManager.IMPORTANCE_DEFAULT ); + NotificationManager manager = getSystemService(NotificationManager.class); + manager.createNotificationChannel(channel); + + } + + NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), "Moje powiadomienie"); + builder.setContentTitle("WRSD Home"); + builder.setContentText(tempTextView + " w urządzeniu " + device + " w pokoju: " + room + " została zmieniona."); + builder.setStyle( new + NotificationCompat.BigTextStyle().bigText(tempTextView + " w urządzeniu " + device + " w pokoju: " + room + " została zmieniona.")); + builder.setSmallIcon(R.drawable.wrsdlogo); + builder.setAutoCancel(false); + + NotificationManagerCompat managerCompat = NotificationManagerCompat.from(getApplicationContext()); + managerCompat.notify(techNotification,builder.build()); + techNotification++; + + } + } + + @Override + public void onStartTrackingTouch(SeekBar seekBar) { + System.out.println("onStartTracking:" + progress + "/" + seekBar.getMax()); + } + + @Override + public void onStopTrackingTouch(SeekBar seekBar) { + System.out.println("onStopTracking:" + progress + "/" + seekBar.getMax()); + //DO WHATEVER YOU NEED TO DO WHEN PROGRESS IS DONE CHANGING + + started = false; //remember to set variable to false + } + + }); + + + } + + + + public void Notification(View view){ + if (type.equals("Bulb")) { + if (onOff == "włączone") { + stateSwitch.setText("Wyłącz oświetlenie"); + + } else { + stateSwitch.setText("Włącz oświetlenie"); + } + }else if (type.equals("Heater")) { + if (onOff == "włączone") { + stateSwitch.setText("Wyłącz ogrzewanie"); + + } else { + stateSwitch.setText("Włącz ogrzewanie"); + } + }else if (type.equals("Blinds")) { + if (onOff == "włączone") { + stateSwitch.setText("Opuść rolety"); + + } else { + stateSwitch.setText("Podnieś rolety"); + } + } + + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){ + NotificationChannel channel = new NotificationChannel("Moje powiadomienie", "Moje powiadomienie", NotificationManager.IMPORTANCE_DEFAULT ); + NotificationManager manager = getSystemService(NotificationManager.class); + manager.createNotificationChannel(channel); + + } + + NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), "Moje powiadomienie"); + builder.setContentTitle("WRSD Home"); + builder.setContentText("Urządzenie: " + device + " w pokoju: " + room + " zostało " + onOff); + builder.setStyle( new + NotificationCompat.BigTextStyle().bigText("Urządzenie: " + device + " w pokoju: " + room + " zostało " + onOff)); + builder.setSmallIcon(R.drawable.wrsdlogo); + builder.setAutoCancel(false); + + NotificationManagerCompat managerCompat = NotificationManagerCompat.from(getApplicationContext()); + managerCompat.notify(techNotification,builder.build()); + techNotification++; + + } + + public void GetSeekBarValue(){ + } } \ No newline at end of file diff --git a/WRSD2/app/src/main/java/com/example/wrsd/DeviceType.java b/WRSD2/app/src/main/java/com/example/wrsd/DeviceType.java new file mode 100644 index 0000000..6f41683 --- /dev/null +++ b/WRSD2/app/src/main/java/com/example/wrsd/DeviceType.java @@ -0,0 +1,18 @@ +package com.example.wrsd; + +public enum DeviceType { + BLINDS("b"), + BULB("l"), + HEATER("h"); + + private String shortCut; + + DeviceType(String shortcut) { + this.shortCut = shortcut; + } + + @Override + public String toString() { + return this.shortCut; + } +} diff --git a/WRSD2/app/src/main/java/com/example/wrsd/HeaterDevice.java b/WRSD2/app/src/main/java/com/example/wrsd/HeaterDevice.java new file mode 100644 index 0000000..d3a75e5 --- /dev/null +++ b/WRSD2/app/src/main/java/com/example/wrsd/HeaterDevice.java @@ -0,0 +1,37 @@ +package com.example.wrsd; + +public class HeaterDevice extends Device { + + public boolean state; + + public int temperature; + + public HeaterDevice(String id, String name) { + super(id, name, DeviceType.HEATER); + this.state = false; + this.temperature = 000; + } + + public boolean isState() { + return state; + } + + public void setState(boolean state) { + this.state = state; + } + + public int getTemperature() { + return temperature; + } + + public void setTemperature(int temperature) { + this.temperature = temperature; + } + + @Override + public String toString() { + String stateString = state? "1" : "0"; + String temperatureString = Integer.toString(temperature); + return deviceType.toString() + name + "?" + stateString + "?" + temperatureString; + } +} diff --git a/WRSD2/app/src/main/java/com/example/wrsd/RegisterUser.java b/WRSD2/app/src/main/java/com/example/wrsd/RegisterUser.java index 18606e7..56083de 100644 --- a/WRSD2/app/src/main/java/com/example/wrsd/RegisterUser.java +++ b/WRSD2/app/src/main/java/com/example/wrsd/RegisterUser.java @@ -28,8 +28,14 @@ import com.google.firebase.auth.AuthResult; import com.google.firebase.auth.FirebaseAuth; import com.google.firebase.auth.FirebaseUser; import com.google.firebase.database.FirebaseDatabase; +import com.google.firebase.firestore.FirebaseFirestore; import java.io.FileInputStream; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.regex.Pattern; import kotlin.text.Typography; @@ -116,6 +122,7 @@ public class RegisterUser extends AppCompatActivity{ mAuth.createUserWithEmailAndPassword(email, password).addOnSuccessListener(new OnSuccessListener() { @Override public void onSuccess(AuthResult authResult) { + Rooms(); Toast.makeText(getApplicationContext(), "Użytkownik zarejestrowany", Toast.LENGTH_LONG).show(); Login(); @@ -128,7 +135,25 @@ public class RegisterUser extends AppCompatActivity{ } }); + } + + public void Rooms() { + String email = editTextEmail.getText().toString().trim(); + FirebaseFirestore rootRef = FirebaseFirestore.getInstance(); + Map room = new HashMap<>(); + List rooms = new ArrayList<>(); + rooms.add("Salon"); + rooms.add("Sypialnia"); + rooms.add("Kuchnia"); + rooms.add("Lazienka"); + rooms.add("Toaleta"); + Map docData = new HashMap<>(); + docData.put("Urzadzenia", Arrays.asList()); + + for (String r1 : rooms) { + rootRef.collection("users").document(email).collection("rooms").document(r1).set(docData); } + } public void Login() { Intent intent = new Intent(this, LoginActivity.class); diff --git a/WRSD2/app/src/main/java/com/example/wrsd/RoomActivity_1.java b/WRSD2/app/src/main/java/com/example/wrsd/RoomActivity_1.java index 4f60696..2934a21 100644 --- a/WRSD2/app/src/main/java/com/example/wrsd/RoomActivity_1.java +++ b/WRSD2/app/src/main/java/com/example/wrsd/RoomActivity_1.java @@ -11,34 +11,23 @@ import android.content.Intent; import android.graphics.Color; import android.os.Build; import android.os.Bundle; -import android.util.Log; import android.view.View; import android.view.Window; import android.view.WindowManager; -import android.widget.Button; -import android.widget.EditText; -import android.widget.LinearLayout; +import android.widget.Adapter; +import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; -import com.google.android.gms.tasks.OnCompleteListener; +import com.example.wrsd.databinding.ActivityDeviceSettingsBinding; import com.google.android.gms.tasks.OnFailureListener; import com.google.android.gms.tasks.OnSuccessListener; -import com.google.android.gms.tasks.Task; -import com.google.firebase.firestore.CollectionReference; import com.google.firebase.firestore.DocumentReference; import com.google.firebase.firestore.DocumentSnapshot; import com.google.firebase.firestore.FieldValue; import com.google.firebase.firestore.FirebaseFirestore; -import com.google.firebase.firestore.Query; -import com.google.firebase.firestore.QueryDocumentSnapshot; -import com.google.firebase.firestore.QuerySnapshot; - -import org.w3c.dom.Text; import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.regex.Pattern; @@ -50,12 +39,12 @@ public class RoomActivity_1 extends AppCompatActivity { public List temp = new ArrayList<>(); private RecyclerView recyclerView; private SingleRoomAdapter mAdapter; - private String userEmail; - Integer roomIndex; - public String roomvalue; + private String userEmail, code; + public String roomvalue, deviceState; private TextView roomName1; + public Integer la; TextView myTextView; - private TextView deviceName; + ImageView ivDevice; public static void setWindowFlag(Activity activity, final int bits, boolean on) { @@ -102,6 +91,7 @@ public class RoomActivity_1 extends AppCompatActivity { recyclerView.setItemAnimator(new DefaultItemAnimator()); recyclerView.setAdapter(mAdapter); + roomName1 = (TextView) findViewById(R.id.textView); roomName1.setText(roomvalue); @@ -113,6 +103,7 @@ public class RoomActivity_1 extends AppCompatActivity { private void getData() { deviceFromBase.clear(); + deviceList.clear(); FirebaseFirestore rootRef = FirebaseFirestore.getInstance(); rootRef.collection("users").document(userEmail).collection("rooms").document(roomvalue) @@ -121,30 +112,21 @@ public class RoomActivity_1 extends AppCompatActivity { @Override public void onSuccess(DocumentSnapshot documentSnapshot) { if (documentSnapshot!=null) { - temp.add(documentSnapshot.getData().toString()); - if(!temp.get(0).equals("{Urzadzenia=[]}")) { + Map data = documentSnapshot.getData(); + if (data != null) { + temp.add(data.toString()); + if (!temp.get(0).equals("{Urzadzenia=[]}")) { String[] parts = temp.get(0).split(Pattern.quote("[")); String[] p2 = parts[1].split(Pattern.quote("]")); String[] devices = p2[0].split("(,)|(\\s+)"); - for (Integer i = 0; i < devices.length; i = i + 2) { - deviceFromBase.add(devices[i]); - } - Device d1; - Integer j = 0; - - for (String d : deviceFromBase) { - String jStr = Integer.toString(j); - d1 = new Device(jStr, d); - deviceList.add(d1); - } + decodeDevices(devices); mAdapter.notifyDataSetChanged(); - } - else{ + } else { Toast.makeText(getApplicationContext(), "Brak urządzeń w pokoju", Toast.LENGTH_LONG).show(); } - + } } else { - Toast.makeText(getApplicationContext(), "Brak urządzeń w pokoju", Toast.LENGTH_LONG).show(); + Toast.makeText(getApplicationContext(), "Błąd", Toast.LENGTH_LONG).show(); } } }).addOnFailureListener(new OnFailureListener() { @@ -157,9 +139,36 @@ public class RoomActivity_1 extends AppCompatActivity { }); } + private void decodeDevices(String[] devicesNames) { + for (String deviceName : devicesNames) { + decodeDevice(deviceName); + } + } + private void decodeDevice(String encodedDevice) { + if (encodedDevice.equals("")) { + return; + } + String[] parts = encodedDevice.split("\\?"); + String deviceType = parts[0].substring(0,1); + String deviceName = parts[0].substring(1); + deviceState = parts[1]; + String deviceId = getLastUsedId(); + if (deviceType.equals(DeviceType.BLINDS.toString())) { + deviceList.add(new BlindsDevice(deviceId, deviceName)); + } else if (deviceType.equals(DeviceType.BULB.toString())) { + deviceList.add(new BulbDevice(deviceId, deviceName)); + } else if (deviceType.equals(DeviceType.HEATER.toString())) { + deviceList.add(new HeaterDevice(deviceId, deviceName)); + } + } + private String getLastUsedId() { + String lastIdString = deviceList.isEmpty()? "0" : deviceList.get(deviceList.size() - 1).id; + int incrementedId = Integer.parseInt(lastIdString) + 1; + return Integer.toString(incrementedId); + } // CollectionReference usersCol = rootRef.collection("users"); // Query query = usersCol.whereEqualTo("email", userEmail).whereEqualTo("rooms", roomvalue); @@ -183,59 +192,59 @@ public class RoomActivity_1 extends AppCompatActivity { // } - - - -// private void prepareRoomData() { -// getData(); -// -// Device d1; -// Integer j = 0; -// -// for(String d: deviceFromBase){ -// String jStr = Integer.toString(j); -// d1 = new Device(jStr, d); -// deviceList.add(d1); -// } - -// d1 = new Device("1", "Oświetlenie"); -// deviceList.add(d1); -// d1 = new Device("2", "Głośniki"); -// deviceList.add(d1); -// d1 = new Device("3", "Rolety"); -// deviceList.add(d1); -// d1 = new Device("4", "Ogrzewanie"); -// deviceList.add(d1); -// d1 = new Device("5", "Odkurzacz"); -// deviceList.add(d1); - -// mAdapter.notifyDataSetChanged(); +// public void onBackClicked(View view) { +// startActivity(new Intent(getApplicationContext(), MainActivity.class)); +// finish(); // } - public void onBackClicked(View view) { - startActivity(new Intent(getApplicationContext(), MainActivity.class)); - finish(); - } - public void addDevice(View view){ Intent i = new Intent(this, AddDeviceActivity.class); startActivityForResult(i, 1); } + public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1) { if(resultCode == RESULT_OK) { FirebaseFirestore rootRef = FirebaseFirestore.getInstance(); String strEditText = data.getStringExtra("name"); - Device d1 = new Device("6", strEditText); - deviceList.add(d1); + String deviceType = data.getStringExtra("type"); + if(!deviceType.equals("!")){ + Device newDevice; + String deviceId = getLastUsedId(); + + if (deviceType.equals("Rolety")) { + newDevice = new BlindsDevice(deviceId, strEditText); + } else if (deviceType.equals("Oświetlenie")) { + newDevice = new BulbDevice(deviceId, strEditText); + } else if (deviceType.equals("Ogrzewanie")) { + newDevice = new HeaterDevice(deviceId, strEditText); + } else { + newDevice = new BlindsDevice(deviceId, strEditText); + } + deviceList.add(newDevice); + + mAdapter.notifyDataSetChanged(); + + DocumentReference docRef = rootRef.collection("users").document(userEmail).collection("rooms").document(roomvalue); + docRef.update("Urzadzenia", FieldValue.arrayUnion(newDevice.toString())); + Toast.makeText(getApplicationContext(), "Dodano " + strEditText, Toast.LENGTH_LONG).show(); + + }else{ + for(int j=0; j @@ -18,39 +19,65 @@ android:minHeight="48dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" - app:layout_constraintHorizontal_bias="0.129" + app:layout_constraintHorizontal_bias="0.233" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" - app:layout_constraintVertical_bias="0.311" /> + app:layout_constraintVertical_bias="0.439" /> + app:layout_constraintVertical_bias="0.176" /> + +