animation

This commit is contained in:
callebtc
2025-08-30 15:00:20 +02:00
parent 2a7c004db2
commit c14762f82e
3 changed files with 47 additions and 14 deletions
@@ -126,19 +126,27 @@ object NostrProtocol {
if (powSettings.enabled && powSettings.difficulty > 0) {
Log.d(TAG, "PoW enabled for geohash event: difficulty=${powSettings.difficulty}")
// Mine the event before signing
val minedEvent = NostrProofOfWork.mineEvent(
event = event,
targetDifficulty = powSettings.difficulty,
maxIterations = 2_000_000 // Allow up to 2M iterations for reasonable mining time
)
if (minedEvent != null) {
event = minedEvent
val actualDifficulty = NostrProofOfWork.calculateDifficulty(event.id)
Log.d(TAG, "✅ PoW mining successful: target=${powSettings.difficulty}, actual=$actualDifficulty, nonce=${NostrProofOfWork.getNonce(event)}")
} else {
Log.w(TAG, "❌ PoW mining failed, proceeding without PoW")
try {
// Start mining state for animated indicators
PoWPreferenceManager.startMining()
// Mine the event before signing
val minedEvent = NostrProofOfWork.mineEvent(
event = event,
targetDifficulty = powSettings.difficulty,
maxIterations = 2_000_000 // Allow up to 2M iterations for reasonable mining time
)
if (minedEvent != null) {
event = minedEvent
val actualDifficulty = NostrProofOfWork.calculateDifficulty(event.id)
Log.d(TAG, "✅ PoW mining successful: target=${powSettings.difficulty}, actual=$actualDifficulty, nonce=${NostrProofOfWork.getNonce(event)}")
} else {
Log.w(TAG, "❌ PoW mining failed, proceeding without PoW")
}
} finally {
// Always stop mining state when done (success or failure)
PoWPreferenceManager.stopMining()
}
}
@@ -26,6 +26,10 @@ object PoWPreferenceManager {
private val _powDifficulty = MutableStateFlow(DEFAULT_POW_DIFFICULTY)
val powDifficulty: StateFlow<Int> = _powDifficulty.asStateFlow()
// Mining state for animated indicators
private val _isMining = MutableStateFlow(false)
val isMining: StateFlow<Boolean> = _isMining.asStateFlow()
private lateinit var sharedPrefs: SharedPreferences
private var isInitialized = false
@@ -121,4 +125,25 @@ object PoWPreferenceManager {
32 to "Maximum (~8h)"
)
}
/**
* Get current mining state
*/
fun isMining(): Boolean {
return _isMining.value
}
/**
* Start mining state - triggers animated indicators
*/
fun startMining() {
_isMining.value = true
}
/**
* Stop mining state - stops animated indicators
*/
fun stopMining() {
_isMining.value = false
}
}
@@ -23,11 +23,11 @@ import com.bitchat.android.nostr.PoWPreferenceManager
@Composable
fun PoWStatusIndicator(
modifier: Modifier = Modifier,
isMining: Boolean = false,
style: PoWIndicatorStyle = PoWIndicatorStyle.COMPACT
) {
val powEnabled by PoWPreferenceManager.powEnabled.collectAsState()
val powDifficulty by PoWPreferenceManager.powDifficulty.collectAsState()
val isMining by PoWPreferenceManager.isMining.collectAsState()
val colorScheme = MaterialTheme.colorScheme
val isDark = colorScheme.background.red + colorScheme.background.green + colorScheme.background.blue < 1.5f