diff --git a/app/src/main/java/org/linphone/ui/assistant/adapter/CountryPickerAdapter.kt b/app/src/main/java/org/linphone/ui/assistant/adapter/CountryPickerAdapter.kt new file mode 100644 index 000000000..d08872b58 --- /dev/null +++ b/app/src/main/java/org/linphone/ui/assistant/adapter/CountryPickerAdapter.kt @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2010-2020 Belledonne Communications SARL. + * + * This file is part of linphone-android + * (see https://www.linphone.org). + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.linphone.ui.assistant.adapter + +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.BaseAdapter +import android.widget.Filter +import android.widget.Filterable +import android.widget.TextView +import kotlin.collections.ArrayList +import org.linphone.R +import org.linphone.core.DialPlan +import org.linphone.core.Factory + +class CountryPickerAdapter : BaseAdapter(), Filterable { + private var countries: ArrayList + + init { + val dialPlans = Factory.instance().dialPlans + countries = arrayListOf() + countries.addAll(dialPlans) + } + + override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { + val view: View = convertView ?: LayoutInflater.from(parent.context).inflate( + R.layout.assistant_country_picker_cell, + parent, + false + ) + val dialPlan: DialPlan = countries[position] + + val name = view.findViewById(R.id.country_name) + name.text = dialPlan.country + + val dialCode = view.findViewById(R.id.country_prefix) + dialCode.text = String.format("(%s)", dialPlan.countryCallingCode) + + view.tag = dialPlan + return view + } + + override fun getItem(position: Int): DialPlan { + return countries[position] + } + + override fun getItemId(position: Int): Long { + return position.toLong() + } + + override fun getCount(): Int { + return countries.size + } + + override fun getFilter(): Filter { + return object : Filter() { + override fun performFiltering(constraint: CharSequence): FilterResults { + val filteredCountries = arrayListOf() + for (dialPlan in Factory.instance().dialPlans) { + if (dialPlan.country.contains(constraint, ignoreCase = true) || + dialPlan.countryCallingCode.contains(constraint) + ) { + filteredCountries.add(dialPlan) + } + } + val filterResults = FilterResults() + filterResults.values = filteredCountries + return filterResults + } + + @Suppress("UNCHECKED_CAST") + override fun publishResults( + constraint: CharSequence, + results: FilterResults + ) { + countries = results.values as ArrayList + notifyDataSetChanged() + } + } + } +} diff --git a/app/src/main/java/org/linphone/ui/assistant/fragment/CountryPickerFragment.kt b/app/src/main/java/org/linphone/ui/assistant/fragment/CountryPickerFragment.kt new file mode 100644 index 000000000..5fdbf0319 --- /dev/null +++ b/app/src/main/java/org/linphone/ui/assistant/fragment/CountryPickerFragment.kt @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2010-2020 Belledonne Communications SARL. + * + * This file is part of linphone-android + * (see https://www.linphone.org). + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.linphone.ui.assistant.fragment + +import android.os.Bundle +import android.text.Editable +import android.text.TextWatcher +import android.view.* +import androidx.annotation.UiThread +import androidx.fragment.app.DialogFragment +import org.linphone.R +import org.linphone.core.DialPlan +import org.linphone.databinding.AssistantCountryPickerFragmentBinding +import org.linphone.ui.assistant.adapter.CountryPickerAdapter + +@UiThread +class CountryPickerFragment : DialogFragment() { + private var _binding: AssistantCountryPickerFragmentBinding? = null + private val binding get() = _binding!! + private lateinit var adapter: CountryPickerAdapter + + var listener: CountryPickedListener? = null + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setStyle(STYLE_NO_TITLE, R.style.Theme_LinphoneDialog) + } + + override fun onDestroyView() { + super.onDestroyView() + _binding = null + } + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + _binding = AssistantCountryPickerFragmentBinding.inflate(inflater, container, false) + + adapter = CountryPickerAdapter() + binding.countryList.adapter = adapter + + binding.countryList.setOnItemClickListener { _, _, position, _ -> + if (position >= 0 && position < adapter.count) { + val dialPlan = adapter.getItem(position) + listener?.onCountryClicked(dialPlan) + } + dismiss() + } + + binding.searchCountry.addTextChangedListener(object : TextWatcher { + override fun afterTextChanged(s: Editable?) { + adapter.filter.filter(s) + } + + override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { } + + override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { } + }) + + binding.setCancelClickListener { + dismiss() + } + + return binding.root + } + + interface CountryPickedListener { + fun onCountryClicked(dialPlan: DialPlan) + } +} diff --git a/app/src/main/java/org/linphone/ui/assistant/fragment/RegisterFragment.kt b/app/src/main/java/org/linphone/ui/assistant/fragment/RegisterFragment.kt index eb0327f4e..2cb98025a 100644 --- a/app/src/main/java/org/linphone/ui/assistant/fragment/RegisterFragment.kt +++ b/app/src/main/java/org/linphone/ui/assistant/fragment/RegisterFragment.kt @@ -79,7 +79,9 @@ class RegisterFragment : Fragment() { } binding.setShowCountryPickerClickListener { - // TODO FIXME + val countryPickerFragment = CountryPickerFragment() + countryPickerFragment.listener = viewModel + countryPickerFragment.show(childFragmentManager, "CountryPicker") } binding.setOpenSubscribeWebPageClickListener { diff --git a/app/src/main/java/org/linphone/ui/assistant/viewmodel/AccountCreationViewModel.kt b/app/src/main/java/org/linphone/ui/assistant/viewmodel/AccountCreationViewModel.kt index 35a2db940..5a8c50ca8 100644 --- a/app/src/main/java/org/linphone/ui/assistant/viewmodel/AccountCreationViewModel.kt +++ b/app/src/main/java/org/linphone/ui/assistant/viewmodel/AccountCreationViewModel.kt @@ -33,14 +33,17 @@ import kotlinx.coroutines.withContext import org.json.JSONException import org.json.JSONObject import org.linphone.LinphoneApplication.Companion.coreContext +import org.linphone.LinphoneApplication.Companion.corePreferences import org.linphone.core.AccountCreator import org.linphone.core.AccountCreatorListenerStub import org.linphone.core.Core import org.linphone.core.CoreListenerStub +import org.linphone.core.DialPlan import org.linphone.core.tools.Log +import org.linphone.ui.assistant.fragment.CountryPickerFragment import org.linphone.utils.Event -class AccountCreationViewModel @UiThread constructor() : ViewModel() { +class AccountCreationViewModel @UiThread constructor() : ViewModel(), CountryPickerFragment.CountryPickedListener { companion object { private const val TAG = "[Account Creation ViewModel]" } @@ -238,6 +241,11 @@ class AccountCreationViewModel @UiThread constructor() : ViewModel() { } } + @UiThread + override fun onCountryClicked(dialPlan: DialPlan) { + internationalPrefix.value = "+${dialPlan.countryCallingCode}" + } + @UiThread override fun onCleared() { coreContext.postOnCoreThread { core -> @@ -318,10 +326,11 @@ class AccountCreationViewModel @UiThread constructor() : ViewModel() { private fun checkUsername() { usernameError.postValue("") accountCreator.username = username.value.orEmpty().trim() + accountCreator.domain = corePreferences.defaultDomain operationInProgress.postValue(true) val status = accountCreator.isAccountExist - Log.i("$TAG isAccountExist returned $status") + Log.i("$TAG isAccountExist for username [${accountCreator.username}] returned $status") if (status != AccountCreator.Status.RequestOk) { Log.e("$TAG Can't check if account already exists [$status]") operationInProgress.postValue(false) diff --git a/app/src/main/res/layout/assistant_country_picker_cell.xml b/app/src/main/res/layout/assistant_country_picker_cell.xml new file mode 100644 index 000000000..0340845ef --- /dev/null +++ b/app/src/main/res/layout/assistant_country_picker_cell.xml @@ -0,0 +1,22 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/assistant_country_picker_fragment.xml b/app/src/main/res/layout/assistant_country_picker_fragment.xml new file mode 100644 index 000000000..46e0c4727 --- /dev/null +++ b/app/src/main/res/layout/assistant_country_picker_fragment.xml @@ -0,0 +1,88 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file