From 5dc550fc5ce99a99af87ade639bff402d75f7362 Mon Sep 17 00:00:00 2001 From: Ferran Pons Date: Wed, 11 Apr 2018 18:38:06 +0200 Subject: [PATCH 1/5] Added schedule content and adapter and recycler view --- app/build.gradle | 2 +- .../spacehub/passTimes/PassTimesAdapter.kt | 59 +++++----- .../spacehub/schedule/ScheduleFragment.kt | 27 ++++- app/src/main/res/layout/fragment_schedule.xml | 103 +++++++++++++++++- .../main/res/layout/loading_custom_view.xml | 23 ++++ app/src/main/res/values/dimens.xml | 21 ++++ 6 files changed, 192 insertions(+), 43 deletions(-) create mode 100644 app/src/main/res/layout/loading_custom_view.xml create mode 100644 app/src/main/res/values/dimens.xml diff --git a/app/build.gradle b/app/build.gradle index 8c83e99..8981272 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -21,7 +21,7 @@ android { defaultConfig { applicationId "com.ferranpons.spacehub" - minSdkVersion 15 + minSdkVersion 17 targetSdkVersion 27 versionCode 4 versionName "1.0" diff --git a/app/src/main/java/com/ferranpons/spacehub/passTimes/PassTimesAdapter.kt b/app/src/main/java/com/ferranpons/spacehub/passTimes/PassTimesAdapter.kt index a6f51f6..321bb96 100644 --- a/app/src/main/java/com/ferranpons/spacehub/passTimes/PassTimesAdapter.kt +++ b/app/src/main/java/com/ferranpons/spacehub/passTimes/PassTimesAdapter.kt @@ -1,60 +1,55 @@ package com.ferranpons.spacehub.passTimes -import android.annotation.TargetApi import android.content.Context import android.icu.text.SimpleDateFormat +import android.support.v7.widget.RecyclerView +import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import android.widget.ArrayAdapter import android.widget.TextView import com.ferranpons.spacehub.R import com.ferranpons.spacehub.issTracking.IssTrackingApiInterface -import java.util.Locale -import java.util.concurrent.TimeUnit import org.joda.time.DateTime +import java.util.* +import java.util.concurrent.TimeUnit -class PassTimesAdapter(context: Context, passTimes: List) : ArrayAdapter(context, R.layout.row_pass_time, passTimes) { +class PassTimesAdapter(private val passTimes: List) : RecyclerView.Adapter() { - class ViewHolder(view: View) { - internal var riseTime: TextView = view.findViewById(R.id.riseTime) as TextView - internal var duration: TextView = view.findViewById(R.id.duration) as TextView + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PassTimesViewHolder { + val v = LayoutInflater.from(parent.context).inflate(R.layout.row_pass_time, parent, false) + return PassTimesViewHolder(v) } - @TargetApi(24) - override fun getView(position: Int, view: View?, parent: ViewGroup): View? { - val holder: ViewHolder - var customView = view - if (view == null) { - customView = View.inflate(parent.context, R.layout.row_pass_time, null) - holder = ViewHolder(customView) - customView.tag = holder - } else { - holder = view.tag as ViewHolder - } - val passTime = getItem(position) - if (passTime != null && passTime.riseTime > -1) { - val date = DateTime(passTime.riseTime * 1000L) + override fun getItemCount(): Int = passTimes.size + + override fun onBindViewHolder(holder: PassTimesViewHolder, position: Int) { + val passTime = passTimes[position] + if (passTime.riseTime > -1) { + /*val date = DateTime(passTime.riseTime * 1000L) var sdf: SimpleDateFormat? = null if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { sdf = SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault()) } - var riseTimeUnformatted = arrayOfNulls(0) if (sdf != null) { - riseTimeUnformatted = sdf.format(date).split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() - } - val riseTime = riseTimeUnformatted[0] + " at " + riseTimeUnformatted[1] + "h" - holder.riseTime.text = riseTime + val riseTimeUnformatted = sdf.format(date).split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() + val riseTime = riseTimeUnformatted[0] + " at " + riseTimeUnformatted[1] + "h" + holder.riseTime.text = riseTime + }*/ } - if (passTime != null && passTime.duration > -1) { + if (passTime.duration > -1) { val duration = TimeUnit.SECONDS.toMinutes(passTime.duration.toLong()) val durationTime: String - if (duration >= 1) { - durationTime = "during " + java.lang.Long.toString(duration) + " min" + durationTime = if (duration >= 1) { + "during " + java.lang.Long.toString(duration) + " min" } else { - durationTime = "during " + Integer.toString(passTime.duration) + " secs" + "during " + Integer.toString(passTime.duration) + " secs" } holder.duration.text = durationTime } - return customView + } + + inner class PassTimesViewHolder(view: View) : RecyclerView.ViewHolder(view) { + var riseTime = view.findViewById(R.id.riseTime) as TextView + var duration = view.findViewById(R.id.duration) as TextView } } diff --git a/app/src/main/java/com/ferranpons/spacehub/schedule/ScheduleFragment.kt b/app/src/main/java/com/ferranpons/spacehub/schedule/ScheduleFragment.kt index 9592a01..5286d96 100644 --- a/app/src/main/java/com/ferranpons/spacehub/schedule/ScheduleFragment.kt +++ b/app/src/main/java/com/ferranpons/spacehub/schedule/ScheduleFragment.kt @@ -3,19 +3,19 @@ package com.ferranpons.spacehub.schedule import android.content.Context import android.os.Bundle import android.support.v4.app.Fragment +import android.support.v7.widget.DefaultItemAnimator +import android.support.v7.widget.LinearLayoutManager +import android.support.v7.widget.RecyclerView import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import com.ferranpons.spacehub.R -import com.ferranpons.spacehub.issTracking.IssTrackingApi -import com.ferranpons.spacehub.issTracking.IssTrackingApiInterface -import com.ferranpons.spacehub.issTracking.IssTrackingPresenterInterface -import com.ferranpons.spacehub.issTracking.IssTrackingViewInterface -import com.ferranpons.spacehub.issTracking.IssTrackingPresenter -import com.ferranpons.spacehub.issTracking.IssTrackingInteractor +import com.ferranpons.spacehub.issTracking.* +import com.ferranpons.spacehub.passTimes.PassTimesAdapter class ScheduleFragment : Fragment(), IssTrackingViewInterface { private lateinit var issTrackingPresenter: IssTrackingPresenterInterface + private lateinit var recyclerView: RecyclerView override fun onAttach(context: Context?) { super.onAttach(context) @@ -29,6 +29,16 @@ class ScheduleFragment : Fragment(), IssTrackingViewInterface { return view } + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + + val layoutManager = LinearLayoutManager(view.context) + recyclerView = view.findViewById(R.id.schedule_recyclerview) + recyclerView.layoutManager = layoutManager + recyclerView.setHasFixedSize(true) + recyclerView.itemAnimator = DefaultItemAnimator() + } + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) issTrackingPresenter.retrieveCurrentPosition() @@ -39,6 +49,7 @@ class ScheduleFragment : Fragment(), IssTrackingViewInterface { } override fun setIssPosition(position: IssTrackingApiInterface.IssPosition?) { + issTrackingPresenter.retrievePassTimes(position?.latitude!!, position.longitude) } override fun willRetrievePassTimes() { @@ -60,6 +71,10 @@ class ScheduleFragment : Fragment(), IssTrackingViewInterface { } override fun showPassTimes(passTimes: MutableList?) { + val scheduleAdapter = PassTimesAdapter(passTimes!!) + + recyclerView.adapter = scheduleAdapter + scheduleAdapter.notifyDataSetChanged() } override fun showPeopleInSpace(people: MutableList?) { diff --git a/app/src/main/res/layout/fragment_schedule.xml b/app/src/main/res/layout/fragment_schedule.xml index de144fa..fe45f5a 100644 --- a/app/src/main/res/layout/fragment_schedule.xml +++ b/app/src/main/res/layout/fragment_schedule.xml @@ -1,8 +1,103 @@ - + android:layout_height="match_parent" + android:orientation="vertical"> - \ No newline at end of file + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/loading_custom_view.xml b/app/src/main/res/layout/loading_custom_view.xml new file mode 100644 index 0000000..efbf65e --- /dev/null +++ b/app/src/main/res/layout/loading_custom_view.xml @@ -0,0 +1,23 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml new file mode 100644 index 0000000..55b13a7 --- /dev/null +++ b/app/src/main/res/values/dimens.xml @@ -0,0 +1,21 @@ + + + 16dp + 16dp + + 72dp + 48dp + 56dp + 40dp + 32dp + 36dp + 24dp + 20dp + 16dp + 12dp + 10dp + 8dp + 6dp + 4dp + 2dp + From 8d621a577c06dfdd67426c311c9ce04307e3f654 Mon Sep 17 00:00:00 2001 From: Ferran Pons Date: Thu, 12 Apr 2018 15:23:29 +0200 Subject: [PATCH 2/5] Extracted and update libs --- app/build.gradle | 5 +++++ .../com/ferranpons/spacehub/schedule/ScheduleFragment.kt | 9 ++++++--- app/src/main/res/layout/fragment_schedule.xml | 4 ++-- app/src/main/res/values/styles.xml | 2 +- build.gradle | 2 +- 5 files changed, 15 insertions(+), 7 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 8981272..d21c98e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -68,6 +68,11 @@ dependencies { implementation 'io.reactivex.rxjava2:rxandroid:2.0.2' implementation 'io.reactivex.rxjava2:rxjava:2.1.12' + implementation ('com.schibstedspain.android:leku:4.0.1') { + exclude group: 'com.google.android.gms' + exclude group: 'com.android.support' + } + def butterKnife = '8.8.1' implementation "com.jakewharton:butterknife:$butterKnife" kapt "com.jakewharton:butterknife-compiler:$butterKnife" diff --git a/app/src/main/java/com/ferranpons/spacehub/schedule/ScheduleFragment.kt b/app/src/main/java/com/ferranpons/spacehub/schedule/ScheduleFragment.kt index 5286d96..05a8166 100644 --- a/app/src/main/java/com/ferranpons/spacehub/schedule/ScheduleFragment.kt +++ b/app/src/main/java/com/ferranpons/spacehub/schedule/ScheduleFragment.kt @@ -17,16 +17,19 @@ class ScheduleFragment : Fragment(), IssTrackingViewInterface { private lateinit var issTrackingPresenter: IssTrackingPresenterInterface private lateinit var recyclerView: RecyclerView + companion object { + const val OPEN_NOTIFY_API = "http://api.open-notify.org" + } + override fun onAttach(context: Context?) { super.onAttach(context) - issTrackingPresenter = IssTrackingPresenter(IssTrackingInteractor(IssTrackingApi.getIssTrackingApi("http://api.open-notify.org"))) + issTrackingPresenter = IssTrackingPresenter(IssTrackingInteractor(IssTrackingApi.getIssTrackingApi(OPEN_NOTIFY_API))) (issTrackingPresenter as IssTrackingPresenter).setView(this) } override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { - val view = inflater.inflate(R.layout.fragment_schedule, container, false) - return view + return inflater.inflate(R.layout.fragment_schedule, container, false) } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { diff --git a/app/src/main/res/layout/fragment_schedule.xml b/app/src/main/res/layout/fragment_schedule.xml index fe45f5a..084f537 100644 --- a/app/src/main/res/layout/fragment_schedule.xml +++ b/app/src/main/res/layout/fragment_schedule.xml @@ -39,8 +39,6 @@ - - + + \ No newline at end of file diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index dee0b47..55ea946 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -1,7 +1,7 @@