简体   繁体   English

如何禁用按钮一天

[英]How to disable button for one day

I want to disable the upload button for one day for after uploading firebase photo. 我想在上传Firebase照片后禁用一天的上传按钮。 i tried these codes, countdown works incorrectly when I change activity. 我尝试了这些代码,当我更改活动时,倒数计时工作不正确。

   SharedPreferences prefs = getSharedPreferences("time", Context.MODE_PRIVATE);
                long currentTime = new Date().getTime();
                SharedPreferences.Editor editor = prefs.edit();
                editor.putLong("time", currentTime);
                editor.apply();
                dwn.setEnabled(false);



    SharedPreferences prefs = getSharedPreferences("time", Context.MODE_PRIVATE);
    long previousTime = prefs.getLong("time", 0);
    long currentTime = new Date().getTime();
    if (currentTime - previousTime > 60*1000){

        dwn.setEnabled(true);
    } else {
        dwn.setEnabled(false);
        new CountDownTimer(currentTime - previousTime, 1000) {
            public void onTick(long millisUntilFinished) {
                dwn.setText("fdfjhsn" + millisUntilFinished / 1000);
            }

            public void onFinish() {
                dwn.setEnabled(true);
            }
        }.start();

This might be more than you were looking for, but I would extract the decision making to a separate class. 这可能比您想要的要多,但我会将决策提取到一个单独的类中。 Consider the following: 考虑以下:

interface UserUploadHelper {
    void userUploadedImage();
    boolean canUserUploadImages();
}

We can have an implementation of this, backed by SharedPreferences. 我们可以在SharedPreferences的支持下实现此实现。 This object can then be injected into wherever we need it: 然后可以将该对象注入到我们需要的任何位置:

class SharedPreferencesUserUploadHelper implements UserUploadHelper {
    private static final String LAST_IMAGE_UPLOAD_TIME = "last.user.upload.time";
    private static final String PREFS_NAME = "user.upload";
    private static final String PREFS_MODE = Context.MODE_PRIVATE;
    private static final String MILLIS_IN_DAY = 24 * 60 * 60 * 1000;

    private final SharedPreferences sharedPreferences;

    SharedPreferencesUserUploadHelper(Context context) {
        sharedPreferences = context.getSharedPreferences(PREFS_NAME, PREFS_MODE);
    }

    @Override
    void userUploadedImage() {
        sharedPreferences.edit().putLong(
            LAST_IMAGE_UPLOAD_TIME, 
            new Date().getTime()
        ).apply();
    }

    @Override
    boolean canUserUploadImages() {
        long lastUploadTime = sharedPreferences.getLong(LAST_IMAGE_UPLOAD_TIME, 0L);
        long now = new Date().getTime();
        return lastUploatTime - now > MILLIS_IN_DAY;
    }
}

Inside your upload code, when the code finishes uploading, you can call userUploadedImage() 在您的上传代码中,当代码完成上传后,您可以调用userUploadedImage()

Inside your activity code, you can check whether the button should be enabled with canUserUploadImages() . 在活动代码中,您可以检查是否应使用canUserUploadImages()启用按钮。

Doing this lets you change how you store that value and calculate the timeout without changing your firebase logic or your view logic. 这样做可以让您更改存储该值的方式并计算超时,而无需更改Firebase逻辑或视图逻辑。

You can inject this into a class using a tool like Dagger , or simply by creating an instance of it via its constructor. 您可以使用Dagger类的工具将其注入到类中,或者仅通过其构造函数创建其实例即可。 If you want to hide the concrete SharedPreferencesUserUploadHelper implementation, you can use a factory! 如果要隐藏具体的SharedPreferencesUserUploadHelper实现,则可以使用工厂! This is good when you might decide to change how this class is implemented later. 当您可能决定稍后更改此类的实现方式时,这很好。

public UserUploadHelperFactory {
    public UserUploadHelper create(Context context) {
        return new SharedPreferencesUploadHelper();
    }
}

If we wanted to go a step further, we would actually break our original interface into two different interfaces, one for each method, since each method has different interested parties, and, for example, the view shouldn't be able to call userUploadedImage (Which makes that original interface a slight violation of the Interface Segregation Principle!) 如果想进一步,我们实际上会将原始接口分为两个不同的接口,每个接口一个,因为每个方法都有不同的关注方,例如,视图不应调用userUploadedImage (这使得该原始接口略微违反了接口隔离原则!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM