/* * Copyright (C) 2017 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. */ package com.example.android.architecture.blueprints.todoapp.util
/** * Various extension functions for AppCompatActivity. */
const val ADD_EDIT_RESULT_OK = Activity.RESULT_FIRST_USER + 1 const val DELETE_RESULT_OK = Activity.RESULT_FIRST_USER + 2 const val EDIT_RESULT_OK = Activity.RESULT_FIRST_USER + 3
/** * The `fragment` is added to the container view with id `frameId`. The operation is * performed by the `fragmentManager`. */ fun AppCompatActivity.replaceFragmentInActivity(fragment: Fragment, frameId: Int) { supportFragmentManager.transact { replace(frameId, fragment) } }
/** * The `fragment` is added to the container view with tag. The operation is * performed by the `fragmentManager`. */ fun AppCompatActivity.addFragmentToActivity(fragment: Fragment, tag: String) { supportFragmentManager.transact { add(fragment, tag) } }
/** * Transforms static java function Snackbar.make() to an extension function on View. */ fun View.showSnackbar(snackbarText: String, timeLength: Int) { Snackbar.make(this, snackbarText, timeLength).show() }
/** * Triggers a snackbar message when the value contained by snackbarTaskMessageLiveEvent is modified. */ fun View.setupSnackbar(lifecycleOwner: LifecycleOwner, snackbarMessageLiveEvent: SingleLiveEvent<Int>, timeLength: Int) { snackbarMessageLiveEvent.observe(lifecycleOwner, Observer { // 这个it是singleLiveEvent.value,存放的是snackbar要展示的文本的resId // 如果it不是null,才做showSnackbar的操作 it?.let { showSnackbar(context.getString(it), timeLength) } }) }
/** * Reloads the data when the pull-to-refresh is triggered. * * Creates the `android:onRefresh` for a [SwipeRefreshLayout]. */ @BindingAdapter("android:onRefresh") fun ScrollChildSwipeRefreshLayout.setSwipeRefreshLayoutOnRefreshListener( viewModel: TasksViewModel) { setOnRefreshListener { viewModel.loadTasks(true) } }
@MainThread override fun observe(owner: LifecycleOwner, observer: Observer<T>) { // 判断是否曾经注册过监听了 if (hasActiveObservers()) { Log.w(TAG, "Multiple observers registered but only one will be notified of changes.") }
// Observe the internal MutableLiveData super.observe(owner, Observer<T> { t -> if (pending.compareAndSet(true, false)) { observer.onChanged(t) } }) }
@MainThread override fun setValue(t: T?) { pending.set(true) super.setValue(t) }
/** * Used for cases where T is Void, to make calls cleaner. */ @MainThread fun call() { value = null }
companion object { private const val TAG = "SingleLiveEvent" } }
在vm中建立一个对象 val newTaskEvent= SingleLiveEvent<Void>() 在某个Activity或Fragment中注册监听。
1 2 3 4 5 6
viewModel = obtainViewModel().apply { // Subscribe to "new task" event newTaskEvent.observe(this@TasksActivity, Observer<Void> { this@TasksActivity.addNewTask() }) }
13、 可以看到分包是根据PBF(package by feature)的,而不是PBL (package by layer),根据业务特征来分类。而其中有一个data包是这样的