Skip to content

Commit

Permalink
fix lifecycle
Browse files Browse the repository at this point in the history
Signed-off-by: alperozturk <[email protected]>
  • Loading branch information
alperozturk96 committed Feb 5, 2025
1 parent cd7ad98 commit a7bcab0
Showing 1 changed file with 48 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import android.widget.AdapterView
import android.widget.ArrayAdapter
import androidx.core.app.ActivityCompat.finishAffinity
import androidx.fragment.app.DialogFragment
import androidx.lifecycle.lifecycleScope
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.nextcloud.android.lib.resources.tos.GetTermsRemoteOperation
import com.nextcloud.android.lib.resources.tos.SignTermRemoteOperation
Expand All @@ -29,9 +30,9 @@ import com.owncloud.android.lib.common.operations.RemoteOperationResult
import com.owncloud.android.lib.common.utils.Log_OC
import com.owncloud.android.utils.DisplayUtils
import com.owncloud.android.utils.theme.ViewThemeUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import javax.inject.Inject

class TermsOfServiceDialog : DialogFragment(), Injectable {
Expand All @@ -50,52 +51,56 @@ class TermsOfServiceDialog : DialogFragment(), Injectable {
lateinit var terms: List<Term>
lateinit var languages: Map<String, String>

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

fetchTerms()
}

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

return createDialogBuilder().create()
}

override fun onStart() {
super.onStart()
fetchTerms()
}

private fun updateDialog() {
binding.message.setHtmlContent(terms[0].renderedBody)

val arrayAdapter: ArrayAdapter<String> = ArrayAdapter(
val arrayAdapter: ArrayAdapter<String> = ArrayAdapter<String>(
binding.root.context,
android.R.layout.simple_spinner_item
)
).apply {
for ((_, _, languageCode) in terms) {
add(languages[languageCode])
}

for ((_, _, languageCode) in terms) {
arrayAdapter.add(languages[languageCode])
setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
}

arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
setupLanguageDropdown(arrayAdapter)
}

binding.languageDropdown.adapter = arrayAdapter
binding.languageDropdown.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(adapterView: AdapterView<*>?, view: View, position: Int, l: Long) {
binding.message
.setHtmlContent(terms[position].renderedBody)
}
private fun setupLanguageDropdown(arrayAdapter: ArrayAdapter<String>) {
binding.languageDropdown.run {
adapter = arrayAdapter
onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(adapterView: AdapterView<*>?, view: View, position: Int, l: Long) {
binding.message
.setHtmlContent(terms[position].renderedBody)
}

override fun onNothingSelected(adapterView: AdapterView<*>?) = Unit
}
override fun onNothingSelected(adapterView: AdapterView<*>?) = Unit
}

if (terms.size == 1) {
binding.languageDropdown.visibility = View.GONE
if (terms.size == 1) {
visibility = View.GONE
}
}
}

@Suppress("DEPRECATION")
private fun fetchTerms() {
CoroutineScope(Dispatchers.IO).launch {
//viewLifecycleOwner.lifecycleScope.launch {
lifecycleScope.launch(Dispatchers.IO) {
try {
client = clientFactory.createNextcloudClient(accountManager.getUser())
client = clientFactory.createNextcloudClient(accountManager.user)
val result = GetTermsRemoteOperation().execute(client)

if (result.isSuccess &&
Expand All @@ -105,8 +110,7 @@ class TermsOfServiceDialog : DialogFragment(), Injectable {
languages = result.resultData.languages
terms = result.resultData.terms

CoroutineScope(Dispatchers.Main).launch {
// withContext(Dispatchers.Main) {
withContext(Dispatchers.Main) {
updateDialog()
}
}
Expand All @@ -125,17 +129,25 @@ class TermsOfServiceDialog : DialogFragment(), Injectable {
}
.setPositiveButton(R.string.terms_of_services_agree) { dialog, _ ->
dialog.dismiss()
Thread {
val id = binding.languageDropdown.selectedItemPosition
val signResult: RemoteOperationResult<Void> =
SignTermRemoteOperation(terms.get(id).id).execute(client)
if (!signResult.isSuccess) {
DisplayUtils.showSnackMessage(view, R.string.sign_tos_failed)
}
}.start()
agreeToS()
}
}

private fun agreeToS() {
lifecycleScope.launch(Dispatchers.IO) {
val id = binding.languageDropdown.selectedItemPosition
val signResult: RemoteOperationResult<Void> =
SignTermRemoteOperation(terms[id].id).execute(client)

if (!signResult.isSuccess) {
withContext(Dispatchers.Main) {
DisplayUtils.showSnackMessage(view, R.string.sign_tos_failed)
}
}
}
}


companion object {
private const val TAG = "TermsOfServiceDialog"
}
Expand Down

0 comments on commit a7bcab0

Please sign in to comment.