Last active
August 29, 2015 14:10
-
-
Save st-f/3536f12296a5a622e2dc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.Toast; | |
import com.example.android.R; | |
import butterknife.ButterKnife; | |
import butterknife.InjectView; | |
/** | |
* Headless activity to convert view declarations to view injections. | |
*/ | |
public class ButterknifeConversionActivity extends Activity { | |
//! include tags are not yet supported https://github.com/inmite/android-butterknife-zelezny/issues/8 | |
private static final String LOG_TAG = "ButterknifeConversionActivity"; | |
@InjectView(R.id.toolButterknifeInput) EditText toolButterknifeInput; | |
@InjectView(R.id.toolButterknifeButton) Button toolButterknifeButton; | |
@InjectView(R.id.toolButterknifeButton2) Button toolButterknifeButton2; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
Log.i(LOG_TAG, "ButterknifeConversionActivity onCreate"); | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.tool_butterknife_conversion); | |
ButterKnife.inject(this); | |
toolButterknifeButton.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
toolButterknifeInput.setText(""); | |
} | |
}); | |
toolButterknifeButton2.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
convertViewDeclarationToViewInjection(String.valueOf(toolButterknifeInput.getText())); | |
} | |
}); | |
} | |
/** | |
* Converts this: | |
* postedBy = (TextView) layout.findViewById(R.id.mainRowPostedByText); | |
* to this: | |
* | |
* @param string | |
* @return | |
* @InjectView(R.id.mainRowPostedByText) TextView postedBy; | |
*/ | |
public void convertViewDeclarationToViewInjection(String string) { | |
String ret = ""; | |
String[] lines = string.split("\\n"); | |
for (String line : lines) { | |
ret = ret + convertViewDeclarationLine(line) + "\n"; | |
} | |
Toast.makeText(this, "Converted, text is in logs", Toast.LENGTH_SHORT).show(); | |
Log.i(LOG_TAG, "\n" + ret); | |
} | |
public String convertViewDeclarationLine(String string) { | |
string = string.trim(); | |
Log.i(LOG_TAG, "convertViewDeclarationLine : " + string); | |
final String varString = string.split(" ")[0]; | |
final String idString = string.substring(string.lastIndexOf("(") + 1, string.lastIndexOf(")")); | |
final String type = string.substring(string.indexOf("(") + 1, string.indexOf(")")); | |
final String returnText = "@InjectView(" + idString + ") " + type + " " + varString + ";"; | |
return returnText; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment