제공된 일치 도구가 사용 사례에 충분하지 않은 경우
DeepLinkMatcher 클래스를 확장하여 직접 만들 수 있습니다.
DeepLinkMatcher<T, R>을 확장할 때는 대상 탐색 키 유형 (T : Any)과 일치 결과 유형(R : DeepLinkMatcher.MatchResult<T>)이라는 두 가지 유형 인수를 지정해야 합니다. 일치 도구에 맞춤 결과 순위 또는 추가 메타데이터가 필요하지 않은 경우 DeepLinkMatcher.MatchResult<T>를 두 번째 유형 인수로 사용합니다.
예를 들어 다음은 TelDeepLinkMatcher가 tel URI (tel:5550100 등)를 지원하는 기본 구현입니다. 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 } }