Skip to content

Commit

Permalink
Fix: Replaced OnCreateView with Fragment LayoutId constructor in all …
Browse files Browse the repository at this point in the history
…Fragments. This Addresses #73
  • Loading branch information
wangerekaharun committed Jan 16, 2020
1 parent 0c39597 commit 49f0941
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,9 @@ import kotlinx.android.synthetic.main.fragment_about.*
import org.jetbrains.anko.toast
import org.koin.android.ext.android.inject

class AboutFragment : Fragment() {
class AboutFragment : Fragment(R.layout.fragment_about) {
private val aboutViewModel: AboutViewModel by inject()

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_about, container, false)
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,10 @@ import org.jetbrains.anko.toast
import org.koin.android.ext.android.inject
import java.util.*

class AgendaFragment : Fragment() {
class AgendaFragment : Fragment(R.layout.fragment_agenda) {
private var agendaModelList: List<AgendaModel> = ArrayList()
private val agendaViewModel: AgendaViewModel by inject()

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_agenda, container, false)
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,11 @@ import kotlinx.android.synthetic.main.fragment_announcements.*
import org.jetbrains.anko.toast
import org.koin.android.ext.android.inject

class AnnouncementFragment : Fragment() {
class AnnouncementFragment : Fragment(R.layout.fragment_announcements) {
private val announcementViewModel: AnnouncementViewModel by inject()
private var announcementList: List<Announcement> = ArrayList()
private lateinit var announcementAdapter: AnnouncementsAdapter

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_announcements, container, false)
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import com.google.android.material.snackbar.Snackbar
import kotlinx.android.synthetic.main.fragment_event.*
import org.koin.android.ext.android.inject

class EventFragment : Fragment() {
class EventFragment : Fragment(R.layout.fragment_event) {
private val eventTypeViewModel: EventTypeViewModel by inject()

private val fetchErrorSnackbar: Snackbar by lazy {
Expand All @@ -28,10 +28,6 @@ class EventFragment : Fragment() {
).setAction("Retry") { eventTypeViewModel.retry() }
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_event, container, false)
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ import org.koin.androidx.viewmodel.ext.android.viewModel
import org.koin.core.parameter.parametersOf
import org.threeten.bp.format.DateTimeFormatter

class ScheduleFragment : Fragment() {

class ScheduleFragment : Fragment(R.layout.fragment_schedule) {
private val auth: FirebaseAuth by inject()
private val sessionDetailsViewModel: SessionDetailsViewModel by viewModel()
private val sharedPreferences: SharedPreferences by inject { parametersOf(context) }
Expand All @@ -54,15 +53,13 @@ class ScheduleFragment : Fragment() {
SessionsViewPagerAdapter(childFragmentManager)
}


override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_schedule, container, false)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
lifecycleScope.launchWhenStarted {
if (auth.isSignedIn()) {
sessionDetailsViewModel.fetchFavourites(sharedPreferences, auth.currentUser!!.uid)
}
}
return view
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import org.koin.androidx.viewmodel.ext.android.sharedViewModel
import org.koin.core.parameter.parametersOf
import java.util.*

class SessionDetailsFragment : Fragment() {
class SessionDetailsFragment : Fragment(R.layout.fragment_session_details) {
private val sessionDetailsViewModel: SessionDetailsViewModel by sharedViewModel()
private val sharedPreferences: SharedPreferences by inject { parametersOf(context) }
private val firebaseAuth: FirebaseAuth by inject()
Expand All @@ -46,13 +46,6 @@ class SessionDetailsFragment : Fragment() {
}
}

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_session_details, container, false)
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
Expand Down Expand Up @@ -167,14 +160,12 @@ class SessionDetailsFragment : Fragment() {
divider_top_feedback.visibility = View.GONE
session_feedback_title.visibility = View.GONE
sessionFeedbackText.visibility = View.GONE
// session_slide_button.visibility = View.GONE
sessionReserveSeatText.visibility = View.VISIBLE
}
"sessionEnded" -> {
divider_top_feedback.visibility = View.VISIBLE
session_feedback_title.visibility = View.VISIBLE
sessionFeedbackText.visibility = View.VISIBLE
// session_slide_button.visibility = View.VISIBLE
sessionReserveSeatText.visibility = View.GONE
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import org.jetbrains.anko.toast
import org.koin.android.ext.android.inject
import org.koin.core.parameter.parametersOf

class SessionDayFragment : Fragment() {
class SessionDayFragment : Fragment(R.layout.fragment_day_session) {
private val day: EventDay by lazy {
checkNotNull(arguments?.getSerializable(KEY_EVENT_DAY) as EventDay)
}
Expand All @@ -36,10 +36,6 @@ class SessionDayFragment : Fragment() {
SessionsAdapter(favoritesStore) { redirectToSessionDetails(it) }
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_day_session, container, false)
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,9 @@ import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import kotlinx.android.synthetic.main.fragment_speaker.*

class SpeakerFragment : Fragment() {
class SpeakerFragment : Fragment(R.layout.fragment_speaker) {
private val speakerArgs: SpeakerFragmentArgs by navArgs()

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_speaker, container, false)

}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
sharedElementEnterTransition = TransitionInflater.from(context).inflateTransition(R.transition.speaker_shared_enter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@ import com.android254.droidconke19.ui.widget.CollapsibleCard
import kotlinx.android.synthetic.main.fragment_travel.*
import org.koin.android.ext.android.inject

class TravelFragment : Fragment() {
class TravelFragment : Fragment(R.layout.fragment_travel) {
private val firebaseRemoteConfig: FirebaseRemoteConfig by inject()

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_travel, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,26 @@ import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.Marker
import com.google.android.gms.maps.model.MarkerOptions
import com.google.android.material.bottomsheet.BottomSheetBehavior
import kotlinx.android.synthetic.main.map_bottom_sheet.*
import kotlinx.android.synthetic.main.map_bottom_sheet.view.*
import org.koin.android.ext.android.inject
import org.koin.core.parameter.parametersOf

class MapFragment : Fragment(), OnMapReadyCallback {
class MapFragment : Fragment(R.layout.fragment_map), OnMapReadyCallback {
private val senteuPlaza = LatLng(-1.289256, 36.783180)
private var mMap: GoogleMap? = null
private var senteuMarker: Marker? = null
private var bottomSheetBehavior: BottomSheetBehavior<*>? = null
private val mLastKnownLocation: Location? = null
internal var mCameraPosition: CameraPosition? = null
lateinit var mLocationRequest: LocationRequest
internal var currentLatLng: LatLng? = null
private var mFusedLocationProviderClient: FusedLocationProviderClient? = null
private var permissionsRequired = arrayOf(Manifest.permission.ACCESS_FINE_LOCATION)
private var sentToSettings = false
val sharedPreferences: SharedPreferences by inject { parametersOf(context) }
private val sharedPreferences: SharedPreferences by inject { parametersOf(context) }

private var mLocationCallback: LocationCallback = object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult) {
locationResult.locations.forEach { location ->
Log.i("MapsActivity", "Location: " + location.latitude + " " + location.longitude)
currentLatLng = LatLng(location.latitude, location.longitude)
}
}
Expand All @@ -72,8 +70,8 @@ class MapFragment : Fragment(), OnMapReadyCallback {
//check if all permissions are granted
var allgranted = false
loop@ for (i in grantResults.indices) {
when {
grantResults[i] == PackageManager.PERMISSION_GRANTED -> allgranted = true
when (PackageManager.PERMISSION_GRANTED) {
grantResults[i] -> allgranted = true
else -> {
allgranted = false
break@loop
Expand All @@ -82,13 +80,13 @@ class MapFragment : Fragment(), OnMapReadyCallback {
}
when {
allgranted -> mFusedLocationProviderClient!!.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper())
ActivityCompat.shouldShowRequestPermissionRationale(activity!!, permissionsRequired[0]) -> {
val builder = AlertDialog.Builder(activity!!)
ActivityCompat.shouldShowRequestPermissionRationale(requireActivity(), permissionsRequired[0]) -> {
val builder = AlertDialog.Builder(requireActivity())
builder.setTitle(getString(R.string.need_multiple_permissions))
builder.setMessage(getString(R.string.need_location_storage))
builder.setPositiveButton(getString(R.string.cancel)) { dialog, _ ->
dialog.cancel()
ActivityCompat.requestPermissions(activity!!, permissionsRequired, PERMISSION_CALLBACK_CONSTANT)
ActivityCompat.requestPermissions(requireActivity(), permissionsRequired, PERMISSION_CALLBACK_CONSTANT)
}
builder.setNegativeButton(getString(R.string.cancel)) { dialog, _ -> dialog.cancel() }
builder.show()
Expand All @@ -102,12 +100,11 @@ class MapFragment : Fragment(), OnMapReadyCallback {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(requireActivity())
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_map, container, false)

mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(activity!!)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

//show toolbar if its hidden
(activity as AppCompatActivity).supportActionBar?.show()
Expand All @@ -116,18 +113,18 @@ class MapFragment : Fragment(), OnMapReadyCallback {
bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetView)

//collapse bottom sheet
view.collapseBottomSheetImg.setOnClickListener {
collapseBottomSheetImg.setOnClickListener {
when (bottomSheetBehavior?.state) {
BottomSheetBehavior.STATE_EXPANDED -> bottomSheetBehavior?.state = BottomSheetBehavior.STATE_COLLAPSED
}
}

//open google maps intent to get directions
view.googleDirectionsBtn.setOnClickListener {
googleDirectionsBtn.setOnClickListener {
when {
currentLatLng != null -> {
val uri = "http://maps.google.com/maps?f=d&hl=en&saddr=" + currentLatLng!!.latitude + "," + currentLatLng!!.longitude + "&daddr=" + senteuPlaza.latitude + "," + senteuPlaza.longitude
val intent = Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri))
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(uri))
startActivity(Intent.createChooser(intent, "Open with"))
}
else -> Toast.makeText(activity, getString(R.string.problem_getting_location), Toast.LENGTH_SHORT).show()
Expand All @@ -141,7 +138,7 @@ class MapFragment : Fragment(), OnMapReadyCallback {
.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)

return view

}

@SuppressLint("MissingPermission")
Expand All @@ -154,7 +151,7 @@ class MapFragment : Fragment(), OnMapReadyCallback {
mLocationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
val builder = LocationSettingsRequest.Builder()
.addLocationRequest(mLocationRequest)
val client = LocationServices.getSettingsClient(activity!!)
val client = LocationServices.getSettingsClient(requireActivity())
val task = client.checkLocationSettings(builder.build())
task.addOnCompleteListener { task1 ->
try {
Expand Down Expand Up @@ -209,39 +206,39 @@ class MapFragment : Fragment(), OnMapReadyCallback {

private fun checkLocationPermission() {
when {
ActivityCompat.checkSelfPermission(activity!!, permissionsRequired[0]) != PackageManager.PERMISSION_GRANTED -> {
ActivityCompat.checkSelfPermission(requireActivity(), permissionsRequired[0]) != PackageManager.PERMISSION_GRANTED -> {
when {
ActivityCompat.shouldShowRequestPermissionRationale(activity!!, permissionsRequired[0]) -> {
ActivityCompat.shouldShowRequestPermissionRationale(requireActivity(), permissionsRequired[0]) -> {
//Show Information about why you need the permission
val builder = AlertDialog.Builder(activity!!)
val builder = AlertDialog.Builder(requireActivity())
builder.setTitle(getString(R.string.need_multiple_permissions))
builder.setMessage(getString(R.string.need_location_storage))
builder.setPositiveButton(getString(R.string.grant)) { dialog, _ ->
dialog.cancel()
ActivityCompat.requestPermissions(activity!!, permissionsRequired, PERMISSION_CALLBACK_CONSTANT)
ActivityCompat.requestPermissions(requireActivity(), permissionsRequired, PERMISSION_CALLBACK_CONSTANT)
}
builder.setNegativeButton(getString(R.string.cancel)) { dialog, _ -> dialog.cancel() }
builder.show()
}
sharedPreferences.getBoolean(permissionsRequired[0], false) -> {
//Previously Permission Request was cancelled with 'Dont Ask Again',
// Redirect to Settings after showing Information about why you need the permission
val builder = AlertDialog.Builder(activity!!)
val builder = AlertDialog.Builder(requireActivity())
builder.setTitle(getString(R.string.need_multiple_permissions))
builder.setMessage(getString(R.string.need_location_storage))
builder.setPositiveButton(getString(R.string.grant)) { dialog, _ ->
dialog.cancel()
sentToSettings = true
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
val uri = Uri.fromParts("package", activity!!.packageName, null)
val uri = Uri.fromParts("package", requireActivity().packageName, null)
intent.data = uri
startActivityForResult(intent, REQUEST_PERMISSION_SETTING)
Toast.makeText(activity, getString(R.string.grant_storage_location), Toast.LENGTH_LONG).show()
}
builder.setNegativeButton(getString(R.string.cancel)) { dialog, _ -> dialog.cancel() }
}
else -> //just request the permission
ActivityCompat.requestPermissions(activity!!, permissionsRequired, PERMISSION_CALLBACK_CONSTANT)
ActivityCompat.requestPermissions(requireActivity(), permissionsRequired, PERMISSION_CALLBACK_CONSTANT)
}
sharedPreferences.edit().putBoolean(permissionsRequired[0], true).apply()
}
Expand Down

0 comments on commit 49f0941

Please sign in to comment.