implemented things to the point where simple set/get of color is possible
added complete add stripe mechanic
This commit is contained in:
@@ -0,0 +1,457 @@
|
||||
/*
|
||||
* LEDD Project
|
||||
* Copyright (C) 2015 LEDD Team
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.idlegandalf.ledd.fragments;
|
||||
|
||||
|
||||
import android.app.Dialog;
|
||||
import android.app.DialogFragment;
|
||||
import android.content.DialogInterface;
|
||||
import android.os.Bundle;
|
||||
import android.support.design.widget.Snackbar;
|
||||
import android.support.design.widget.TextInputLayout;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
import android.text.Editable;
|
||||
import android.text.TextWatcher;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ImageButton;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.RelativeLayout;
|
||||
import android.widget.Spinner;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.idlegandalf.ledd.ColorActivity;
|
||||
import com.idlegandalf.ledd.ColorApplication;
|
||||
import com.idlegandalf.ledd.R;
|
||||
import com.idlegandalf.ledd.callbacks.AddStripeCallback;
|
||||
import com.idlegandalf.ledd.callbacks.StripesCallback;
|
||||
import com.idlegandalf.ledd.components.Controller;
|
||||
import com.idlegandalf.ledd.components.LedDDaemon;
|
||||
import com.idlegandalf.ledd.components.LedStripe;
|
||||
import com.idlegandalf.ledd.helper.LedDHelper;
|
||||
|
||||
import org.json.JSONException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.text.NumberFormat;
|
||||
import java.text.ParsePosition;
|
||||
import java.util.List;
|
||||
|
||||
import butterknife.Bind;
|
||||
import butterknife.ButterKnife;
|
||||
import butterknife.OnClick;
|
||||
|
||||
public class AddStripeDialog extends DialogFragment implements DialogInterface.OnShowListener {
|
||||
|
||||
public static AddStripeDialog instance;
|
||||
@Bind(R.id.spinner_daemon)
|
||||
Spinner daemonSpinner;
|
||||
@Bind(R.id.spinner_controller)
|
||||
Spinner controllerSpinner;
|
||||
@Bind(R.id.choose_controller_lay)
|
||||
RelativeLayout controllerLayout;
|
||||
@Bind(R.id.input_stripe_name_lay)
|
||||
TextInputLayout stripeNameLay;
|
||||
@Bind(R.id.input_stripe_name)
|
||||
EditText stripeNameText;
|
||||
@Bind(R.id.input_channel_r_lay)
|
||||
TextInputLayout channelRLay;
|
||||
@Bind(R.id.input_channel_r)
|
||||
EditText channelR;
|
||||
@Bind(R.id.input_channel_g_lay)
|
||||
TextInputLayout channelGLay;
|
||||
@Bind(R.id.input_channel_g)
|
||||
EditText channelG;
|
||||
@Bind(R.id.input_channel_b_lay)
|
||||
TextInputLayout channelBLay;
|
||||
@Bind(R.id.input_channel_b)
|
||||
EditText channelB;
|
||||
@Bind(R.id.stripe_mapping_lay)
|
||||
LinearLayout stripeMapping;
|
||||
@Bind(R.id.imgbuttn_togglechannel_r)
|
||||
ImageButton channelRBttn;
|
||||
@Bind(R.id.imgbuttn_togglechannel_g)
|
||||
ImageButton channelGBttn;
|
||||
@Bind(R.id.imgbuttn_togglechannel_b)
|
||||
ImageButton channelBBttn;
|
||||
ArrayAdapter<LedDDaemon> daemonArrayAdapter;
|
||||
ArrayAdapter<Controller> controllerArrayAdapter;
|
||||
boolean mDaemonSelected = false;
|
||||
boolean mControllerSelected = false;
|
||||
|
||||
private static boolean isNumeric(String str) {
|
||||
NumberFormat formatter = NumberFormat.getInstance();
|
||||
ParsePosition pos = new ParsePosition(0);
|
||||
formatter.parse(str, pos);
|
||||
return str.length() == pos.getIndex();
|
||||
}
|
||||
|
||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||
View v = View.inflate(getActivity(), R.layout.fragment_addstripe, null);
|
||||
ButterKnife.bind(this, v);
|
||||
instance = this;
|
||||
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
||||
builder.setView(v).setPositiveButton("Add", null).setNegativeButton("Cancel", null);
|
||||
|
||||
AlertDialog dialog = builder.create();
|
||||
|
||||
dialog.setOnShowListener(this);
|
||||
|
||||
return dialog;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onShow(final DialogInterface dialog) {
|
||||
|
||||
final AlertDialog alertDialog = (AlertDialog) dialog;
|
||||
toggleAll(controllerLayout, false);
|
||||
channelRBttn.setEnabled(false);
|
||||
channelGBttn.setEnabled(false);
|
||||
channelBBttn.setEnabled(false);
|
||||
|
||||
stripeNameText.addTextChangedListener(new TextWatcher() {
|
||||
@Override
|
||||
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTextChanged(Editable s) {
|
||||
stripeNameLay.setError(null);
|
||||
}
|
||||
});
|
||||
|
||||
channelR.addTextChangedListener(new TextWatcher() {
|
||||
@Override
|
||||
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTextChanged(Editable s) {
|
||||
if (s.toString().isEmpty()) {
|
||||
channelRBttn.setEnabled(false);
|
||||
} else {
|
||||
channelRBttn.setEnabled(true);
|
||||
}
|
||||
}
|
||||
});
|
||||
channelG.addTextChangedListener(new TextWatcher() {
|
||||
@Override
|
||||
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTextChanged(Editable s) {
|
||||
if (s.toString().isEmpty()) {
|
||||
channelGBttn.setEnabled(false);
|
||||
} else {
|
||||
channelGBttn.setEnabled(true);
|
||||
}
|
||||
}
|
||||
});
|
||||
channelB.addTextChangedListener(new TextWatcher() {
|
||||
@Override
|
||||
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTextChanged(Editable s) {
|
||||
if (s.toString().isEmpty()) {
|
||||
channelBBttn.setEnabled(false);
|
||||
} else {
|
||||
channelBBttn.setEnabled(true);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
ImageButton.OnClickListener imgListener = new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
switch (v.getId()) {
|
||||
case R.id.imgbuttn_togglechannel_r:
|
||||
testChannel(channelR, (ImageButton) v);
|
||||
break;
|
||||
case R.id.imgbuttn_togglechannel_g:
|
||||
testChannel(channelG, (ImageButton) v);
|
||||
break;
|
||||
case R.id.imgbuttn_togglechannel_b:
|
||||
testChannel(channelB, (ImageButton) v);
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
channelRBttn.setOnClickListener(imgListener);
|
||||
channelGBttn.setOnClickListener(imgListener);
|
||||
channelBBttn.setOnClickListener(imgListener);
|
||||
|
||||
alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
alertDialog.dismiss();
|
||||
}
|
||||
});
|
||||
|
||||
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (!mDaemonSelected) {
|
||||
Toast.makeText(getActivity(), "Please select Daemon and Controller", Toast.LENGTH_LONG).show();
|
||||
return;
|
||||
}
|
||||
|
||||
if (stripeNameText.getText().toString().isEmpty()) {
|
||||
stripeNameLay.setError("Please set the Stripe name");
|
||||
return;
|
||||
}
|
||||
|
||||
if (channelR.getText().toString().isEmpty() || channelG.getText().toString().isEmpty() || channelB.getText().toString().isEmpty()) {
|
||||
Toast.makeText(getActivity(), "Please add mapping for all channels", Toast.LENGTH_LONG).show();
|
||||
return;
|
||||
}
|
||||
|
||||
LedStripe stripe = new LedStripe(Integer.parseInt(channelR.getText().toString()), Integer.parseInt(channelG.getText().toString()),
|
||||
Integer.parseInt(channelB.getText().toString()), stripeNameText.getText().toString());
|
||||
stripe.setLedDDaemon((LedDDaemon) daemonSpinner.getSelectedItem());
|
||||
stripe.setController((Controller) controllerSpinner.getSelectedItem());
|
||||
|
||||
LedDHelper helper = null;
|
||||
try {
|
||||
helper = ColorApplication.getInstance().getHelperForDaemon((LedDDaemon) daemonSpinner.getSelectedItem());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
if (helper != null) {
|
||||
try {
|
||||
helper.addStripe(stripe, new AddStripeCallback() {
|
||||
@Override
|
||||
public void onAddSuccessfully(final LedStripe stripe) {
|
||||
ColorActivity activity = ((ColorActivity) getActivity());
|
||||
try {
|
||||
activity.refreshStripes();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
getActivity().runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Snackbar.make(((ViewGroup) getActivity().getWindow().getDecorView().findViewById(android.R.id.content))
|
||||
.getChildAt(0), "Added stripe (" + stripe.getId() + ")", Snackbar.LENGTH_LONG).show();
|
||||
}
|
||||
});
|
||||
dismiss();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnectionFailed(final String message) {
|
||||
getActivity().runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Snackbar.make(((ViewGroup) getActivity().getWindow().getDecorView().findViewById(android.R.id.content))
|
||||
.getChildAt(0), "Failed to add stripe: " + message, Snackbar.LENGTH_LONG).show();
|
||||
}
|
||||
});
|
||||
dismiss();
|
||||
}
|
||||
});
|
||||
} catch (JSONException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
daemonArrayAdapter = new ArrayAdapter<>(getActivity().getApplicationContext(), R.layout.spinner_item);
|
||||
daemonArrayAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
|
||||
daemonArrayAdapter.addAll(ColorApplication.getInstance().getDaemons());
|
||||
|
||||
controllerArrayAdapter = new ArrayAdapter<>(getActivity().getApplicationContext(), R.layout.spinner_item);
|
||||
controllerArrayAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
|
||||
|
||||
daemonSpinner.setAdapter(daemonArrayAdapter);
|
||||
controllerSpinner.setAdapter(controllerArrayAdapter);
|
||||
|
||||
daemonSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
|
||||
@Override
|
||||
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
|
||||
mDaemonSelected = true;
|
||||
toggleAll(controllerLayout, true);
|
||||
refreshController((LedDDaemon) daemonSpinner.getSelectedItem());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNothingSelected(AdapterView<?> parent) {
|
||||
toggleAll(controllerLayout, false);
|
||||
controllerArrayAdapter.clear();
|
||||
mDaemonSelected = false;
|
||||
mControllerSelected = false;
|
||||
}
|
||||
});
|
||||
|
||||
controllerSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
|
||||
@Override
|
||||
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
|
||||
mControllerSelected = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNothingSelected(AdapterView<?> parent) {
|
||||
mControllerSelected = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@OnClick(R.id.imgbuttn_addcontroller)
|
||||
public void addController(ImageButton button) {
|
||||
AddControllerDialog dialog = new AddControllerDialog();
|
||||
Bundle b = new Bundle();
|
||||
b.putString("daemon", new Gson().toJson(daemonSpinner.getSelectedItem()));
|
||||
dialog.setArguments(b);
|
||||
dialog.show(getActivity().getFragmentManager(), "");
|
||||
}
|
||||
|
||||
@OnClick(R.id.imgbuttn_adddaemon)
|
||||
public void addDaemon(ImageButton button) {
|
||||
new AddDaemonDialog().show(getActivity().getFragmentManager(), "");
|
||||
}
|
||||
|
||||
private void toggleAll(ViewGroup v, boolean status) {
|
||||
for (int i = 0; i < v.getChildCount(); i++) {
|
||||
v.getChildAt(i).setEnabled(status);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
ColorApplication.getInstance().onResume();
|
||||
if (daemonArrayAdapter != null) {
|
||||
daemonArrayAdapter.clear();
|
||||
daemonArrayAdapter.addAll(ColorApplication.getInstance().getDaemons());
|
||||
}
|
||||
|
||||
if (controllerArrayAdapter != null && mDaemonSelected) {
|
||||
refreshController((LedDDaemon) daemonSpinner.getSelectedItem());
|
||||
}
|
||||
}
|
||||
|
||||
private void testChannel(EditText text, ImageButton button) {
|
||||
if (mDaemonSelected && mControllerSelected) {
|
||||
if (!text.getText().toString().isEmpty() && isNumeric(text.getText().toString())) {
|
||||
int val;
|
||||
boolean toggle = false;
|
||||
if (text.getTag() != null) toggle = (boolean) text.getTag();
|
||||
if (!toggle) {
|
||||
val = 1;
|
||||
toggle = true;
|
||||
button.setImageResource(R.drawable.ic_visibility_black_48dp);
|
||||
} else {
|
||||
val = 0;
|
||||
toggle = false;
|
||||
button.setImageResource(R.drawable.ic_visibility_off_black_48dp);
|
||||
}
|
||||
text.setTag(toggle);
|
||||
|
||||
LedDHelper helper = null;
|
||||
try {
|
||||
helper = ColorApplication.getInstance().getHelperForDaemon((LedDDaemon) daemonSpinner.getSelectedItem());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
if (helper != null) {
|
||||
try {
|
||||
helper.testChannel((Controller) controllerSpinner.getSelectedItem(), Integer.parseInt(text.getText().toString()), val);
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void refreshController(final LedDDaemon ledDDaemon) {
|
||||
controllerArrayAdapter.clear();
|
||||
LedDHelper helper = null;
|
||||
try {
|
||||
helper = ColorApplication.getInstance().getHelperForDaemon(ledDDaemon);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
if (helper != null) {
|
||||
try {
|
||||
helper.getStripes(new StripesCallback() {
|
||||
@Override
|
||||
public void onSuccess(List<LedStripe> stripes) {
|
||||
getActivity().runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
controllerArrayAdapter.addAll(ledDDaemon.getControllers());
|
||||
toggleAll(stripeMapping, true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onGetFailed(String message) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnectionFailed(String message) {
|
||||
|
||||
}
|
||||
});
|
||||
} catch (JSONException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user