Skip to content

Commit

Permalink
feat: add sharing view
Browse files Browse the repository at this point in the history
  • Loading branch information
remi-espie committed Nov 18, 2024
1 parent e70a31a commit 8a8604f
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 0 deletions.
8 changes: 8 additions & 0 deletions app/src/main/java/com/geeksville/mesh/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ class MainActivity : AppCompatActivity(), Logging {
}
} catch (e: SerializationException) {
debug("Failed to decode JSON: ${e.message}; falling back to default message")
shareMessages(text)
}
}
}
Expand Down Expand Up @@ -648,6 +649,13 @@ class MainActivity : AppCompatActivity(), Logging {
}
}

private fun shareMessages(message: String?) {
model.setCurrentTab(0)
if (message != null) {
supportFragmentManager.navigateToShareMessage(message)
}
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.menu_main, menu)
Expand Down
130 changes: 130 additions & 0 deletions app/src/main/java/com/geeksville/mesh/ui/ShareFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package com.geeksville.mesh.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.core.os.bundleOf
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.geeksville.mesh.android.Logging
import com.geeksville.mesh.R
import com.geeksville.mesh.databinding.ShareFragmentBinding
import com.geeksville.mesh.model.Contact
import com.geeksville.mesh.model.UIViewModel
import com.geeksville.mesh.ui.theme.AppTheme
import dagger.hilt.android.AndroidEntryPoint

internal fun FragmentManager.navigateToShareMessage(message: String) {
val shareFragment = ShareFragment().apply {
arguments = bundleOf("message" to message)
}
beginTransaction()
.add(R.id.mainActivityLayout, shareFragment)
.addToBackStack(null)
.commit()
}

@AndroidEntryPoint
class ShareFragment : ScreenFragment("Messages"), Logging {

private val model: UIViewModel by activityViewModels()
private var _binding: ShareFragmentBinding? = null

// This property is only valid between onCreateView and onDestroyView.
private val binding get() = _binding!!

private val contacts get() = model.contactList.value
private var selectedContact = mutableStateOf("")

private fun shareMessage(contact: Contact) {
debug("calling MessagesFragment filter:${contact.contactKey}")
parentFragmentManager.navigateToPreInitMessages(
contact.contactKey,
contact.longName,
arguments?.getString("message").toString()
)
}

private fun onClick(contact: Contact) {
if (selectedContact.value == contact.contactKey) {
selectedContact.value = ""
binding.shareButton.isEnabled = false
} else {
selectedContact.value = contact.contactKey
binding.shareButton.isEnabled = true
}
}

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = ShareFragmentBinding.inflate(inflater, container, false)
return binding.root
}

@Suppress("LongMethod", "CyclomaticComplexMethod")
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.toolbar.setNavigationOnClickListener {
parentFragmentManager.popBackStack()
}

binding.shareButton.isEnabled = false

binding.shareButton.setOnClickListener {
debug("User clicked shareButton")
val contact = contacts.find { c -> c.contactKey == selectedContact.value }
if (contact != null) {
shareMessage(contact)
}
}

binding.contactListView.setContent {
val contacts by model.contactList.collectAsStateWithLifecycle()
AppTheme {
ShareContactListView(
contacts = contacts,
selectedContact = selectedContact.value,
onClick = ::onClick,
)
}
}
}
}


@Composable
fun ShareContactListView(
contacts: List<Contact>,
selectedContact: String,
onClick: (Contact) -> Unit,
) {
LazyColumn(
modifier = Modifier
.fillMaxSize(),
contentPadding = PaddingValues(6.dp),
) {
items(contacts, key = { it.contactKey }) { contact ->
val selected = contact.contactKey == selectedContact
ContactItem(
contact = contact,
selected = selected,
onClick = { onClick(contact) },
onLongClick = {},
)
}
}
}
63 changes: 63 additions & 0 deletions app/src/main/res/layout/share_fragment.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?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:background="@color/colorAdvancedBackground">

<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
style="@style/MyToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/toolbarBackground"
android:title="@string/share_to"
app:title="@string/share_to"
app:layout_constraintHeight_min="?attr/actionBarSize"
app:layout_constraintTop_toTopOf="parent"
app:navigationIcon="?android:attr/homeAsUpIndicator"
app:titleMargin="4dp" />

<androidx.compose.ui.platform.ComposeView
android:id="@+id/contactListView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="8dp"
android:contentDescription="@string/text_messages"
app:layout_constraintBottom_toTopOf="@+id/quickChatView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/toolbar">

</androidx.compose.ui.platform.ComposeView>

<HorizontalScrollView
android:id="@+id/quickChatView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/shareButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">

<LinearLayout
android:id="@+id/quickChatLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" />
</HorizontalScrollView>


<ImageButton
android:id="@+id/shareButton"
android:layout_width="match_parent"
android:layout_height="64dp"
android:contentDescription="@string/share_message"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="@drawable/ic_twotone_send_24" />

</androidx.constraintlayout.widget.ConstraintLayout>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@
<string name="fair">Fair</string>
<string name="good">Good</string>
<string name="none_quality">None</string>
<string name="share_to">Share to…</string>
<string name="share_message">Share message</string>
<string name="signal">Signal</string>
<string name="signal_quality">Signal Quality</string>
<string name="traceroute_log">Traceroute Log</string>
Expand Down

0 comments on commit 8a8604f

Please sign in to comment.