Skip to content

Commit

Permalink
Showing number of meteorites
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal Holécy committed Dec 7, 2018
1 parent 21cc9a5 commit e7c28e0
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 7 deletions.
6 changes: 6 additions & 0 deletions app/src/main/java/sk/mholecy/meteorites/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sk.mholecy.meteorites

import android.os.Bundle
import android.view.Menu
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.navigation.NavController
Expand All @@ -18,4 +19,9 @@ class MainActivity : AppCompatActivity() {
supportActionBar?.setDisplayHomeAsUpEnabled(true)
navController = findNavController(R.id.meteorites_nav_fragment)
}

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu_meteorites_fragment, menu)
return true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ internal val viewModelModule = module {
viewModel {
MeteoritesListViewModel(
get<MeteoritesDatabaseSyncService>(),
androidApplication()
androidApplication(),
get<MeteoritesDao>()
)
}
viewModel {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,20 @@ package sk.mholecy.meteorites.common.extensions

import android.content.Context
import android.net.ConnectivityManager
import androidx.annotation.ColorRes
import androidx.core.content.ContextCompat
import com.google.android.material.snackbar.Snackbar

fun Context.isOnline(): Boolean {
val connectivityManager = this.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val activeNetwork = connectivityManager.activeNetworkInfo
return activeNetwork?.isConnected == true
}

fun Snackbar.setBackgroundColor(@ColorRes colorId: Int) {
this.view.setBackgroundColor(ContextCompat.getColor(this.context, colorId))
}

fun Snackbar.setActionTextColorId(@ColorRes colorId: Int) {
this.setActionTextColor(ContextCompat.getColor(this.context, colorId))
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ interface MeteoritesDao {

@Query("SELECT * from meteorites WHERE id = :meteoriteId")
fun getMeteorite(meteoriteId: Long): LiveData<DbMeteoriteModel>

@Query("SELECT count(*) from meteorites")
fun getMeteoritesCount(): LiveData<Long>
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@ package sk.mholecy.meteorites.meteorites.ui.list

import android.os.Bundle
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import kotlinx.android.synthetic.main.fragment_meteorites_list.view.*
import com.google.android.material.snackbar.Snackbar
import org.koin.android.ext.android.inject
import org.koin.androidx.viewmodel.ext.android.viewModel
import sk.mholecy.meteorites.R
import sk.mholecy.meteorites.common.extensions.setActionTextColorId
import sk.mholecy.meteorites.common.extensions.setBackgroundColor
import sk.mholecy.meteorites.databinding.FragmentMeteoritesListBinding
import sk.mholecy.meteorites.meteorites.ui.list.adapter.MeteoritesAdapter

class MeteoritesListFragment : Fragment() {
private val meteoritesViewModel: MeteoritesListViewModel by viewModel()
private val meteoritesListAdapter: MeteoritesAdapter by inject()
private lateinit var rootView: View
private var meteoriteCountSnackBar: Snackbar? = null

override fun onCreateView(
inflater: LayoutInflater,
Expand All @@ -24,16 +30,47 @@ class MeteoritesListFragment : Fragment() {
): View? {
val binding = FragmentMeteoritesListBinding.inflate(inflater, container, false)
val activity = activity as AppCompatActivity
activity.setSupportActionBar(binding.meteoritesList.toolbar)
activity.setSupportActionBar(binding.toolbar)
binding.meteoritesList.adapter = meteoritesListAdapter
setHasOptionsMenu(true)
subscribeUi()
return binding.root
rootView = binding.root
return rootView
}

private fun subscribeUi() {
meteoritesViewModel.meteorites.observe(viewLifecycleOwner, Observer { meteorites ->
meteoritesListAdapter.submitList(meteorites)
})
meteoritesViewModel.meteoritesCount.observe(viewLifecycleOwner, Observer {
meteoriteCountSnackBar?.setText(
String.format(getString(R.string.meteorites_count_message), it)
)
})
meteoritesViewModel.fetchMeteorites()
}

override fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) {
R.id.action_show_meteorites_count -> {
createMeteoriteCountSnackBar()
true
}
else -> {
super.onOptionsItemSelected(item)
}
}

private fun createMeteoriteCountSnackBar() {
meteoriteCountSnackBar = Snackbar.make(
rootView,
String.format(getString(R.string.meteorites_count_message), meteoritesViewModel.meteoritesCount.value),
Snackbar.LENGTH_INDEFINITE
)
meteoriteCountSnackBar!!.setAction(getString(R.string.snackbar_acknowledge)) {
meteoriteCountSnackBar!!.dismiss()
}
meteoriteCountSnackBar!!.setBackgroundColor(R.color.primaryLightColor)
meteoriteCountSnackBar!!.setActionTextColorId(R.color.secondaryLightColor)
meteoriteCountSnackBar!!.show()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import androidx.lifecycle.ViewModel
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import sk.mholecy.meteorites.common.extensions.isOnline
import sk.mholecy.meteorites.meteorites.database.dao.MeteoritesDao
import sk.mholecy.meteorites.meteorites.service.MeteoritesDatabaseSyncService

class MeteoritesListViewModel(
private val meteoritesService: MeteoritesDatabaseSyncService,
private val context: Context
private val context: Context,
meteoritesDao: MeteoritesDao
) : ViewModel() {
val meteorites = meteoritesService.meteorites
val meteoritesCount = meteoritesDao.getMeteoritesCount()

fun fetchMeteorites() {
if (context.isOnline()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import androidx.paging.PagedListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.snackbar.Snackbar
import sk.mholecy.meteorites.R
import sk.mholecy.meteorites.common.extensions.setBackgroundColor
import sk.mholecy.meteorites.databinding.ItemMeteoriteBinding
import sk.mholecy.meteorites.meteorites.database.model.DbMeteoriteModel
import sk.mholecy.meteorites.meteorites.ui.list.MeteoritesListFragmentDirections
Expand Down Expand Up @@ -44,8 +45,13 @@ class MeteoritesAdapter(
MeteoritesListFragmentDirections.ActionMeteoritesListFragmentToMeteoriteMapFragment(meteorite.id)
it.findNavController().navigate(direction)
} else {
Snackbar.make(it, it.context.getString(R.string.meteorite_location_unknown), Snackbar.LENGTH_SHORT)
.show()
val snackBar = Snackbar.make(
it,
it.context.getString(R.string.meteorite_location_unknown),
Snackbar.LENGTH_SHORT
)
snackBar.setBackgroundColor(R.color.primaryLightColor)
snackBar.show()
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/menu/menu_meteorites_fragment.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_show_meteorites_count"
android:icon="@drawable/ic_meteorite_color"
android:title="@string/action_show_meteorites_count"
app:showAsAction="always"/>
</menu>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<resources>
<string name="app_name">Meteorites</string>
<string name="meteorite_location_unknown">Location of meteorite is unknown</string>
<string name="snackbar_acknowledge">Got it</string>
<string name="meteorites_count_message">%d meteorites have fallen since 2011</string>
<string name="action_show_meteorites_count">Show meteorites count</string>
</resources>

0 comments on commit e7c28e0

Please sign in to comment.