Skip to content

Commit

Permalink
Merge pull request #19 from YuanLiou/experiment/material3
Browse files Browse the repository at this point in the history
feat: implemented Material U to Android 12 devices
  • Loading branch information
YuanLiou authored Dec 5, 2021
2 parents 844a406 + a3eb2f6 commit a2204c0
Show file tree
Hide file tree
Showing 22 changed files with 494 additions and 127 deletions.
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ dependencies {
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-play-services"

implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.core:core-ktx:1.6.0'
implementation "androidx.core:core-splashscreen:1.0.0-alpha01"
implementation 'com.google.android.material:material:1.6.0-alpha01'
implementation 'androidx.core:core-ktx:1.7.0'
implementation "androidx.core:core-splashscreen:1.0.0-alpha02"
implementation "androidx.fragment:fragment-ktx:1.3.6"

// HTTP Client
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package tw.com.louis383.coffeefinder

import android.app.Application
import androidx.appcompat.app.AppCompatDelegate
import com.google.android.material.color.DynamicColors
import dagger.hilt.android.HiltAndroidApp
import tw.com.louis383.coffeefinder.utils.QuickCheckUtils

/**
* Created by louis383 on 2017/1/24.
Expand All @@ -13,5 +15,8 @@ class CoffeeTripApplication : Application() {
override fun onCreate() {
super.onCreate()
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)
if (QuickCheckUtils.canApplyDynamicColor()) {
DynamicColors.applyToActivitiesIfAvailable(this)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package tw.com.louis383.coffeefinder.details

import android.content.res.ColorStateList
import android.graphics.BlendMode
import android.graphics.ColorMatrix
import android.graphics.ColorMatrixColorFilter
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
Expand All @@ -9,6 +13,7 @@ import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.RatingBar
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.core.widget.NestedScrollView
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
Expand All @@ -20,6 +25,7 @@ import tw.com.louis383.coffeefinder.core.domain.model.CoffeeShop
import tw.com.louis383.coffeefinder.uimodel.CoffeeShopUiModel
import tw.com.louis383.coffeefinder.uimodel.getUiModel
import tw.com.louis383.coffeefinder.utils.FragmentArgumentDelegate
import tw.com.louis383.coffeefinder.utils.QuickCheckUtils
import javax.inject.Inject

@AndroidEntryPoint
Expand Down Expand Up @@ -50,6 +56,7 @@ class DetailsFragment: Fragment() {
private lateinit var standingDestText: TextView

private lateinit var distanceText: TextView
private lateinit var shareBackground: ImageView

@Inject
lateinit var currentLocationCarrier: CurrentLocationCarrier
Expand All @@ -61,6 +68,10 @@ class DetailsFragment: Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
retrieveViews(view)
if (QuickCheckUtils.canApplyDynamicColor()) {
applyColorThemes()
}

nestedScrollView = view.findViewById(R.id.detail_view_scrollview)
setDetailInfo(coffeeShop.getUiModel())

Expand All @@ -80,23 +91,39 @@ class DetailsFragment: Fragment() {
}

private fun retrieveViews(view: View) {
titleText = view.findViewById<TextView>(R.id.detail_view_title)
cheapRating = view.findViewById<RatingBar>(R.id.detail_view_expense)
titleText = view.findViewById(R.id.detail_view_title)
cheapRating = view.findViewById(R.id.detail_view_expense)

wiFiPointBar = view.findViewById<ProgressBar>(R.id.detail_view_wifi_quality)
wiFiScoreText = view.findViewById<TextView>(R.id.detail_view_wifi_score)
seatPointBar = view.findViewById<ProgressBar>(R.id.detail_view_seat_quality)
seatScoreText = view.findViewById<TextView>(R.id.detail_view_seat_score)
wiFiPointBar = view.findViewById(R.id.detail_view_wifi_quality)
wiFiScoreText = view.findViewById(R.id.detail_view_wifi_score)
seatPointBar = view.findViewById(R.id.detail_view_seat_quality)
seatScoreText = view.findViewById(R.id.detail_view_seat_score)

webSiteText = view.findViewById<TextView>(R.id.detail_view_website)
openTimeText = view.findViewById<TextView>(R.id.detail_view_opentime)
mrtText = view.findViewById<TextView>(R.id.detail_view_mrt)
webSiteText = view.findViewById(R.id.detail_view_website)
openTimeText = view.findViewById(R.id.detail_view_opentime)
mrtText = view.findViewById(R.id.detail_view_mrt)

limitedTimeText = view.findViewById<TextView>(R.id.detail_view_limited_time)
socketText = view.findViewById<TextView>(R.id.detail_view_socket)
standingDestText = view.findViewById<TextView>(R.id.detail_view_standing_desk)
limitedTimeText = view.findViewById(R.id.detail_view_limited_time)
socketText = view.findViewById(R.id.detail_view_socket)
standingDestText = view.findViewById(R.id.detail_view_standing_desk)

distanceText = view.findViewById<TextView>(R.id.detail_view_distance)
distanceText = view.findViewById(R.id.detail_view_distance)
shareBackground = view.findViewById(R.id.detail_view_share_background_image)
}

private fun applyColorThemes() {
cheapRating.progressTintList = ColorStateList.valueOf(ContextCompat.getColor(requireContext(), R.color.dynamic_light_primary))
wiFiPointBar.progressTintList = ColorStateList.valueOf(ContextCompat.getColor(requireContext(), R.color.dynamic_light_secondary))
wiFiPointBar.progressTintBlendMode = BlendMode.HUE
seatPointBar.progressTintList = ColorStateList.valueOf(ContextCompat.getColor(requireContext(), R.color.dynamic_light_secondary))
seatPointBar.progressTintBlendMode = BlendMode.HUE

// set share background to monochrome
shareBackground.colorFilter = ColorMatrixColorFilter(
ColorMatrix().also {
it.setSaturation(0f)
}
)
}

fun setDetailInfo(uiModel: CoffeeShopUiModel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import android.animation.ObjectAnimator
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.content.res.ColorStateList
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.view.View
import android.view.ViewGroup
import android.view.WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS
import android.view.animation.AnticipateInterpolator
import android.widget.FrameLayout
Expand All @@ -23,14 +25,17 @@ import androidx.core.content.ContextCompat
import androidx.core.splashscreen.SplashScreen
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.core.splashscreen.SplashScreenViewProvider
import androidx.core.view.*
import androidx.lifecycle.LifecycleOwner
import androidx.viewpager.widget.ViewPager
import com.google.android.gms.common.api.ResolvableApiException
import com.google.android.gms.maps.model.LatLng
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.snackbar.Snackbar
import com.rd.PageIndicatorView
import com.trafi.anchorbottomsheetbehavior.AnchorBottomSheetBehavior
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
import tw.com.louis383.coffeefinder.R
import tw.com.louis383.coffeefinder.adapter.ViewPagerAdapter
import tw.com.louis383.coffeefinder.core.domain.model.CoffeeShop
Expand All @@ -40,16 +45,21 @@ import tw.com.louis383.coffeefinder.list.ListFragment
import tw.com.louis383.coffeefinder.maps.MapsClickHandler
import tw.com.louis383.coffeefinder.maps.MapsFragment
import tw.com.louis383.coffeefinder.uimodel.getUiModel
import tw.com.louis383.coffeefinder.utils.QuickCheckUtils
import tw.com.louis383.coffeefinder.utils.bindView
import javax.inject.Inject

/**
* Created by louis383 on 2017/2/17.
*/

@AndroidEntryPoint
class MainActivity : AppCompatActivity(), MainView, MapsClickHandler, ListFragment.Callback,
DetailsItemClickListener, View.OnClickListener {
class MainActivity :
AppCompatActivity(),
MainView,
MapsClickHandler,
ListFragment.Callback,
DetailsItemClickListener,
View.OnClickListener {
private val locationSettingResolution = 1001

@Inject
Expand All @@ -61,6 +71,7 @@ class MainActivity : AppCompatActivity(), MainView, MapsClickHandler, ListFragme
private val rootView: CoordinatorLayout by bindView(R.id.main_rootview)
private val mainContainer: FrameLayout by bindView(R.id.main_container)
private val myLocationButton: ImageButton by bindView(R.id.main_my_location_button)
private val indicatorView: PageIndicatorView by bindView(R.id.main_view_pager_indicator)

// Bottom Sheet
private lateinit var bottomSheetViewPager: ViewPager
Expand All @@ -69,9 +80,9 @@ class MainActivity : AppCompatActivity(), MainView, MapsClickHandler, ListFragme

// View States
sealed class ViewState {
data class EnterDetailInfoFromMap(val coffeeshop: CoffeeShop): ViewState()
data class EnterDetailInfoFromList(val coffeeshop: CoffeeShop): ViewState()
object Browsing: ViewState()
data class EnterDetailInfoFromMap(val coffeeshop: CoffeeShop) : ViewState()
data class EnterDetailInfoFromList(val coffeeshop: CoffeeShop) : ViewState()
object Browsing : ViewState()
}
private var detailViewState: ViewState = ViewState.Browsing

Expand All @@ -93,6 +104,11 @@ class MainActivity : AppCompatActivity(), MainView, MapsClickHandler, ListFragme
bottomSheetBehavior = getViewPagerBottomSheetBehavior()
bottomSheetBehavior.state = AnchorBottomSheetBehavior.STATE_COLLAPSED

if (QuickCheckUtils.canApplyDynamicColor()) {
myLocationButton.imageTintList = ColorStateList.valueOf(ContextCompat.getColor(this, R.color.dynamic_light_primary))
indicatorView.selectedColor = ContextCompat.getColor(this, R.color.dynamic_light_primary)
}

presenter.attachView(this)
myLocationButton.setOnClickListener(this)
}
Expand Down Expand Up @@ -153,7 +169,7 @@ class MainActivity : AppCompatActivity(), MainView, MapsClickHandler, ListFragme

override fun checkLocationPermission(): Boolean {
return ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED ||
hasApproximateLocationPermission()
hasApproximateLocationPermission()
}

override fun hasApproximateLocationPermission(): Boolean {
Expand All @@ -177,13 +193,12 @@ class MainActivity : AppCompatActivity(), MainView, MapsClickHandler, ListFragme
else -> {
// No location access granted.
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_COARSE_LOCATION) &&
ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)
) {
MaterialAlertDialogBuilder(this)
.setMessage(getResourceString(R.string.request_location))
.setPositiveButton(getResourceString(R.string.dialog_auth))
{ _, _ -> presenter.requestLocationPermission() }
.setNegativeButton(getResourceString(R.string.dialog_cancel))
{ _, _ -> showPermissionNeedSnackBar() }
.setPositiveButton(getResourceString(R.string.dialog_auth)) { _, _ -> presenter.requestLocationPermission() }
.setNegativeButton(getResourceString(R.string.dialog_cancel)) { _, _ -> showPermissionNeedSnackBar() }
.create()
.show()
} else {
Expand All @@ -194,10 +209,8 @@ class MainActivity : AppCompatActivity(), MainView, MapsClickHandler, ListFragme
MaterialAlertDialogBuilder(this)
.setTitle(getResourceString(R.string.dialog_auth))
.setMessage(resources.getString(R.string.auth_yourself, appName, permissionName))
.setPositiveButton(getResourceString(R.string.auto_go))
{ _, _ -> openApplicationSetting() }
.setNegativeButton(getResourceString(R.string.dialog_cancel))
{ _, _ -> }
.setPositiveButton(getResourceString(R.string.auto_go)) { _, _ -> openApplicationSetting() }
.setNegativeButton(getResourceString(R.string.dialog_cancel)) { _, _ -> }
.create()
.show()
}
Expand Down Expand Up @@ -249,9 +262,32 @@ class MainActivity : AppCompatActivity(), MainView, MapsClickHandler, ListFragme
window.insetsController?.setSystemBarsAppearance(APPEARANCE_LIGHT_STATUS_BARS, APPEARANCE_LIGHT_STATUS_BARS)
} else {
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
}
fixBottomUiHeight()
}

private fun fixBottomUiHeight() {
ViewCompat.setOnApplyWindowInsetsListener(
window.decorView,
object : OnApplyWindowInsetsListener {
override fun onApplyWindowInsets(
v: View,
insets: WindowInsetsCompat
): WindowInsetsCompat {
val navigationBarHeight =
insets.getInsets(WindowInsetsCompat.Type.systemBars()).bottom
bottomSheetViewPager.setPadding(0, 0, 0, navigationBarHeight)

val indicatorLayoutParams = indicatorView.layoutParams as ViewGroup.MarginLayoutParams
indicatorLayoutParams.updateMargins(bottom = navigationBarHeight + indicatorView.marginBottom)

ViewCompat.setOnApplyWindowInsetsListener(window.decorView, null)
return insets
}
}
)
}

private val internetRequestCallback = registerForActivityResult(
Expand Down Expand Up @@ -460,7 +496,7 @@ class MainActivity : AppCompatActivity(), MainView, MapsClickHandler, ListFragme
presenter.let {
val myLocation = it.currentLocation
val mapFragment = supportFragmentManager.findFragmentByTag(MapsFragment.TAG)
as MapsFragment
as MapsFragment
mapFragment.moveToMyLocation(myLocation)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class MainPresenter @Inject constructor(
override fun onSlide(bottomSheet: View, slideOffset: Float) {
if (slideOffset >= 0f) {
// negative values to move view up
val additionalDistances = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 0.5f, bottomSheet.resources.displayMetrics)
val additionalDistances = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 0.7f, bottomSheet.resources.displayMetrics)
view?.moveMapView((anchorHeight * slideOffset) * (additionalDistances * -1))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,11 @@ class MapsFragment : BaseFragment(), OnMapReadyCallback, MapsView, GoogleMap.OnM
}

override fun getResourceDrawable(resId: Int): Drawable? {
return context?.run {
ContextCompat.getDrawable(this, resId)
}
return ContextCompat.getDrawable(requireContext(), resId)
}

override fun getColorInt(colorResId: Int): Int {
return ContextCompat.getColor(requireContext(), colorResId)
}

fun moveToMyLocation(currentLocation: Location?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@ package tw.com.louis383.coffeefinder.maps

import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.LightingColorFilter
import android.graphics.Paint
import androidx.annotation.ColorRes
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.model.BitmapDescriptor
import com.google.android.gms.maps.model.BitmapDescriptorFactory
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.Marker
import dagger.hilt.android.scopes.FragmentScoped
import javax.inject.Inject
import tw.com.louis383.coffeefinder.BasePresenter
import tw.com.louis383.coffeefinder.R
import tw.com.louis383.coffeefinder.core.domain.model.CoffeeShop
import tw.com.louis383.coffeefinder.utils.QuickCheckUtils
import tw.com.louis383.coffeefinder.utils.toLatLng
import javax.inject.Inject

/**
* Created by louis383 on 2017/1/13.
Expand Down Expand Up @@ -49,18 +53,16 @@ class MapsPresenter @Inject constructor() : BasePresenter<MapsView>(), GoogleMap
}

private fun getDrawableBitmapDescriptor(resId: Int): BitmapDescriptor? {
val drawable = view?.getResourceDrawable(resId)
drawable?.run {
val width = intrinsicWidth
val height = intrinsicHeight
setBounds(0, 0, width, height)

val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
draw(canvas)

return BitmapDescriptorFactory.fromBitmap(bitmap)
} ?: return null
val drawable = view?.getResourceDrawable(resId) ?: return null

val width = drawable.intrinsicWidth
val height = drawable.intrinsicHeight
drawable.setBounds(0, 0, width, height)

val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
drawable.draw(canvas)
return BitmapDescriptorFactory.fromBitmap(bitmap)
}

private fun highlightMarker(marker: Marker, highlight: Boolean) {
Expand Down Expand Up @@ -94,7 +96,7 @@ class MapsPresenter @Inject constructor() : BasePresenter<MapsView>(), GoogleMap
coffeeShop?.run {
view?.openDetailView(this)
}
return true // disable snippet
return true // disable snippet
}
//endregion

Expand Down
Loading

0 comments on commit a2204c0

Please sign in to comment.