Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor #1620: migrated notification screen to compose #1621

Merged
merged 6 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class FetchNotifications @Inject constructor(private val mFineractRepository: Fi

class RequestValues(val clientId: Long) : UseCase.RequestValues
class ResponseValue(
val notificationPayloadList: List<NotificationPayload?>
val notificationPayloadList: List<NotificationPayload>?
) : UseCase.ResponseValue

override fun executeUseCase(requestValues: RequestValues) {
Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[versions]
accompanistSwiperefreshVersion = "0.27.0"
therajanmaurya marked this conversation as resolved.
Show resolved Hide resolved
appcompatVersion = "1.6.1"
androidGradlePlugin = "8.3.0"
compileSdk = "34"
Expand Down Expand Up @@ -65,6 +66,7 @@ androidxBrowser = "1.8.0"
playServicesCodeScanner = "16.1.0"

[libraries]
accompanist-swiperefresh = { module = "com.google.accompanist:accompanist-swiperefresh", version.ref = "accompanistSwiperefreshVersion" }
therajanmaurya marked this conversation as resolved.
Show resolved Hide resolved
androidx-activity-ktx = { module = "androidx.activity:activity-ktx", version.ref = "activityVersion" }
androidx-appcompat = { module = "androidx.appcompat:appcompat", version.ref = "appcompatVersion" }
androidx-constraintlayout = { module = "androidx.constraintlayout:constraintlayout", version.ref = "constraintlayoutVersion" }
Expand Down
3 changes: 3 additions & 0 deletions mifospay/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ dependencies {
implementation("de.hdodenhof:circleimageview:3.1.0")
implementation("com.github.yalantis:ucrop:2.2.2")

// Swipe Refresh
implementation (libs.accompanist.swiperefresh)

kspTest(libs.hilt.compiler)

testImplementation(libs.junit)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.mifospay.notification.presenter

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.mifospay.core.model.domain.NotificationPayload
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import org.mifospay.core.data.base.UseCase
import org.mifospay.core.data.base.UseCaseHandler
import org.mifospay.core.data.domain.usecase.notification.FetchNotifications
import org.mifospay.data.local.LocalRepository
import javax.inject.Inject

@HiltViewModel
class NotificationViewModel @Inject constructor(
private val mUseCaseHandler: UseCaseHandler,
private val mLocalRepository: LocalRepository,
private val fetchNotificationsUseCase: FetchNotifications
) : ViewModel() {

private val _notificationUiState: MutableStateFlow<NotificationUiState> =
MutableStateFlow(NotificationUiState.Loading)
val notificationUiState: StateFlow<NotificationUiState> = _notificationUiState

private val _isRefreshing = MutableStateFlow(false)
val isRefreshing: StateFlow<Boolean> get() = _isRefreshing.asStateFlow()

fun refresh() {
viewModelScope.launch {
_isRefreshing.emit(true)
delay(1000)
_isRefreshing.emit(false)
therajanmaurya marked this conversation as resolved.
Show resolved Hide resolved
}
}

init {
fetchNotifications()
}

fun fetchNotifications() {
mUseCaseHandler.execute(fetchNotificationsUseCase,
FetchNotifications.RequestValues(
mLocalRepository.clientDetails.clientId
),
object : UseCase.UseCaseCallback<FetchNotifications.ResponseValue> {
override fun onSuccess(response: FetchNotifications.ResponseValue) {
_notificationUiState.value =
NotificationUiState.Success(response.notificationPayloadList.orEmpty())
}

override fun onError(message: String) {
_notificationUiState.value = NotificationUiState.Error(message)
}
})
}
}

sealed interface NotificationUiState {
data object Loading : NotificationUiState
data object Empty : NotificationUiState
therajanmaurya marked this conversation as resolved.
Show resolved Hide resolved
data class Success(val notificationList: List<NotificationPayload>) : NotificationUiState
data class Error(val message: String) : NotificationUiState
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.mifospay.notification.ui
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.activity.compose.setContent
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
Expand All @@ -16,6 +17,7 @@ import org.mifospay.notification.NotificationContract
import org.mifospay.notification.NotificationContract.NotificationView
import org.mifospay.notification.presenter.NotificationPresenter
import org.mifospay.common.Constants
import org.mifospay.theme.MifosTheme
import org.mifospay.utils.DebugUtil
import org.mifospay.utils.Toaster
import javax.inject.Inject
Expand All @@ -26,77 +28,21 @@ import javax.inject.Inject
* This feature is yet to be implemented on the server side.
*/
@AndroidEntryPoint
class NotificationActivity : BaseActivity(), NotificationView {
@JvmField
@Inject
var mPresenter: NotificationPresenter? = null
var mNotificationPresenter: NotificationContract.NotificationPresenter? = null
class NotificationActivity : BaseActivity() {

@JvmField
@BindView(R.id.rv_notification)
var mRvNotification: RecyclerView? = null

@JvmField
@BindView(R.id.tv_placeholder)
var tvplaceholder: TextView? = null

@JvmField
@Inject
var mNotificationAdapter: NotificationAdapter? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_notification)
ButterKnife.bind(this)
setToolbarTitle("Notifications")
showColoredBackButton(R.drawable.ic_arrow_back_black_24dp)
setupRecyclerView()
setupSwipeRefreshLayout()
mPresenter?.attachView(this)
showSwipeProgress()
mNotificationPresenter?.fetchNotifications()
}

private fun setupRecyclerView() {
mRvNotification?.layoutManager = LinearLayoutManager(this)
mRvNotification?.adapter = mNotificationAdapter
mRvNotification?.addItemDecoration(
DividerItemDecoration(
this,
DividerItemDecoration.VERTICAL
)
)
}

private fun setupSwipeRefreshLayout() {
setSwipeRefreshEnabled(true)
swipeRefreshLayout?.setOnRefreshListener { mNotificationPresenter?.fetchNotifications() }
}

override fun fetchNotificationsSuccess(notificationPayloadList: List<NotificationPayload?>?) {
hideSwipeProgress()
if (notificationPayloadList.isNullOrEmpty()) {
DebugUtil.log("null")
mRvNotification?.visibility = View.GONE
tvplaceholder?.visibility = View.VISIBLE
} else {
DebugUtil.log("yes")
mRvNotification?.visibility = View.VISIBLE
tvplaceholder?.visibility = View.GONE
mNotificationAdapter?.setNotificationPayloadList(notificationPayloadList as List<NotificationPayload>)
setContent {
MifosTheme {
NotificationScreen()
}
}
mNotificationAdapter?.setNotificationPayloadList(notificationPayloadList as List<NotificationPayload>)
}

override fun fetchNotificationsError(message: String?) {
hideSwipeProgress()
showToast(message)
// mNotificationPresenter?.fetchNotifications()
}

override fun setPresenter(presenter: NotificationContract.NotificationPresenter?) {
mNotificationPresenter = presenter
}
// private fun setupSwipeRefreshLayout() {
// setSwipeRefreshEnabled(true)
// swipeRefreshLayout?.setOnRefreshListener { mNotificationPresenter?.fetchNotifications() }
// }
therajanmaurya marked this conversation as resolved.
Show resolved Hide resolved

fun showToast(message: String?) {
Toaster.showToast(this, message)
}
}
Loading
Loading