// Set up the toolbar. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); ActionBar ab = getSupportActionBar(); ab.setHomeAsUpIndicator(R.drawable.ic_menu); ab.setDisplayHomeAsUpEnabled(true);
/** * 以下的DrawerLayout暂时不看了 */ // Set up the navigation drawer. mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); mDrawerLayout.setStatusBarBackground(R.color.colorPrimaryDark); NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); if (navigationView != null) { setupDrawerContent(navigationView); }
/* * Copyright 2016, The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
/** * Immutable model class for a Task. */ publicfinalclassTask{
@NonNull privatefinal String mId;
@Nullable privatefinal String mTitle;
@Nullable privatefinal String mDescription;
privatefinalboolean mCompleted;
/** * Use this constructor to create a new active Task. * * @param title title of the task * @param description description of the task */ publicTask(@Nullable String title, @Nullable String description){ this(title, description, UUID.randomUUID().toString(), false); }
/** * Use this constructor to create an active Task if the Task already has an id (copy of another * Task). * * @param title title of the task * @param description description of the task * @param id id of the task */ publicTask(@Nullable String title, @Nullable String description, @NonNull String id){ this(title, description, id, false); }
/** * Use this constructor to create a new completed Task. * * @param title title of the task * @param description description of the task * @param completed true if the task is completed, false if it's active */ publicTask(@Nullable String title, @Nullable String description, boolean completed){ this(title, description, UUID.randomUUID().toString(), completed); }
/** * Use this constructor to specify a completed Task if the Task already has an id (copy of * another Task). * * @param title title of the task * @param description description of the task * @param id id of the task * @param completed true if the task is completed, false if it's active */ publicTask(@Nullable String title, @Nullable String description, @NonNull String id, boolean completed){ mId = id; mTitle = title; mDescription = description; mCompleted = completed; }
@NonNull public String getId(){ return mId; }
@Nullable public String getTitle(){ return mTitle; }
@Nullable public String getTitleForList(){ if (!Strings.isNullOrEmpty(mTitle)) { return mTitle; } else { return mDescription; } }
@Nullable public String getDescription(){ return mDescription; }
/** * Returns the single instance of this class, creating it if necessary. * * @param tasksRemoteDataSource the backend data source * @param tasksLocalDataSource the device storage data source * @return the {@link TasksRepository} instance */ publicstatic TasksRepository getInstance(TasksDataSource tasksRemoteDataSource, TasksDataSource tasksLocalDataSource){ if (INSTANCE == null) { INSTANCE = new TasksRepository(tasksRemoteDataSource, tasksLocalDataSource); } return INSTANCE; }
// Do in memory cache update to keep the app UI up to date if (mCachedTasks == null) { mCachedTasks = new LinkedHashMap<>(); } mCachedTasks.put(task.getId(), task); }
// Respond immediately with cache if available and not dirty if (mCachedTasks != null && !mCacheIsDirty) { callback.onTasksLoaded(new ArrayList<>(mCachedTasks.values())); return; }
if (mCacheIsDirty) { // If the cache is dirty we need to fetch new data from the network. getTasksFromRemoteDataSource(callback); } else { // Query the local storage if available. If not, query the network. mTasksLocalDataSource.getTasks(new LoadTasksCallback() { @Override publicvoidonTasksLoaded(List<Task> tasks){ refreshCache(tasks); callback.onTasksLoaded(new ArrayList<>(mCachedTasks.values())); }