제공된 매처가 사용 사례에 충분하지 않은 경우 DeepLinkMatcher 클래스를 확장하여 자체 매처를 만들 수 있습니다.
DeepLinkMatcher<T, R>를 확장할 때는 대상 탐색 키 유형 (T : Any)과 일치 결과 유형(R : DeepLinkMatcher.MatchResult<T>)이라는 두 가지 유형 인수를 지정해야 합니다. 매처에 맞춤 결과 순위 지정이나 추가 메타데이터가 필요하지 않은 경우 DeepLinkMatcher.MatchResult<T>를 두 번째 유형 인수로 사용합니다.
예를 들어 다음은 tel URI (예: tel:5550100)를 지원하는 TelDeepLinkMatcher의 기본 구현입니다. tel URI는 불투명하므로 UriDeepLinkMatcher는 이를 지원하지 않습니다.
class TelDeepLinkMatcher : DeepLinkMatcher<DialerKey, DeepLinkMatcher.MatchResult<DialerKey>>() { override fun matchRequest(request: DeepLinkRequest): MatchResult<DialerKey>? { val uri = request.uri ?: return null if (uri.scheme != "tel") return null // Note: schemeSpecificPart is only available on Android val phoneNumber = uri.schemeSpecificPart ?: return null return MatchResult(DialerKey(phoneNumber = phoneNumber)) } }
커스텀 MatchResult 클래스 순위 지정
커스텀 매처의 일치 결과를 비교하는 의미 있는 방법이 있는 경우 DeepLinkMatcher.MatchResult를 확장하고 compareTo 메서드를 재정의하여 결과의 순위를 매겨야 합니다.
커스텀 MatchResult 서브클래스를 만들 때 일치자의 클래스 선언을 업데이트하여 해당 서브클래스를 두 번째 유형 매개변수 R (예: class TelDeepLinkMatcher : DeepLinkMatcher<DialerKey, TelMatchResult>())로 지정합니다. 이렇게 하면 호출자가 확인되지 않은 다운캐스팅 없이 커스텀 결과 속성에 액세스할 수 있습니다.
예를 들어 TelDeepLinkMatcher에서 와일드 카드 패턴 일치를 지원하려면 TelMatchResult를 구현하여 정확한 일치 항목이 와일드 카드 일치 항목보다 높은 순위에 오르도록 할 수 있습니다. 매처가 withBackStack로 래핑된 경우 비교하기 전에 WrappedMatchResult를 래핑 해제하여 기본 일치 결과에 대해 우선순위가 평가되도록 합니다.
class TelMatchResult( key: DialerKey, val isExactMatch: Boolean, val patternLength: Int ) : DeepLinkMatcher.MatchResult<DialerKey>(key) { override fun compareTo(other: DeepLinkMatcher.MatchResult<DialerKey>): Int { // Unwrap if the other result is wrapped in a BackStackMatchResult or custom WrappedMatchResult val target = if (other is WrappedMatchResult<*>) other.matchResult else other if (target !is TelMatchResult) { // Determine precedence relative to other MatchResult types (e.g. UriMatchResult) return 1 } // An exact match wins over a wildcard/prefix match if (isExactMatch && !target.isExactMatch) return 1 if (!isExactMatch && target.isExactMatch) return -1 // The more specific (longer) pattern wins (e.g., tel:1800* versus tel:*) val lengthDiff = this.patternLength - target.patternLength if (lengthDiff != 0) { return lengthDiff } return 0 } }