From 1091ae41ad95c41bb5aa6f7331081c3b269c0a5d Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Mon, 19 Sep 2022 11:18:49 +0200 Subject: [PATCH] Default structure for the new code --- vector/src/main/AndroidManifest.xml | 1 + .../v2/rename/RenameSessionActivity.kt | 52 +++++++++++++++ .../devices/v2/rename/RenameSessionArgs.kt | 25 ++++++++ .../v2/rename/RenameSessionFragment.kt | 63 +++++++++++++++++++ .../v2/rename/RenameSessionViewEvent.kt | 23 +++++++ .../v2/rename/RenameSessionViewModel.kt | 57 +++++++++++++++++ .../v2/rename/RenameSessionViewNavigator.kt | 28 +++++++++ .../v2/rename/RenameSessionViewState.kt | 28 +++++++++ .../res/layout/fragment_session_rename.xml | 8 +++ 9 files changed, 285 insertions(+) create mode 100644 vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionActivity.kt create mode 100644 vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionArgs.kt create mode 100644 vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionFragment.kt create mode 100644 vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionViewEvent.kt create mode 100644 vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionViewModel.kt create mode 100644 vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionViewNavigator.kt create mode 100644 vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionViewState.kt create mode 100644 vector/src/main/res/layout/fragment_session_rename.xml diff --git a/vector/src/main/AndroidManifest.xml b/vector/src/main/AndroidManifest.xml index bb8ca8cf5f..b16e4505a6 100644 --- a/vector/src/main/AndroidManifest.xml +++ b/vector/src/main/AndroidManifest.xml @@ -325,6 +325,7 @@ + diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionActivity.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionActivity.kt new file mode 100644 index 0000000000..36161610cd --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionActivity.kt @@ -0,0 +1,52 @@ +/* + * Copyright 2022 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.app.features.settings.devices.v2.rename + +import android.content.Context +import android.content.Intent +import android.os.Bundle +import com.airbnb.mvrx.Mavericks +import dagger.hilt.android.AndroidEntryPoint +import im.vector.app.core.extensions.addFragment +import im.vector.app.core.platform.SimpleFragmentActivity + +/** + * Display the screen to rename a Session. + */ +@AndroidEntryPoint +class RenameSessionActivity : SimpleFragmentActivity() { + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + if (isFirstCreation()) { + addFragment( + container = views.container, + fragmentClass = RenameSessionFragment::class.java, + params = intent.getParcelableExtra(Mavericks.KEY_ARG) + ) + } + } + + companion object { + fun newIntent(context: Context, deviceId: String): Intent { + return Intent(context, RenameSessionActivity::class.java).apply { + putExtra(Mavericks.KEY_ARG, RenameSessionArgs(deviceId)) + } + } + } +} diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionArgs.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionArgs.kt new file mode 100644 index 0000000000..d43d472946 --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionArgs.kt @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2022 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.app.features.settings.devices.v2.rename + +import android.os.Parcelable +import kotlinx.parcelize.Parcelize + +@Parcelize +data class RenameSessionArgs( + val deviceId: String +) : Parcelable diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionFragment.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionFragment.kt new file mode 100644 index 0000000000..2b071e524d --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionFragment.kt @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2022 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.app.features.settings.devices.v2.rename + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import com.airbnb.mvrx.fragmentViewModel +import com.airbnb.mvrx.withState +import dagger.hilt.android.AndroidEntryPoint +import im.vector.app.core.platform.VectorBaseFragment +import im.vector.app.databinding.FragmentSessionRenameBinding +import javax.inject.Inject + +/** + * Display the screen to rename a Session. + */ +@AndroidEntryPoint +class RenameSessionFragment : + VectorBaseFragment() { + + private val viewModel: RenameSessionViewModel by fragmentViewModel() + + @Inject lateinit var viewNavigator: RenameSessionViewNavigator + + override fun getBinding(inflater: LayoutInflater, container: ViewGroup?): FragmentSessionRenameBinding { + return FragmentSessionRenameBinding.inflate(inflater, container, false) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + observeViewEvents() + } + + private fun observeViewEvents() { + viewModel.observeViewEvents { + when (it) { + is RenameSessionViewEvent.SessionRenamed -> { + viewNavigator.navigateBack(requireActivity()) + } + } + } + } + + override fun invalidate() = withState(viewModel) { _ -> + // TODO + } +} diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionViewEvent.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionViewEvent.kt new file mode 100644 index 0000000000..656c5bb165 --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionViewEvent.kt @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2022 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.app.features.settings.devices.v2.rename + +import im.vector.app.core.platform.VectorViewEvents + +sealed class RenameSessionViewEvent : VectorViewEvents { + object SessionRenamed : RenameSessionViewEvent() +} diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionViewModel.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionViewModel.kt new file mode 100644 index 0000000000..ebd30b7ef6 --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionViewModel.kt @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2022 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.app.features.settings.devices.v2.rename + +import com.airbnb.mvrx.MavericksViewModelFactory +import dagger.assisted.Assisted +import dagger.assisted.AssistedFactory +import dagger.assisted.AssistedInject +import im.vector.app.core.di.MavericksAssistedViewModelFactory +import im.vector.app.core.di.hiltMavericksViewModelFactory +import im.vector.app.core.platform.EmptyAction +import im.vector.app.core.platform.VectorViewModel +import im.vector.app.features.settings.devices.v2.overview.GetDeviceFullInfoUseCase +import kotlinx.coroutines.flow.launchIn +import kotlinx.coroutines.flow.onEach + +// TODO add unit tests +class RenameSessionViewModel @AssistedInject constructor( + @Assisted val initialState: RenameSessionViewState, + private val getDeviceFullInfoUseCase: GetDeviceFullInfoUseCase, +) : VectorViewModel(initialState) { + + companion object : MavericksViewModelFactory by hiltMavericksViewModelFactory() + + @AssistedFactory + interface Factory : MavericksAssistedViewModelFactory { + override fun create(initialState: RenameSessionViewState): RenameSessionViewModel + } + + init { + observeSessionInfo(initialState.deviceId) + } + + private fun observeSessionInfo(deviceId: String) { + getDeviceFullInfoUseCase.execute(deviceId) + .onEach { setState { copy(deviceName = it.deviceInfo.displayName.orEmpty()) } } + .launchIn(viewModelScope) + } + + override fun handle(action: EmptyAction) { + // do nothing + } +} diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionViewNavigator.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionViewNavigator.kt new file mode 100644 index 0000000000..229d0bc70c --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionViewNavigator.kt @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2022 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.app.features.settings.devices.v2.rename + +import androidx.fragment.app.FragmentActivity +import javax.inject.Inject + +// TODO add unit tests +class RenameSessionViewNavigator @Inject constructor() { + + fun navigateBack(activity: FragmentActivity) { + activity.finish() + } +} diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionViewState.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionViewState.kt new file mode 100644 index 0000000000..18abba9c8d --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionViewState.kt @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2022 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.app.features.settings.devices.v2.rename + +import com.airbnb.mvrx.MavericksState + +data class RenameSessionViewState( + val deviceId: String, + val deviceName: String = "", +) : MavericksState { + constructor(args: RenameSessionArgs) : this( + deviceId = args.deviceId + ) +} diff --git a/vector/src/main/res/layout/fragment_session_rename.xml b/vector/src/main/res/layout/fragment_session_rename.xml new file mode 100644 index 0000000000..7911f36b00 --- /dev/null +++ b/vector/src/main/res/layout/fragment_session_rename.xml @@ -0,0 +1,8 @@ + + + +