Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

Commit

Permalink
Change IS + Agree to terms
Browse files Browse the repository at this point in the history
  • Loading branch information
BillCarsonFr committed Sep 9, 2019
1 parent 134e941 commit c0c0130
Show file tree
Hide file tree
Showing 18 changed files with 730 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class ReviewTermsActivity : SimpleFragmentActivity() {

private const val EXTRA_INFO = "EXTRA_INFO"

fun intent(context: Context, serviceType: TermsManager.ServiceType, baseUrl: String, token: String): Intent {
fun intent(context: Context, serviceType: TermsManager.ServiceType, baseUrl: String, token: String?): Intent {
return Intent(context, ReviewTermsActivity::class.java).also {
it.putExtra(EXTRA_INFO, ServiceTermsArgs(serviceType, baseUrl, token))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@ package im.vector.fragments
import android.content.Context
import android.os.Bundle
import android.os.Parcelable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import butterknife.ButterKnife
import com.airbnb.mvrx.MvRx
import com.airbnb.mvrx.MvRxView
import com.airbnb.mvrx.MvRxViewModelStore
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import im.vector.R
import im.vector.activity.VectorAppCompatActivity
import java.util.*

Expand All @@ -34,6 +40,8 @@ abstract class VectorBaseBottomSheetDialogFragment : BottomSheetDialogFragment()
private lateinit var mvrxPersistedViewId: String
final override val mvrxViewId: String by lazy { mvrxPersistedViewId }

@LayoutRes
abstract fun getLayoutResId(): Int

protected var vectorActivity: VectorAppCompatActivity? = null

Expand All @@ -49,6 +57,11 @@ abstract class VectorBaseBottomSheetDialogFragment : BottomSheetDialogFragment()
vectorActivity = null
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(getLayoutResId(), container, false)
ButterKnife.bind(this, view)
return view
}

override fun onCreate(savedInstanceState: Bundle?) {
mvrxViewModelStore.restoreViewModels(this, savedInstanceState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
*/
package im.vector.fragments.discovery

import androidx.lifecycle.MutableLiveData
import com.airbnb.mvrx.*
import im.vector.Matrix
import im.vector.ui.arch.LiveEvent
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.matrix.androidsdk.MXSession
Expand All @@ -25,16 +27,24 @@ import org.matrix.androidsdk.core.model.MatrixError
import org.matrix.androidsdk.features.identityserver.IdentityServerManager


data class PIState(
data class PidInfo(
val value: String,
val isShared: Async<Boolean>
)
val isShared: Async<SharedState>
) {
enum class SharedState {
SHARED,
NOT_SHARED,
PENDING
}
}

data class PidState(val value: String, val isShared: Async<Boolean>)

data class DiscoverySettingsState(
val modalLoadingState: Async<Boolean> = Success(false),
val identityServer: Async<String?> = Success(null),
val emailList: Async<List<PIState>> = Success(emptyList()),
val phoneNumbersList: Async<List<PIState>> = Success(emptyList())
val emailList: Async<List<PidInfo>> = Success(emptyList()),
val phoneNumbersList: Async<List<PidInfo>> = Success(emptyList())
) : MvRxState

class DiscoverySettingsViewModel(initialState: DiscoverySettingsState, private val mxSession: MXSession?) : BaseMvRxViewModel<DiscoverySettingsState>(initialState, false) {
Expand All @@ -52,6 +62,7 @@ class DiscoverySettingsViewModel(initialState: DiscoverySettingsState, private v
identityServer = Success(server)
)
}
refreshModel()
}

override fun onUnexpectedError(e: Exception?) {
Expand Down Expand Up @@ -117,7 +128,7 @@ class DiscoverySettingsViewModel(initialState: DiscoverySettingsState, private v
copy(emailList = Success(
currentMails.map {
if (it.value == email) {
it.copy(isShared = Success(true))
it.copy(isShared = Success(PidInfo.SharedState.PENDING))
} else {
it
}
Expand Down Expand Up @@ -147,7 +158,7 @@ class DiscoverySettingsViewModel(initialState: DiscoverySettingsState, private v
copy(emailList = Success(
currentMails.map {
if (it.value == email) {
it.copy(isShared = Success(false))
it.copy(isShared = Success(PidInfo.SharedState.NOT_SHARED))
} else {
it
}
Expand Down Expand Up @@ -177,7 +188,7 @@ class DiscoverySettingsViewModel(initialState: DiscoverySettingsState, private v
copy(phoneNumbersList = Success(
currentPN.map {
if (it.value == pn) {
it.copy(isShared = Success(false))
it.copy(isShared = Success(PidInfo.SharedState.NOT_SHARED))
} else {
it
}
Expand Down Expand Up @@ -207,7 +218,7 @@ class DiscoverySettingsViewModel(initialState: DiscoverySettingsState, private v
copy(phoneNumbersList = Success(
currentPN.map {
if (it.value == pn) {
it.copy(isShared = Success(false))
it.copy(isShared = Success(PidInfo.SharedState.SHARED))
} else {
it
}
Expand Down Expand Up @@ -271,7 +282,7 @@ class DiscoverySettingsViewModel(initialState: DiscoverySettingsState, private v
setState {
copy(
emailList = Success(
linkedMailsInfo?.map { PIState(it.address, Loading()) }
linkedMailsInfo?.map { PidInfo(it.address, Loading()) }
?: emptyList()
)
)
Expand All @@ -284,10 +295,11 @@ class DiscoverySettingsViewModel(initialState: DiscoverySettingsState, private v
setState {
copy(
emailList = Success(linkedMailsInfo.map {
PIState(
val hasMatrxId = info?.get(knownEmailList.indexOf(it.address))?.isBlank()?.not()
?: false
PidInfo(
value = it.address,
isShared = Success(info?.get(knownEmailList.indexOf(it.address))?.isBlank()?.not()
?: false)
isShared = Success( PidInfo.SharedState.SHARED.takeIf { hasMatrxId } ?: PidInfo.SharedState.NOT_SHARED)
)
})
)
Expand All @@ -313,7 +325,7 @@ class DiscoverySettingsViewModel(initialState: DiscoverySettingsState, private v
setState {
copy(
phoneNumbersList = Success(
linkedPNInfo?.map { PIState(it.address, Loading()) }
linkedPNInfo?.map { PidInfo(it.address, Loading()) }
?: emptyList()
)
)
Expand All @@ -326,10 +338,11 @@ class DiscoverySettingsViewModel(initialState: DiscoverySettingsState, private v
setState {
copy(
phoneNumbersList = Success(linkedPNInfo.map {
PIState(
val hasMatrixId = (info?.get(knownPns.indexOf(it.address))?.isBlank()?.not()
?: false)
PidInfo(
value = it.address,
isShared = Success(info?.get(knownPns.indexOf(it.address))?.isBlank()?.not()
?: false)
isShared = Success(PidInfo.SharedState.SHARED.takeIf { hasMatrixId } ?: PidInfo.SharedState.NOT_SHARED)
)
})
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2019 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.fragments.discovery

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import im.vector.ui.arch.LiveEvent

class DiscoverySharedViewModel : ViewModel() {

var navigateEvent = MutableLiveData<LiveEvent<Pair<String, String>>>()

companion object {
const val NEW_IDENTITY_SERVER_SET_REQUEST = "NEW_IDENTITY_SERVER_SET_REQUEST"
}

fun requestChangeToIdentityServer(server: String) {
navigateEvent.postValue(LiveEvent(NEW_IDENTITY_SERVER_SET_REQUEST to server))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
/*
* Copyright 2019 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.fragments.discovery

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.os.Parcelable
import android.text.Editable
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import android.widget.ProgressBar
import androidx.appcompat.app.AlertDialog
import androidx.core.view.isVisible
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import butterknife.BindView
import butterknife.OnTextChanged
import com.airbnb.mvrx.MvRx
import com.airbnb.mvrx.fragmentViewModel
import com.airbnb.mvrx.withState
import im.vector.R
import im.vector.activity.MXCActionBarActivity
import im.vector.activity.ReviewTermsActivity
import im.vector.activity.util.TERMS_REQUEST_CODE
import im.vector.extensions.withArgs
import im.vector.fragments.VectorBaseMvRxFragment
import kotlinx.android.parcel.Parcelize
import org.matrix.androidsdk.features.terms.TermsManager

@Parcelize
data class SetIdentityServerFragmentArgs(
var matrixId: String,
var serverName: String? = null
) : Parcelable


class SetIdentityServerFragment : VectorBaseMvRxFragment() {

override fun getLayoutResId() = R.layout.fragment_set_identity_server

@BindView(R.id.discovery_identity_server_enter_til)
lateinit var mKeyInputLayout: com.google.android.material.textfield.TextInputLayout

@BindView(R.id.discovery_identity_server_enter_edittext)
lateinit var mKeyTextEdit: EditText

@BindView(R.id.discovery_identity_server_loading)
lateinit var mProgressBar: ProgressBar


private val viewModel by fragmentViewModel(SetIdentityServerViewModel::class)

lateinit var sharedViewModel: DiscoverySharedViewModel

override fun invalidate() = withState(viewModel) { state ->
if (state.isVerifyingServer) {
mKeyTextEdit.isEnabled = false
mProgressBar.isVisible = true
} else {
mKeyTextEdit.isEnabled = true
mProgressBar.isVisible = false
}
val newText = state.newIdentityServer ?: ""
if (!newText.equals(mKeyTextEdit.text.toString())) {
mKeyTextEdit.setText(newText)
}
mKeyInputLayout.error = state.errorMessageId?.let { getString(it) }
}

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

sharedViewModel = ViewModelProviders.of(requireActivity()).get(DiscoverySharedViewModel::class.java)

mKeyTextEdit.setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
withState(viewModel) { state ->
if (!state.isVerifyingServer) {
viewModel.doChangeServerName()
}
}
return@setOnEditorActionListener true
}
return@setOnEditorActionListener false
}


viewModel.navigateEvent.observe(this, Observer {
it.getContentIfNotHandled()?.let { event ->

when (event) {
SetIdentityServerViewModel.NAVIGATE_NO_TERMS -> {
AlertDialog.Builder(requireContext())
.setTitle(R.string.settings_discovery_no_terms_title)
.setMessage(R.string.settings_discovery_no_terms)
.setPositiveButton(R.string._continue) { dialog, which ->
processIdentityServerChange()
}
.setNegativeButton(R.string.cancel, null)
.show()
}

SetIdentityServerViewModel.NAVIGATE_TERMS_ACCEPTED -> {
processIdentityServerChange()
}

SetIdentityServerViewModel.NAVIGATE_SHOW_TERMS -> {
withState(viewModel) { state ->
ReviewTermsActivity.intent(requireContext(),
TermsManager.ServiceType.IdentityService,
viewModel.sanitatizeBaseURL(state.newIdentityServer ?: ""),
null).also {
startActivityForResult(it, TERMS_REQUEST_CODE)
}
}
}
else -> {
}

}
}
})

}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == TERMS_REQUEST_CODE) {
if (Activity.RESULT_OK == resultCode) {
processIdentityServerChange()
} else {
//add some error?
}
}
super.onActivityResult(requestCode, resultCode, data)
}

private fun processIdentityServerChange() {
withState(viewModel) { state ->
if (state.newIdentityServer != null) {
sharedViewModel.requestChangeToIdentityServer(state.newIdentityServer)
requireFragmentManager().popBackStack()
}
}
}

@OnTextChanged(R.id.discovery_identity_server_enter_edittext)
fun onTextEditChange(s: Editable?) {
s?.toString()?.let { viewModel.updateServerName(it) }
}

override fun onResume() {
super.onResume()
(activity as? MXCActionBarActivity)?.supportActionBar?.setTitle(R.string.identity_server)
}

companion object {
fun newInstance(matrixId: String, existingServer: String?) = SetIdentityServerFragment()
.withArgs {
putParcelable(MvRx.KEY_ARG, SetIdentityServerFragmentArgs(matrixId, existingServer))
}
}
}
Loading

0 comments on commit c0c0130

Please sign in to comment.