-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
283 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 0 additions & 13 deletions
13
app/src/main/java/com/gvoltr/placeshere/presentation/MainActivity.kt
This file was deleted.
Oops, something went wrong.
4 changes: 3 additions & 1 deletion
4
app/src/main/java/com/gvoltr/placeshere/presentation/di/PresentationModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
package com.gvoltr.placeshere.presentation.di | ||
|
||
import com.gvoltr.placeshere.presentation.address.viewmodel.AddressViewModel | ||
import com.gvoltr.placeshere.presentation.main.viewmodel.MainViewModel | ||
import com.gvoltr.placeshere.presentation.places.viewmodel.PlacesViewModel | ||
import org.koin.androidx.viewmodel.dsl.viewModel | ||
import org.koin.dsl.module | ||
|
||
private val viewModelsModule = module { | ||
viewModel { PlacesViewModel(get(), get(), get(), get()) } | ||
viewModel { PlacesViewModel(get(), get(), get()) } | ||
viewModel { AddressViewModel(get()) } | ||
viewModel { MainViewModel(get(), get()) } | ||
} | ||
|
||
val presentationModule = viewModelsModule |
79 changes: 79 additions & 0 deletions
79
app/src/main/java/com/gvoltr/placeshere/presentation/main/view/MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.gvoltr.placeshere.presentation.main.view | ||
|
||
import android.Manifest | ||
import android.content.pm.PackageManager | ||
import android.os.Bundle | ||
import androidx.appcompat.app.AlertDialog | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.core.app.ActivityCompat | ||
import androidx.lifecycle.Observer | ||
import com.gvoltr.placeshere.R | ||
import com.gvoltr.placeshere.presentation.main.viewmodel.InteractionEvent | ||
import com.gvoltr.placeshere.presentation.main.viewmodel.MainViewModel | ||
import org.koin.androidx.viewmodel.ext.android.viewModel | ||
|
||
class MainActivity : AppCompatActivity() { | ||
private val LOCATION_PERMISSION_REQUEST_CODE = 1 | ||
|
||
private val mainViewModel: MainViewModel by viewModel() | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_main) | ||
subscribeToVM() | ||
} | ||
|
||
override fun onRequestPermissionsResult( | ||
requestCode: Int, | ||
permissions: Array<out String>, | ||
grantResults: IntArray | ||
) { | ||
if (requestCode == LOCATION_PERMISSION_REQUEST_CODE) { | ||
if (grantResults.isNotEmpty() && grantResults.first() == PackageManager.PERMISSION_GRANTED) { | ||
mainViewModel.locationAllowed() | ||
} else { | ||
mainViewModel.locationDisallowed() | ||
} | ||
} | ||
} | ||
|
||
private fun subscribeToVM() { | ||
mainViewModel.getInteractionEventsLiveData() | ||
.observe(this, Observer { event -> | ||
event.getContentIfNotHandled()?.let { handleInteractionEvent(it) } | ||
}) | ||
} | ||
|
||
private fun handleInteractionEvent(event: InteractionEvent) { | ||
when (event) { | ||
is InteractionEvent.RequestLocation -> askToEnableLocationPermission() | ||
} | ||
} | ||
|
||
private fun askToEnableLocationPermission() { | ||
val shouldShowRationale = ActivityCompat.shouldShowRequestPermissionRationale( | ||
this, | ||
Manifest.permission.ACCESS_FINE_LOCATION | ||
) | ||
if (shouldShowRationale) { | ||
AlertDialog.Builder(this) | ||
.setTitle(R.string.label_permission_required) | ||
.setMessage(R.string.location_permission_required_message) | ||
.setPositiveButton(R.string.ok) { _, _ -> | ||
requestLocationPermission() | ||
} | ||
.create() | ||
.show() | ||
} else { | ||
requestLocationPermission() | ||
} | ||
} | ||
|
||
private fun requestLocationPermission() { | ||
ActivityCompat.requestPermissions( | ||
this, | ||
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), | ||
LOCATION_PERMISSION_REQUEST_CODE | ||
) | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...tion/places/viewmodel/InteractionEvent.kt → ...tation/main/viewmodel/InteractionEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
app/src/main/java/com/gvoltr/placeshere/presentation/main/viewmodel/MainViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.gvoltr.placeshere.presentation.main.viewmodel | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import com.gvoltr.placeshere.domain.LocationInteractor | ||
import com.gvoltr.placeshere.domain.PermissionInteractor | ||
import com.gvoltr.placeshere.presentation.utils.Event | ||
|
||
class MainViewModel( | ||
private val locationInteractor: LocationInteractor, | ||
private val permissionInteractor: PermissionInteractor | ||
) : ViewModel() { | ||
|
||
|
||
//Used to pass events to the view that require user interaction e.g. location request | ||
private val interactionLiveData = MutableLiveData<Event<InteractionEvent>>() | ||
|
||
init { | ||
initLocationUpdates() | ||
} | ||
|
||
override fun onCleared() { | ||
super.onCleared() | ||
locationInteractor.stopLocationUpdates() | ||
} | ||
|
||
fun getInteractionEventsLiveData(): LiveData<Event<InteractionEvent>> = interactionLiveData | ||
|
||
fun locationAllowed() { | ||
initLocationUpdates() | ||
} | ||
|
||
fun locationDisallowed() { | ||
requestLocation() | ||
} | ||
|
||
private fun initLocationUpdates() { | ||
if (permissionInteractor.locationPermissionGranted()) { | ||
locationInteractor.startLocationUpdates() | ||
} else { | ||
requestLocation() | ||
} | ||
} | ||
|
||
private fun requestLocation() { | ||
interactionLiveData.value = Event(InteractionEvent.RequestLocation) | ||
} | ||
|
||
} |
70 changes: 70 additions & 0 deletions
70
app/src/main/java/com/gvoltr/placeshere/presentation/places/view/PlaceCategoriesAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.gvoltr.placeshere.presentation.places.view | ||
|
||
|
||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.CheckBox | ||
import androidx.recyclerview.widget.DiffUtil | ||
import androidx.recyclerview.widget.ListAdapter | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.gvoltr.placeshere.R | ||
import com.gvoltr.placeshere.presentation.places.viewmodel.PlaceCategoryItem | ||
import kotlinx.android.synthetic.main.list_item_place_category.view.* | ||
|
||
class PlaceCategoriesAdapter( | ||
private val itemSelected: (PlaceCategoryItem) -> Unit, | ||
private val itemUnselected: (PlaceCategoryItem) -> Unit | ||
) : ListAdapter<PlaceCategoryItem, PlaceCategoriesAdapter.PlaceCategoryViewHolder>(DiffCallback()) { | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PlaceCategoryViewHolder { | ||
val inflater = LayoutInflater.from(parent.context) | ||
val view = inflater.inflate(R.layout.list_item_place_category, parent, false) | ||
return PlaceCategoryViewHolder(view, itemSelected, itemUnselected) | ||
} | ||
|
||
override fun onBindViewHolder(holder: PlaceCategoryViewHolder, position: Int) { | ||
holder.bindData(getItem(position)) | ||
} | ||
|
||
class PlaceCategoryViewHolder( | ||
itemView: View, | ||
itemSelected: (PlaceCategoryItem) -> Unit, | ||
itemUnselected: (PlaceCategoryItem) -> Unit | ||
) : RecyclerView.ViewHolder(itemView) { | ||
|
||
lateinit var placeCategoryItem: PlaceCategoryItem | ||
|
||
init { | ||
itemView.checkbox.setOnClickListener { | ||
if ((it as CheckBox).isChecked) { | ||
itemSelected.invoke(placeCategoryItem) | ||
} else { | ||
itemUnselected.invoke(placeCategoryItem) | ||
} | ||
} | ||
} | ||
|
||
fun bindData(placeCategory: PlaceCategoryItem) { | ||
this.placeCategoryItem = placeCategory | ||
itemView.checkbox.text = placeCategory.placeCategory.title | ||
itemView.checkbox.isChecked = placeCategory.checked | ||
} | ||
} | ||
|
||
private class DiffCallback : DiffUtil.ItemCallback<PlaceCategoryItem>() { | ||
override fun areItemsTheSame( | ||
oldItem: PlaceCategoryItem, | ||
newItem: PlaceCategoryItem | ||
): Boolean { | ||
return oldItem === newItem | ||
} | ||
|
||
override fun areContentsTheSame( | ||
oldItem: PlaceCategoryItem, | ||
newItem: PlaceCategoryItem | ||
): Boolean { | ||
return oldItem == newItem | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
app/src/main/java/com/gvoltr/placeshere/presentation/places/viewmodel/PlaceCategoryItem.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.gvoltr.placeshere.presentation.places.viewmodel | ||
|
||
import com.gvoltr.placeshere.data.entity.category.PlaceCategory | ||
|
||
data class PlaceCategoryItem ( | ||
val placeCategory: PlaceCategory, | ||
var checked: Boolean = false | ||
) |
Oops, something went wrong.