Skip to content

Commit

Permalink
Minor performance improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
103sbavert committed Dec 22, 2020
1 parent d7b2bf1 commit 2fe2ef0
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 20 deletions.
12 changes: 8 additions & 4 deletions app/src/main/java/com/sbeve/jada/fragments/main/MainFragment.kt
Expand Up @@ -41,12 +41,15 @@ class MainFragment : Fragment(R.layout.fragment_main), RecentQueriesAdapter.OnCl
private val savedLanguageIndex: Int
get() = mainActivityContext.applicationPreferences
.getInt(getString(R.string.language_setting_key), 0)
private val adapter by lazy {
RecentQueriesAdapter(emptyList(), this)
}

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

fragmentMainBinding = FragmentMainBinding.bind(view)
initializeRecyclerView()
updateRecyclerView()
fragmentMainBinding.changeLanguageGearIcon.setOnClickListener {
createChangeLanguageDialog().show()
}
Expand All @@ -55,6 +58,7 @@ class MainFragment : Fragment(R.layout.fragment_main), RecentQueriesAdapter.OnCl
}
fragmentMainBinding.queriesRecyclerView.layoutManager = LinearLayoutManager(mainActivityContext)
fragmentMainBinding.queriesRecyclerView.setHasFixedSize(true)
fragmentMainBinding.queriesRecyclerView.adapter = adapter

//set the text view's text to show whichever language is selected and update the text whenever the setting is changed
fragmentMainBinding.currentLanguage.text = RetrofitInit.supportedLanguages.first[savedLanguageIndex]
Expand Down Expand Up @@ -102,10 +106,10 @@ class MainFragment : Fragment(R.layout.fragment_main), RecentQueriesAdapter.OnCl
imm.hideSoftInputFromWindow(view.windowToken, 0)
}

private fun initializeRecyclerView() {
private fun updateRecyclerView() {
viewModel.allQueries.observe(viewLifecycleOwner) {
val adapter = RecentQueriesAdapter(it, this)
fragmentMainBinding.queriesRecyclerView.adapter = adapter
adapter.dataSet = it
adapter.notifyDataSetChanged()
}
}

Expand Down
Expand Up @@ -50,7 +50,6 @@ class ResultFragment : Fragment(R.layout.fragment_result) {
mainActivityContext.theme.resolveAttribute(R.attr.suppressed_text, examplesTextColor, true)

viewModel.fetchWordInfo(args.queryText, args.queryLanguage)

viewModel.fetchWordInfoResultType.observe(viewLifecycleOwner) {
if (it != null) {
when (it) {
Expand All @@ -76,7 +75,6 @@ class ResultFragment : Fragment(R.layout.fragment_result) {
//make errorMessage visible if it's not already
when (state) {
ErrorType.CallFailed -> fragmentResultBinding.loadingErrorMessage.text = getString(R.string.call_failed)

ErrorType.NoMatch -> fragmentResultBinding.loadingErrorMessage.text = getString(R.string.no_match)
}
}
Expand Down Expand Up @@ -104,8 +102,7 @@ class ResultFragment : Fragment(R.layout.fragment_result) {
//if there is no provided information about the origin, don't add anything to the textview and set the textview to gone
if (WORD.origin.isNullOrEmpty()) wordLayoutBinding.originTextview.visibility = View.GONE
else wordLayoutBinding.originTextview.text = getString(R.string.origin_info, WORD.origin)



//add the each meaning_layout to the current word_item_layout
val meaningsLayoutArray = getMeaningsLayoutList(WORD, wordLayoutBinding)
meaningsLayoutArray.forEach { wordLayoutBinding.wordLinearLayout.addView(it.root) }
Expand Down
Expand Up @@ -11,7 +11,7 @@ import java.text.SimpleDateFormat
import java.util.*
import kotlin.properties.Delegates

class RecentQueriesAdapter(private val dataSet: List<RecentQuery>, private val onClickListener: OnClickListener) :
class RecentQueriesAdapter(var dataSet: List<RecentQuery>, private val onClickListener: OnClickListener) :
RecyclerView.Adapter<RecentQueriesAdapter.ViewHolder>() {

class ViewHolder(myItemView: QueryLayoutBinding, private val onClickListener: OnClickListener) :
Expand All @@ -32,7 +32,7 @@ class RecentQueriesAdapter(private val dataSet: List<RecentQuery>, private val o
}

fun setTimeText(timeValue: Long) {
time.text = SimpleDateFormat.getDateTimeInstance().format(Date(timeValue))
time.text = SimpleDateFormat("HH:mm, dd MMM, yyyy", Locale.getDefault()).format(timeValue)
}

init {
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/layout/fragment_main.xml
Expand Up @@ -83,6 +83,9 @@
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="@dimen/medium_margin_padding"
android:clipToPadding="false"
android:paddingHorizontal="@dimen/medium_margin_padding"
android:paddingBottom="@dimen/medium_margin_padding"
android:background="@drawable/top_corners_rounded_rectangle"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
Expand Down
32 changes: 22 additions & 10 deletions app/src/main/res/layout/query_layout.xml
Expand Up @@ -5,7 +5,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="@dimen/medium_margin_padding">
android:layout_marginTop="@dimen/medium_margin_padding">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
Expand All @@ -23,14 +23,14 @@
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.025"
app:layout_constraintVertical_bias="0.0"
tools:text="query" />

<TextView
android:id="@+id/language"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginTop="1dp"
android:textAppearance="?attr/textAppearanceSubtitle2"
android:textColor="?attr/suppressed_text"
app:layout_constraintBottom_toBottomOf="parent"
Expand All @@ -45,28 +45,40 @@
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/tiny_margin_padding"
android:textAppearance="?attr/textAppearanceCaption"
android:textColor="?attr/suppressed_text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/delete_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/language"
app:layout_constraintVertical_bias="0.0"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
tools:text="HH:MM YYYY MM SS" />

<ImageButton
android:id="@+id/delete_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="?attr/colorSurface"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:contentDescription="@string/delete_query_button_text"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toTopOf="@+id/divider"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="@drawable/ic_delete" />

<View
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_marginTop="@dimen/small_margin_padding"
android:layout_marginBottom="@dimen/small_margin_padding"
android:background="?android:attr/listDivider"
app:layout_constraintBottom_toTopOf="@+id/time"
app:layout_constraintEnd_toStartOf="@+id/delete_button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/language" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>

0 comments on commit 2fe2ef0

Please sign in to comment.