{"record":{"id":"37105923d1d0a069","repo":"DrKLO/Telegram","slug":"snap-preference-should-be-one-of-the-constants-def","errorCode":null,"errorMessage":"snap preference should be one of the constants defined in SmoothScroller, starting with SNAP_","messagePattern":"snap preference should be one of the constants defined in SmoothScroller, starting with SNAP_","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"TMessagesProj/src/main/java/androidx/recyclerview/widget/LinearSmoothScroller.java","lineNumber":298,"sourceCode":"    public int calculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, int\n            snapPreference) {\n        switch (snapPreference) {\n            case SNAP_TO_START:\n                return boxStart - viewStart;\n            case SNAP_TO_END:\n                return boxEnd - viewEnd;\n            case SNAP_TO_ANY:\n                final int dtStart = boxStart - viewStart;\n                if (dtStart > 0) {\n                    return dtStart;\n                }\n                final int dtEnd = boxEnd - viewEnd;\n                if (dtEnd < 0) {\n                    return dtEnd;\n                }\n                break;\n            default:\n                throw new IllegalArgumentException(\"snap preference should be one of the\"\n                        + \" constants defined in SmoothScroller, starting with SNAP_\");\n        }\n        return 0;\n    }\n\n    /**\n     * Calculates the vertical scroll amount necessary to make the given view fully visible\n     * inside the RecyclerView.\n     *\n     * @param view           The view which we want to make fully visible\n     * @param snapPreference The edge which the view should snap to when entering the visible\n     *                       area. One of {@link #SNAP_TO_START}, {@link #SNAP_TO_END} or\n     *                       {@link #SNAP_TO_ANY}.\n     * @return The vertical scroll amount necessary to make the view visible with the given\n     * snap preference.\n     */\n    public int calculateDyToMakeVisible(View view, int snapPreference) {\n        final RecyclerView.LayoutManager layoutManager = getLayoutManager();","sourceCodeStart":280,"sourceCodeEnd":316,"githubUrl":"https://github.com/DrKLO/Telegram/blob/45ab8f4308496e1f01026a97fcdb0d58a5274474/TMessagesProj/src/main/java/androidx/recyclerview/widget/LinearSmoothScroller.java#L280-L316","documentation":"LinearSmoothScroller.calculateDtToFit() computes the scroll delta to bring a view into the visible box. Its snapPreference switch only handles SNAP_TO_START, SNAP_TO_END, SNAP_TO_ANY. The default branch rejects any other value because the snapping direction is undefined and the math would be arbitrary.","triggerScenarios":"A subclass overrides calculateDtToFit or calls it directly with an int that is not one of the three SNAP_* constants defined on SmoothScroller. Passing an arbitrary 'gravity'-style flag (e.g. Gravity.CENTER) into calculateDxToMakeVisible/calculateDyToMakeVisible which forward snapPreference.","commonSituations":"Custom SmoothScroller subclass hard-codes a snap preference constant that drifts from the base constants after a version bump. Reusing a Gravity.* or a custom SNAP_*-like value (e.g. 99) without aligning to the framework constants.","solutions":["Use only LinearSmoothScroller.SNAP_TO_START, SNAP_TO_END, or SNAP_TO_END/SNAP_TO_ANY.","When overriding calculateDtToFit, ensure snapPreference passed to super is one of the three constants.","If you need custom snapping, override calculateDtToFit entirely and do not delegate to super with a non-standard value."],"exampleFix":"// before\nreturn calculateDtToFit(vStart, vEnd, bStart, bEnd, 99); // custom flag\n\n// after\nreturn calculateDtToFit(vStart, vEnd, bStart, bEnd, LinearSmoothScroller.SNAP_TO_ANY);","handlingStrategy":"validation","validationCode":"static boolean isValidSnap(int s) {\n    return s == LinearSmoothScroller.SNAP_TO_START\n        || s == LinearSmoothScroller.SNAP_TO_END\n        || s == LinearSmoothScroller.SNAP_TO_ANY;\n}\n// only call calculateDtToFit(..., snap) when isValidSnap(snap) is true","typeGuard":"static boolean isSnapConstant(int s) {\n    return s == LinearSmoothScroller.SNAP_TO_START\n        || s == LinearSmoothScroller.SNAP_TO_END\n        || s == LinearSmoothScroller.SNAP_TO_ANY;\n}","tryCatchPattern":"null","preventionTips":["When subclassing LinearSmoothScroller, pass only the framework SNAP_* constants to calculateDtToFit.","Avoid reusing Gravity or custom flags as snap preferences.","Override calculateDtToFit entirely if you need non-standard snapping."],"tags":["recyclerview","smoothscroller","snap","validation"],"backgroundTag":null,"analyzedSha":"45ab8f4308496e1f01026a97fcdb0d58a5274474","analyzedAt":"2026-08-14T05:19:30.815Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}