Skip to content

Commit

Permalink
Merge pull request #11 from KoalaSat/amber-button
Browse files Browse the repository at this point in the history
amber button
  • Loading branch information
KoalaSat authored Oct 25, 2024
2 parents fc1a9f9 + 8cf8b25 commit 029a035
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 69 deletions.
43 changes: 0 additions & 43 deletions app/src/main/java/com/koalasat/pokey/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package com.koalasat.pokey

import android.Manifest
import android.app.Activity
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
Expand All @@ -18,10 +15,6 @@ import androidx.navigation.ui.setupWithNavController
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.koalasat.pokey.databinding.ActivityMainBinding
import com.koalasat.pokey.models.EncryptedStorage
import com.vitorpamplona.quartz.signers.ExternalSignerLauncher
import com.vitorpamplona.quartz.signers.SignerType
import java.util.UUID
import kotlin.coroutines.cancellation.CancellationException

class MainActivity : AppCompatActivity() {
private val requestCodePostNotifications: Int = 1
Expand Down Expand Up @@ -62,8 +55,6 @@ class MainActivity : AppCompatActivity() {
requestCodePostNotifications,
)
}

if (EncryptedStorage.pubKey.value.isNullOrEmpty()) connectExternalSigner()
}

override fun onRequestPermissionsResult(
Expand All @@ -80,38 +71,4 @@ class MainActivity : AppCompatActivity() {
}
}
}

private fun connectExternalSigner() {
val id = UUID.randomUUID().toString()
val externalSignerLauncher = ExternalSignerLauncher("", signerPackageName = "")

val nostrSignerLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode != Activity.RESULT_OK) {
Log.e("Pokey", "ExternalSigner result error: ${result.resultCode}")
} else {
result.data?.let { externalSignerLauncher.newResult(it) }
}
}
externalSignerLauncher.registerLauncher(
launcher = {
try {
nostrSignerLauncher.launch(it) // This can remain if you still need to launch it
} catch (e: Exception) {
if (e is CancellationException) throw e
Log.e("Pokey", "Error opening Signer app", e)
}
},
contentResolver = { Pokey.getInstance().contentResolverFn() },
)
externalSignerLauncher.openSignerApp(
"",
SignerType.GET_PUBLIC_KEY,
"",
id,
) { result ->
val split = result.split("-")
val pubkey = split.first()
if (split.first().isNotEmpty()) EncryptedStorage.updatePubKey(pubkey)
}
}
}
52 changes: 52 additions & 0 deletions app/src/main/java/com/koalasat/pokey/models/ExternalSigner.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.koalasat.pokey.models

import android.app.Activity
import android.content.Intent
import android.util.Log
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.fragment.app.Fragment
import com.koalasat.pokey.Pokey
import com.vitorpamplona.quartz.signers.ExternalSignerLauncher
import com.vitorpamplona.quartz.signers.SignerType
import java.util.UUID
import kotlin.coroutines.cancellation.CancellationException

class ExternalSigner(fragment: Fragment) {
private var nostrSignerLauncher: ActivityResultLauncher<Intent>
private var externalSignerLauncher: ExternalSignerLauncher = ExternalSignerLauncher("", signerPackageName = "")

init {
nostrSignerLauncher = fragment.registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode != Activity.RESULT_OK) {
Log.e("Pokey", "ExternalSigner result error: ${result.resultCode}")
} else {
result.data?.let { externalSignerLauncher.newResult(it) }
}
}
externalSignerLauncher.registerLauncher(
launcher = {
try {
nostrSignerLauncher.launch(it) // This can remain if you still need to launch it
} catch (e: Exception) {
if (e is CancellationException) throw e
Log.e("Pokey", "Error opening Signer app", e)
}
},
contentResolver = { Pokey.getInstance().contentResolverFn() },
)
}

fun savePubKey() {
externalSignerLauncher.openSignerApp(
"",
SignerType.GET_PUBLIC_KEY,
"",
UUID.randomUUID().toString(),
) { result ->
val split = result.split("-")
val pubkey = split.first()
if (split.first().isNotEmpty()) EncryptedStorage.updatePubKey(pubkey)
}
}
}
22 changes: 17 additions & 5 deletions app/src/main/java/com/koalasat/pokey/ui/home/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.fragment.app.viewModels
import com.koalasat.pokey.Pokey
import com.koalasat.pokey.R
import com.koalasat.pokey.databinding.FragmentHomeBinding
import com.koalasat.pokey.models.ExternalSigner

class HomeFragment : Fragment() {
private var _binding: FragmentHomeBinding? = null
Expand All @@ -30,11 +31,7 @@ class HomeFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

viewModel.npubInput.observe(viewLifecycleOwner) { value ->
if (binding.npubInput.text.toString() != value) {
binding.npubInput.setText(value)
}
}
val externalSigner = ExternalSigner(this)

binding.npubInput.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
Expand All @@ -52,25 +49,40 @@ class HomeFragment : Fragment() {
} else {
if (viewModel.validationResult.value == true) {
viewModel.updateServiceStart(true)
binding.npubInput.error = null
} else {
binding.npubInput.error = getString(R.string.invalid_npub)
}
}
}

viewModel.npubInput.observe(viewLifecycleOwner) { value ->
if (binding.npubInput.text.toString() != value) {
binding.npubInput.setText(value)
}
}

Pokey.isEnabled.observe(viewLifecycleOwner) {
if (it) {
val typedValue = TypedValue()
requireContext().theme.resolveAttribute(android.R.attr.colorButtonNormal, typedValue, true)
binding.serviceStart.setBackgroundColor(typedValue.data)
binding.serviceStart.text = getString(R.string.stop)
binding.amber.isEnabled = false
binding.npubInput.isEnabled = false
} else {
val typedValue = TypedValue()
requireContext().theme.resolveAttribute(android.R.attr.colorPrimary, typedValue, true)
binding.serviceStart.setBackgroundColor(typedValue.data)
binding.serviceStart.text = getString(R.string.start)
binding.amber.isEnabled = true
binding.npubInput.isEnabled = true
}
}

binding.amber.setOnClickListener {
externalSigner.savePubKey()
}
}

override fun onDestroyView() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.koalasat.pokey.ui.home

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
Expand All @@ -21,7 +22,6 @@ class HomeViewModel : ViewModel() {
init {
_npubInput.value = EncryptedStorage.pubKey.value
_serviceStart.value = Pokey.isEnabled.value
_validationResult.value = EncryptedStorage.pubKey.value?.isNotEmpty()
EncryptedStorage.pubKey.observeForever { text ->
_npubInput.value = text
}
Expand All @@ -39,17 +39,22 @@ class HomeViewModel : ViewModel() {
fun updateNpubInput(text: String) {
_npubInput.value = text
validateNpubInput()
Log.e("Pokey", "validation: " + _validationResult.value.toString())
if (_validationResult.value == true) {
EncryptedStorage.updatePubKey(text)
}
}

private fun validateNpubInput() {
val parseReturn = uriToRoute(_npubInput.value)

when (parseReturn?.entity) {
is Nip19Bech32.NPub -> {
_validationResult.value = true
return
}
}

_validationResult.value = false
}
}
21 changes: 21 additions & 0 deletions app/src/main/res/drawable/ic_amber.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:aapt="http://schemas.android.com/aapt"
android:viewportWidth="512"
android:viewportHeight="512"
android:width="24dp"
android:height="24dp">
<path
android:pathData="M25.438046 210.42424l209.016024 -2.11128 4.22256 263.90911z"
android:fillColor="#FFFFFF" />
<path
android:pathData="M490.70059 210.38399l-209.01604 -2.11126 -4.22257 263.90904z"
android:fillColor="#FFFFFF" />
<path
android:pathData="M14.881692 170.31006H143.66934L211.23008 39.411116 78.219875 37.299854Z"
android:fillColor="#FFFFFF" />
<path
android:pathData="M499.8572 169.51085H371.06957L303.50881 38.611891 436.51907 36.50063Z"
android:fillColor="#FFFFFF" />
<path
android:pathData="M186.53241 169.91315L255.56681 37.299854 329.46134 170.31006Z"
android:fillColor="#FFFFFF" />
</vector>
59 changes: 39 additions & 20 deletions app/src/main/res/layout/fragment_home.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,64 @@
tools:context=".ui.home.HomeFragment">


<EditText
android:id="@+id/npub_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autofillHints="npub"
android:ems="10"
android:hint="@string/input_npub"
android:inputType="text"
android:minHeight="48dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/service_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="76dp"
android:layout_marginTop="68dp"
android:text="@string/start"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/npub_input"
app:layout_constraintTop_toTopOf="@id/mainLayout"
tools:layout_constraintTop_toBottomOf="@id/npub_input" />

<ImageView
android:id="@+id/imageView2"
android:layout_width="109dp"
android:layout_height="90dp"
android:contentDescription="Logo"
android:layout_marginTop="100dp"
android:contentDescription="@string/logo"
android:src="@mipmap/ic_launcher"
app:layout_constraintBottom_toTopOf="@+id/npub_input"
app:layout_constraintBottom_toTopOf="@id/service_start"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.5"
tools:ignore="MissingConstraints" />

</androidx.constraintlayout.widget.ConstraintLayout>
<LinearLayout
android:id="@+id/mainLayout"
android:layout_width="286dp"
android:layout_height="50dp"
android:layout_marginTop="32dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView2">

<EditText
android:id="@+id/npub_input"
android:layout_width="220dp"
android:layout_height="match_parent"
android:autofillHints="npub"
android:ems="10"
android:hint="@string/input_npub"
android:inputType="text"
android:minHeight="48dp" />

<Button
android:id="@+id/amber"
android:layout_width="57dp"
android:layout_height="48dp"
android:drawableTop="@drawable/ic_amber"
android:drawablePadding="22dp"
android:layout_marginStart="9dp"
android:gravity="center"
tools:ignore="MissingConstraints" />

</LinearLayout>


</androidx.constraintlayout.widget.ConstraintLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@
<string name="relays">Your relays</string>
<string name="start">Start</string>
<string name="stop">Stop</string>
<string name="logo">Logo</string>
</resources>

0 comments on commit 029a035

Please sign in to comment.