अगर उपलब्ध मैच करने वाले फ़ंक्शन आपके इस्तेमाल के उदाहरणों के लिए काफ़ी नहीं हैं, तो DeepLinkMatcher क्लास को बढ़ाकर, अपने मैच करने वाले फ़ंक्शन बनाए जा सकते हैं.
DeepLinkMatcher<T, R> को बढ़ाते समय, आपको दो टाइप आर्ग्युमेंट तय करने होंगे: डेस्टिनेशन नेविगेशन की टाइप (T : Any) और मैच के नतीजे का टाइप (R : DeepLinkMatcher.MatchResult<T>). अगर आपके मैच करने वाले फ़ंक्शन को कस्टम नतीजे की रैंकिंग या अतिरिक्त मेटाडेटा की ज़रूरत नहीं है, तो दूसरे टाइप आर्ग्युमेंट के तौर पर DeepLinkMatcher.MatchResult<T> का इस्तेमाल करें.
उदाहरण के लिए, यहां TelDeepLinkMatcher को लागू करने का एक बुनियादी तरीका बताया गया है. यह tel यूआरआई (जैसे कि tel:5550100) के साथ काम करता है. tel यूआरआई अपारदर्शी होते हैं, इसलिए 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 } }