Coverage Summary for Class: NumpadSession (com.opty.numpad)

Class Class, % Method, % Branch, % Line, % Instruction, %
NumpadSession 100% (1/1) 100% (1/1) 100% (6/6) 100% (27/27)


 /*
  * Copyright 2024 Opty  All rights reserved.
  *
  * This software and associated documentation files (the "Software") are protected by copyright law and
  * international treaties. This software has been licensed to users under the terms of the End User
  * License Agreement (EULA) and Terms of Service (ToS) available within the application.
  *
  * Unauthorized copying, reproduction, modification, distribution, display, performance, sublicense, sale,
  * access or use of this Software, or any part thereof, is strictly prohibited and will be prosecuted
  * under applicable laws.
  *
  * The Software contains valuable trade secrets and proprietary information of Opty.
  * No part of the Software may be reproduced, distributed, or transmitted in any form or by any means,
  * including photocopying, recording, or other electronic or mechanical methods without the prior written
  * permission of Opty.
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
  * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  *
  * For licensing inquiries, please contact: support@optymacros.com
  */
 package com.opty.numpad
 
 import androidx.compose.runtime.Stable
 import androidx.compose.runtime.getValue
 import androidx.compose.runtime.mutableStateOf
 import androidx.compose.runtime.setValue
 import androidx.compose.ui.text.TextRange
 import androidx.compose.ui.text.input.TextFieldValue
 
 @Stable
 class NumpadController {
     internal var session by mutableStateOf<NumpadSession?>(null)
         private set
 
     val isVisible: Boolean
         get() = session != null
 
     fun show(
         owner: Any,
         value: TextFieldValue,
         spec: NumpadInputSpec,
         selectAllOnOpen: Boolean = true,
         onValueChange: (TextFieldValue) -> Unit,
         onDone: () -> Unit,
     ) {
         val initialValue =
             if (selectAllOnOpen) {
                 value.copy(selection = TextRange(0, value.text.length))
             } else {
                 value
             }
         session =
             NumpadSession(
                 owner = owner,
                 value = initialValue,
                 spec = spec,
                 onValueChange = onValueChange,
                 onDone = onDone,
             )
     }
 
     fun dismiss(owner: Any) {
         if (session?.owner === owner) session = null
     }
 
     fun done() {
         val currentSession = session ?: return
         session = null
         currentSession.onDone()
     }
     
     fun dismiss() {
         session = null
     }
 
     fun inputDigit(digit: Int) {
         updateValue(NumpadKey.Digit(digit))
     }
 
     fun inputDecimalSeparator() {
         updateValue(NumpadKey.DecimalSeparator)
     }
 
     fun backspace() {
         updateValue(NumpadKey.Backspace)
     }
 
     private fun updateValue(key: NumpadKey) {
         val currentSession = session ?: return
         val newValue = currentSession.value.applyNumpadKey(key, currentSession.spec)
         if (newValue == currentSession.value) return
 
         session = currentSession.copy(value = newValue)
         currentSession.onValueChange(newValue)
     }
 }
 
 internal data class NumpadSession(
     val owner: Any,
     val value: TextFieldValue,
     val spec: NumpadInputSpec,
     val onValueChange: (TextFieldValue) -> Unit,
     val onDone: () -> Unit,
 )