Skip to content

Commit

Permalink
#695 Adding thermostat mode
Browse files Browse the repository at this point in the history
  • Loading branch information
galadril committed Jun 27, 2023
1 parent 31f3d59 commit ea92501
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 164 deletions.
83 changes: 67 additions & 16 deletions app/src/main/java/nl/hnogames/domoticz/adapters/UtilityAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Filter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;
Expand Down Expand Up @@ -59,6 +62,7 @@
import nl.hnogames.domoticz.helpers.ItemMoveAdapter;
import nl.hnogames.domoticz.interfaces.UtilityClickListener;
import nl.hnogames.domoticz.utils.SharedPrefUtil;
import nl.hnogames.domoticzapi.Containers.DevicesInfo;
import nl.hnogames.domoticzapi.Containers.UtilitiesInfo;
import nl.hnogames.domoticzapi.Domoticz;
import nl.hnogames.domoticzapi.DomoticzIcons;
Expand Down Expand Up @@ -108,14 +112,7 @@ public void setData(ArrayList<UtilitiesInfo> data) {

private ArrayList<UtilitiesInfo> SortData(ArrayList<UtilitiesInfo> dat) {
ArrayList<UtilitiesInfo> data = new ArrayList<>();
if (Build.VERSION.SDK_INT >= 21) {
data = dat;
} else {
for (UtilitiesInfo d : dat) {
if (d.getIdx() != MainActivity.ADS_IDX)
data.add(d);
}
}
data = dat;
ArrayList<UtilitiesInfo> customdata = new ArrayList<>();
if (mSharedPrefs.enableCustomSorting() && mCustomSorting != null) {
UtilitiesInfo adView = null;
Expand Down Expand Up @@ -149,13 +146,8 @@ public Filter getFilter() {
@Override
public DataObjectHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
if (Build.VERSION.SDK_INT >= 21) {
view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.utilities_row_default, parent, false);
} else {
view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.utilities_row_default_noads, parent, false);
}
view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.utilities_row_default, parent, false);
return new DataObjectHolder(view);
}

Expand All @@ -173,6 +165,10 @@ public void onBindViewHolder(final DataObjectHolder holder, final int position)
(mUtilitiesInfo.getSubType() != null && DomoticzValues.Device.Utility.SubType.SMARTWARES.equalsIgnoreCase(mUtilitiesInfo.getSubType()))) {
setButtons(holder, Buttons.THERMOSTAT);
CreateThermostatRow(holder, mUtilitiesInfo, setPoint);
} else if ((mUtilitiesInfo.getType() != null && DomoticzValues.Device.Utility.Type.GENERAL.equalsIgnoreCase(mUtilitiesInfo.getType())) &&
(mUtilitiesInfo.getSubType() != null && DomoticzValues.Device.Utility.SubType.THERMOSTAT_MODE.equalsIgnoreCase(mUtilitiesInfo.getSubType()))) {
setButtons(holder, Buttons.THERMOSTAT_MODE);
CreateThermostatRow(holder, mUtilitiesInfo, setPoint);
} else {
if (DomoticzValues.Device.Utility.SubType.TEXT.equalsIgnoreCase(mUtilitiesInfo.getSubType()) || DomoticzValues.Device.Utility.SubType.ALERT.equalsIgnoreCase(mUtilitiesInfo.getSubType())) {
CreateTextRow(holder, mUtilitiesInfo);
Expand Down Expand Up @@ -401,6 +397,37 @@ private void CreateThermostatRow(DataObjectHolder holder, UtilitiesInfo mUtiliti
holder.on_button.setId(mUtilitiesInfo.getIdx());
holder.on_button.setOnClickListener(v -> handleThermostatClick(v.getId()));

int loadMode = mUtilitiesInfo.getModeId();
final ArrayList<String> modes = mUtilitiesInfo.getModes();
if (modes != null && modes.size() > 0) {
holder.spSelector.setId(mUtilitiesInfo.getIdx());
holder.spSelector.setVisibility(View.VISIBLE);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item, modes);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
holder.spSelector.setAdapter(dataAdapter);
holder.spSelector.setSelection(loadMode);
holder.spSelector.setTag(mUtilitiesInfo);
}
else{
holder.spSelector.setVisibility(View.GONE);
}

holder.spSelector.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
if ((holder.spSelector.getId()) == mUtilitiesInfo.getIdx()) {
holder.spSelector.setId(mUtilitiesInfo.getIdx() * 3);
} else {
String selValue = holder.spSelector.getItemAtPosition(arg2).toString();
handleSelectorChange(mUtilitiesInfo, arg2, selValue);
}
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {}
});

holder.dayButton.setId(mUtilitiesInfo.getIdx());
holder.dayButton.setOnClickListener(v -> {
for (UtilitiesInfo t : filteredData) {
Expand Down Expand Up @@ -442,12 +469,19 @@ private void CreateThermostatRow(DataObjectHolder holder, UtilitiesInfo mUtiliti
listener.onLogClick(t, DomoticzValues.Graph.Range.YEAR);
}
});

holder.name.setText(mUtilitiesInfo.getName());
holder.hardware.setText(mUtilitiesInfo.getLastUpdate());
holder.data.setText(context.getString(R.string.set_point) + ": " + setPoint);
Picasso.get().load(DomoticzIcons.getDrawableIcon(mUtilitiesInfo.getTypeImg(), mUtilitiesInfo.getType(), mUtilitiesInfo.getSubType(), false, false, null)).into(holder.iconRow);
}

private void handleSelectorChange(UtilitiesInfo device, int id, String mode) {
if (device != null) {
listener.OnModeChanged(device, id, mode);
}
}

public void handleThermostatClick(int idx) {
listener.onThermostatClick(idx);
}
Expand Down Expand Up @@ -481,7 +515,9 @@ public void setButtons(DataObjectHolder holder, int button) {
if (holder.on_button != null) {
holder.on_button.setVisibility(View.GONE);
}

if (holder.spSelector != null) {
holder.spSelector.setVisibility(View.GONE);
}
switch (button) {
case Buttons.ADS:
if (holder.adview != null)
Expand Down Expand Up @@ -517,6 +553,18 @@ public void setButtons(DataObjectHolder holder, int button) {
if (holder.adview != null)
holder.adview.setVisibility(View.GONE);
break;
case Buttons.THERMOSTAT_MODE:
if (holder.contentWrapper != null)
holder.contentWrapper.setVisibility(View.VISIBLE);
holder.on_button.setVisibility(View.VISIBLE);
holder.dayButton.setVisibility(View.VISIBLE);
holder.monthButton.setVisibility(View.VISIBLE);
holder.weekButton.setVisibility(View.VISIBLE);
holder.yearButton.setVisibility(View.VISIBLE);
holder.spSelector.setVisibility(View.VISIBLE);
if (holder.adview != null)
holder.adview.setVisibility(View.GONE);
break;
}
}

Expand Down Expand Up @@ -571,6 +619,7 @@ interface Buttons {
int TEXT = 1;
int THERMOSTAT = 2;
int ADS = 3;
int THERMOSTAT_MODE = 4;
}

public static class DataObjectHolder extends RecyclerView.ViewHolder implements RVHViewHolder {
Expand All @@ -591,6 +640,7 @@ public static class DataObjectHolder extends RecyclerView.ViewHolder implements
LinearLayout extraPanel;
TemplateView adview;
RelativeLayout contentWrapper;
Spinner spSelector;

public DataObjectHolder(View itemView) {
super(itemView);
Expand All @@ -610,6 +660,7 @@ public DataObjectHolder(View itemView) {
buttonLog = itemView.findViewById(R.id.log_button);
data = itemView.findViewById(R.id.utilities_data);
hardware = itemView.findViewById(R.id.utilities_hardware);
spSelector = itemView.findViewById(R.id.spSelector);

extraPanel = itemView.findViewById(R.id.extra_panel);
if (extraPanel != null)
Expand Down
33 changes: 32 additions & 1 deletion app/src/main/java/nl/hnogames/domoticz/fragments/Utilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,38 @@ private UtilitiesInfo getUtility(int idx) {
public void onClick(UtilitiesInfo utility) {
}

@Override
public void OnModeChanged(UtilitiesInfo utility, int id, String mode) {
UserInfo user = getCurrentUser(mContext, StaticHelper.getDomoticz(mContext));
if (user != null && user.getRights() <= 0) {
UsefulBits.showSnackbar(mContext, frameLayout, mContext.getString(R.string.security_no_rights), Snackbar.LENGTH_SHORT);
if (getActivity() instanceof MainActivity)
((MainActivity) getActivity()).Talk(R.string.security_no_rights);
refreshFragment();
return;
}

addDebugText("OnModeChanged");
addDebugText("Set idx " + utility.getIdx() + " to " + mode);
StaticHelper.getDomoticz(mContext).setAction(utility.getIdx(),
DomoticzValues.Json.Url.Set.THERMOSTAT,
DomoticzValues.Device.Thermostat.Action.TMODE,
id,
null,
new setCommandReceiver() {
@Override
public void onReceiveResult(String result) {
successHandling(result, false);
}

@Override

public void onError(Exception error) {
errorHandling(error);
}
});
}

@Override

public void onLogClick(final UtilitiesInfo utility, final String range) {
Expand Down Expand Up @@ -373,7 +405,6 @@ public void onLogClick(final UtilitiesInfo utility, final String range) {
}

@Override

public void onThermostatClick(final int idx) {
UserInfo user = getCurrentUser(mContext, StaticHelper.getDomoticz(mContext));
if (user != null && user.getRights() <= 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

public interface UtilityClickListener {
void onClick(UtilitiesInfo utility);
void OnModeChanged(UtilitiesInfo utility, int id, String mode);

void onLogClick(UtilitiesInfo utility, String range);

Expand Down
19 changes: 19 additions & 0 deletions app/src/main/res/layout/utilities_row_default.xml
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,26 @@
android:textSize="10dp" />

</LinearLayout>
</LinearLayout>

<LinearLayout
android:id="@+id/linearLayout4ttt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/listviewRowBackground"
android:layout_below="@+id/textwrapper"
android:layout_gravity="right"
android:foregroundGravity="right"
android:gravity="right"
android:orientation="horizontal">

<Spinner
android:id="@+id/spSelector"
android:layout_width="129dp"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
android:foregroundGravity="right"
android:minWidth="150dp" />
</LinearLayout>

<include layout="@layout/log_all_buttons" />
Expand Down
Loading

0 comments on commit ea92501

Please sign in to comment.