Skip to content

Convert language related classes to Kotlin#175

Merged
tuancoltech merged 3 commits intomainfrom
convert_language_package_to_kotlin
Mar 7, 2025
Merged

Convert language related classes to Kotlin#175
tuancoltech merged 3 commits intomainfrom
convert_language_package_to_kotlin

Conversation

@tuancoltech
Copy link
Copy Markdown
Member

@tuancoltech tuancoltech commented Mar 6, 2025

Convert language related classes to Kotlin

Summary by CodeRabbit

  • New Features

    • Introduced an enhanced language selection interface that presents language options in a modal display, offering a smoother and more intuitive selection process.
  • Refactor

    • Replaced legacy language selection components with updated implementations to improve performance, stability, and maintainability.

@tuancoltech tuancoltech requested a review from a team as a code owner March 6, 2025 08:47
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 6, 2025

Walkthrough

This pull request refactors the language selection functionality by removing the previous Java implementations and introducing new Kotlin-based versions. The original LanguageListDialogFragment and SelectLanguageActivity written in Java have been removed. Their Kotlin counterparts now provide equivalent functionality through a modal bottom sheet for language display, including a RecyclerView with an adapter for handling item clicks, logging, preference updates, and navigation to the main activity. The updated flow adheres to modern Android development practices.

Changes

File(s) Change Summary
app/src/main/java/ai/elimu/.../language/LanguageListDialogFragment.java
app/src/main/java/ai/elimu/.../language/SelectLanguageActivity.java
Removed outdated Java implementations for displaying and handling language selection UI components.
app/src/main/java/ai/elimu/.../language/LanguageListDialogFragment.kt
app/src/main/java/ai/elimu/.../language/SelectLanguageActivity.kt
Added new Kotlin implementations: a LanguageListDialogFragment that sets up a RecyclerView with a click-enabled adapter triggering logging, preference updates, and navigation, and a SelectLanguageActivity that launches the fragment.

Sequence Diagram(s)

sequenceDiagram
    participant S as SelectLanguageActivity
    participant F as LanguageListDialogFragment
    participant A as LanguageAdapter
    participant M as MainActivity

    S->>F: onStart calls newInstance() to display dialog
    F->>A: Initialize RecyclerView & adapter
    A->>F: On language item click (log & persist selection)
    F->>M: Launch MainActivity and finish current activity
Loading

Possibly related PRs

Suggested reviewers

  • jo-elimu

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cd4d22c and ae25071.

📒 Files selected for processing (1)
  • app/src/main/java/ai/elimu/content_provider/language/LanguageListDialogFragment.kt (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • app/src/main/java/ai/elimu/content_provider/language/LanguageListDialogFragment.kt
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: build (ubuntu-latest, 17)
  • GitHub Check: build (windows-latest, 17)
  • GitHub Check: build (macos-latest, 17)
  • GitHub Check: build (windows-latest, 21)

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (8)
app/src/main/java/ai/elimu/content_provider/language/SelectLanguageActivity.kt (2)

17-22: Consider adding dialog dismissal handling.

The dialog fragment is shown in onStart(), which is correct, but there's no handling to prevent showing multiple instances if onStart() is called multiple times (like when returning from background). Consider checking if the dialog is already showing before creating a new instance.

override fun onStart() {
    Log.i(javaClass.name, "onStart")
    super.onStart()

-   newInstance().show(supportFragmentManager, "dialog")
+   if (supportFragmentManager.findFragmentByTag("dialog") == null) {
+       newInstance().show(supportFragmentManager, "dialog")
+   }
}

4-4: Use standard Kotlin import convention.

Consider using a direct import instead of importing the companion object method. This is more idiomatic in Kotlin.

-import ai.elimu.content_provider.language.LanguageListDialogFragment.Companion.newInstance
+import ai.elimu.content_provider.language.LanguageListDialogFragment

Then update the usage:

-newInstance().show(supportFragmentManager, "dialog")
+LanguageListDialogFragment.newInstance().show(supportFragmentManager, "dialog")
app/src/main/java/ai/elimu/content_provider/language/LanguageListDialogFragment.kt (6)

74-74: Use Kotlin best practices for enum collection handling.

Instead of .entries.toTypedArray(), you can directly use .values() for enum values in Kotlin. This is more idiomatic and avoids unnecessary conversions.

-private val languages = Language.entries.toTypedArray()
+private val languages = Language.values()

83-95: Use Kotlin's lambda syntax for click listeners.

Replace the anonymous View.OnClickListener with Kotlin's lambda syntax for a more concise and idiomatic implementation.

-holder.text.setOnClickListener(object : View.OnClickListener {
-    override fun onClick(v: View) {
-        Log.i(javaClass.name, "onClick")
-        Log.i(javaClass.name, "language: $language")
-        SharedPreferencesHelper.storeLanguage(context, language)
-        val mainActivityIntent = Intent(
-            context,
-            MainActivity::class.java
-        )
-        startActivity(mainActivityIntent)
-        activity!!.finish()
-    }
-})
+holder.text.setOnClickListener { 
+    Log.i(javaClass.name, "onClick")
+    Log.i(javaClass.name, "language: $language")
+    SharedPreferencesHelper.storeLanguage(context, language)
+    val mainActivityIntent = Intent(
+        context,
+        MainActivity::class.java
+    )
+    startActivity(mainActivityIntent)
+    activity?.finish()
+}

103-109: Simplify companion object function.

The newInstance() function can be simplified since it's not using any parameters.

companion object {
    @JvmStatic
    fun newInstance(): LanguageListDialogFragment {
-       val fragment = LanguageListDialogFragment()
-       return fragment
+       return LanguageListDialogFragment()
    }
}

41-53: Consider implementing view binding.

Using view binding would make the code safer by eliminating the need for casting and findViewById calls.

First, enable view binding in your build.gradle:

android {
    ...
    buildFeatures {
        viewBinding true
    }
}

Then update your code to use view binding:

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

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View {
    _binding = FragmentLanguageListDialogListDialogBinding.inflate(inflater, container, false)
    return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    binding.recyclerView.layoutManager = LinearLayoutManager(context)
    binding.recyclerView.adapter = LanguageAdapter()
    binding.recyclerView.addItemDecoration(
        DividerItemDecoration(
            context,
            DividerItemDecoration.VERTICAL
        )
    )

    isCancelable = false
}

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

19-27: Update documentation style.

The KDoc style documentation is using JavaDoc format with HTML tags. Consider updating it to use proper KDoc format.

/**
- *
- * A fragment that shows a list of items as a modal bottom sheet.
- *
- * You can show this modal bottom sheet from your activity like this:
- * <pre>
- * LanguageListDialogFragment.newInstance().show(getSupportFragmentManager(), "dialog");
- *</pre> *
+ * A fragment that shows a list of items as a modal bottom sheet.
+ * 
+ * You can show this modal bottom sheet from your activity like this:
+ * ```
+ * LanguageListDialogFragment.newInstance().show(supportFragmentManager, "dialog")
+ * ```
 */

88-91: Simplify Intent creation.

Use Kotlin's simplified syntax for creating Intents.

-val mainActivityIntent = Intent(
-    context,
-    MainActivity::class.java
-)
+val mainActivityIntent = Intent(context, MainActivity::class.java)
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 468dd89 and cd4d22c.

📒 Files selected for processing (4)
  • app/src/main/java/ai/elimu/content_provider/language/LanguageListDialogFragment.java (0 hunks)
  • app/src/main/java/ai/elimu/content_provider/language/LanguageListDialogFragment.kt (1 hunks)
  • app/src/main/java/ai/elimu/content_provider/language/SelectLanguageActivity.java (0 hunks)
  • app/src/main/java/ai/elimu/content_provider/language/SelectLanguageActivity.kt (1 hunks)
💤 Files with no reviewable changes (2)
  • app/src/main/java/ai/elimu/content_provider/language/SelectLanguageActivity.java
  • app/src/main/java/ai/elimu/content_provider/language/LanguageListDialogFragment.java
⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: build (windows-latest, 21)
  • GitHub Check: build (windows-latest, 17)
  • GitHub Check: build (macos-latest, 17)
  • GitHub Check: build (ubuntu-latest, 21)
  • GitHub Check: build (ubuntu-latest, 17)
🔇 Additional comments (2)
app/src/main/java/ai/elimu/content_provider/language/SelectLanguageActivity.kt (1)

9-15: Lifecycle handling looks good.

The activity properly overrides onCreate, calls super.onCreate and sets the content view layout.

app/src/main/java/ai/elimu/content_provider/language/LanguageListDialogFragment.kt (1)

87-87: Consider handling potential null context.

When storing the language preference, you're using the context directly which could be null. Consider using a safe call or requireContext() to handle this case properly.

-SharedPreferencesHelper.storeLanguage(context, language)
+SharedPreferencesHelper.storeLanguage(requireContext(), language)

MainActivity::class.java
)
startActivity(mainActivityIntent)
activity!!.finish()
Copy link
Copy Markdown

@coderabbitai coderabbitai bot Mar 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Replace unsafe null assertion with safe call.

The use of !! can lead to NullPointerExceptions. Replace with the safe call operator ?. for better null safety.

-activity!!.finish()
+activity?.finish()
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
activity!!.finish()
activity?.finish()

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@coderabbitai Fixed.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tuancoltech Thanks for making that change! Using safe call operators is a great practice in Kotlin for better null safety.


If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!

@tuancoltech tuancoltech merged commit b4401fe into main Mar 7, 2025
6 checks passed
@tuancoltech tuancoltech deleted the convert_language_package_to_kotlin branch March 7, 2025 03:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants