-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #251 from Nexters/feature/250
Feature/250 링크 하이라이팅
- Loading branch information
Showing
5 changed files
with
102 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
presentation/src/main/java/com/nexters/boolti/presentation/util/UrlParser.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.nexters.boolti.presentation.util | ||
|
||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.text.AnnotatedString | ||
import androidx.compose.ui.text.SpanStyle | ||
import androidx.compose.ui.text.buildAnnotatedString | ||
import androidx.compose.ui.text.style.TextDecoration | ||
|
||
class UrlParser( | ||
url: String, | ||
urlRegex: Regex = "(https?://\\S+\\b)".toRegex(), | ||
) { | ||
val annotatedString: AnnotatedString | ||
|
||
private val _urlOffsets = mutableListOf<UrlOffset>() | ||
val urlOffsets get() = _urlOffsets.toList() | ||
|
||
init { | ||
val linkMatch = urlRegex.toPattern().matcher(url) | ||
|
||
while (linkMatch.find()) { | ||
_urlOffsets.add(UrlOffset(linkMatch.start(), linkMatch.end())) | ||
} | ||
|
||
annotatedString = buildAnnotatedString { | ||
append(url) | ||
_urlOffsets.forEach { (start, end) -> | ||
addStyle( | ||
SpanStyle( | ||
textDecoration = TextDecoration.Underline, | ||
color = Color(0xFF46A6FF) | ||
), | ||
start, | ||
end, | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
data class UrlOffset( | ||
val start: Int, | ||
val end: Int | ||
) |
17 changes: 0 additions & 17 deletions
17
presentation/src/test/java/com/nexters/boolti/presentation/ExampleUnitTest.kt
This file was deleted.
Oops, something went wrong.
24 changes: 24 additions & 0 deletions
24
presentation/src/test/java/com/nexters/boolti/presentation/util/UrlParserTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.nexters.boolti.presentation.util | ||
|
||
import io.kotest.core.spec.style.BehaviorSpec | ||
import io.kotest.matchers.shouldBe | ||
|
||
class UrlParserTest : BehaviorSpec() { | ||
init { | ||
given("url이 포함된 문자열이 주어지고") { | ||
val targetString = "https://www.naver.com 적당한https://www.naver.com 문자열 http://www.naver.com" | ||
|
||
`when`("이 문자열을 파싱하면") { | ||
val urlParser = UrlParser(targetString) | ||
val urlOffsets = urlParser.urlOffsets | ||
|
||
then("문자열 내 url의 start, end offset을 확인할 수 있다") { | ||
urlOffsets.size shouldBe 3 | ||
urlOffsets[0] shouldBe UrlOffset(0, 21) | ||
urlOffsets[1] shouldBe UrlOffset(25, 46) | ||
urlOffsets[2] shouldBe UrlOffset(51, 71) | ||
} | ||
} | ||
} | ||
} | ||
} |