Search code examples
androidkotlinmvvmviewmodel

Send data from one function to another


Take a deep look at the inside of function insertUser and insertMoney

I want to send the value of id in insertUser to insertMoney and id is a variable

I have tried var idT, and insertUser that works but in other function that is insertMoney returns "".

Here is my viewModel :

var idT= ""
class AddUserViewModel(
    val mUserInfoDAO: UserDAO,
    val mTransactionDAO: TransactionsDAO,
    val mLoan: LoanDAO,
    val mBankDAO: BankDAO,
    application: Application
) :
AndroidViewModel(application) {
var viewModelJob = Job()
val uiScope = CoroutineScope(Dispatchers.Main + viewModelJob)


    fun insertUser(
    fullName: String,
    accountId: String,
    mobileNumber: String,
    phoneNumber: String,
    dateOfCreation: String,
    address: String,
) {
    uiScope.launch {
        try {
            Log.d("TAG", "insertTest")
            val user = UserInfo(
                0L,
                fullName,
                accountId,
                mobileNumber,
                phoneNumber,
                dateOfCreation,
                address
            )
            withContext(Dispatchers.IO) {
           
                #*#*#*#*#*#* I WANT TO SENT VALUE OF *id* TO insertMoney #*#*#*#*#*#* 

                val id = mUserInfoDAO.insert(user)
             
   
               idT=id.toString()
                Log.d("TAG", "insertUser: $idT")
            }
        } catch (e: Exception) {
            Log.d("TAG", "insertContact: ${e.message}")
        }
    }
}

fun insertMoney(
    amount: String,
    increasePage: String?
) {
    Log.d("TAG", "insertUserTTT: $idT")

    var bankId: Long = bankInfo.value?.get(selectedItemPosition)?.bankId!!
    uiScope.launch {
        mTransactionDAO.insert(
            Transaction(
                0L,
            
                *#*#*#*#*#HERE#*#*#*#*#*#
                // id //,
                null,
                bankId,
                null,
                amount,
                null,
                null,
                null,
                increasePage
            )
        )

    }

}

And I call these 2 functions in fragment :

   mAddUserViewModel.addUser.observe(viewLifecycleOwner, Observer {
        if (it == true) {
          
          
               mAddUserViewModel.insertUser(
                mAddUserListBinding.fullName.text.toString(),
                mAddUserListBinding.accountId.text.toString(),
                mAddUserListBinding.mobileNumber.text.toString(),
                mAddUserListBinding.phoneNumber.text.toString(),
                mAddUserListBinding.createdDate.text.toString(),
                mAddUserListBinding.edtAddress.text.toString(),
            )
         
         
            val firstMoney = "firstMoney"
            mAddUserViewModel.insertMoney(
                mAddUserListBinding.firstMoney.text.toString(),
                firstMoney
            )
            findNavController().popBackStack()
        }
    })

Solution

  • I solved it.

    I mixed both functions body I remove insertMoney function and add its body to insertUser and I get the value of editText and insert it in insert User

     fun insertUser(
        fullName: String,
        accountId: String,
        mobileNumber: String,
        phoneNumber: String,
        dateOfCreation: String,
        address: String,
    ) {
        uiScope.launch {
            try {
                Log.d("TAG", "insertTest")
                val user = UserInfo(
                    0L,
                    fullName,
                    accountId,
                    mobileNumber,
                    phoneNumber,
                    dateOfCreation,
                    address
                )
                withContext(Dispatchers.IO) {
                    val id = mUserInfoDAO.insert(user)
                    Log.d("TAG", "insertUser: $amount")
                    val increasePage = "firstMoney"
                    val bankId: Long = bankInfo.value?.get(selectedItemPosition)?.bankId!!
                    mTransactionDAO.insert(
                        Transaction(
                            0L,
                            id,
                            null,
                            bankId,
                            null,
                            amount,
                            null,
                            null,
                            null,
                            increasePage
                        )
                    )
                }
    
            } catch (e: Exception) {
                Log.d("TAG", "insertContact: ${e.message}")
            }
    
        }
    }
    private var username = ObservableField("").toString()
    fun afterUserNameChange(s: CharSequence) {
        Log.i("truck", s.toString());
        amount = s.toString()
        this.username = s.toString()
    }