Skip to content

Commit

Permalink
Merge pull request #3738 from nextcloud/set-mob-dialog
Browse files Browse the repository at this point in the history
Use DialogFragment for Phone Number Integration
  • Loading branch information
mahibi authored Mar 22, 2024
2 parents 304f6ad + 20378ae commit 7a64c53
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 69 deletions.
84 changes: 15 additions & 69 deletions app/src/main/java/com/nextcloud/talk/settings/SettingsActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ import android.annotation.SuppressLint
import android.app.KeyguardManager
import android.content.Context
import android.content.DialogInterface
import android.content.DialogInterface.OnShowListener
import android.content.Intent
import android.content.pm.PackageManager
import android.content.res.ColorStateList
import android.graphics.PorterDuff
import android.graphics.drawable.ColorDrawable
import android.media.RingtoneManager
Expand All @@ -46,18 +44,12 @@ import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.security.KeyChain
import android.text.Editable
import android.text.InputType
import android.text.TextUtils
import android.text.TextWatcher
import android.util.Log
import android.view.View
import android.view.WindowManager
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.view.ContextThemeWrapper
import androidx.core.content.ContextCompat
import androidx.core.content.res.ResourcesCompat
import androidx.core.view.ViewCompat
Expand Down Expand Up @@ -88,17 +80,18 @@ import com.nextcloud.talk.jobs.ContactAddressBookWorker.Companion.deleteAll
import com.nextcloud.talk.models.json.generic.GenericOverall
import com.nextcloud.talk.models.json.userprofile.UserProfileOverall
import com.nextcloud.talk.profile.ProfileActivity
import com.nextcloud.talk.ui.dialog.SetPhoneNumberDialogFragment
import com.nextcloud.talk.users.UserManager
import com.nextcloud.talk.utils.ApiUtils
import com.nextcloud.talk.utils.SpreedFeatures
import com.nextcloud.talk.utils.CapabilitiesUtil
import com.nextcloud.talk.utils.ClosedInterfaceImpl
import com.nextcloud.talk.utils.DisplayUtils
import com.nextcloud.talk.utils.LoggingUtils.sendMailWithAttachment
import com.nextcloud.talk.utils.NotificationUtils
import com.nextcloud.talk.utils.NotificationUtils.getCallRingtoneUri
import com.nextcloud.talk.utils.NotificationUtils.getMessageRingtoneUri
import com.nextcloud.talk.utils.SecurityUtils
import com.nextcloud.talk.utils.CapabilitiesUtil
import com.nextcloud.talk.utils.SpreedFeatures
import com.nextcloud.talk.utils.database.user.CurrentUserProviderNew
import com.nextcloud.talk.utils.permissions.PlatformPermissionUtil
import com.nextcloud.talk.utils.power.PowerManagerUtils
Expand All @@ -122,7 +115,7 @@ import javax.inject.Inject

@Suppress("LargeClass", "TooManyFunctions")
@AutoInjector(NextcloudTalkApplication::class)
class SettingsActivity : BaseActivity() {
class SettingsActivity : BaseActivity(), SetPhoneNumberDialogFragment.SetPhoneNumberDialogClickListener {
private lateinit var binding: ActivitySettingsBinding

@Inject
Expand Down Expand Up @@ -1241,61 +1234,15 @@ class SettingsActivity : BaseActivity() {
}

private fun askForPhoneNumber() {
val phoneNumberLayoutWrapper = LinearLayout(context)
phoneNumberLayoutWrapper.orientation = LinearLayout.VERTICAL
phoneNumberLayoutWrapper.setPadding(PHONE_NUMBER_SIDE_PADDING, 0, PHONE_NUMBER_SIDE_PADDING, 0)
val phoneNumberInputLayout = TextInputLayout(ContextThemeWrapper(this, R.style.TextInputLayoutTheme))
val phoneNumberField = EditText(context)
phoneNumberInputLayout.setHelperTextColor(
ColorStateList.valueOf(resources!!.getColor(R.color.nc_darkRed, null))
)
phoneNumberField.inputType = InputType.TYPE_CLASS_PHONE
phoneNumberField.setText("+")
phoneNumberField.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable) {
// unused atm
}

override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
// unused atm
}

override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
phoneNumberInputLayout.helperText = ""
}
})
phoneNumberInputLayout.addView(phoneNumberField)
phoneNumberLayoutWrapper.addView(phoneNumberInputLayout)
val dialogBuilder = MaterialAlertDialogBuilder(phoneNumberInputLayout.context)
.setTitle(R.string.nc_settings_phone_book_integration_phone_number_dialog_title)
.setMessage(R.string.nc_settings_phone_book_integration_phone_number_dialog_description)
.setView(phoneNumberLayoutWrapper)
.setPositiveButton(context.resources.getString(R.string.nc_common_set), null)
.setNegativeButton(context.resources.getString(R.string.nc_common_skip), null)

viewThemeUtils.dialog.colorMaterialAlertDialogBackground(phoneNumberInputLayout.context, dialogBuilder)

val dialog = dialogBuilder.create()
dialog.setOnShowListener(object : OnShowListener {
override fun onShow(dialogInterface: DialogInterface) {
val button = dialog.getButton(AlertDialog.BUTTON_POSITIVE)
button.setOnClickListener(object : View.OnClickListener {
override fun onClick(view: View) {
setPhoneNumber(phoneNumberInputLayout, dialog)
}
})
}
})

dialog.show()
val dialog = SetPhoneNumberDialogFragment.newInstance()
dialog.show(supportFragmentManager, SetPhoneNumberDialogFragment.TAG)
}

viewThemeUtils.platform.colorTextButtons(
dialog.getButton(AlertDialog.BUTTON_POSITIVE),
dialog.getButton(AlertDialog.BUTTON_NEGATIVE)
)
override fun onSubmitClick(textInputLayout: TextInputLayout, dialog: DialogInterface) {
setPhoneNumber(textInputLayout, dialog)
}

private fun setPhoneNumber(textInputLayout: TextInputLayout, dialog: AlertDialog) {
private fun setPhoneNumber(textInputLayout: TextInputLayout, dialog: DialogInterface) {
val phoneNumber = textInputLayout.editText!!.text.toString()
ncApi.setUserData(
ApiUtils.getCredentials(currentUser!!.username, currentUser!!.token),
Expand All @@ -1315,15 +1262,15 @@ class SettingsActivity : BaseActivity() {
dialog.dismiss()
Snackbar.make(
binding.root,
context.resources.getString(
getString(
R.string.nc_settings_phone_book_integration_phone_number_dialog_success
),
Snackbar.LENGTH_LONG
).show()
}

else -> {
textInputLayout.helperText = context.resources.getString(
textInputLayout.helperText = getString(
R.string.nc_settings_phone_book_integration_phone_number_dialog_invalid
)
Log.d(TAG, "failed to set phoneNumber. statusCode=$statusCode")
Expand All @@ -1332,10 +1279,10 @@ class SettingsActivity : BaseActivity() {
}

override fun onError(e: Throwable) {
textInputLayout.helperText = context.resources.getString(
textInputLayout.helperText = getString(
R.string.nc_settings_phone_book_integration_phone_number_dialog_invalid
)
Log.e(TAG, "setPhoneNumber error", e)
Log.e(SetPhoneNumberDialogFragment.TAG, "setPhoneNumber error", e)
}

override fun onComplete() {
Expand Down Expand Up @@ -1427,7 +1374,6 @@ class SettingsActivity : BaseActivity() {
private const val START_DELAY: Long = 5000
private const val DISABLED_ALPHA: Float = 0.38f
private const val ENABLED_ALPHA: Float = 1.0f
private const val HTTP_CODE_OK: Int = 200
private const val PHONE_NUMBER_SIDE_PADDING: Int = 50
const val HTTP_CODE_OK: Int = 200
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Nextcloud Talk application
*
* @author Andy Scherzinger
* @author Parneet Singh
* Copyright (C) 2022 Andy Scherzinger <[email protected]>
* Copyright (C) 2024 Parneet Singh <[email protected]>
*
* 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 <http://www.gnu.org/licenses/>.
*/
package com.nextcloud.talk.ui.dialog

import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.content.res.ColorStateList
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.DialogFragment
import autodagger.AutoInjector
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.textfield.TextInputLayout
import com.nextcloud.talk.R
import com.nextcloud.talk.application.NextcloudTalkApplication
import com.nextcloud.talk.databinding.DialogSetPhoneNumberBinding
import com.nextcloud.talk.ui.theme.ViewThemeUtils
import javax.inject.Inject

@AutoInjector(NextcloudTalkApplication::class)
class SetPhoneNumberDialogFragment : DialogFragment() {

@Inject
lateinit var viewThemeUtils: ViewThemeUtils

private lateinit var clickListener: SetPhoneNumberDialogClickListener

private lateinit var binding: DialogSetPhoneNumberBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
NextcloudTalkApplication.sharedApplication?.componentApplication?.inject(this)
}

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
binding = DialogSetPhoneNumberBinding.inflate(requireActivity().layoutInflater)

val dialogBuilder =
MaterialAlertDialogBuilder(requireActivity())
.setTitle(R.string.nc_settings_phone_book_integration_phone_number_dialog_title)
.setView(binding.root)
.setMessage(R.string.nc_settings_phone_book_integration_phone_number_dialog_description)
.setPositiveButton(requireContext().resources.getString(R.string.nc_common_set)) { dialog, _ ->
clickListener
.onSubmitClick(binding.phoneInputLayout, dialog)
}
.setNegativeButton(requireContext().resources.getString(R.string.nc_common_skip), null)

viewThemeUtils.dialog.colorMaterialAlertDialogBackground(requireActivity(), dialogBuilder)

binding.phoneInputLayout.setHelperTextColor(
ColorStateList.valueOf(resources.getColor(R.color.nc_darkRed, null))
)

binding.phoneEditTextField.addTextChangedListener(object : TextWatcher {
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
binding.phoneInputLayout.helperText = ""
}

override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
// unused atm
}
override fun afterTextChanged(s: Editable?) {
// unused atm
}
})

return dialogBuilder.create()
}

override fun onStart() {
super.onStart()
val alertDialog = dialog as AlertDialog?
alertDialog?.let {
viewThemeUtils.platform.colorTextButtons(it.getButton(AlertDialog.BUTTON_POSITIVE))
viewThemeUtils.platform.colorTextButtons(it.getButton(AlertDialog.BUTTON_NEGATIVE))
}
}

override fun onAttach(context: Context) {
super.onAttach(context)
clickListener = context as SetPhoneNumberDialogClickListener
}

interface SetPhoneNumberDialogClickListener {
fun onSubmitClick(textInputLayout: TextInputLayout, dialog: DialogInterface)
}

companion object {
val TAG: String = SetPhoneNumberDialogFragment::class.java.simpleName

fun newInstance(): DialogFragment {
return SetPhoneNumberDialogFragment()
}
}
}
42 changes: 42 additions & 0 deletions app/src/main/res/layout/dialog_set_phone_number.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!--
~ Nextcloud Talk application
~
~ @author Parneet Singh
~ Copyright (C) 2024 Parneet Singh <[email protected]>
~
~ 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 <http://www.gnu.org/licenses/>.
-->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:paddingStart="@dimen/standard_half_padding"
android:paddingEnd="@dimen/standard_half_padding">

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/phone_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:prefixText="+"
android:hint="@string/nc_settings_phone_book_integration_phone_number_dialog_edit_text_hint">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/phone_edit_text_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:inputType="phone" />

</com.google.android.material.textfield.TextInputLayout>
</FrameLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ How to translate with transifex:
<string name="nc_settings_phone_book_integration_phone_number_dialog_description">You can set your phone number so other users will be able to find you</string>
<string name="nc_settings_phone_book_integration_phone_number_dialog_invalid">Invalid phone number</string>
<string name="nc_settings_phone_book_integration_phone_number_dialog_success">Phone number set successfully</string>
<string name="nc_settings_phone_book_integration_phone_number_dialog_edit_text_hint">Enter phone number</string>
<string name="no_phone_book_integration_due_to_permissions">No phone number integration due to missing permissions</string>
<string name="nc_phone_book_integration_chat_via">Chat via %s</string>
<string name="nc_phone_book_integration_account_not_found">Account not found</string>
Expand Down

0 comments on commit 7a64c53

Please sign in to comment.