Skip to content

Commit e7329ac

Browse files
PratyushSingh07therajanmaurya
authored andcommitted
refactor #1537: migrated faq screen to compose
1 parent bdec63f commit e7329ac

File tree

7 files changed

+214
-99
lines changed

7 files changed

+214
-99
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package org.mifos.mobilewallet.mifospay.designsystem.component
2+
3+
import androidx.compose.animation.AnimatedVisibility
4+
import androidx.compose.animation.core.Spring
5+
import androidx.compose.animation.core.spring
6+
import androidx.compose.animation.expandVertically
7+
import androidx.compose.animation.fadeIn
8+
import androidx.compose.foundation.clickable
9+
import androidx.compose.foundation.layout.Column
10+
import androidx.compose.foundation.layout.Row
11+
import androidx.compose.foundation.layout.fillMaxWidth
12+
import androidx.compose.foundation.layout.padding
13+
import androidx.compose.material.icons.Icons
14+
import androidx.compose.material.icons.filled.ArrowDropDown
15+
import androidx.compose.material3.Divider
16+
import androidx.compose.material3.Icon
17+
import androidx.compose.material3.MaterialTheme
18+
import androidx.compose.material3.Text
19+
import androidx.compose.runtime.Composable
20+
import androidx.compose.runtime.getValue
21+
import androidx.compose.runtime.mutableStateOf
22+
import androidx.compose.runtime.remember
23+
import androidx.compose.runtime.setValue
24+
import androidx.compose.ui.Alignment
25+
import androidx.compose.ui.Modifier
26+
import androidx.compose.ui.draw.scale
27+
import androidx.compose.ui.graphics.Color
28+
import androidx.compose.ui.text.font.FontWeight
29+
import androidx.compose.ui.unit.dp
30+
31+
@Composable
32+
fun FaqItemScreen(
33+
question: String?,
34+
answer: String?,
35+
) {
36+
var isSelected by remember { mutableStateOf(false) }
37+
38+
Column(
39+
modifier = Modifier
40+
.fillMaxWidth()
41+
.padding(horizontal = 16.dp)
42+
) {
43+
Row(
44+
modifier = Modifier
45+
.clickable {
46+
isSelected = !isSelected
47+
}
48+
.padding(vertical = 8.dp),
49+
verticalAlignment = Alignment.CenterVertically
50+
) {
51+
Text(
52+
text = question.orEmpty(),
53+
style = MaterialTheme.typography.bodyMedium.copy(fontWeight = FontWeight.Bold),
54+
color = Color.Black,
55+
modifier = Modifier
56+
.fillMaxWidth()
57+
.weight(1f),
58+
)
59+
60+
Icon(
61+
imageVector = Icons.Default.ArrowDropDown,
62+
contentDescription = "drop down",
63+
tint = Color.Black,
64+
modifier = Modifier
65+
.scale(1f, if (isSelected) -1f else 1f)
66+
)
67+
}
68+
69+
AnimatedVisibility(
70+
visible = isSelected,
71+
enter = fadeIn() + expandVertically(
72+
animationSpec = spring(
73+
stiffness = Spring.StiffnessMedium
74+
)
75+
)
76+
) {
77+
Text(
78+
text = answer.orEmpty(),
79+
style = MaterialTheme.typography.bodyMedium,
80+
color = Color.Black,
81+
modifier = Modifier
82+
.fillMaxWidth()
83+
.padding(bottom = 8.dp)
84+
)
85+
}
86+
87+
Divider()
88+
}
89+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.mifos.mobilewallet.mifospay.faq.presenter
2+
3+
import androidx.lifecycle.ViewModel
4+
import dagger.hilt.android.lifecycle.HiltViewModel
5+
import org.mifos.mobilewallet.mifospay.R
6+
import org.mifos.mobilewallet.mifospay.faq.ui.FAQ
7+
import javax.inject.Inject
8+
9+
@HiltViewModel
10+
class FAQViewModel @Inject constructor() : ViewModel() {
11+
12+
fun getFAQ(): List<FAQ> {
13+
return listOf(
14+
FAQ(R.string.question1, R.string.answer1),
15+
FAQ(R.string.question2, R.string.answer2),
16+
FAQ(R.string.question3, R.string.answer3),
17+
FAQ(R.string.question4, R.string.answer4)
18+
)
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.mifos.mobilewallet.mifospay.faq.ui
2+
3+
data class FAQ(
4+
var question: Int,
5+
var answer: Int? = null,
6+
)
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,27 @@
11
package org.mifos.mobilewallet.mifospay.faq.ui
22

33
import android.os.Bundle
4-
import android.widget.ExpandableListView
5-
import org.mifos.mobilewallet.mifospay.R
4+
import androidx.activity.compose.setContent
65
import org.mifos.mobilewallet.mifospay.base.BaseActivity
7-
import org.mifos.mobilewallet.mifospay.faq.FAQContract
8-
import org.mifos.mobilewallet.mifospay.faq.FAQContract.FAQView
9-
import org.mifos.mobilewallet.mifospay.faq.FAQListAdapter
10-
import org.mifos.mobilewallet.mifospay.faq.presenter.FAQPresenter
11-
import org.mifos.mobilewallet.mifospay.utils.Constants
12-
import javax.inject.Inject
6+
import org.mifos.mobilewallet.mifospay.theme.MifosTheme
137

148
/**
159
* This class is the UI component of the Architecture.
1610
*
1711
* @author ankur
1812
* @since 11/July/2018
1913
*/
20-
class FAQActivity : BaseActivity(), FAQView {
21-
@JvmField
22-
@Inject
23-
var mPresenter: FAQPresenter? = null
24-
private var mFAQPresenter: FAQContract.FAQPresenter? = null
25-
private var expandableListView: ExpandableListView? = null
26-
private var faqListAdapter: FAQListAdapter? = null
27-
private var listDataGroup: MutableList<String>? = null
28-
private var listDataChild: HashMap<String, List<String>>? = null
29-
override fun onCreate(savedInstanceState: Bundle?) {
30-
super.onCreate(savedInstanceState)
31-
setContentView(R.layout.activity_faq)
32-
showColoredBackButton(Constants.BLACK_BACK_BUTTON)
33-
setToolbarTitle(Constants.FAQ)
34-
35-
// initializing the views
36-
initViews()
37-
38-
// initializing the objects
39-
initObjects()
40-
41-
// preparing list data
42-
initListData()
43-
}
44-
45-
override fun setPresenter(presenter: FAQContract.FAQPresenter?) {
46-
mFAQPresenter = presenter
47-
}
48-
49-
/**
50-
* Method to initialize the views
51-
*/
52-
override fun initViews() {
53-
expandableListView = findViewById(R.id.faq_list)
54-
}
55-
56-
/**
57-
* Method to initialize the objects
58-
*/
59-
override fun initObjects() {
6014

61-
// initializing the list of groups
62-
listDataGroup = ArrayList()
15+
class FAQActivity : BaseActivity() {
6316

64-
// initializing the list of child
65-
listDataChild = HashMap()
66-
67-
// initializing the adapter object
68-
faqListAdapter = FAQListAdapter(this, listDataGroup as ArrayList<String>, listDataChild!!)
69-
70-
// setting list adapter
71-
expandableListView?.setAdapter(faqListAdapter)
72-
}
73-
74-
/**
75-
* Preparing the list data
76-
* Dummy Items
77-
*/
78-
override fun initListData() {
79-
80-
81-
// Adding group data
82-
listDataGroup?.add(getString(R.string.question1))
83-
listDataGroup?.add(getString(R.string.question2))
84-
listDataGroup?.add(getString(R.string.question3))
85-
listDataGroup?.add(getString(R.string.question4))
86-
87-
// list of alcohol
88-
val question1List: MutableList<String> = ArrayList()
89-
question1List.add(getString(R.string.answer1))
90-
91-
// list of coffee
92-
val question2List: MutableList<String> = ArrayList()
93-
question2List.add(getString(R.string.answer2))
94-
95-
// list of pasta
96-
val question3List: MutableList<String> = ArrayList()
97-
question3List.add(getString(R.string.answer3))
98-
99-
100-
// list of cold drinks
101-
val question4List: MutableList<String> = ArrayList()
102-
question4List.add(getString(R.string.answer4))
103-
104-
105-
// Adding child data
106-
listDataChild!![listDataGroup!![0]] = question1List
107-
listDataChild!![listDataGroup!![1]] = question2List
108-
listDataChild!![listDataGroup!![2]] = question3List
109-
listDataChild!![listDataGroup!![3]] = question4List
110-
111-
// notify the adapter
112-
faqListAdapter?.notifyDataSetChanged()
17+
override fun onCreate(savedInstanceState: Bundle?) {
18+
super.onCreate(savedInstanceState)
19+
setContent {
20+
MifosTheme {
21+
FaqScreen(
22+
navigateBack = { onBackPressedDispatcher.onBackPressed() }
23+
)
24+
}
25+
}
11326
}
11427
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package org.mifos.mobilewallet.mifospay.faq.ui
2+
3+
import androidx.compose.foundation.layout.Column
4+
import androidx.compose.foundation.layout.fillMaxSize
5+
import androidx.compose.foundation.layout.fillMaxWidth
6+
import androidx.compose.foundation.lazy.LazyColumn
7+
import androidx.compose.foundation.lazy.itemsIndexed
8+
import androidx.compose.runtime.Composable
9+
import androidx.compose.ui.Modifier
10+
import androidx.compose.ui.res.stringResource
11+
import androidx.compose.ui.tooling.preview.Preview
12+
import androidx.hilt.navigation.compose.hiltViewModel
13+
import org.mifos.mobilewallet.mifospay.R
14+
import org.mifos.mobilewallet.mifospay.designsystem.component.FaqItemScreen
15+
import org.mifos.mobilewallet.mifospay.designsystem.component.MifosTopBar
16+
import org.mifos.mobilewallet.mifospay.faq.presenter.FAQViewModel
17+
import org.mifos.mobilewallet.mifospay.utils.FAQUtils.getFAQPreviewList
18+
19+
@Composable
20+
fun FaqScreen(
21+
navigateBack: () -> Unit,
22+
faqViewModel: FAQViewModel = hiltViewModel()
23+
) {
24+
Column(modifier = Modifier.fillMaxSize()) {
25+
MifosTopBar(
26+
topBarTitle = R.string.frequently_asked_questions,
27+
backPress = { navigateBack.invoke() })
28+
LazyColumn(
29+
modifier = Modifier
30+
.weight(1f)
31+
.fillMaxWidth()
32+
) {
33+
itemsIndexed(items = faqViewModel.getFAQ()) { _, faqItem ->
34+
FaqItemScreen(
35+
question = stringResource(id = faqItem.question),
36+
answer = faqItem.answer?.let { stringResource(id = it) }
37+
)
38+
}
39+
}
40+
}
41+
}
42+
43+
@Composable
44+
fun FaqScreen(
45+
navigateBack: () -> Unit,
46+
faqList: List<FAQ>
47+
) {
48+
Column(modifier = Modifier.fillMaxSize()) {
49+
MifosTopBar(
50+
topBarTitle = R.string.frequently_asked_questions,
51+
backPress = { navigateBack.invoke() })
52+
LazyColumn(
53+
modifier = Modifier
54+
.weight(1f)
55+
.fillMaxWidth()
56+
) {
57+
itemsIndexed(items = faqList) { _, faqItem ->
58+
FaqItemScreen(
59+
question = stringResource(id = faqItem.question),
60+
answer = faqItem.answer?.let { stringResource(id = it) }
61+
)
62+
}
63+
}
64+
}
65+
}
66+
67+
@Preview(showSystemUi = true)
68+
@Composable
69+
fun FaqScreenPreview() {
70+
FaqScreen({}, getFAQPreviewList())
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.mifos.mobilewallet.mifospay.utils
2+
3+
import org.mifos.mobilewallet.mifospay.R
4+
import org.mifos.mobilewallet.mifospay.faq.ui.FAQ
5+
6+
object FAQUtils {
7+
fun getFAQPreviewList(): List<FAQ> {
8+
return listOf(
9+
FAQ(R.string.question1, R.string.answer1),
10+
FAQ(R.string.question2, R.string.answer2),
11+
FAQ(R.string.question3, R.string.answer3),
12+
FAQ(R.string.question4, R.string.answer4)
13+
)
14+
}
15+
}

mifospay/src/main/res/values/strings.xml

+1
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@
203203
<string name="type">Type</string>
204204
<string name="edit_profile">Edit Profile</string>
205205
<string name="faq">FAQ</string>
206+
<string name="frequently_asked_questions">Frequently Asked Question</string>
206207
<string name="settings">Settings</string>
207208
<string name="mobile_number">Mobile Number</string>
208209
<string name="passcode_title">Passcode will be reset, are you sure?</string>

0 commit comments

Comments
 (0)