Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/mvvm_architecture' into login-ac…
Browse files Browse the repository at this point in the history
…tivity-mvvm-migration

# Conflicts:
#	app/src/main/java/org/mifos/mobile/injection/module/RepositoryModule.kt
#	app/src/main/java/org/mifos/mobile/repositories/ClientRepository.kt
#	app/src/main/java/org/mifos/mobile/repositories/ClientRepositoryImp.kt
#	app/src/main/java/org/mifos/mobile/repositories/UserAuthRepository.kt
#	app/src/main/java/org/mifos/mobile/repositories/UserAuthRepositoryImp.kt
#	app/src/test/java/org/mifos/mobile/repositories/ClientRepositoryImpTest.kt
  • Loading branch information
gururani-abhishek committed Jul 17, 2023
2 parents 81ba0d1 + 85f34a6 commit 48461b7
Show file tree
Hide file tree
Showing 20 changed files with 735 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import org.mifos.mobile.api.DataManager
import org.mifos.mobile.api.local.PreferencesHelper
import org.mifos.mobile.repositories.ClientRepository
import org.mifos.mobile.repositories.ClientRepositoryImp
import org.mifos.mobile.repositories.RecentTransactionRepository
import org.mifos.mobile.repositories.RecentTransactionRepositoryImp
import org.mifos.mobile.repositories.UserAuthRepository
import org.mifos.mobile.repositories.UserAuthRepositoryImp

Expand All @@ -26,4 +28,9 @@ class RepositoryModule {
): ClientRepository {
return ClientRepositoryImp(dataManager, preferencesHelper)
}

@Provides
fun providesRecentTransactionRepository(dataManager: DataManager): RecentTransactionRepository {
return RecentTransactionRepositoryImp(dataManager)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ interface ClientRepository {
fun setClientId(clientId: Long?)

fun clearPrefHelper()

fun updateAuthenticationToken(password: String)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.mifos.mobile.repositories

import io.reactivex.Observable
import okhttp3.Credentials
import org.mifos.mobile.api.BaseApiManager
import org.mifos.mobile.api.DataManager
import org.mifos.mobile.api.local.PreferencesHelper
Expand Down Expand Up @@ -49,4 +50,14 @@ class ClientRepositoryImp @Inject constructor(
override fun clearPrefHelper() {
preferencesHelper.clear()
}

override fun updateAuthenticationToken(password: String) {
val authenticationToken = Credentials.basic(preferencesHelper.userName!!, password)
preferencesHelper.saveToken(authenticationToken)
BaseApiManager.createService(
preferencesHelper.baseUrl,
preferencesHelper.tenant,
preferencesHelper.token,
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.mifos.mobile.repositories


import io.reactivex.Observable
import org.mifos.mobile.models.Page
import org.mifos.mobile.models.Transaction

interface RecentTransactionRepository {

fun recentTransactions(
offset: Int?,
limit: Int?
): Observable<Page<Transaction?>?>?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.mifos.mobile.repositories

import io.reactivex.Observable
import org.mifos.mobile.api.DataManager
import org.mifos.mobile.models.Page
import org.mifos.mobile.models.Transaction
import javax.inject.Inject

class RecentTransactionRepositoryImp @Inject constructor(private val dataManager: DataManager) :
RecentTransactionRepository {

override fun recentTransactions(offset: Int?, limit: Int?): Observable<Page<Transaction?>?>? {
return limit?.let { offset?.let { it1 -> dataManager.getRecentTransactions(it1, it) } }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ interface UserAuthRepository {
): Observable<ResponseBody?>?

fun login(username: String, password: String): Observable<User?>?

fun updateAccountPassword(
newPassword: String, confirmPassword: String
): Observable<ResponseBody?>?

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import okhttp3.ResponseBody
import org.mifos.mobile.api.DataManager
import org.mifos.mobile.models.User
import org.mifos.mobile.models.payload.LoginPayload
import org.mifos.mobile.models.UpdatePasswordPayload
import org.mifos.mobile.models.register.RegisterPayload
import javax.inject.Inject

class UserAuthRepositoryImp @Inject constructor(private val dataManager: DataManager) :
UserAuthRepository {
class UserAuthRepositoryImp @Inject constructor(private val dataManager: DataManager) : UserAuthRepository {

override fun registerUser(
accountNumber: String?,
Expand Down Expand Up @@ -42,4 +42,16 @@ class UserAuthRepositoryImp @Inject constructor(private val dataManager: DataMan
return dataManager.login(loginPayload)
}


override fun updateAccountPassword(
newPassword: String, confirmPassword: String
): Observable<ResponseBody?>? {
val payload = UpdatePasswordPayload().apply {
this.password = newPassword
this.repeatPassword = confirmPassword
}

return dataManager.updateAccountPassword(payload)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout.OnRefreshListener
Expand All @@ -14,35 +15,30 @@ import dagger.hilt.android.AndroidEntryPoint
import org.mifos.mobile.R
import org.mifos.mobile.databinding.FragmentRecentTransactionsBinding
import org.mifos.mobile.models.Transaction
import org.mifos.mobile.presenters.RecentTransactionsPresenter
import org.mifos.mobile.ui.activities.base.BaseActivity
import org.mifos.mobile.ui.adapters.RecentTransactionListAdapter
import org.mifos.mobile.ui.fragments.base.BaseFragment
import org.mifos.mobile.ui.views.RecentTransactionsView
import org.mifos.mobile.utils.Constants
import org.mifos.mobile.utils.DividerItemDecoration
import org.mifos.mobile.utils.EndlessRecyclerViewScrollListener
import org.mifos.mobile.utils.*
import org.mifos.mobile.utils.Network.isConnected
import org.mifos.mobile.utils.Toaster
import org.mifos.mobile.viewModels.RecentTransactionViewModel
import javax.inject.Inject

/**
* @author Vishwwajeet
* @since 09/08/16
*/
@AndroidEntryPoint
class RecentTransactionsFragment : BaseFragment(), RecentTransactionsView, OnRefreshListener {
class RecentTransactionsFragment : BaseFragment(), OnRefreshListener {

private var _binding: FragmentRecentTransactionsBinding? = null
private val binding get() = _binding!!

@JvmField
@Inject
var recentTransactionsPresenter: RecentTransactionsPresenter? = null

@JvmField
@Inject
var recentTransactionsListAdapter: RecentTransactionListAdapter? = null

private lateinit var recentTransactionViewModel: RecentTransactionViewModel

private var sweetUIErrorHandler: SweetUIErrorHandler? = null
private var recentTransactionList: MutableList<Transaction?>? = null
override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -56,18 +52,41 @@ class RecentTransactionsFragment : BaseFragment(), RecentTransactionsView, OnRef
savedInstanceState: Bundle?,
): View {
_binding = FragmentRecentTransactionsBinding.inflate(inflater, container, false)
recentTransactionsPresenter?.attachView(this)
recentTransactionViewModel = ViewModelProvider(this)[RecentTransactionViewModel::class.java]
sweetUIErrorHandler = SweetUIErrorHandler(activity, binding.root)
showUserInterface()
setToolbarTitle(getString(R.string.recent_transactions))
if (savedInstanceState == null) {
recentTransactionsPresenter?.loadRecentTransactions(false, 0)
recentTransactionViewModel.loadRecentTransactions(false, 0)
}
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

recentTransactionViewModel.recentTransactionUiState.observe(viewLifecycleOwner) {
when (it) {
is RecentTransactionUiState.Loading -> showProgress()
is RecentTransactionUiState.RecentTransactions -> {
hideProgress()
showRecentTransactions(it.transactions)
}
is RecentTransactionUiState.Error -> {
hideProgress()
showMessage(getString(it.message))
}
is RecentTransactionUiState.EmptyTransaction -> {
hideProgress()
showEmptyTransaction()
}
is RecentTransactionUiState.LoadMoreRecentTransactions -> {
hideProgress()
showLoadMoreRecentTransactions(it.transactions)
}
}
}

binding.layoutError.btnTryAgain.setOnClickListener {
retryClicked()
}
Expand Down Expand Up @@ -95,7 +114,7 @@ class RecentTransactionsFragment : BaseFragment(), RecentTransactionsView, OnRef
/**
* Setting up `rvRecentTransactions`
*/
override fun showUserInterface() {
fun showUserInterface() {
val layoutManager = LinearLayoutManager(activity)
layoutManager.orientation = LinearLayoutManager.VERTICAL
binding.rvRecentTransactions.layoutManager = layoutManager
Expand All @@ -111,7 +130,7 @@ class RecentTransactionsFragment : BaseFragment(), RecentTransactionsView, OnRef
binding.rvRecentTransactions.addOnScrollListener(
object : EndlessRecyclerViewScrollListener(layoutManager) {
override fun onLoadMore(page: Int, totalItemsCount: Int, view: RecyclerView?) {
recentTransactionsPresenter?.loadRecentTransactions(true, totalItemsCount)
recentTransactionViewModel.loadRecentTransactions(true, totalItemsCount)
}

override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
Expand All @@ -138,13 +157,13 @@ class RecentTransactionsFragment : BaseFragment(), RecentTransactionsView, OnRef
if (binding.layoutError.root.visibility == View.VISIBLE) {
resetUI()
}
recentTransactionsPresenter?.loadRecentTransactions(false, 0)
recentTransactionViewModel.loadRecentTransactions(false, 0)
}

/**
* Shows a Toast
*/
override fun showMessage(message: String?) {
fun showMessage(message: String?) {
(activity as BaseActivity?)?.showToast(message!!)
}

Expand All @@ -154,7 +173,7 @@ class RecentTransactionsFragment : BaseFragment(), RecentTransactionsView, OnRef
*
* @param recentTransactionList List of [Transaction]
*/
override fun showRecentTransactions(recentTransactionList: List<Transaction?>?) {
fun showRecentTransactions(recentTransactionList: List<Transaction?>?) {
this.recentTransactionList = recentTransactionList as MutableList<Transaction?>?
recentTransactionsListAdapter?.setTransactions(recentTransactionList)
}
Expand All @@ -164,12 +183,12 @@ class RecentTransactionsFragment : BaseFragment(), RecentTransactionsView, OnRef
*
* @param transactions List of [Transaction]
*/
override fun showLoadMoreRecentTransactions(transactions: List<Transaction?>?) {
fun showLoadMoreRecentTransactions(transactions: List<Transaction?>?) {
this.recentTransactionList?.addAll(recentTransactionList!!)
recentTransactionsListAdapter?.notifyDataSetChanged()
}

override fun resetUI() {
fun resetUI() {
sweetUIErrorHandler?.hideSweetErrorLayoutUI(
binding.rvRecentTransactions,
binding.layoutError.root,
Expand All @@ -179,7 +198,7 @@ class RecentTransactionsFragment : BaseFragment(), RecentTransactionsView, OnRef
/**
* Hides `rvRecentTransactions` and shows a textview prompting no transactions
*/
override fun showEmptyTransaction() {
fun showEmptyTransaction() {
sweetUIErrorHandler?.showSweetEmptyUI(
getString(R.string.recent_transactions),
R.drawable.ic_error_black_24dp,
Expand All @@ -193,7 +212,7 @@ class RecentTransactionsFragment : BaseFragment(), RecentTransactionsView, OnRef
*
* @param message Error message that tells the user about the problem.
*/
override fun showErrorFetchingRecentTransactions(message: String?) {
fun showErrorFetchingRecentTransactions(message: String?) {
if (!isConnected(requireActivity())) {
sweetUIErrorHandler?.showSweetNoInternetUI(
binding.rvRecentTransactions,
Expand All @@ -214,7 +233,7 @@ class RecentTransactionsFragment : BaseFragment(), RecentTransactionsView, OnRef
binding.rvRecentTransactions,
binding.layoutError.root,
)
recentTransactionsPresenter?.loadRecentTransactions(false, 0)
recentTransactionViewModel.loadRecentTransactions(false, 0)
} else {
Toast.makeText(
context,
Expand All @@ -224,23 +243,22 @@ class RecentTransactionsFragment : BaseFragment(), RecentTransactionsView, OnRef
}
}

override fun showProgress() {
fun showProgress() {
showSwipeRefreshLayout(true)
}

override fun hideProgress() {
fun hideProgress() {
showSwipeRefreshLayout(false)
}

override fun showSwipeRefreshLayout(show: Boolean) {
fun showSwipeRefreshLayout(show: Boolean) {
binding.swipeTransactionContainer.post {
binding.swipeTransactionContainer.isRefreshing = show
}
}

override fun onDestroyView() {
super.onDestroyView()
recentTransactionsPresenter?.detachView()
_binding = null
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ class RegistrationFragment : BaseFragment() {
when (state) {
RegistrationUiState.Loading -> showProgress()

RegistrationUiState.RegistrationSuccessful -> {
RegistrationUiState.Success -> {
hideProgress()
showRegisteredSuccessfully()
}

is RegistrationUiState.ErrorOnRegistration -> {
is RegistrationUiState.Error -> {
hideProgress()
showError(MFErrorParser.errorMessage(state.exception))
}
Expand Down
Loading

0 comments on commit 48461b7

Please sign in to comment.