Plan and personal plan now use the same Kotlin class, food fragment doesnt work properly (yet)

This commit is contained in:
Deniz Düzgören
2019-05-26 11:35:36 +02:00
parent 0c043683f9
commit 9f22717e63
11 changed files with 242 additions and 51 deletions
+3
View File
@@ -48,6 +48,9 @@
<service <service
android:name=".ScheduledJobService" android:name=".ScheduledJobService"
android:permission="android.permission.BIND_JOB_SERVICE" /> android:permission="android.permission.BIND_JOB_SERVICE" />
<service
android:name=".NotificationService"
android:permission="android.permission.BIND_JOB_SERVICE" />
</application> </application>
</manifest> </manifest>
@@ -1,5 +1,6 @@
package com.denizd.substitutionplan package com.denizd.substitutionplan
import android.content.Context
import android.content.SharedPreferences import android.content.SharedPreferences
import android.os.Bundle import android.os.Bundle
import android.preference.PreferenceManager import android.preference.PreferenceManager
@@ -12,47 +13,53 @@ import com.madapps.prefrences.EasyPrefrences
class FoodFragment : Fragment(R.layout.food_layout) { class FoodFragment : Fragment(R.layout.food_layout) {
private lateinit var foodArrayList: ArrayList<Food> public val foodArrayList = ArrayList<Food>()
private lateinit var recyclerView: RecyclerView
private val mAdapter = FoodAdapter(foodArrayList) private val mAdapter = FoodAdapter(foodArrayList)
private lateinit var mContext: Context
private lateinit var prefs: SharedPreferences
// private lateinit var edit: SharedPreferences.Editor
private lateinit var easyPrefs: EasyPrefrences
private lateinit var foodListPopulation: ArrayList<String>
private lateinit var recyclerView: RecyclerView
// override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { override fun onAttach(context: Context) {
// return inflater.inflate(R.layout.food_layout, null) super.onAttach(context)
// } // TODO do I need this? mContext = context
prefs = PreferenceManager.getDefaultSharedPreferences(mContext)
// edit = prefs.edit()
easyPrefs = EasyPrefrences(mContext)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
val pullToRefresh = getView()?.findViewById(R.id.pullToRefresh) as SwipeRefreshLayout val pullToRefresh = view.findViewById<SwipeRefreshLayout>(R.id.pullToRefresh)
val prefs = PreferenceManager.getDefaultSharedPreferences(activity) as SharedPreferences foodListPopulation = ArrayList(easyPrefs.getListString("foodListPrefs"))
// val edit = prefs.edit() as SharedPreferences.Editor
val easyPrefs = EasyPrefrences(context!!)
val foodListPopulation = ArrayList<String>(easyPrefs.getListString("foodListPrefs"))
try { try {
recyclerView = getView()?.findViewById(R.id.linear_food) as RecyclerView recyclerView = view.findViewById(R.id.linear_food)
recyclerView.hasFixedSize() recyclerView.hasFixedSize()
recyclerView.layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL ,false) recyclerView.layoutManager = LinearLayoutManager(mContext, RecyclerView.VERTICAL,false)
recyclerView.adapter = mAdapter recyclerView.adapter = mAdapter
try { try {
recyclerView.removeAllViews() recyclerView.removeAllViews()
} catch (e: NullPointerException) {} } catch (ignored: NullPointerException) {}
for (i in 0..foodListPopulation.size) { for (i in 0 until foodListPopulation.size) {
foodArrayList.add(Food(foodListPopulation[i])) foodArrayList.add(Food(foodListPopulation[i]))
mAdapter.setFood(foodArrayList) mAdapter.setFood(foodArrayList)
recyclerView.scheduleLayoutAnimation() recyclerView.scheduleLayoutAnimation()
} }
} catch (e: NullPointerException) {} } catch (ignored: NullPointerException) {}
if (prefs.getBoolean("autoRefresh", false)) { if (prefs.getBoolean("autoRefresh", false)) {
pullToRefresh.isRefreshing = true pullToRefresh.isRefreshing = true
// coroutines DataFetcher(false, true, false, mContext, activity!!.application, view.rootView).execute()
} }
pullToRefresh.setOnRefreshListener { pullToRefresh.setOnRefreshListener {
pullToRefresh.isRefreshing = true pullToRefresh.isRefreshing = true
// coroutines DataFetcher(false, true, false, mContext, activity!!.application, view.rootView).execute()
} }
} }
@@ -49,7 +49,7 @@ public class FragmentFood extends Fragment {
@Override @Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); super.onViewCreated(view, savedInstanceState);
final SwipeRefreshLayout pullToRefresh = getView().findViewById(R.id.pullToRefreshFood); final SwipeRefreshLayout pullToRefresh = getView().findViewById(R.id.pullToRefresh);
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
EasyPrefrences easyPrefs = new EasyPrefrences(getContext()); EasyPrefrences easyPrefs = new EasyPrefrences(getContext());
@@ -91,7 +91,7 @@ public class FragmentFood extends Fragment {
boolean attempt = false, npe = false; boolean attempt = false, npe = false;
Elements foodElements; Elements foodElements;
Document docFood; Document docFood;
final SwipeRefreshLayout pullToRefresh = getView().findViewById(R.id.pullToRefreshFood); final SwipeRefreshLayout pullToRefresh = getView().findViewById(R.id.pullToRefresh);
protected fetcher() { protected fetcher() {
} }
@@ -258,15 +258,15 @@ class Main : AppCompatActivity(R.layout.activity_main) {
lateinit var fragment: Fragment lateinit var fragment: Fragment
when (item.itemId) { when (item.itemId) {
R.id.plan -> { R.id.plan -> {
fragment = FragmentPlan() fragment = PlanFragment(false)
toolbarTxt.text = getString(R.string.app_name) toolbarTxt.text = getString(R.string.app_name)
} }
R.id.personal -> { R.id.personal -> {
fragment = FragmentPersonal() fragment = PlanFragment(true)
toolbarTxt.text = userPlan toolbarTxt.text = userPlan
} }
R.id.menu -> { R.id.menu -> {
fragment = FragmentFood() fragment = FoodFragment()
toolbarTxt.text = getString(R.string.foodmenu) toolbarTxt.text = getString(R.string.foodmenu)
} }
R.id.openinfopanel -> { R.id.openinfopanel -> {
@@ -317,9 +317,7 @@ class Main : AppCompatActivity(R.layout.activity_main) {
} }
appbarlayout.setExpanded(true) appbarlayout.setExpanded(true)
} }
} }
} }
private fun loadFragment(fragment: Fragment): Boolean { private fun loadFragment(fragment: Fragment): Boolean {
@@ -328,7 +326,7 @@ class Main : AppCompatActivity(R.layout.activity_main) {
} }
private fun notificationJob() { private fun notificationJob() {
val componentName = ComponentName(this, ScheduledJobService::class.java) val componentName = ComponentName(this, NotificationService::class.java)
val info = JobInfo.Builder(42, componentName) val info = JobInfo.Builder(42, componentName)
.setRequiresCharging(false) .setRequiresCharging(false)
.setPersisted(true) .setPersisted(true)
@@ -1,4 +1,50 @@
package com.denizd.substitutionplan package com.denizd.substitutionplan
class NotificationService { import android.app.job.JobParameters
import android.app.job.JobService
import android.preference.PreferenceManager
import java.io.IOException
import java.lang.NullPointerException
import java.net.MalformedURLException
import java.net.URL
class NotificationService : JobService() {
private var jobCancelled = false
private val context = this
override fun onStartJob(params: JobParameters): Boolean {
doBackgroundWork(params)
return true
}
override fun onStopJob(params: JobParameters?): Boolean {
jobCancelled = true
return true
}
private fun doBackgroundWork(params: JobParameters) {
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
val edit = prefs.edit()
Thread(Runnable {
if (jobCancelled) {
return@Runnable
}
if (prefs.getBoolean("notif", false)) {
edit.putInt("notificationTestNumberDev", prefs.getInt("notificationTestNumberDev", 0) + 1).apply()
try {
val url = URL("https://djd4rkn355.github.io/subst.html")
val connection = url.openConnection()
if (!connection.getHeaderField("Last-Modified").equals(prefs.getString("time", ""))) {
DataFetcher(true, true, true, context, application, null).execute()
edit.putString("time", connection.getHeaderField("Last-Modified")).apply()
}
} catch (ignored: NullPointerException) {}
catch (ignored: IOException) {}
catch (ignored: MalformedURLException) {}
}
jobFinished(params, false)
}).start()
}
} }
@@ -0,0 +1,4 @@
package com.denizd.substitutionplan
class PersonalFragment {
}
@@ -0,0 +1,151 @@
package com.denizd.substitutionplan
import android.content.Context
import android.content.SharedPreferences
import android.content.res.Configuration
import android.os.Bundle
import android.os.Handler
import android.preference.PreferenceManager
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import java.util.*
import kotlin.collections.ArrayList
class PlanFragment(isPersonal: Boolean) : Fragment(R.layout.plan) {
private lateinit var recyclerView: RecyclerView
private lateinit var mAdapter: CardAdapter
private lateinit var layoutManager: GridLayoutManager
private val planCardList = ArrayList<Subst>()
private lateinit var bottomSheetText: TextView
private lateinit var substViewModel: SubstViewModel
private lateinit var mContext: Context
private lateinit var prefs: SharedPreferences
private lateinit var edit: SharedPreferences.Editor
private val personal = isPersonal
private lateinit var smileydown: TextView
private lateinit var smileydowntext: TextView
private lateinit var linearsmiley: LinearLayout
private var persPlanEmpty: Boolean = true
private val handler = Handler()
override fun onAttach(context: Context) {
super.onAttach(context)
mContext = context
prefs = PreferenceManager.getDefaultSharedPreferences(mContext)
edit = prefs.edit()
}
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
val grid = when (newConfig.orientation) {
Configuration.ORIENTATION_PORTRAIT -> 1
else -> 2
}
layoutManager = GridLayoutManager(mContext, grid)
recyclerView.layoutManager = layoutManager
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val pullToRefresh = view.findViewById<SwipeRefreshLayout>(R.id.pullToRefresh)
bottomSheetText = view.rootView.findViewById(R.id.bottom_sheet_text)
bottomSheetText.text = prefs.getString("informational", getString(R.string.noinfo))
recyclerView = view.findViewById(R.id.linearRecycler)
recyclerView.hasFixedSize()
val grid = when (resources.configuration.orientation) {
Configuration.ORIENTATION_PORTRAIT -> 1
else -> 2
}
layoutManager = GridLayoutManager(mContext, grid)
recyclerView.layoutManager = layoutManager
mAdapter = CardAdapter(planCardList)
recyclerView.adapter = mAdapter
if (personal) {
smileydown = view.findViewById(R.id.smileydown)
smileydowntext = view.findViewById(R.id.smileydowntext)
linearsmiley = view.findViewById(R.id.linearsmiley)
}
if (prefs.getInt("firstTimeOpening", 0) == 2) {
if (!prefs.getBoolean("notif", false)) {
pullToRefresh.isRefreshing = true
DataFetcher(true, false, false, mContext, activity!!.application, view.rootView).execute()
edit.putInt("firstTimeOpening", 3).apply()
}
}
if (prefs.getBoolean("autoRefresh", false)) {
pullToRefresh.isRefreshing = true
DataFetcher(true, false, false, mContext, activity!!.application, view.rootView).execute()
bottomSheetText.text = prefs.getString("informational", getString(R.string.noinfo))
}
substViewModel = ViewModelProviders.of(activity!!).get(SubstViewModel::class.java)
substViewModel.allSubst.observe(this, Observer<List<Subst>> {
if (personal) {
planCardList.clear()
smileydown.visibility = View.GONE
smileydowntext.visibility = View.GONE
linearsmiley.visibility = View.GONE
persPlanEmpty = true
recyclerView.visibility = View.VISIBLE
for (i in 0 until it.size) {
if (prefs.getString("courses", "").isEmpty() && prefs.getString("classes", "").isNotEmpty()) {
if (it[i].group.toString().isNotEmpty() && !it[i].group.equals("")) {
if (prefs.getString("classes", "").contains(it[i].group.toString()) || it[i].group.toString().contains(prefs.getString("classes", "").toString())) {
planCardList.add(it[i])
persPlanEmpty = false
}
}
} else if (prefs.getString("classes", "").isNotEmpty() && prefs.getString("courses", "").isNotEmpty()) {
if (!it[i].group.equals("") && !it[i].course.equals("")) {
if (prefs.getString("courses", "").contains(it[i].course.toString())) {
if (prefs.getString("classes", "").contains(it[i].group.toString()) || it[i].group.toString().contains(prefs.getString("classes", "").toString())) {
planCardList.add(it[i])
persPlanEmpty = false
}
}
}
}
// recyclerView.adapter!!.notifyDataSetChanged() // TODO check whether this is necessary
planCardList.sortWith(Comparator { lhs, rhs -> Integer.compare(rhs.priority, lhs.priority) })
recyclerView.scheduleLayoutAnimation()
mAdapter.setSubst(planCardList)
bottomSheetText.text = prefs.getString("informational", getString(R.string.noinfo))
handler.postDelayed({
if (persPlanEmpty) {
recyclerView.visibility = View.GONE
smileydown.visibility = View.VISIBLE
smileydowntext.visibility = View.VISIBLE
linearsmiley.visibility = View.VISIBLE
linearsmiley.scheduleLayoutAnimation()
}
}, 64)
}
} else {
mAdapter.setSubst(it)
recyclerView.scheduleLayoutAnimation()
bottomSheetText.text = prefs.getString("informational", getString(R.string.noinfo))
}
})
pullToRefresh.setOnRefreshListener {
pullToRefresh.isRefreshing = true
DataFetcher(true, false, false, mContext, activity!!.application, view.rootView).execute()
bottomSheetText.text = prefs.getString("informational", getString(R.string.noinfo))
}
}
}
@@ -424,8 +424,6 @@ class SettingsFragment : Fragment(R.layout.content_settings), View.OnClickListen
val girlCb = dateDialogView.findViewById<CheckBox>(R.id.cb2) val girlCb = dateDialogView.findViewById<CheckBox>(R.id.cb2)
val percentage = dateDialogView.findViewById<TextView>(R.id.dating_txtp) val percentage = dateDialogView.findViewById<TextView>(R.id.dating_txtp)
dateDialogView.findViewById<TextView>(R.id.textviewtitle).text = getString(R.string.dating)
datingBtn.setOnClickListener { datingBtn.setOnClickListener {
try { try {
val dg = MiscData() val dg = MiscData()
+2 -1
View File
@@ -14,7 +14,8 @@
android:layout_marginTop="16dp" android:layout_marginTop="16dp"
android:textSize="22sp" android:textSize="22sp"
android:layout_gravity="center_horizontal" android:layout_gravity="center_horizontal"
android:textColor="@color/datingPink"/> android:textColor="@color/datingPink"
android:text="@string/dating"/>
<androidx.core.widget.NestedScrollView <androidx.core.widget.NestedScrollView
android:layout_width="match_parent" android:layout_width="match_parent"
+3 -12
View File
@@ -1,18 +1,9 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/pullToRefreshFood" android:id="@+id/pullToRefresh"
android:layout_width="0dp" android:layout_width="match_parent"
android:layout_height="0dp" android:layout_height="match_parent"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:layout_marginEnd="0dp"
android:layout_marginBottom="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/background"> android:background="@color/background">
<androidx.recyclerview.widget.RecyclerView <androidx.recyclerview.widget.RecyclerView
+2 -10
View File
@@ -1,16 +1,8 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/pullToRefresh" android:id="@+id/pullToRefresh"
android:layout_width="0dp" android:layout_width="match_parent"
android:layout_height="0dp" android:layout_height="match_parent"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:layout_marginEnd="0dp"
android:layout_marginBottom="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/background"> android:background="@color/background">