Skip to content

Commit

Permalink
📦 Schedule import update
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbatovK committed Jul 7, 2022
1 parent 9d3bda8 commit eab3e36
Show file tree
Hide file tree
Showing 10 changed files with 244 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.albatros.kplanner.model.module
import com.albatros.kplanner.ui.fragments.enter.EnterViewModel
import com.albatros.kplanner.ui.fragments.list.ListViewModel
import com.albatros.kplanner.ui.fragments.main.MainViewModel
import com.albatros.kplanner.ui.fragments.profile.ProfileViewModel
import com.albatros.kplanner.ui.fragments.register.RegisterViewModel
import com.albatros.kplanner.ui.fragments.stats.StatsViewModel
import com.albatros.kplanner.ui.fragments.users.UsersListViewModel
Expand All @@ -18,4 +19,5 @@ val viewModelModule = module {
viewModel { MainViewModel(get(), get(), get()) }
viewModel { StatsViewModel(get(), get()) }
viewModel { UsersListViewModel(get(), get()) }
viewModel { parameters -> ProfileViewModel(get(), get(), parameters.get()) }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.albatros.kplanner.ui.fragments.profile

import android.animation.ArgbEvaluator
import android.animation.ValueAnimator
import android.os.Bundle
import android.transition.TransitionInflater
import android.view.*
import android.widget.ImageView
import android.widget.Toast
import androidx.cardview.widget.CardView
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.navArgs
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import com.albatros.kplanner.R
import com.albatros.kplanner.databinding.ProfileFragmentBinding
import com.albatros.kplanner.domain.getMonth
import com.albatros.kplanner.domain.playFadeInAnimation
import com.albatros.kplanner.model.data.DiraNote
import com.albatros.kplanner.model.data.Schedule
import com.albatros.kplanner.ui.activity.MainActivity
import com.albatros.kplanner.ui.adapter.schedule.ScheduleAdapter
import com.albatros.kplanner.ui.adapter.schedule.ScheduleAdapterListener
import org.koin.androidx.viewmodel.ext.android.viewModel
import org.koin.core.parameter.parametersOf
import java.util.*

class ProfileFragment : Fragment() {

private val viewModel: ProfileViewModel by viewModel { parametersOf(arguments.user) }
private lateinit var binding: ProfileFragmentBinding
private val arguments: ProfileFragmentArgs by navArgs()

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = ProfileFragmentBinding.inflate(inflater, container, false)
return binding.root
}

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.profile_menu, menu)
super.onCreateOptionsMenu(menu, inflater)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean = when(item.itemId) {
R.id.action_import -> {
viewModel.importSchedule()
true
} else -> false
}

private val listener = object: ScheduleAdapterListener {

override fun OnFirstNoteBinded(view: CardView) {}

override fun onNoteFinished(note: DiraNote, schedule: Schedule, view: ImageView) {}
}

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

postponeEnterTransition()
sharedElementEnterTransition =
TransitionInflater.from(context).inflateTransition(android.R.transition.move)
startPostponedEnterTransition()

viewModel.onImported.observe(viewLifecycleOwner) {
if (it) {
(activity as MainActivity).binding.toolbar.menu.findItem(R.id.action_import).setIcon(R.drawable.ic_check)
}
}

viewModel.userSchedule.observe(viewLifecycleOwner) {
binding.list.adapter = ScheduleAdapter(it, listener)
binding.list.layoutManager = LinearLayoutManager(context)
val calendar = Calendar.getInstance()
binding.date.text = context?.getString(
R.string.date_str,
calendar.get(Calendar.DAY_OF_MONTH),
getMonth(calendar.get(Calendar.MONTH))
)
binding.date.playFadeInAnimation(500L)
}

(activity as MainActivity).binding.toolbar.title = arguments.user.nickname
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.albatros.kplanner.ui.fragments.profile

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.albatros.kplanner.model.api.DiraApi
import com.albatros.kplanner.model.data.DiraUser
import com.albatros.kplanner.model.data.Schedule
import com.albatros.kplanner.model.repo.UserRepo
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class ProfileViewModel(private val repo: UserRepo, private val api: DiraApi, private val chosen: DiraUser) : ViewModel() {

private val _userSchedule: MutableLiveData<Schedule> = MutableLiveData<Schedule>().apply {
viewModelScope.launch(Dispatchers.Main) {
value = api.getScheduleByOwnerId(chosen.tokenId)
}
}

val userSchedule: LiveData<Schedule> = _userSchedule

private val _onImported: MutableLiveData<Boolean> = MutableLiveData(false)
val onImported: LiveData<Boolean> = _onImported

fun importSchedule() {
viewModelScope.launch(Dispatchers.Main) {
val schedule = api.getScheduleByOwnerId(chosen.tokenId).copy(ownerId = repo.diraUser.tokenId)
schedule.tasks.forEach { it.finished = false }
api.createSchedule(schedule)
repo.schedule = schedule
_onImported.value = true
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ import android.widget.ImageView
import android.widget.LinearLayout
import androidx.appcompat.widget.SearchView
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import com.albatros.kplanner.R
import com.albatros.kplanner.databinding.ListFragmentBinding
import com.albatros.kplanner.model.data.DiraUser
import com.albatros.kplanner.ui.activity.MainActivity
import com.albatros.kplanner.ui.adapter.user.info.UserAdapterListener
import com.albatros.kplanner.ui.adapter.user.info.UserInfoAdapter
import org.koin.androidx.viewmodel.ext.android.viewModel


class UsersListFragment : Fragment() {
class UsersListFragment : Fragment(), MainActivity.IOnBackPressed {

private lateinit var binding: ListFragmentBinding
private val viewModel: UsersListViewModel by viewModel()
Expand All @@ -33,9 +35,16 @@ class UsersListFragment : Fragment() {
return binding.root
}

override fun onBackPressed(): Boolean {
val direction = UsersListFragmentDirections.actionUsersListFragmentToMainFragment()
findNavController().navigate(direction)
return true
}

private val listener = object : UserAdapterListener {
override fun onFriendClicked(user: DiraUser, view: View) {
//Toast.makeText(context, "A", Toast.LENGTH_SHORT).show()
val direction = UsersListFragmentDirections.actionUserListFragmentToProfileFragment(user)
findNavController().navigate(direction)
}

override fun onUserClicked(user: DiraUser, view: ImageView) {
Expand All @@ -59,9 +68,10 @@ class UsersListFragment : Fragment() {
true
}
android.R.id.home -> {
super.onOptionsItemSelected(item)
}
else -> true
val direction = UsersListFragmentDirections.actionUsersListFragmentToMainFragment()
findNavController().navigate(direction)
true
} else -> true
}

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/drawable-v24/ic_check.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<vector android:height="32dp" android:tint="@color/dark_cyan"
<vector android:height="32dp" android:tint="@color/neon_green"
android:viewportHeight="24" android:viewportWidth="24"
android:width="32dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M9,16.17L4.83,12l-1.42,1.41L9,19 21,7l-1.41,-1.41z"/>
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_import.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="32dp" android:tint="@color/dark_cyan"
android:viewportHeight="24" android:viewportWidth="24"
android:width="32dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M19.35,10.04C18.67,6.59 15.64,4 12,4 9.11,4 6.6,5.64 5.35,8.04 2.34,8.36 0,10.91 0,14c0,3.31 2.69,6 6,6h13c2.76,0 5,-2.24 5,-5 0,-2.64 -2.05,-4.78 -4.65,-4.96zM17,13l-5,5 -5,-5h3V9h4v4h3z"/>
</vector>
59 changes: 59 additions & 0 deletions app/src/main/res/layout/profile_fragment.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.core.widget.NestedScrollView
android:id="@+id/scroll_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="600dp"
android:orientation="vertical"
android:paddingStart="8dp"
android:paddingEnd="8dp">

<TextView
android:id="@+id/date"
style="@style/NoteCardTextStyleData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Сегодня, 3 июля"
android:layout_marginTop="24dp"
android:visibility="invisible"
android:gravity="center"
android:textColor="@color/dark_cyan"
app:layout_constraintTop_toTopOf="parent" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_marginTop="8dp"
android:overScrollMode="never"

android:gravity="start"
android:minHeight="700dp"
android:orientation="vertical"
android:paddingBottom="300dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/date" />

</androidx.constraintlayout.widget.ConstraintLayout>

</androidx.core.widget.NestedScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>
11 changes: 11 additions & 0 deletions app/src/main/res/menu/profile_menu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/action_import"
android:orderInCategory="1"
android:title="@string/app_name"
android:icon="@drawable/ic_import"
app:showAsAction="always" />
</menu>
21 changes: 21 additions & 0 deletions app/src/main/res/navigation/nav_graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@
android:name="com.albatros.kplanner.ui.fragments.users.UsersListFragment"
android:label=""
tools:layout="@layout/users_list_layout" >

<action
android:id="@+id/action_UserListFragment_to_ProfileFragment"
app:destination="@id/ProfileFragment" />

<action
android:id="@+id/action_UsersListFragment_to_MainFragment"
app:destination="@id/MainFragment" />

</fragment>

<fragment
Expand All @@ -39,6 +48,18 @@

</fragment>

<fragment
android:id="@+id/ProfileFragment"
android:name="com.albatros.kplanner.ui.fragments.profile.ProfileFragment"
android:label=""
tools:layout="@layout/list_fragment" >

<argument
android:name="user"
app:argType="com.albatros.kplanner.model.data.DiraUser" />

</fragment>


<fragment
android:id="@+id/ListFragment"
Expand Down

0 comments on commit eab3e36

Please sign in to comment.