• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.deskclock.widget
18 
19 import android.transition.Fade
20 import android.transition.Transition
21 import android.transition.TransitionManager
22 import android.transition.TransitionSet
23 import android.view.View
24 import android.view.ViewGroup
25 
26 import com.android.deskclock.Utils
27 
28 /**
29  * Controller that displays empty view and handles animation appropriately.
30  *
31  * @param contentView The view that should be displayed when empty view is hidden.
32  * @param emptyView The view that should be displayed when main view is empty.
33  */
34 class EmptyViewController(
35     private val mMainLayout: ViewGroup,
36     private val mContentView: View,
37     private val mEmptyView: View
38 ) {
39     private var mEmptyViewTransition: Transition? = null
40     private var mIsEmpty = false
41 
42     init {
43         mEmptyViewTransition = if (USE_TRANSITION_FRAMEWORK) {
44             TransitionSet()
45                     .setOrdering(TransitionSet.ORDERING_SEQUENTIAL)
46                     .addTarget(mContentView)
47                     .addTarget(mEmptyView)
48                     .addTransition(Fade(Fade.OUT))
49                     .addTransition(Fade(Fade.IN))
50                     .setDuration(ANIMATION_DURATION.toLong())
51         } else {
52             null
53         }
54     }
55 
56     /**
57      * Sets the state for the controller. If it's empty, it will display the empty view.
58      *
59      * @param isEmpty Whether or not the controller should transition into empty state.
60      */
setEmptynull61     fun setEmpty(isEmpty: Boolean) {
62         if (mIsEmpty == isEmpty) {
63             return
64         }
65         mIsEmpty = isEmpty
66         // State changed, perform transition.
67         if (USE_TRANSITION_FRAMEWORK) {
68             TransitionManager.beginDelayedTransition(mMainLayout, mEmptyViewTransition)
69         }
70         mEmptyView.visibility = if (mIsEmpty) View.VISIBLE else View.GONE
71         mContentView.visibility = if (mIsEmpty) View.GONE else View.VISIBLE
72     }
73 
74     companion object {
75         private const val ANIMATION_DURATION = 300
76         private val USE_TRANSITION_FRAMEWORK = Utils.isLOrLater
77     }
78 }