Skip to content

Commit

Permalink
little refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethosa committed Mar 19, 2022
1 parent a9f8d02 commit 084d2b2
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 57 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
applicationId "com.ethosa.ktc"
minSdk 21
targetSdk 32
versionCode 5
versionName "0.4.0"
versionCode 6
versionName "0.4.1"
ndk {
abiFilters 'armeabi-v7a','arm64-v8a','x86','x86_64'
}
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/ethosa/ktc/ui/adapters/AlbumAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class AlbumAdapter(
private const val MAX_DIM_AMOUNT = 0.95f
private const val HIDE_OFFSET = 400
private const val CHANGE_IMAGE_OFFSET = 200
private const val Y_PRIORITY = 3
private const val Y_PRIORITY = 2
private const val DIM_STEP = 0.1f
}

Expand Down Expand Up @@ -115,7 +115,7 @@ class AlbumAdapter(
/**
* Provides dialog behavior
*/
@SuppressLint("ClickableViewAccessibility", "ObjectAnimatorBinding")
@SuppressLint("ClickableViewAccessibility")
private fun setupDialog() {
// set content view
dialog.window?.setContentView(R.layout.layout_album_photo)
Expand Down
7 changes: 3 additions & 4 deletions app/src/main/java/com/ethosa/ktc/ui/adapters/CourseAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ class CourseAdapter(
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val binding = holder.binding
val item = items[position]
val back = timetableFragment.resources.getColorStateList(
R.color.primary, timetableFragment.requireContext().theme)
val fore = timetableFragment.resources.getColor(
R.color.btn_text, timetableFragment.requireContext().theme)
val theme = timetableFragment.requireContext().theme
val back = timetableFragment.resources.getColorStateList(R.color.primary, theme)
val fore = timetableFragment.resources.getColor(R.color.btn_text, theme)
binding.courseTitle.text = "${item.course} курс"
for (group in item.groups) {
val chip = Chip(timetableFragment.context)
Expand Down
12 changes: 6 additions & 6 deletions app/src/main/java/com/ethosa/ktc/ui/adapters/SettingsAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import com.ethosa.ktc.ui.fragments.SettingsFragment
class SettingsAdapter (
private val fragment: SettingsFragment
) : RecyclerView.Adapter<SettingsAdapter.ViewHolder>() {
private var titles: TypedArray = fragment.requireContext()
.resources.obtainTypedArray(R.array.settings_titles)
private var icons: TypedArray = fragment.requireContext()
.resources.obtainTypedArray(R.array.settings_icons)
private var actions: TypedArray = fragment.requireContext()
.resources.obtainTypedArray(R.array.settings_actions)
private val context = fragment.requireContext()
private val res = context.resources

private val titles: TypedArray = res.obtainTypedArray(R.array.settings_titles)
private val icons: TypedArray = res.obtainTypedArray(R.array.settings_icons)
private val actions: TypedArray = res.obtainTypedArray(R.array.settings_actions)

/**
* Provides RecyclerView.ViewHolder behavior
Expand Down
49 changes: 23 additions & 26 deletions app/src/main/java/com/ethosa/ktc/ui/fragments/TimetableFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences
import android.os.Bundle
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.ethosa.ktc.college.CollegeApi
Expand All @@ -18,6 +16,7 @@ import com.ethosa.ktc.databinding.FragmentTimetableBinding
import com.ethosa.ktc.ui.adapters.BranchAdapter
import com.ethosa.ktc.ui.adapters.CourseAdapter
import com.ethosa.ktc.ui.adapters.TimetableAdapter
import com.ethosa.ktc.utils.IOFragmentBackPressed
import com.ethosa.ktc.utils.SpacingItemDecoration
import com.google.gson.Gson
import okhttp3.Call
Expand All @@ -27,7 +26,7 @@ import okhttp3.Response
* Provides working with KTC timetable.
* Includes branches, courses with groups and timetable for any week.
*/
class TimetableFragment : Fragment() {
class TimetableFragment : IOFragmentBackPressed() {
private var _binding: FragmentTimetableBinding? = null
private lateinit var itemDecoration: RecyclerView.ItemDecoration
private lateinit var preferences: SharedPreferences
Expand Down Expand Up @@ -73,7 +72,7 @@ class TimetableFragment : Fragment() {
// Analog for back button
binding.back.setOnClickListener {
// back button disabled when state isn't 0
binding.back.isEnabled = !backState()
binding.back.isEnabled = !onBackPressed()
}

// Next week
Expand All @@ -99,19 +98,8 @@ class TimetableFragment : Fragment() {
_binding = null
}

override fun onResume() {
super.onResume()
view?.isFocusableInTouchMode = true
view?.requestFocus()
view?.setOnKeyListener(View.OnKeyListener { _, keyCode, event ->
if (event.action == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK)
// return true if at branches
return@OnKeyListener backState()
return@OnKeyListener false
})
}

private fun changeWeek(i: Int) {
// Provides week changing behavior.
week += i
binding.next.isEnabled = false
binding.previous.isEnabled = false
Expand All @@ -127,7 +115,8 @@ class TimetableFragment : Fragment() {
}
}

private fun backState(): Boolean {
override fun onBackPressed(): Boolean {
// Provides behavior on Back pressed.
when (state) {
1 -> fetchBranches()
2 -> fetchCourses(branch!!.id)
Expand All @@ -136,13 +125,25 @@ class TimetableFragment : Fragment() {
return true
}

private fun updateState(current: Int = 0) {
// Updates fragment's state and saves it into SharedPreferences
state = current
preferences.edit().putInt(STATE, state).apply()
}

private fun updateTimetable(timetable: Week) {
// Updates fragment's group and saves it into SharedPreferences
preferences.edit().putInt(GROUP, group!!.id).apply()
preferences.edit().putInt(WEEK, timetable.week_number).apply()
preferences.edit().putString(GROUP_TITLE, group!!.title).apply()
}

/**
* Fetches branches and shows it.
*/
@Suppress("MemberVisibilityCanBePrivate")
fun fetchBranches() {
state = 0
preferences.edit().putInt(STATE, state).apply()
updateState()
college.fetchBranches(object : CollegeCallback {
override fun onResponse(call: Call, response: Response) {
if (_binding == null) return
Expand All @@ -164,7 +165,7 @@ class TimetableFragment : Fragment() {
* @param branchId unique branch ID.
*/
fun fetchCourses(branchId: Int) {
state = 1
updateState(1)
college.fetchCourses(branchId, object : CollegeCallback {
@SuppressLint("SetTextI18n")
override fun onResponse(call: Call, response: Response) {
Expand All @@ -180,7 +181,6 @@ class TimetableFragment : Fragment() {
binding.previous.visibility = View.GONE
binding.timetableTitle.text = "Курсы"
binding.timetable.adapter = CourseAdapter(this@TimetableFragment, courses)
preferences.edit().putInt(STATE, state).apply()
preferences.edit().putInt(BRANCH, branch!!.id).apply()
}
}
Expand All @@ -193,7 +193,7 @@ class TimetableFragment : Fragment() {
* @param week by default is current week.
*/
fun fetchTimetable(groupId: Int, week: Int? = null) {
state = 2
updateState(2)
college.fetchTimetable(groupId, object : CollegeCallback {
@SuppressLint("SetTextI18n")
override fun onResponse(call: Call, response: Response) {
Expand All @@ -212,10 +212,7 @@ class TimetableFragment : Fragment() {
binding.next.visibility = View.VISIBLE
binding.previous.visibility = View.VISIBLE
binding.timetable.adapter = TimetableAdapter(this@TimetableFragment, timetable)
preferences.edit().putInt(STATE, state).apply()
preferences.edit().putInt(GROUP, group!!.id).apply()
preferences.edit().putInt(WEEK, timetable.week_number).apply()
preferences.edit().putString(GROUP_TITLE, group!!.title).apply()
updateTimetable(timetable)
}
}
}, week)
Expand Down
12 changes: 0 additions & 12 deletions app/src/main/java/com/ethosa/ktc/utils/IOBackPressed.kt

This file was deleted.

28 changes: 28 additions & 0 deletions app/src/main/java/com/ethosa/ktc/utils/IOFragmentBackPressed.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.ethosa.ktc.utils

import android.view.KeyEvent
import android.view.View
import androidx.fragment.app.Fragment

/**
* Provides interface which includes onBackPressed function.
*/
abstract class IOFragmentBackPressed : Fragment() {
/**
* onBackPressed function should be called only when
* user press on back button
*/
abstract fun onBackPressed(): Boolean

override fun onResume() {
super.onResume()
view?.isFocusableInTouchMode = true
view?.requestFocus()
view?.setOnKeyListener(View.OnKeyListener { _, keyCode, event ->
if (event.action == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK)
// return true if at branches
return@OnKeyListener onBackPressed()
return@OnKeyListener false
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ class SpacingItemDecoration(
outRect.bottom = spaceV

// Add top margin only for the first item to avoid double space between items
outRect.top =
if (parent.getChildLayoutPosition(view) == 0)
spaceV
else
0
val layoutPos = parent.getChildLayoutPosition(view)
outRect.top = if (layoutPos == 0) spaceV else 0
}
}

0 comments on commit 084d2b2

Please sign in to comment.