Notes
Notes - notes.io |
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.telephony.TelephonyManager
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.gson.JsonArray
import com.google.gson.JsonObject
import com.voicematch.app.constants.AppConstants
import com.voicematch.app.data.ApiClient
import com.voicematch.app.data.SessionManager
import com.voicematch.app.databinding.ActivityProfileSetupBinding
import com.voicematch.app.ui.main.MainActivity
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.Locale
class ProfileSetupActivity : AppCompatActivity() {
private lateinit var binding: ActivityProfileSetupBinding
private lateinit var apiClient: ApiClient
private lateinit var sessionManager: SessionManager
private var selectedGender: String? = null
private var selectedMoods = mutableListOf<String>()
private var selectedZodiac: String? = null
private var selectedPet: String? = null
private var selectedCountry: String? = null
// Full country list
private val countryList: List<Pair<String, String>> by lazy {
Locale.getISOCountries().map { code ->
val locale = Locale("", code)
Pair(code, locale.displayCountry)
}.sortedBy { it.second }
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityProfileSetupBinding.inflate(layoutInflater)
setContentView(binding.root)
apiClient = ApiClient(this)
sessionManager = SessionManager(this)
setupGenderButtons()
setupMoodChips()
setupZodiacSpinner()
setupPetButtons()
setupCountrySpinner()
binding.btnFinish.setOnClickListener {
if (validateForm()) submitProfile()
}
}
private fun detectCountry(): String {
// Try TelephonyManager first (no permission needed)
try {
val tm = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
val networkCountry = tm.networkCountryIso
if (!networkCountry.isNullOrBlank()) {
return networkCountry.uppercase()
}
val simCountry = tm.simCountryIso
if (!simCountry.isNullOrBlank()) {
return simCountry.uppercase()
}
} catch (e: Exception) {
android.util.Log.e("VOICEMATCH", "TelephonyManager error: ${e.message}")
}
// Fallback to device locale
return Locale.getDefault().country
}
private fun setupCountrySpinner() {
val countryNames = countryList.map { it.second }
val adapter = android.widget.ArrayAdapter(
this,
android.R.layout.simple_spinner_item,
countryNames
)
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
binding.spinnerCountry.adapter = adapter
// Auto-detect and pre-select country
val detectedCode = detectCountry()
val detectedIndex = countryList.indexOfFirst { it.first == detectedCode }
if (detectedIndex >= 0) {
binding.spinnerCountry.setSelection(detectedIndex)
selectedCountry = countryList[detectedIndex].first
}
binding.spinnerCountry.onItemSelectedListener = object : android.widget.AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: android.widget.AdapterView<*>?, view: android.view.View?, position: Int, id: Long) {
selectedCountry = countryList[position].first
}
override fun onNothingSelected(parent: android.widget.AdapterView<*>?) {}
}
}
private fun setupGenderButtons() {
binding.btnMale.setOnClickListener {
selectedGender = "male"
binding.btnMale.alpha = 1f
binding.btnFemale.alpha = 0.5f
binding.btnOther.alpha = 0.5f
}
binding.btnFemale.setOnClickListener {
selectedGender = "female"
binding.btnFemale.alpha = 1f
binding.btnMale.alpha = 0.5f
binding.btnOther.alpha = 0.5f
}
binding.btnOther.setOnClickListener {
selectedGender = "other"
binding.btnOther.alpha = 1f
binding.btnMale.alpha = 0.5f
binding.btnFemale.alpha = 0.5f
}
}
private fun setupMoodChips() {
val chipGroup = binding.chipGroupMoods
AppConstants.MOOD_LIST.forEach { mood ->
val chip = com.google.android.material.chip.Chip(this).apply {
text = mood
isCheckable = true
setTextColor(getColor(android.R.color.white))
chipBackgroundColor = android.content.res.ColorStateList.valueOf(
android.graphics.Color.parseColor("#4A1040")
)
setOnCheckedChangeListener { _, isChecked ->
if (isChecked) selectedMoods.add(mood)
else selectedMoods.remove(mood)
}
}
chipGroup.addView(chip)
}
}
private fun setupZodiacSpinner() {
val adapter = android.widget.ArrayAdapter(
this,
android.R.layout.simple_spinner_item,
AppConstants.ZODIAC_LIST
)
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
binding.spinnerZodiac.adapter = adapter
binding.spinnerZodiac.onItemSelectedListener = object : android.widget.AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: android.widget.AdapterView<*>?, view: android.view.View?, position: Int, id: Long) {
selectedZodiac = AppConstants.ZODIAC_LIST[position]
}
override fun onNothingSelected(parent: android.widget.AdapterView<*>?) {}
}
}
private fun setupPetButtons() {
binding.btnCat.setOnClickListener { selectPet("Cat") }
binding.btnDog.setOnClickListener { selectPet("Dog") }
binding.btnBoth.setOnClickListener { selectPet("Both") }
binding.btnNone.setOnClickListener { selectPet("None") }
}
private fun selectPet(pet: String) {
selectedPet = pet
listOf(binding.btnCat, binding.btnDog, binding.btnBoth, binding.btnNone).forEach {
it.alpha = 0.5f
}
when (pet) {
"Cat" -> binding.btnCat.alpha = 1f
"Dog" -> binding.btnDog.alpha = 1f
"Both" -> binding.btnBoth.alpha = 1f
"None" -> binding.btnNone.alpha = 1f
}
}
private fun validateForm(): Boolean {
if (binding.etFirstName.text.isNullOrBlank()) {
Toast.makeText(this, "Enter your first name", Toast.LENGTH_SHORT).show()
return false
}
if (selectedGender == null) {
Toast.makeText(this, "Select your gender", Toast.LENGTH_SHORT).show()
return false
}
if (selectedMoods.isEmpty()) {
Toast.makeText(this, "Select at least one mood", Toast.LENGTH_SHORT).show()
return false
}
return true
}
private fun submitProfile() {
showLoading(true)
val moodsArray = JsonArray()
selectedMoods.forEach { moodsArray.add(it) }
val body = JsonObject().apply {
addProperty("first_name", binding.etFirstName.text.toString().trim())
addProperty("last_name", binding.etLastName.text.toString().trim())
addProperty("gender", selectedGender)
add("mood_tags", moodsArray)
addProperty("zodiac", selectedZodiac ?: "")
addProperty("pet_pref", selectedPet ?: "None")
addProperty("country", selectedCountry ?: "")
}
CoroutineScope(Dispatchers.Main).launch {
val response = apiClient.post(AppConstants.ENDPOINT_REGISTER, body)
showLoading(false)
if (response.success) {
val user = response.data?.getAsJsonObject("user")
user?.let {
sessionManager.saveUserId(it.get("id").asInt)
sessionManager.saveFirstName(it.get("first_name").asString)
sessionManager.saveGender(it.get("gender").asString)
sessionManager.setRegistered(true)
}
startActivity(Intent(this@ProfileSetupActivity, MainActivity::class.java))
finishAffinity()
} else {
Toast.makeText(this@ProfileSetupActivity, "Error: ${response.error}", Toast.LENGTH_SHORT).show()
}
}
}
private fun showLoading(show: Boolean) {
binding.progressBar.visibility = if (show) View.VISIBLE else View.GONE
binding.btnFinish.isEnabled = !show
}
}
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team
