Skip to content

Commit

Permalink
added realisation of search list, alpha version 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Ferum-bot committed Jan 12, 2021
1 parent 79c9e87 commit 9c72dd6
Show file tree
Hide file tree
Showing 23 changed files with 445 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fun MarsPhotoVO.toMarsPhoto(): MarsPhoto {
return MarsPhoto(
id,
solDate,
earthDate,
earthDate.toDefaultDateFormat(),
imageSrc,
cameraVO.name,
rover.name,
Expand All @@ -29,7 +29,7 @@ fun MarsPhotoVO.toMarsPhotoDB(): MarsPhotoDB {
return MarsPhotoDB(
id,
solDate,
earthDate,
earthDate.toDefaultDateFormat(),
imageSrc,
cameraVO.name,
rover.name,
Expand Down Expand Up @@ -74,7 +74,7 @@ fun PictureOfDayVO.toPictureOfDayPhotoDB(): PictureOfDayPhotoDB {
author,
title,
description,
date,
date.toDefaultDateFormat(),
false,
imageSrc
)
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/com/example/nasa_mars_api_service/core/Utills.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,14 @@ fun getBaseRequestOptions(): RequestOptions {
.placeholder(R.drawable.loading_animation)
.error(R.drawable.connection_error_image)
.diskCacheStrategy(DiskCacheStrategy.ALL)
}

fun String.toSearchDateFormat(): String {
val listOfDates = this.split(".")
return listOfDates[2] + "-" + listOfDates[1] + "-" + listOfDates[0]
}

fun String.toDefaultDateFormat(): String {
val listOfDates = this.split("-")
return listOfDates[2] + "." + listOfDates[1] + "." + listOfDates[0]
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ package com.example.nasa_mars_api_service.core.enums
* Time: 23:02
* Project: NASA-Mars-API-Service
*/
enum class MarsDateTypes(val date: String = "") {
MARS_SOL, EARTH_DATE
enum class MarsDateTypes(var date: String = "") {
MARS_SOL(), EARTH_DATE()
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class MainRepository private constructor(
}
}
updateNumberOfPhotosInPreferences(result)
updateNumberOfSearchMarsPhotosPagesInPreferences(page)
localSourceMarsPhotos.insertPhotos(result.map { it.toMarsPhotoDB() })
return result.map { it.toMarsPhoto() }
}
Expand Down Expand Up @@ -149,6 +150,10 @@ class MainRepository private constructor(
localSourceFavoritePhotos.deletePhoto(pictureOfDayPhoto.toFavouritePhotoDB())
}

override fun clearNumberOfAvailableSearchMarsPhotos() {
preferences.updateNumberOfAvailableSearchMarsPhotoPages(0)
}

private fun deleteFavouritePhotoFromPreferences() {
val number = preferences.getNumberOfFavouritePhotos()
preferences.updateNumberOfFavouritePhotos(number - 1)
Expand All @@ -170,6 +175,13 @@ class MainRepository private constructor(
}
}

private fun updateNumberOfSearchMarsPhotosPagesInPreferences(page: Int) {
val currentNumberOfPages = preferences.getNumberOfAvailableSearchMarsPhotoPages()
if (currentNumberOfPages < page) {
preferences.updateNumberOfAvailableSearchMarsPhotoPages(page)
}
}

companion object {

@Volatile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ interface BaseRepository {
fun getNumberOfCashedFavouritesPhotos(): Int
fun getNumberOfCashedPictureOfDayPhotos(): Int

fun clearNumberOfAvailableSearchMarsPhotos()

val numberOfAvailableMarsPhotosPages: Int
val numberOfAvailableSearchMarsPhotosPages: Int
}
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ class SearchMarsPhotoFragment: Fragment() {
val editText = view as EditText
val date = editText.text.toString()
if (viewModel.isDateCorrect(date)) {
viewModel.date = date
viewModel.setNewDate(date)
chooseDateButtonState = ButtonStates.INFORMATION_CHOSEN
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ class SearchMarsPhotoViewModel: ViewModel() {
}
}

fun setNewDate(date: String) {
this.date = date
this.dateType!!.date = date
}

fun getChosenRoverCameraShortName(): String {
if (camera != null) {
return camera!!.name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,23 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import com.example.nasa_mars_api_service.R
import com.example.nasa_mars_api_service.core.Variables
import com.example.nasa_mars_api_service.core.enums.PhotoTypes
import com.example.nasa_mars_api_service.core.models.MarsPhoto
import com.example.nasa_mars_api_service.database.db.MainDatabase
import com.example.nasa_mars_api_service.databinding.FragmentSearchListBinding
import com.example.nasa_mars_api_service.network.api.MarsPhotosApi
import com.example.nasa_mars_api_service.preferences.implementations.AppPreferences
import com.example.nasa_mars_api_service.preferences.interfaces.BaseApplicationPreferences
import com.example.nasa_mars_api_service.repository.implementations.MainRepository
import com.example.nasa_mars_api_service.repository.interfaces.BaseRepository
import com.example.nasa_mars_api_service.ui.recycler_views.adapters.SearchListAdapter

/**
Expand All @@ -19,28 +30,139 @@ import com.example.nasa_mars_api_service.ui.recycler_views.adapters.SearchListAd
* Project: NASA-Mars-API-Service
*/
class SearchListFragment: Fragment() {

private lateinit var viewModel: SearchListViewModel

private lateinit var binding: FragmentSearchListBinding

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

val factory = SearchListViewModelFactory()
viewModel = ViewModelProvider(this, factory).get(SearchListViewModel::class.java)
private lateinit var searchListAdapter: SearchListAdapter

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_search_list, container, false)

getViewModel()
setBaseVisibilityForViews()
return binding.root
}

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

val adapter = SearchListAdapter()
adapter.items = listOf(
MarsPhoto(), MarsPhoto(), MarsPhoto(), MarsPhoto(), MarsPhoto(), MarsPhoto(), MarsPhoto(), MarsPhoto(), MarsPhoto(), MarsPhoto(), MarsPhoto(), MarsPhoto(),
setUpAdapter()
setAllClickListeners()
setAllObservers()
requireMarsPhotosOrIssue()
}

private fun getViewModel() {
val context = requireContext()
val preferences: BaseApplicationPreferences = AppPreferences.getInstance(context)
val dataBase = MainDatabase.getInstance(context)
val remoteSource = MarsPhotosApi.marsPhotosService
val repository: BaseRepository = MainRepository.getInstance(
dataBase.marsPhotoDao,
dataBase.favoritePhotoDao,
dataBase.pictureOfDayDao,
remoteSource,
preferences
)
binding.searchRecyclerView.adapter = adapter

val factory = SearchListViewModelFactory(repository)
viewModel = ViewModelProvider(this, factory).get(SearchListViewModel::class.java)
}

private fun requireMarsPhotosOrIssue() {
if (!Variables.isNetworkConnectionAvailable) {
showErrorMessage(getString(R.string.no_internet_connection))
showErrorImage()
return
}
val args = SearchListFragmentArgs.fromBundle(requireArguments())
val marsPhotoToSearch = args.MarsPhotoToSearch
viewModel.searchMarsPhotos(marsPhotoToSearch)
}

private fun setUpAdapter() {
searchListAdapter = SearchListAdapter(
// Mars photo Item Click Listener
fun (marsPhoto: MarsPhoto) {
findNavController().navigate(SearchListFragmentDirections.actionSearchListFragmentToMarsPhotoDescriptionFragment(marsPhoto.id))
},

// Image long Click listener for mars photo item
fun (marsPhoto: MarsPhoto) {
findNavController().navigate(SearchListFragmentDirections.actionSearchListFragmentToPhotoViewFragment(marsPhoto.id, PhotoTypes.MARS_PHOTO))
},

// Add to favourites click Listener
fun (marsPhoto: MarsPhoto) {
when(marsPhoto.isFavourite) {
true -> viewModel.removePhotoFromFavourites(marsPhoto)
false -> viewModel.addPhotoToFavourites(marsPhoto)
}
},

// Click listener for load more button
fun () {
val args = SearchListFragmentArgs.fromBundle(requireArguments())
val photoType = args.MarsPhotoToSearch
viewModel.downloadMoreMarsPhotos(photoType)
},

// Live data to observe status
viewModel.statusForLoadingNewPhotos,

// Lifecycle for Observer
viewLifecycleOwner
)

binding.searchRecyclerView.adapter = searchListAdapter
}

private fun setAllObservers() {
viewModel.resultListForRecyclerView.observe(viewLifecycleOwner, Observer { newList->
if (newList.isNotEmpty()) {
searchListAdapter.items = newList
}
})

viewModel.errorMessage.observe(viewLifecycleOwner, Observer { newMessage ->
if (newMessage != null) {
showErrorMessage("Something went wrong: $newMessage")
}
})
}

private fun setAllClickListeners() {
binding.appBar.setOnMenuItemClickListener {
if (!Variables.isNetworkConnectionAvailable) {
showErrorMessage(getString(R.string.no_internet_connection))
return@setOnMenuItemClickListener false
}
val args = SearchListFragmentArgs.fromBundle(requireArguments())
val photoToSearch = args.MarsPhotoToSearch
viewModel.refreshSearch(photoToSearch)
true
}

binding.appBar.setNavigationOnClickListener {
findNavController().popBackStack()
}
}

private fun showErrorMessage(message: String) {
val context = requireContext()
Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
}

private fun showErrorImage() {
binding.errorImage.setImageResource(R.drawable.connection_error_image)
binding.errorImage.visibility = View.VISIBLE
binding.searchRecyclerView.visibility = View.GONE
}

private fun setBaseVisibilityForViews() {
binding.errorImage.visibility = View.GONE
binding.searchRecyclerView.visibility = View.VISIBLE
}

}
Loading

0 comments on commit 9c72dd6

Please sign in to comment.