事件即将开始,开始或结束时更新应用程序

时间:2018-07-18 00:15:40

标签: android service google-cloud-firestore android-workmanager

我是新来的,所以我不确定自己做的是否正确。

我正在创建一个应用程序,可以在其中将主题(基本上是定时事件)上载到Firestore数据库。我遇到的问题是如何正确管理这些事件。目前,我正在使用WorkManager来安排每小时从数据库中读取数据,以检查是否添加了任何新主题,但是这样做的问题是该服务可能尚未启动,因此在打开应用程序时将显示我创建的空状态,但我想不出一种方法来通过滑动刷新或类似操作手动启动服务。

目前,它还可以将我的离线房间db与新主题同步(实际上是从firestore中获取它们的),效果很好。但是我想使其更加流畅和自动化,这就是为什么我选择使用WorkManager

如果值得注意,我尝试使用SharedPreferences来跟踪主题的状态,并根据该状态(无论它是开始,开始还是结束)发送通知,但是问题在于每次安装应用程序时,我都必须清除应用程序数据。

您到底会怎么做?

倾向于朝正确的方向移动,但也欢迎使用代码示例(也许是伪代码)。

工人阶级:

@Override
@NonNull
public Result doWork() {
        try{

            Query query = firestore.collection("themes").orderBy("start_time");

            query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if(task.isSuccessful()){
                        for(DocumentSnapshot snapshot : task.getResult()){
                            if(snapshot.exists()){
                                Themes themes = snapshot.toObject(Themes.class);

                                themeKey = snapshot.getId();

                                DateTime today = new DateTime();
                                DateTime start_time = new DateTime(themes.start_time);
                                DateTime end_time = start_time.plus(INTERVAL);

                                if (today.isBefore(start_time)) {
                                    CurrentTheme currentTheme = new CurrentTheme(themes.theme_name, themes.theme_description,
                                            String.valueOf(start_time.getMillis()), "Starts in: ", themeKey, themes.sponsor_url, themes.theme_img);

                                    mFirebaseMethods.insert(currentTheme);

                                    if(!FireApp.getPreferences().getBoolean("starting", false)){
                                        notifyThemeStatus(currentTheme.getTheme_name() + " starting at " +
                                                start_time.toString(), FireApp.getApp()
                                                .getApplicationContext().getSharedPreferences(Config.SHARED_PREF, 0).getString("regId", null));

                                        editor = FireApp.getPreferences().edit();
                                        editor.putBoolean("starting", true);
                                        editor.putBoolean("started", false);
                                        editor.putBoolean("ended", false);
                                        editor.apply();
                                    }

                                } else if(today.isAfter(start_time) && today.isBefore(end_time)){
                                    mFirebaseMethods.deleteByTime(String.valueOf(start_time.getMillis())); 

                                    CurrentTheme currentTheme = new CurrentTheme(themes.theme_name, themes.theme_description,
                                            String.valueOf(end_time.getMillis()), "Ends in: ", themeKey, themes.sponsor_url, themes.theme_img);

                                    mFirebaseMethods.update(currentTheme);

                                    if(!FireApp.getPreferences().getBoolean("started", false)) {
                                        notifyThemeStatus(themes.theme_name + " ends " +
                                                getRelativeTimeSpanString(themes.start_time + INTERVAL).toString(), FireApp.getApp()
                                                .getApplicationContext().getSharedPreferences(Config.SHARED_PREF, 0).getString("regId", null));

                                        editor = FireApp.getPreferences().edit();
                                        editor.putBoolean("starting", false);
                                        editor.putBoolean("started", true);
                                        editor.apply();
                                    }
                                } else if (today.isAfter(end_time)){

                                    CurrentTheme currentTheme = new CurrentTheme(themes.theme_name, themes.theme_description,
                                            String.valueOf(0), "Ended", themeKey, themes.sponsor_url, themes.theme_img);

                                    mFirebaseMethods.update(currentTheme);

                                    //checkWinner(themeKey, themes.start_time, themes.theme_name);

                                    if(!FireApp.getPreferences().getBoolean("ended", false)) {

                                        notifyThemeStatus(themes.theme_name + " has ended.", FireApp.getApp()
                                                .getApplicationContext().getSharedPreferences(Config.SHARED_PREF, 0).getString("regId", null));

                                        editor = FireApp.getPreferences().edit();
                                        editor.putBoolean("ended", true);
                                        editor.putBoolean("started", false);
                                        editor.apply();
                                    }

                                }
                            }
                        }
                    }
                }
            });
            return Result.SUCCESS;
        }catch (Throwable throwable){
            Log.e("PopulateThemeCache", "Error caching", throwable);
            return Result.FAILURE;
        }
    }

我在应用程序类中启动工作程序的位置:

Constraints constraints = new Constraints.Builder()
            .setRequiredNetworkType(NetworkType.CONNECTED)
            .build();

PeriodicWorkRequest populateCache = new PeriodicWorkRequest.Builder(PopulateThemeCache.class, 1, TimeUnit.HOURS)
            .setConstraints(constraints)
            .addTag("Theme_Update")
            .build();

WorkManager.getInstance().enqueue(populateCache);

mFirebaseMethods是一个存储库类,其中包含用于执行会议室db操作的所有asynctask代码。

0 个答案:

没有答案