إذا لم تكن أدوات المطابقة المقدَّمة كافية لحالات الاستخدام، يمكنك إنشاء أدوات المطابقة الخاصة بك من خلال توسيع فئة DeepLinkMatcher.
عند توسيع DeepLinkMatcher<T, R>، يجب تحديد وسيطتَي نوع: نوع مفتاح التنقّل في الوجهة (T : Any) ونوع نتيجة المطابقة (R : DeepLinkMatcher.MatchResult<T>). إذا لم تكن أداة المطابقة بحاجة إلى ترتيب مخصّص للنتائج أو بيانات وصفية إضافية، استخدِم DeepLinkMatcher.MatchResult<T> كوسيطة النوع الثانية.
على سبيل المثال، إليك تنفيذ أساسي لـ TelDeepLinkMatcher يتيح استخدام معرّفات الموارد المنتظمة (URI) من النوع tel (مثل tel:5550100). وبما أنّ معرّفات الموارد المنتظمة (URI) من النوع 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 } }