-
-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e70a31a
commit 8a8604f
Showing
4 changed files
with
203 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
app/src/main/java/com/geeksville/mesh/ui/ShareFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = {}, | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters