From d5c9df3864cc33337660c79a97107e97bb533433 Mon Sep 17 00:00:00 2001
From: Taras Smakula <tarassmakula@gmail.com>
Date: Tue, 25 Jul 2023 16:46:23 +0300
Subject: [PATCH] Create dialog fragment for timeline options

---
 .../options/TimelineOptionsDialogFragment.kt  | 122 ++++++++++++
 .../dialog_fragment_timeline_options.xml      | 174 ++++++++++++++++++
 2 files changed, 296 insertions(+)
 create mode 100644 app/src/main/java/org/futo/circles/feature/timeline/options/TimelineOptionsDialogFragment.kt
 create mode 100644 app/src/main/res/layout/dialog_fragment_timeline_options.xml

diff --git a/app/src/main/java/org/futo/circles/feature/timeline/options/TimelineOptionsDialogFragment.kt b/app/src/main/java/org/futo/circles/feature/timeline/options/TimelineOptionsDialogFragment.kt
new file mode 100644
index 000000000..753cd80a8
--- /dev/null
+++ b/app/src/main/java/org/futo/circles/feature/timeline/options/TimelineOptionsDialogFragment.kt
@@ -0,0 +1,122 @@
+package org.futo.circles.feature.timeline.options
+
+import android.os.Bundle
+import android.view.View
+import androidx.fragment.app.viewModels
+import androidx.navigation.fragment.navArgs
+import dagger.hilt.android.AndroidEntryPoint
+import org.futo.circles.R
+import org.futo.circles.core.extensions.isCurrentUserAbleToChangeSettings
+import org.futo.circles.core.extensions.isCurrentUserAbleToInvite
+import org.futo.circles.core.extensions.isCurrentUserOnlyAdmin
+import org.futo.circles.core.extensions.observeData
+import org.futo.circles.core.extensions.observeResponse
+import org.futo.circles.core.extensions.onBackPressed
+import org.futo.circles.core.extensions.setIsVisible
+import org.futo.circles.core.extensions.showDialog
+import org.futo.circles.core.extensions.withConfirmation
+import org.futo.circles.core.fragment.BaseFullscreenDialogFragment
+import org.futo.circles.core.model.CircleRoomTypeArg
+import org.futo.circles.core.provider.PreferencesProvider
+import org.futo.circles.core.utils.getTimelineRoomFor
+import org.futo.circles.databinding.DialogFragmentTimelineOptionsBinding
+import org.futo.circles.model.DeleteCircle
+import org.futo.circles.model.DeleteGroup
+import org.futo.circles.model.LeaveGroup
+
+@AndroidEntryPoint
+class TimelineOptionsDialogFragment :
+    BaseFullscreenDialogFragment(DialogFragmentTimelineOptionsBinding::inflate) {
+
+    private val binding by lazy {
+        getBinding() as DialogFragmentTimelineOptionsBinding
+    }
+
+    private val args: TimelineOptionsDialogFragmentArgs by navArgs()
+    private val isGroupMode by lazy { args.type == CircleRoomTypeArg.Group }
+    private val timelineId by lazy {
+        if (isGroupMode) args.roomId
+        else getTimelineRoomFor(args.roomId)?.roomId ?: throw IllegalArgumentException(
+            "Timeline not found"
+        )
+    }
+
+    private val viewModel by viewModels<TimelineOptionsViewModel>()
+    private val preferencesProvider by lazy { PreferencesProvider(requireContext()) }
+    private val navigator by lazy { TimelineOptionsNavigator(this) }
+
+    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
+        super.onViewCreated(view, savedInstanceState)
+        setupViews()
+        setupObservers()
+    }
+
+    private fun setupViews() {
+        with(binding) {
+            tvConfigure.apply {
+                setText(
+                    getString(if (isGroupMode) R.string.configure_group else R.string.configure_circle)
+                )
+                setOnClickListener { navigator.navigateToUpdateRoom(args.roomId, args.type) }
+            }
+            tvDelete.apply {
+                text = getString(if (isGroupMode) R.string.delete_group else R.string.delete_circle)
+                setOnClickListener {
+                    withConfirmation(if (isGroupMode) DeleteGroup() else DeleteCircle()) {
+                        viewModel.delete(isGroupMode)
+                    }
+                }
+            }
+            tvStateEvents.apply {
+                setIsVisible(preferencesProvider.isDeveloperModeEnabled())
+                setOnClickListener { navigator.navigateToStateEvents(timelineId) }
+            }
+            tvInviteMembers.apply {
+                setText(getString(if (isGroupMode) R.string.invite_members else R.string.invite_followers))
+                setOnClickListener { navigator.navigateToInviteMembers(timelineId) }
+            }
+            tvLeave.apply {
+                setIsVisible(isGroupMode)
+                setOnClickListener { showLeaveGroupDialog() }
+            }
+            tvShare.setOnClickListener { navigator.navigateToShareRoom(timelineId) }
+            tvManageMembers.apply {
+                setIsVisible(isGroupMode)
+                setOnClickListener { navigator.navigateToManageMembers(timelineId, args.type) }
+            }
+            tvMyFollowers.apply {
+                setIsVisible(isGroupMode.not())
+                setOnClickListener { navigator.navigateToManageMembers(timelineId, args.type) }
+            }
+            tvPeopleImFollowing.apply {
+                setIsVisible(isGroupMode.not())
+                setOnClickListener { navigator.navigateToFollowing(args.roomId) }
+            }
+        }
+
+    }
+
+    private fun setupObservers() {
+        viewModel.leaveDeleteEventLiveData.observeResponse(this, success = { onBackPressed() })
+        viewModel.accessLevelLiveData.observeData(this) { groupPowerLevelsContent ->
+            if (!isGroupMode) return@observeData
+            with(binding) {
+                tvConfigure.setIsVisible(groupPowerLevelsContent.isCurrentUserAbleToChangeSettings())
+                tvInviteMembers.setIsVisible(groupPowerLevelsContent.isCurrentUserAbleToInvite())
+                tvDelete.setIsVisible(groupPowerLevelsContent.isCurrentUserOnlyAdmin(args.roomId))
+            }
+        }
+    }
+
+
+    private fun showLeaveGroupDialog() {
+        if (viewModel.canLeaveRoom()) {
+            withConfirmation(LeaveGroup()) { viewModel.leaveGroup() }
+        } else {
+            showDialog(
+                titleResIdRes = R.string.leave_group,
+                messageResId = R.string.select_another_admin_message
+            )
+        }
+    }
+}
\ No newline at end of file
diff --git a/app/src/main/res/layout/dialog_fragment_timeline_options.xml b/app/src/main/res/layout/dialog_fragment_timeline_options.xml
new file mode 100644
index 000000000..6c0b756bc
--- /dev/null
+++ b/app/src/main/res/layout/dialog_fragment_timeline_options.xml
@@ -0,0 +1,174 @@
+<?xml version="1.0" encoding="utf-8"?>
+<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:orientation="vertical">
+
+    <com.google.android.material.appbar.MaterialToolbar
+        android:id="@+id/toolbar"
+        android:layout_width="0dp"
+        android:layout_height="?attr/actionBarSize"
+        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintTop_toTopOf="parent"
+        app:navigationIcon="?attr/homeAsUpIndicator"
+        app:titleCentered="true"
+        tools:title="Room name" />
+
+
+    <View
+        android:id="@+id/divider"
+        android:layout_width="0dp"
+        android:layout_height="@dimen/divider_height"
+        android:background="@color/divider_color"
+        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintTop_toBottomOf="@id/toolbar" />
+
+    <ScrollView
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintTop_toBottomOf="@id/divider">
+
+        <LinearLayout
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:orientation="vertical">
+
+            <com.google.android.material.imageview.ShapeableImageView
+                android:id="@+id/ivCover"
+                android:layout_width="@dimen/group_icon_size"
+                android:layout_height="@dimen/group_icon_size"
+                android:layout_gravity="center_horizontal"
+                android:layout_marginTop="8dp"
+                android:clickable="true"
+                android:focusable="true"
+                android:scaleType="centerCrop"
+                app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.App.GroupIconRadius"
+                tools:background="@color/highlight_color" />
+
+            <androidx.constraintlayout.widget.ConstraintLayout
+                android:id="@+id/lPushNotifications"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                android:layout_marginTop="16dp"
+                android:background="?selectableItemBackground"
+                android:clickable="true"
+                android:focusable="true"
+                android:orientation="horizontal">
+
+                <TextView
+                    android:id="@+id/tvPushNotifications"
+                    style="@style/settingMenuItem"
+                    android:layout_width="match_parent"
+                    android:layout_height="wrap_content"
+                    android:text="@string/push_notifications"
+                    app:drawableStartCompat="@drawable/ic_notifications"
+                    app:layout_constraintBottom_toBottomOf="parent"
+                    app:layout_constraintEnd_toEndOf="parent"
+                    app:layout_constraintStart_toStartOf="parent"
+                    app:layout_constraintTop_toTopOf="parent" />
+
+
+                <com.google.android.material.switchmaterial.SwitchMaterial
+                    android:id="@+id/svPushNotifications"
+                    android:layout_width="wrap_content"
+                    android:layout_height="wrap_content"
+                    android:layout_marginEnd="8dp"
+                    android:clickable="false"
+                    android:focusable="false"
+                    app:layout_constraintBottom_toBottomOf="parent"
+                    app:layout_constraintEnd_toEndOf="parent"
+                    app:layout_constraintTop_toTopOf="parent" />
+
+            </androidx.constraintlayout.widget.ConstraintLayout>
+
+
+            <View
+                android:layout_width="match_parent"
+                android:layout_height="@dimen/divider_height"
+                android:layout_marginHorizontal="8dp"
+                android:background="@color/divider_color" />
+
+            <org.futo.circles.view.SettingsMenuItemView
+                android:id="@+id/tvShare"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                app:optionIcon="@drawable/ic_share"
+                app:optionName="@string/share" />
+
+            <org.futo.circles.view.SettingsMenuItemView
+                android:id="@+id/tvStateEvents"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                app:optionIcon="@drawable/ic_info"
+                app:optionName="@string/state_events" />
+
+
+            <org.futo.circles.view.SettingsMenuItemView
+                android:id="@+id/tvConfigure"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                app:optionIcon="@drawable/ic_settings"
+                app:optionName="@string/configure_group" />
+
+
+            <org.futo.circles.view.SettingsMenuItemView
+                android:id="@+id/tvManageMembers"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                app:optionIcon="@drawable/ic_round_people"
+                app:optionName="@string/manage_members" />
+
+
+            <org.futo.circles.view.SettingsMenuItemView
+                android:id="@+id/tvMyFollowers"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                app:optionIcon="@drawable/ic_outline_people"
+                app:optionName="@string/my_followers" />
+
+
+            <org.futo.circles.view.SettingsMenuItemView
+                android:id="@+id/tvPeopleImFollowing"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                app:optionIcon="@drawable/ic_round_people"
+                app:optionName="@string/people_i_m_following" />
+
+
+            <org.futo.circles.view.SettingsMenuItemView
+                android:id="@+id/tvInviteMembers"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                app:optionIcon="@drawable/ic_invite"
+                app:optionName="@string/invite_members" />
+
+
+            <org.futo.circles.view.SettingsMenuItemView
+                android:id="@+id/tvLeave"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                app:optionIcon="@drawable/ic_logout"
+                app:optionName="@string/leave_group" />
+
+            <TextView
+                android:id="@+id/tvDelete"
+                style="@style/settingMenuItem"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                android:text="@string/delete"
+                android:textColor="@color/red"
+                app:drawableStartCompat="@drawable/ic_delete"
+                app:drawableTint="@color/red" />
+
+
+        </LinearLayout>
+
+    </ScrollView>
+
+</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
-- 
GitLab