Skip to content

Instantly share code, notes, and snippets.

@cjbrooks12
Last active February 3, 2020 20:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cjbrooks12/e7cc62db8f6018c752ed73a57e8c0fc7 to your computer and use it in GitHub Desktop.
Save cjbrooks12/e7cc62db8f6018c752ed73a57e8c0fc7 to your computer and use it in GitHub Desktop.
Spaghetti Screens
class MainActivity : AppCompatActivity() {
var username: String? = null
var passwordValue: String? = null
var passwordField: EditText? = null
val listener = object : TextWatcher {
override fun afterTextChanged(s: Editable?) {}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
username = s.toString()
onPasswordChanged()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val view = findViewById<TextView>(R.id.screen_header)
view.setText("Login Screen")
login_form_username.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) { onPasswordChanged() }
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { username = s.toString() }
})
passwordField = findViewById<EditText>(R.id.login_form_password)
passwordField!!.addTextChangedListener(listener)
login_form_submit_button.setOnClickListener {
if(BuildConfig.DEBUG) {
DebugLoginService().login() {
startActivity(Intent(this, HomeActivity::class.java))
}
}
else {
ProductionLoginService(LoginApi()).login(username!!, passwordValue!!) {
startActivity(Intent(this, HomeActivity::class.java))
}
}
}
}
fun onPasswordChanged() {
if(username != null) {
if(passwordValue != null) {
login_form_submit_button.isEnabled = true
}
else {
login_form_submit_button.isEnabled = false
}
}
else {
if(passwordValue != null) {
login_form_submit_button.isEnabled = false
}
else {
login_form_submit_button.isEnabled = false
}
}
}
}
class ProductionLoginService(val loginApi: LoginApi) {
fun login(username: String, password: String, onLoggedIn: ()->Unit) {}
}
class DebugLoginService {
fun login(onLoggedIn: ()->Unit) {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment