Created
December 29, 2019 20:24
-
-
Save Binary-Finery/c8d894e7c6debdc66145579f420e7ec4 to your computer and use it in GitHub Desktop.
temp converter (.kt)
This file contains hidden or 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.content.ClipData | |
import android.content.ClipboardManager | |
import android.content.Context | |
import android.os.Bundle | |
import android.support.v7.app.AppCompatActivity; | |
import android.text.Editable | |
import android.text.TextWatcher | |
import android.view.View | |
import kotlinx.android.synthetic.main.activity_main.* | |
import kotlinx.android.synthetic.main.content_main.* | |
import java.util.* | |
import android.content.Intent | |
class MainActivity : AppCompatActivity(), TextWatcher, View.OnFocusChangeListener { | |
override fun onFocusChange(v: View?, hasFocus: Boolean) { | |
when { | |
hasFocus -> when (v) { | |
fahrenheit -> { | |
celsius.removeTextChangedListener(this) | |
fahrenheit.addTextChangedListener(this) | |
} | |
celsius -> { | |
fahrenheit.removeTextChangedListener(this) | |
celsius.addTextChangedListener(this) | |
} | |
} | |
} | |
} | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
setSupportActionBar(toolbar) | |
supportActionBar?.hide() | |
celsius.onFocusChangeListener = this | |
fahrenheit.onFocusChangeListener = this | |
copy.setOnClickListener { copy() } | |
share.setOnClickListener { share() } | |
clear.setOnClickListener { | |
celsius.setText("") | |
fahrenheit.setText("") | |
} | |
} | |
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) { | |
val str = s.toString() | |
try { | |
when { | |
celsius.hasFocus() -> if (str.isNotEmpty()) { | |
fahrenheit.setText(String.format(Locale.getDefault(), "%.1f", (str.toDouble() * 1.8000) + 32.0)) | |
} else fahrenheit.setText("") | |
else -> if (str.isNotEmpty()) { | |
celsius.setText(String.format(Locale.getDefault(), "%.1f", ((str.toDouble() - 32.0) * 5.0) / 9.0)) | |
} else celsius.setText("") | |
} | |
} catch (e: Exception) { | |
e.printStackTrace() | |
} | |
} | |
private fun copy() { | |
when { | |
hasValues() -> { | |
val manager = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager | |
val clip = ClipData.newPlainText("text", buildSharableText()) | |
manager.primaryClip = clip | |
} | |
} | |
} | |
private fun share() { | |
when { | |
hasValues() -> { | |
val i = Intent() | |
i.apply { | |
action = Intent.ACTION_SEND | |
putExtra(Intent.EXTRA_TEXT, buildSharableText()) | |
type = "text/plain" | |
} | |
startActivity(Intent.createChooser(i,"share to...")) | |
} | |
} | |
} | |
private fun hasValues(): Boolean = celsius.text.isNotEmpty() && fahrenheit.text.isNotEmpty() | |
private fun buildSharableText(): String = String.format(Locale.getDefault(), "%.1f°C = %.1f°F", celsius.text.toString().toDouble(), fahrenheit.text.toString().toDouble()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment