Home | History | Annotate | Download | only in bubbles
      1 /*
      2  * Copyright (C) 2019 The Android Open Source Project
      3  * Licensed under the Apache License, Version 2.0 (the "License");
      4  * you may not use this file except in compliance with the License.
      5  * You may obtain a copy of the License at
      6  *
      7  *       http://www.apache.org/licenses/LICENSE-2.0
      8  *
      9  * Unless required by applicable law or agreed to in writing, software
     10  * distributed under the License is distributed on an "AS IS" BASIS,
     11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12  * See the License for the specific language governing permissions and
     13  * limitations under the License.
     14  */
     15 
     16 package com.example.android.bubbles
     17 
     18 import android.content.Intent
     19 import android.os.Bundle
     20 import android.transition.Transition
     21 import android.transition.TransitionInflater
     22 import android.transition.TransitionManager
     23 import android.view.View
     24 import android.view.ViewGroup
     25 import android.widget.ImageView
     26 import android.widget.TextView
     27 import androidx.appcompat.app.AppCompatActivity
     28 import androidx.fragment.app.FragmentManager
     29 import androidx.fragment.app.transaction
     30 import com.example.android.bubbles.ui.chat.ChatFragment
     31 import com.example.android.bubbles.ui.main.MainFragment
     32 import com.example.android.bubbles.ui.photo.PhotoFragment
     33 
     34 /**
     35  * Entry point of the app when it is launched as a full app.
     36  */
     37 class MainActivity : AppCompatActivity(), NavigationController {
     38 
     39     companion object {
     40         private const val FRAGMENT_CHAT = "chat"
     41     }
     42 
     43     private lateinit var appBar: ViewGroup
     44     private lateinit var name: TextView
     45     private lateinit var icon: ImageView
     46 
     47     private lateinit var transition: Transition
     48 
     49     override fun onCreate(savedInstanceState: Bundle?) {
     50         super.onCreate(savedInstanceState)
     51         setContentView(R.layout.main_activity)
     52         setSupportActionBar(findViewById(R.id.toolbar))
     53         transition = TransitionInflater.from(this).inflateTransition(R.transition.app_bar)
     54         appBar = findViewById(R.id.app_bar)
     55         name = findViewById(R.id.name)
     56         icon = findViewById(R.id.icon)
     57         if (savedInstanceState == null) {
     58             supportFragmentManager.transaction(now = true) {
     59                 replace(R.id.container, MainFragment())
     60             }
     61             intent?.let(::handleIntent)
     62         }
     63     }
     64 
     65     override fun onNewIntent(intent: Intent?) {
     66         super.onNewIntent(intent)
     67         if (intent != null) {
     68             handleIntent(intent)
     69         }
     70     }
     71 
     72     private fun handleIntent(intent: Intent) {
     73         if (intent.action == Intent.ACTION_VIEW) {
     74             val id = intent.data.lastPathSegment.toLongOrNull()
     75             if (id != null) {
     76                 openChat(id)
     77             }
     78         }
     79     }
     80 
     81     override fun updateAppBar(showContact: Boolean, hidden: Boolean, body: (name: TextView, icon: ImageView) -> Unit) {
     82         if (hidden) {
     83             appBar.visibility = View.GONE
     84         } else {
     85             appBar.visibility = View.VISIBLE
     86             TransitionManager.beginDelayedTransition(appBar, transition)
     87             if (showContact) {
     88                 supportActionBar?.setDisplayShowTitleEnabled(false)
     89                 name.visibility = View.VISIBLE
     90                 icon.visibility = View.VISIBLE
     91             } else {
     92                 supportActionBar?.setDisplayShowTitleEnabled(true)
     93                 name.visibility = View.GONE
     94                 icon.visibility = View.GONE
     95             }
     96         }
     97         body(name, icon)
     98     }
     99 
    100     override fun openChat(id: Long) {
    101         supportFragmentManager.popBackStack(FRAGMENT_CHAT, FragmentManager.POP_BACK_STACK_INCLUSIVE)
    102         supportFragmentManager.transaction {
    103             addToBackStack(FRAGMENT_CHAT)
    104             replace(R.id.container, ChatFragment.newInstance(id, true))
    105         }
    106     }
    107 
    108     override fun openPhoto(photo: Int) {
    109         supportFragmentManager.transaction {
    110             addToBackStack(null)
    111             replace(R.id.container, PhotoFragment.newInstance(photo))
    112         }
    113     }
    114 }
    115 
    116