Skip to content

Commit 56f7e96

Browse files
committed
Convert cascade if clause to when block for readability
1 parent 752d21c commit 56f7e96

1 file changed

Lines changed: 45 additions & 40 deletions

File tree

app/src/main/java/ai/elimu/content_provider/provider/WordContentProvider.kt

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -50,46 +50,51 @@ class WordContentProvider : ContentProvider() {
5050

5151
val code = MATCHER.match(uri)
5252
Log.i(TAG, "code: $code")
53-
if (code == CODE_WORDS) {
54-
// Get the Room Cursor
55-
val cursor = wordDao.loadAllOrderedByUsageCountAsCursor()
56-
Log.i(TAG, "cursor: $cursor")
57-
58-
cursor.setNotificationUri(context.contentResolver, uri)
59-
60-
return cursor
61-
} else if (code == CODE_WORDS_BY_STORYBOOK_PARAGRAPH_ID) {
62-
// Extract the StoryBookParagraph ID from the URI
63-
val pathSegments = uri.pathSegments
64-
Log.i(TAG, "pathSegments: $pathSegments")
65-
val storyBookParagraphIdAsString = pathSegments[2]
66-
val storyBookParagraphId = storyBookParagraphIdAsString.toLong()
67-
Log.i(TAG, "storyBookParagraphId: $storyBookParagraphId")
68-
69-
// Get the Room Cursor
70-
val cursor = wordDao.loadAllAsCursor(storyBookParagraphId)
71-
Log.i(TAG, "cursor: $cursor")
72-
73-
cursor.setNotificationUri(context.contentResolver, uri)
74-
75-
return cursor
76-
} else if (code == CODE_WORD_ID) {
77-
// Extract the Word ID from the URI
78-
val pathSegments = uri.pathSegments
79-
Log.i(TAG, "pathSegments: $pathSegments")
80-
val wordIdAsString = pathSegments[1]
81-
val wordId = wordIdAsString.toLong()
82-
Log.i(TAG, "wordId: $wordId")
83-
84-
// Get the Room Cursor
85-
val cursor = wordDao.loadAsCursor(wordId)
86-
Log.i(TAG, "cursor: $cursor")
87-
88-
cursor.setNotificationUri(context.contentResolver, uri)
89-
90-
return cursor
91-
} else {
92-
throw IllegalArgumentException("Unknown URI: $uri")
53+
when (code) {
54+
CODE_WORDS -> {
55+
// Get the Room Cursor
56+
val cursor = wordDao.loadAllOrderedByUsageCountAsCursor()
57+
Log.i(TAG, "cursor: $cursor")
58+
59+
cursor.setNotificationUri(context.contentResolver, uri)
60+
61+
return cursor
62+
}
63+
CODE_WORDS_BY_STORYBOOK_PARAGRAPH_ID -> {
64+
// Extract the StoryBookParagraph ID from the URI
65+
val pathSegments = uri.pathSegments
66+
Log.i(TAG, "pathSegments: $pathSegments")
67+
val storyBookParagraphIdAsString = pathSegments[2]
68+
val storyBookParagraphId = storyBookParagraphIdAsString.toLong()
69+
Log.i(TAG, "storyBookParagraphId: $storyBookParagraphId")
70+
71+
// Get the Room Cursor
72+
val cursor = wordDao.loadAllAsCursor(storyBookParagraphId)
73+
Log.i(TAG, "cursor: $cursor")
74+
75+
cursor.setNotificationUri(context.contentResolver, uri)
76+
77+
return cursor
78+
}
79+
CODE_WORD_ID -> {
80+
// Extract the Word ID from the URI
81+
val pathSegments = uri.pathSegments
82+
Log.i(TAG, "pathSegments: $pathSegments")
83+
val wordIdAsString = pathSegments[1]
84+
val wordId = wordIdAsString.toLong()
85+
Log.i(TAG, "wordId: $wordId")
86+
87+
// Get the Room Cursor
88+
val cursor = wordDao.loadAsCursor(wordId)
89+
Log.i(TAG, "cursor: $cursor")
90+
91+
cursor.setNotificationUri(context.contentResolver, uri)
92+
93+
return cursor
94+
}
95+
else -> {
96+
throw IllegalArgumentException("Unknown URI: $uri")
97+
}
9398
}
9499
}
95100

0 commit comments

Comments
 (0)