首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >检查用户验证状态

检查用户验证状态
EN

Code Review用户
提问于 2019-03-20 17:19:38
回答 1查看 52关注 0票数 2

用户故事如下:

允许用户进行“验证”,因此其他用户知道他们已经通过了背景检查。

点击“获取验证”按钮或显示在另一个用户配置文件上的“验证徽章”后,它们将被重定向到一个视图,该视图将提示current user to get verified,指示当前用户拥有already been verified,或指示user they selected is already verified。在代码中,这个逻辑是通过调用checkStatus方法来启动的。

验证过程要求用户首先上传他们的ID (2次尝试成功上传),并同意背景检查。这些步骤是通过使用UIPageViewController来执行的。

这是我的代码,用于确定当前用户在验证过程中的哪个阶段、要切换到的页面以及要显示的视图。我认为我的逻辑设置是相当混乱的,我正在寻求建议,使它更干净和简洁。如有任何建议,敬请见谅。我留下了一些注释,可以帮助阅读代码。这并不复杂,但我认为代码可以改进,我写得很快。

代码语言:javascript
运行
复制
- (void)checkStatus {
    if (![self isCurrentUserEqualToBriefUser]) {
        // Current user tapped on verification badge of another user.
        // Still check progress of current user to enable/disable button to get verified.
        // If current user is verified that display type will overried dispaly indicating other person is verified.
        [self setupViewWithAppearance:AppearanceTypeTheyreVerified];
    }
    if (![self isUserVerified]) {
        [self checkProgress];
    }
}

- (BOOL)isUserVerified {
    // When user completes verification, Core Data is updated with date of verification. If `isVerificationDateValid` is nil, or invalid, they are not verified.
    if ([CoreDataUser currentUser].isVerificationDateValid) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if ([self isCurrentUserEqualToBriefUser]) {
                [self setupViewWithAppearance:AppearanceTypeYoureVerified];
            }
            [self setPermissionToContinue:NO];
            [self removeLoadingAnimation];
        });
        return YES;
    }
    return NO;
}

- (void)checkProgress {
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.view setUserInteractionEnabled:NO];
    });
    RequestTask *task = [[VerificationDataManager sharedInstance] getVerificationStatus:^(NSDictionary *results, NSError *error) {
        if (results) {
            // Check if user failed background check
            if ([results valueForKey:kVerificationIsBackgroundCheckSuccessful] != nil) {
                if ([results[kVerificationIsBackgroundCheckSuccessful] boolValue] == NO) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        if ([self isCurrentUserEqualToBriefUser]) {
                            [self setupViewWithAppearance:AppearanceTypeGetVerified];
                        }
                        [self setPermissionToContinue:NO];
                        [self removeLoadingAnimation];
                    });
                    return;
                }
            }
            // get status of ID and backround check
            if (![self isIdUploadInProgress:results]) {
                [self isBackgroundCheckInProgress:results];
            }
        } else if (error) {
            if (error.code == 204) {
                // 204 error  means no object exist. so user isnt verified yet.
                dispatch_async(dispatch_get_main_queue(), ^{
                    if ([self isCurrentUserEqualToBriefUser]) {
                        [self setupViewWithAppearance:AppearanceTypeGetVerified];
                    }
                    [self setPermissionToContinue:YES];
                    [self removeLoadingAnimation];
                });
            } else {
                // Actual error.
                VerifyPageViewController *parent = (VerifyPageViewController *)[self parentViewController];
                [parent dismissViewControllerAnimated:YES completion:nil];
                [self setPermissionToContinue:NO];
                [self removeLoadingAnimation];
            }
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.view setUserInteractionEnabled:YES];
        });
    }];
    [RequestsDispatcher performSingleRequest:task];
}

- (BOOL)isIdUploadInProgress:(NSDictionary *)results {
    VerifyPageViewController *parent = (VerifyPageViewController *)[self parentViewController];
    if ([results valueForKey:kVerificationIsIdVerified] == nil) {
        // key does not exist so has not attempted to submit ID yet.
        [parent nextPageAtIndex:2];
        return YES;
    } else {
        // key does exist, but ID not verified so check how many attempts were performed.
        if ([results[kVerificationIsIdVerified] boolValue] == NO) {
            // 2 attempts max
            int idAttemptCount = [[results objectForKey:kIdUploadAttempts] intValue];
            if (idAttemptCount > 1) {
                // reached max ID upload attempts.
                // set isIDSubmitting to YES, so page 7 shows proper related error.
                // Maybe use state machine with enums in data manager instead.
                [VerificationDataManager sharedInstance].isIdSumitting = YES; 
                [parent nextPageAtIndex:7];
            } else {
                // segue to ID upload page.
                [parent nextPageAtIndex:2];
            }
            return YES;
        }
    }
    return NO;
}

- (BOOL)isBackgroundCheckInProgress:(NSDictionary *)results {
    if ([results valueForKey:kVerificationIsBackgroundCheckSuccessful] == nil) {
        // key doesnt exist, so havnt perfomred backgorund check yet.
        VerifyPageViewController *parent = (VerifyPageViewController *)[self parentViewController];
        [parent nextPageAtIndex:4];
        return YES;
    } else {
        // already check for backgroundcheck failure and
        dispatch_async(dispatch_get_main_queue(), ^{
            if ([self isCurrentUserEqualToBriefUser]) {
                [self setupViewWithAppearance:AppearanceTypeGetVerified];
            }
            [self setPermissionToContinue:NO];
            [self removeLoadingAnimation];
        });
        return NO;
    }
}
EN

回答 1

Code Review用户

发布于 2019-04-15 19:07:52

这是好的,你是有那种感觉,有些不对劲。你违反了单一责任原则。您的视图控制器应该只负责它的视图层次结构,而不是驱动视图层次结构的数据。

您应该创建一个数据模型/上下文来封装围绕用户验证状态的逻辑。您可以观察该模型中的更改以更新您的UI。

  1. 创建数据模型,并可选择围绕该数据创建上下文。
代码语言:javascript
运行
复制
    @interface User

    @property (nonatomic, readonly) BOOL idUploading;
    @property (nonatomic, readonly) BOOL idUploaded;
    @property (nonatomic, readonly) BOOL verified;

    @end

数据模型/上下文的实现基于验证状态更新属性。

  1. 现在,您可以观察使用键值观测更新页面视图控制器的数据模型。
票数 1
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/215856

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档