Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Migration to navigation component #132

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ plugins {
id("jacoco-android")
id("com.mikepenz.aboutlibraries.plugin")
id("com.google.firebase.crashlytics")
id("androidx.navigation.safeargs")
}

jacoco {
Expand Down Expand Up @@ -144,6 +145,10 @@ dependencies {
implementation appDependencies.koinAndroid
implementation appDependencies.koinAndroidViewModel

//navigation
implementation appDependencies.navigationFragment
implementation appDependencies.navigationUi

//Test Libs
testImplementation testDependencies.junit
testImplementation testDependencies.robolectric
Expand Down
18 changes: 1 addition & 17 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,9 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:theme="@style/AppTheme">

<activity android:name=".base.BaseActivity" />
<activity
android:name=".activities.FavoriteDetailsActivity"
android:parentActivityName=".activities.DashboardActivity" />
<activity
android:name=".activities.AboutActivity"
android:label="@string/title_about"
android:parentActivityName=".activities.SettingsActivity" />
<activity
android:name=".activities.SettingsActivity"
android:label="@string/title_settings"
android:parentActivityName=".activities.DashboardActivity" />
<activity
android:name=".activities.CharacterDetailsActivity"
android:parentActivityName=".activities.DashboardActivity" />

<!-- TODO Use singletop though seems tricky given the UI is using motion layout -->
<activity
android:name=".activities.DashboardActivity"
android:name=".ui.DashboardActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize">
<intent-filter>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@
package com.k0d4black.theforce.base

import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.ViewGroup
import android.view.*
import androidx.annotation.LayoutRes
import androidx.lifecycle.Observer
import com.k0d4black.theforce.R
import com.k0d4black.theforce.activities.IFavoritesBinder
import com.k0d4black.theforce.commons.NavigationUtils
import com.k0d4black.theforce.commons.showSnackbar
import com.k0d4black.theforce.models.FavoritePresentation
import com.k0d4black.theforce.ui.IFavoritesBinder
import com.k0d4black.theforce.viewmodel.FavoriteViewModel
import org.koin.androidx.viewmodel.ext.android.viewModel

internal abstract class BaseFavoritesActivity : BaseActivity(), IFavoritesBinder {
internal abstract class BaseFavoritesFragment(@LayoutRes contentLayoutId: Int) :
BaseFragment(contentLayoutId), IFavoritesBinder {

// region Members

Expand All @@ -44,34 +44,35 @@ internal abstract class BaseFavoritesActivity : BaseActivity(), IFavoritesBinder

// region Android API

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

val favorite =
intent.getParcelableExtra<FavoritePresentation>(NavigationUtils.FAVORITE_PARCEL_KEY)
requireArguments().get(NavigationUtils.FAVORITE_PARCEL_KEY) as FavoritePresentation?

favorite?.let { favoritePresentation ->
bindFavorite(favoritePresentation)
characterName = favoritePresentation.characterPresentation.name
this.favoritePresentation = favoritePresentation
checkIfFavorite()
invalidateOptionsMenu()
requireActivity().invalidateOptionsMenu()
}

observeFavoriteViewState()
}

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.favorites_menu, menu)
return super.onCreateOptionsMenu(menu)
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.favorites_menu, menu)
super.onCreateOptionsMenu(menu, inflater)
}

override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
val menuItem = menu?.getItem(0)
override fun onPrepareOptionsMenu(menu: Menu) {
val menuItem = menu.getItem(0)
if (isFavorite)
menuItem?.setIcon(R.drawable.ic_favs_24dp)
else
menuItem?.setIcon(R.drawable.ic_no_favs_24dp)
return super.onPrepareOptionsMenu(menu)
super.onPrepareOptionsMenu(menu)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
Expand All @@ -86,7 +87,7 @@ internal abstract class BaseFavoritesActivity : BaseActivity(), IFavoritesBinder
isFavorite = !isFavorite
}
}
invalidateOptionsMenu()
requireActivity().invalidateOptionsMenu()
true
}
else -> super.onOptionsItemSelected(item)
Expand Down Expand Up @@ -122,9 +123,9 @@ internal abstract class BaseFavoritesActivity : BaseActivity(), IFavoritesBinder
}

private fun observeFavoriteViewState() {
favoritesViewModel.favoriteViewState.observe(this, Observer {
favoritesViewModel.favoriteViewState.observe(viewLifecycleOwner, Observer {
isFavorite = it.isFavorite
invalidateOptionsMenu()
requireActivity().invalidateOptionsMenu()
it.error?.let { e ->
showSnackbar(rootViewGroup, getString(e.message))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,28 @@ package com.k0d4black.theforce.base
import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.annotation.LayoutRes
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import com.k0d4black.theforce.commons.NetworkUtils
import com.k0d4black.theforce.commons.versionFrom

internal open class BaseActivity : AppCompatActivity() {
internal open class BaseFragment(@LayoutRes layoutId : Int) : Fragment(layoutId) {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
whiteStatusBar()
}

private fun whiteStatusBar() {
if (versionFrom(Build.VERSION_CODES.M)) {
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
window.statusBarColor = getColor(android.R.color.white)
requireActivity().window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
requireActivity().window.statusBarColor = requireContext().getColor(android.R.color.white)
}
}

protected fun onNetworkChange(block: (Boolean) -> Unit) {
NetworkUtils.getNetworkStatus(this)
NetworkUtils.getNetworkStatus(requireContext())
.observe(this, Observer { isConnected ->
block(isConnected)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,10 @@
*/
package com.k0d4black.theforce.commons

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.widget.Toast
import androidx.annotation.ColorRes
import androidx.core.content.ContextCompat

internal fun Context.loadColor(@ColorRes colorRes: Int): Int {
return ContextCompat.getColor(this, colorRes)
}

internal fun Context.showToast(message: String) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}

internal inline fun <reified T : Activity> Context.startActivity(block: Intent.() -> Unit = {}) {
val intent = Intent(this, T::class.java)
block(intent)
startActivity(intent)
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@
*/
package com.k0d4black.theforce.commons

import android.app.Activity
import android.view.View
import androidx.fragment.app.Fragment
import com.google.android.material.snackbar.Snackbar
import com.k0d4black.theforce.R

internal fun Activity.showSnackbar(view: View, message: String, isError: Boolean = false) {
internal fun Fragment.showSnackbar(view: View, message: String, isError: Boolean = false) {
val sb = Snackbar.make(view, message, Snackbar.LENGTH_LONG)
if (isError)
sb.setBackgroundTint(loadColor(R.color.colorError))
.setTextColor(loadColor(R.color.colorOnError))
sb.setBackgroundTint(requireContext().loadColor(R.color.colorError))
.setTextColor(requireContext().loadColor(R.color.colorOnError))
.show()
else
sb.setBackgroundTint(loadColor(R.color.colorSecondary))
.setTextColor(loadColor(R.color.colorOnSecondary))
sb.setBackgroundTint(requireContext().loadColor(R.color.colorSecondary))
.setTextColor(requireContext().loadColor(R.color.colorOnSecondary))
.show()

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
package com.k0d4black.theforce.commons

internal object NavigationUtils {
const val CHARACTER_PARCEL_KEY = "character_url"
const val CHARACTER_PARCEL_KEY = "character"
const val FAVORITE_PARCEL_KEY = "favorite"
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import android.os.Parcelable
import kotlinx.android.parcel.Parcelize

@Parcelize
internal data class CharacterPresentation(
data class CharacterPresentation(
val name: String,
val birthYear: String,
val heightInCm: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import android.os.Parcelable
import kotlinx.android.parcel.Parcelize

@Parcelize
internal data class FavoritePresentation(
data class FavoritePresentation(
val characterPresentation: CharacterPresentation,
val planetPresentation: PlanetPresentation,
val speciePresentation: List<SpeciePresentation>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ import android.os.Parcelable
import kotlinx.android.parcel.Parcelize

@Parcelize
internal data class FilmPresentation(val title: String, val openingCrawl: String) : Parcelable
data class FilmPresentation(val title: String, val openingCrawl: String) : Parcelable
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ import android.os.Parcelable
import kotlinx.android.parcel.Parcelize

@Parcelize
internal data class PlanetPresentation(val name: String, val population: Long) : Parcelable
data class PlanetPresentation(val name: String, val population: Long) : Parcelable
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ import android.os.Parcelable
import kotlinx.android.parcel.Parcelize

@Parcelize
internal data class SpeciePresentation(val name: String, val language: String) : Parcelable
data class SpeciePresentation(val name: String, val language: String) : Parcelable
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,29 @@
* the License.
*
**/
package com.k0d4black.theforce.activities
package com.k0d4black.theforce.ui

import android.os.Bundle
import android.view.View
import com.k0d4black.theforce.R
import com.k0d4black.theforce.base.BaseActivity
import com.k0d4black.theforce.databinding.ActivityAboutBinding
import com.k0d4black.theforce.base.BaseFragment
import com.mikepenz.aboutlibraries.LibsBuilder
import kotlinx.android.synthetic.main.fragment_about.*

internal class AboutActivity : BaseActivity() {
internal class AboutFragment : BaseFragment(R.layout.fragment_about) {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = ActivityAboutBinding.inflate(layoutInflater)
setContentView(binding.root)
setSupportActionBar(binding.aboutToolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

(requireActivity() as DashboardActivity).setSupportActionBar(about_toolbar)
(requireActivity() as DashboardActivity).supportActionBar?.setDisplayHomeAsUpEnabled(true)

val fragment = LibsBuilder()
.withAboutIconShown(true)
.supportFragment()

supportFragmentManager.beginTransaction()
requireActivity().supportFragmentManager.beginTransaction()
.add(R.id.fragment_container, fragment).commit()
}

}
Loading