- Created basic Balances layout with sections to show the account name, a list of Balances and a list of Transactions.

- Created UserAccountViewModel to serve as a bridge between the views and the database, and use it to fetch the current active UserAccount to display it in the BalancesFragment.
This commit is contained in:
Severiano Jaramillo 2018-11-26 12:26:20 -06:00
parent c77c62fb87
commit d42050afa1
12 changed files with 136 additions and 27 deletions

View file

@ -11,6 +11,9 @@ interface UserAccountDao {
@Insert @Insert
fun insert(userAccount: UserAccount) fun insert(userAccount: UserAccount)
@Query("SELECT * FROM user_accounts WHERE user_accounts.id = :id")
fun getUserAccount(id: String): LiveData<UserAccount>
@Query("SELECT * FROM user_accounts") @Query("SELECT * FROM user_accounts")
fun getAllUserAccounts(): LiveData<List<UserAccount>> fun getAllUserAccounts(): LiveData<List<UserAccount>>
} }

View file

@ -3,24 +3,28 @@ package cy.agorise.bitsybitshareswallet.fragments
import androidx.lifecycle.ViewModelProviders import androidx.lifecycle.ViewModelProviders
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.preference.PreferenceManager
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.annotation.Nullable
import androidx.lifecycle.Observer
import cy.agorise.bitsybitshareswallet.R import cy.agorise.bitsybitshareswallet.R
import cy.agorise.bitsybitshareswallet.activities.ReceiveTransactionActivity import cy.agorise.bitsybitshareswallet.activities.ReceiveTransactionActivity
import cy.agorise.bitsybitshareswallet.activities.SendTransactionActivity import cy.agorise.bitsybitshareswallet.activities.SendTransactionActivity
import cy.agorise.bitsybitshareswallet.models.UserAccount
import cy.agorise.bitsybitshareswallet.repositories.UserAccountRepository
import cy.agorise.bitsybitshareswallet.utils.Constants
import cy.agorise.bitsybitshareswallet.viewmodels.BalancesViewModel import cy.agorise.bitsybitshareswallet.viewmodels.BalancesViewModel
import cy.agorise.bitsybitshareswallet.viewmodels.UserAccountViewModel
import kotlinx.android.synthetic.main.fragment_balances.* import kotlinx.android.synthetic.main.fragment_balances.*
class BalancesFragment : Fragment() { class BalancesFragment : Fragment() {
companion object { private lateinit var mUserAccountViewModel: UserAccountViewModel
fun newInstance() = BalancesFragment() private lateinit var mBalancesViewModel: BalancesViewModel
}
private lateinit var viewModel: BalancesViewModel
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, inflater: LayoutInflater, container: ViewGroup?,
@ -29,9 +33,21 @@ class BalancesFragment : Fragment() {
return inflater.inflate(R.layout.fragment_balances, container, false) return inflater.inflate(R.layout.fragment_balances, container, false)
} }
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState) super.onViewCreated(view, savedInstanceState)
viewModel = ViewModelProviders.of(this).get(BalancesViewModel::class.java)
mUserAccountViewModel = ViewModelProviders.of(this).get(UserAccountViewModel::class.java)
val userId = PreferenceManager.getDefaultSharedPreferences(context)
.getString(Constants.KEY_CURRENT_ACCOUNT_ID, "")
mUserAccountViewModel.getUserAccount(userId!!).observe(this, Observer<UserAccount>{ user ->
tvAccountName.text = user.name
})
mBalancesViewModel = ViewModelProviders.of(this).get(BalancesViewModel::class.java)
// TODO: Use the ViewModel // TODO: Use the ViewModel
} }
} }

View file

@ -12,10 +12,6 @@ import cy.agorise.bitsybitshareswallet.viewmodels.MerchantsViewModel
class MerchantsFragment : Fragment() { class MerchantsFragment : Fragment() {
companion object {
fun newInstance() = MerchantsFragment()
}
private lateinit var viewModel: MerchantsViewModel private lateinit var viewModel: MerchantsViewModel
override fun onCreateView( override fun onCreateView(
@ -25,8 +21,9 @@ class MerchantsFragment : Fragment() {
return inflater.inflate(R.layout.fragment_merchants, container, false) return inflater.inflate(R.layout.fragment_merchants, container, false)
} }
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState) super.onViewCreated(view, savedInstanceState)
viewModel = ViewModelProviders.of(this).get(MerchantsViewModel::class.java) viewModel = ViewModelProviders.of(this).get(MerchantsViewModel::class.java)
// TODO: Use the ViewModel // TODO: Use the ViewModel
} }

View file

@ -2,6 +2,7 @@ package cy.agorise.bitsybitshareswallet.repositories
import android.app.Application import android.app.Application
import android.os.AsyncTask import android.os.AsyncTask
import androidx.lifecycle.LiveData
import cy.agorise.bitsybitshareswallet.daos.BitsyDatabase import cy.agorise.bitsybitshareswallet.daos.BitsyDatabase
import cy.agorise.bitsybitshareswallet.daos.UserAccountDao import cy.agorise.bitsybitshareswallet.daos.UserAccountDao
import cy.agorise.bitsybitshareswallet.models.UserAccount import cy.agorise.bitsybitshareswallet.models.UserAccount
@ -19,6 +20,10 @@ class UserAccountRepository internal constructor(application: Application) {
insertAsyncTask(mUserAccountDao).execute(userAccount) insertAsyncTask(mUserAccountDao).execute(userAccount)
} }
fun getUserAccount(id: String): LiveData<UserAccount> {
return mUserAccountDao.getUserAccount(id)
}
private class insertAsyncTask internal constructor(private val mAsyncTaskDao: UserAccountDao) : private class insertAsyncTask internal constructor(private val mAsyncTaskDao: UserAccountDao) :
AsyncTask<UserAccount, Void, Void>() { AsyncTask<UserAccount, Void, Void>() {

View file

@ -1,7 +1,8 @@
package cy.agorise.bitsybitshareswallet.viewmodels package cy.agorise.bitsybitshareswallet.viewmodels
import androidx.lifecycle.ViewModel import android.app.Application
import androidx.lifecycle.AndroidViewModel
class BalancesViewModel : ViewModel() {
// TODO: Implement the ViewModel class BalancesViewModel(application: Application) : AndroidViewModel(application) {
} }

View file

@ -0,0 +1,19 @@
package cy.agorise.bitsybitshareswallet.viewmodels
import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import cy.agorise.bitsybitshareswallet.models.UserAccount
import cy.agorise.bitsybitshareswallet.repositories.UserAccountRepository
class UserAccountViewModel(application: Application) : AndroidViewModel(application) {
private var mRepository = UserAccountRepository(application)
internal fun getUserAccount(id: String): LiveData<UserAccount> {
return mRepository.getUserAccount(id)
}
// fun insert(userAccount: UserAccount) {
// mRepository.insert(userAccount)
// }
}

View file

@ -16,7 +16,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/night_mode" android:text="@string/night_mode"
android:textAppearance="@style/TextAppearance.Body1" android:textAppearance="@style/TextAppearance.Bitsy.Body1"
android:textColor="?android:textColorPrimary"/> android:textColor="?android:textColorPrimary"/>
</LinearLayout> </LinearLayout>

View file

@ -1,14 +1,54 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<FrameLayout <ScrollView
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".fragments.MerchantsFragment"> tools:context=".fragments.MerchantsFragment">
<TextView <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="wrap_content"
android:text="@string/title_balances"/> android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_marginEnd="@dimen/activity_horizontal_margin"
android:orientation="vertical">
</FrameLayout> <TextView
android:id="@+id/tvAccountName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="seventest-5"
android:textAppearance="@style/TextAppearance.Bitsy.Headline6"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/spacing_same_topic"
android:text="@string/title_balances"
android:textAppearance="@style/TextAppearance.Bitsy.Subtitle1"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvBalances"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="@layout/item_balance"
tools:itemCount="3"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/spacing_same_topic"
android:text="@string/title_transactions"
android:textAppearance="@style/TextAppearance.Bitsy.Subtitle1"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvTransactions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="@layout/item_transaction"
tools:itemCount="6"/>
</LinearLayout>
</ScrollView>

View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/tvBalance"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
tools:text="123.45 BTS"/>

View file

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
tools:text="Transaction example"/>
</LinearLayout>

View file

@ -29,6 +29,9 @@
<string name="title_merchants">Merchants</string> <string name="title_merchants">Merchants</string>
<string name="title_send">Send</string> <string name="title_send">Send</string>
<!-- Balances -->
<string name="title_transactions">Transactions</string>
<!-- Settings --> <!-- Settings -->
<string name="title_settings">Settings</string> <string name="title_settings">Settings</string>
<string name="night_mode">Night mode</string> <string name="night_mode">Night mode</string>

View file

@ -42,6 +42,8 @@
</style> </style>
<!-- Text styles --> <!-- Text styles -->
<style name="TextAppearance.Body1" parent="TextAppearance.MaterialComponents.Body1" /> <style name="TextAppearance.Bitsy.Headline6" parent="TextAppearance.MaterialComponents.Headline6" />
<style name="TextAppearance.Body2" parent="TextAppearance.MaterialComponents.Body2" /> <style name="TextAppearance.Bitsy.Subtitle1" parent="TextAppearance.MaterialComponents.Subtitle1" />
<style name="TextAppearance.Bitsy.Body1" parent="TextAppearance.MaterialComponents.Body1" />
<style name="TextAppearance.Bitsy.Body2" parent="TextAppearance.MaterialComponents.Body2" />
</resources> </resources>