Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save btow/cef0137320faea92b8b02a290692c31f to your computer and use it in GitHub Desktop.
Save btow/cef0137320faea92b8b02a290692c31f to your computer and use it in GitHub Desktop.
EditText in the TextView and back
@InjectViewState
class Frag5_Presenter : MvpPresenter<Frag5_View>() {
var mData = Any()
private var checkResult: Boolean = false
val SERVERS_ADDRESS = 1
val DEVICES_ID = 2
private val MODE_SAVE_SETTINGS: Boolean = true
private val MODE_CLOSE_SETTINGS: Boolean = false
fun setData(data: Any?) {
if (data != null)
mData = data
}
fun setCheckResult(parameters: HashMap<String, EditText>?) {
var checkPassed = true
for (parameter in parameters!!) {
checkPassed = checkPassed && !(parameter.value.text.toString()).equals("")
if (!checkPassed) {
when (parameter.key) {
BuildConfig.SERVERS_ADDRESS -> {
viewState.showAlertDialog(
App.instance.resources.getString(R.string.dialog_error_settings_title_0),
App.instance.resources.getString(R.string.dialog_error_servers_address_msg)
)
viewState.setFocusInToServersAddresesFields()
}
BuildConfig.DEVICES_ID -> {
viewState.showAlertDialog(
App.instance.resources.getString(R.string.dialog_error_settings_title_0),
App.instance.resources.getString(R.string.dialog_error_devices_id_msg)
)
viewState.setFocusInToDevicesIDsFields()
}
}
}
}
this.checkResult = checkPassed
}
private fun checkRequiredFillingsParameters(mod_settings: Boolean): Boolean {
viewState.checkRequiredFillingsParameters()
if (!checkResult)
viewState.showAlertDialog(
if (mod_settings) App.instance.resources.getString(R.string.dialog_error_settings_title_0)
else App.instance.resources.getString(R.string.dialog_error_settings_title_1),
App.instance.resources.getString(R.string.dialog_error_some_required_parameters_are_not_filled))
return this.checkResult
}
@SuppressLint("CommitPrefEdits")
fun saveAllSettings(parameters: HashMap<String, EditText>?): Boolean {
if (checkRequiredFillingsParameters(MODE_SAVE_SETTINGS)) {
val editor = App.settings!!.edit()
for (parameter in parameters!!) {
editor.putString(parameter.key, parameter.value.text.toString())
}
editor.apply()
return true
}
return false
}
@SuppressLint("CommitPrefEdits")
fun saveAllSettingsAndCloseFrag(parameters: HashMap<String, EditText>?) {
if (saveAllSettings(parameters))
App.instance.getRouter().exit()
}
@SuppressLint("CommitPrefEdits")
fun closeFragWithoutSettings() {
if (checkRequiredFillingsParameters(MODE_CLOSE_SETTINGS))
App.instance.getRouter().exit()
}
fun setUpParameters(parameters: HashMap<String, EditText>?) {
if (BuildConfig.IS_DEBUGABLE)
for (parameter in parameters!!) {
val s1: String? = setParametersValue(parameter)
parameter.value.setText(s1)
}
else {
for (parameter in parameters!!){
val s1: String? = setParametersValue(parameter)
if (App.settings!!.contains(parameter.key)
&& !(App.settings!!.getString(parameter.key, s1)).equals("")) {
parameter.value.setText(s1)
viewState.hideKeyboard(parameter.value)
}
}
}
}
private fun setParametersValue(parameter: MutableMap.MutableEntry<String, EditText>): String? {
var s1: String?
when (parameter.key) {
BuildConfig.SERVERS_ADDRESS -> s1 = BuildConfig.DEBUG_URL
BuildConfig.DEVICES_ID -> s1 = BuildConfig.DEBUG_DEVICES_ID
else -> s1 = ""
}
return s1
}
fun btnEditAddrsOnClickListener() = View.OnClickListener {
viewState.openKeyboardForFields(SERVERS_ADDRESS)
}
fun btnCheckAddrsOnClickListener() = View.OnClickListener {
viewState.hideKeyboard(SERVERS_ADDRESS)
viewState.checkServersConnect()
}
fun btnEditIdOnClickListener() = View.OnClickListener {
viewState.openKeyboardForFields(DEVICES_ID)
}
fun btnCheckIdOnClickListener() = View.OnClickListener {
viewState.hideKeyboard(DEVICES_ID)
viewState.checkDevicesID()
}
}
class Frag5_Settings() : MvpAppCompatFragment(), Frag5_View {
private var mData: Any? = null
private var parameters: HashMap<kotlin.String, EditText>? = null
@InjectPresenter(type = PresenterType.GLOBAL)
lateinit var mThisPresenter: Frag5_Presenter
@SuppressLint("ValidFragment")
constructor(data: Any?) : this() {
mData = data
}
@ProvidePresenterTag(presenterClass = Frag5_Presenter::class, type = PresenterType.GLOBAL)
fun provideFrag5_PresenterTag(): String = "Frag5_Presenter"
@ProvidePresenter(type = PresenterType.GLOBAL)
fun provideFrag5_Presenter() = Frag5_Presenter()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view: View = inflater.inflate(R.layout.frag5_settings, container, false)
return view
}
override fun onResume() {
super.onResume()
parameters = hashMapOf(
BuildConfig.SERVERS_ADDRESS to etServersAddress,
BuildConfig.DEVICES_ID to etDevicesId
)
mThisPresenter.setData(mData)
mThisPresenter.setUpParameters(parameters)
setUpBtnSettings()
setUpBtnAddress()
setUpBtnDevices()
}
private fun setUpBtnAddress() {
btnEditAddr.setOnClickListener(mThisPresenter.btnEditAddrsOnClickListener())
btnCheckAddr.setOnClickListener(mThisPresenter.btnCheckAddrsOnClickListener())
}
private fun setUpBtnDevices() {
btnEditId.setOnClickListener(mThisPresenter.btnEditIdOnClickListener())
btnCheckId.setOnClickListener(mThisPresenter.btnCheckIdOnClickListener())
}
override fun hideKeyboard(editText: EditText) {
(context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager)
.hideSoftInputFromWindow(editText.windowToken, 0)
}
override fun openKeyboardForFields(internalConst: Int) {
when(internalConst){
mThisPresenter.SERVERS_ADDRESS -> {
(context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager)
.showSoftInput(etServersAddress, 0)
tilServersAddress.backgroundColor = Color.WHITE
etServersAddress.isFocusable = true
etServersAddress.isLongClickable = true
etServersAddress.isCursorVisible = true
etServersAddress.setSelection(etServersAddress.text.lastIndex)
etServersAddress.requestFocus()
}
mThisPresenter.DEVICES_ID -> {
(context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager)
.showSoftInput(etDevicesId, 0)
tilDevicesId.backgroundColor = Color.WHITE
etDevicesId.isFocusable = true
etDevicesId.isLongClickable = true
etDevicesId.isCursorVisible = true
etDevicesId.setSelection(etDevicesId.text.lastIndex)
etDevicesId.requestFocus()
}
}
}
override fun hideKeyboard(internalConst: Int) {
when (internalConst) {
mThisPresenter.SERVERS_ADDRESS -> {
tilServersAddress.backgroundColor = Color.GRAY
etServersAddress.isFocusable = false
etServersAddress.isLongClickable = false
etServersAddress.isCursorVisible = false
hideKeyboard(etServersAddress)
}
mThisPresenter.DEVICES_ID -> {
tilDevicesId.backgroundColor = Color.GRAY
etDevicesId.isFocusable = false
etDevicesId.isLongClickable = false
etDevicesId.isCursorVisible = false
hideKeyboard(etDevicesId)
}
}
}
override fun checkServersConnect() {
val editor = App.settings!!.edit()
editor.putString(BuildConfig.SERVERS_ADDRESS, etServersAddress.text.toString())
editor.apply()
AppCoroutines.getServersConnect(this, etServersAddress.text.toString())
}
override fun checkDevicesID() {
AppCoroutines.getDeviceConfig(this, etDevicesId.text.toString())
}
override fun showSimulatorsName(simulatorsName: String) {
tvSimulatorsName.setText(simulatorsName)
}
override fun showServersStatusToastOK(body: JSONServersAddressConfig) {
val msg = getString(R.string.frag5_msg_servers_addr_status) + body.message
Toast.makeText(context, msg, Toast.LENGTH_LONG).show()
etDevicesId.requestFocus()
}
override fun showServersStatusToastBad(serversStatus: Boolean) {
var msg = getString(R.string.frag5_msg_servers_addr_status)
if (serversStatus)
msg += getString(R.string.frag5_msg_servers_addr_invalid)
else
msg += getString(R.string.frag5_msg_servers_addr_bad)
Toast.makeText(context, msg, Toast.LENGTH_LONG).show()
val editor = App.settings!!.edit()
editor.remove(BuildConfig.SERVERS_ADDRESS)
editor.apply()
etDevicesId.requestFocus()
}
private fun setUpBtnSettings() {
tvBtnSave.setOnClickListener { mThisPresenter.saveAllSettings(parameters) }
tvBtnSaveEndClose.setOnClickListener { mThisPresenter.saveAllSettingsAndCloseFrag(parameters) }
tvBtnCloseWithoutSaving.setOnClickListener { mThisPresenter.closeFragWithoutSettings() }
}
override fun checkRequiredFillingsParameters() {
mThisPresenter.setCheckResult(parameters)
}
override fun showAlertDialog(title: String, msg: String) {
AlertDialog.Builder(context!!)
.setTitle(title)
.setMessage(msg)
.setPositiveButton(getString(R.string.dialog_button_ok)) { dialogInterface, i -> dialogInterface.cancel() }
.create()
.show()
}
override fun setFocusInToServersAddresesFields() {
etServersAddress.requestFocus()
}
override fun setFocusInToDevicesIDsFields() {
etDevicesId.requestFocus()
}
}
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_green_light"
android:orientation="vertical"
android:padding="8dp">
<LinearLayout
android:id="@+id/llSettings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:orientation="vertical">
<LinearLayout
android:id="@+id/llServersAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.design.widget.TextInputLayout
android:id="@+id/tilServersAddress"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:layout_weight="10"
android:background="@android:color/white"
android:padding="8dp">
<EditText
android:id="@+id/etServersAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/frag5_servers_address" />
</android.support.design.widget.TextInputLayout>
<LinearLayout
android:id="@+id/llBtnsAddr"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:orientation="vertical">
<Button
android:id="@+id/btnEditAddr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/frag5_address_edit" />
<Button
android:id="@+id/btnCheckAddr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/frag5_address_check" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/llDevicesIds"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/llDevisesId"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="10"
android:orientation="vertical">
<android.support.design.widget.TextInputLayout
android:id="@+id/tilDevicesId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:background="@android:color/white"
android:padding="8dp">
<EditText
android:id="@+id/etDevicesId"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="@string/frag5_devices_id" />
</android.support.design.widget.TextInputLayout>
<TextView
android:id="@+id/tvSimulatorsName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:text="@string/frag5_simulators_name"
android:textColor="@android:color/white"
android:textSize="18sp"/>
</LinearLayout>
<LinearLayout
android:id="@+id/llBtnsDevicesIds"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:orientation="vertical">
<Button
android:id="@+id/btnEditId"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="@string/frag5_btn_edit"
tools:ignore="NestedWeights" />
<Button
android:id="@+id/btnCheckId"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="@string/frag5_btn_check" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/llBtns"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginTop="8dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/cvBtnSave"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:layout_weight="1"
card_view:cardCornerRadius="8dp"
card_view:cardElevation="3dp">
<com.balysv.materialripple.MaterialRippleLayout
android:id="@+id/mrlBtnSave"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="4dp">
<TextView
android:id="@+id/tvBtnSave"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true"
android:gravity="center"
android:padding="4dp"
android:text="@string/frag5_save_settings"
android:textStyle="bold" />
</com.balysv.materialripple.MaterialRippleLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/cvBtnSaveAndClose"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_weight="1"
card_view:cardCornerRadius="8dp"
card_view:cardElevation="3dp">
<com.balysv.materialripple.MaterialRippleLayout
android:id="@+id/mrlBtnSaveAndClose"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp">
<TextView
android:id="@+id/tvBtnSaveEndClose"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:gravity="center"
android:padding="4dp"
android:text="@string/frag5_save_and_close"
android:textStyle="bold" />
</com.balysv.materialripple.MaterialRippleLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/cvBtnCloseWithoutSaving"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:layout_weight="1"
card_view:cardCornerRadius="8dp"
card_view:cardElevation="3dp">
<com.balysv.materialripple.MaterialRippleLayout
android:id="@+id/mrlBtnCloseWithoutSaving"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp">
<TextView
android:id="@+id/tvBtnCloseWithoutSaving"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:gravity="center"
android:padding="4dp"
android:text="@string/frag5_close_without_saving"
android:textStyle="bold" />
</com.balysv.materialripple.MaterialRippleLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</LinearLayout>
</ScrollView>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment