Skip to content
This repository has been archived by the owner on Aug 13, 2021. It is now read-only.

Commit

Permalink
Merge branch 'release/0.3.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasWanke committed Jun 18, 2019
2 parents ef4cca4 + dd6e3eb commit dcc025a
Show file tree
Hide file tree
Showing 34 changed files with 207 additions and 256 deletions.
2 changes: 2 additions & 0 deletions app/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ dependencies {
implementation 'com.google.firebase:firebase-core:16.0.8'
implementation 'com.crashlytics.sdk.android:crashlytics:2.9.9'

debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.0-alpha-2'

// AndroidX
// architecture
implementation "androidx.multidex:multidex:2.0.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ abstract class BaseActivity : AppCompatActivity(), CoroutineScope {
// region Activity
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.base_action_share -> shareLink(url!!, supportActionBar?.title)
R.id.base_action_share -> shareLink(url.asUri(), supportActionBar?.title)
R.id.base_action_refresh -> performRefresh()
// TODO: Remove when deep linking is readded
R.id.base_action_openInBrowser -> openUrl(url.asUri())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import androidx.navigation.fragment.navArgs
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.fragment_course.*
import org.schulcloud.mobile.R
import org.schulcloud.mobile.controllers.file.FileFragmentArgs
import org.schulcloud.mobile.controllers.main.MainFragment
import org.schulcloud.mobile.controllers.main.MainFragmentConfig
import org.schulcloud.mobile.controllers.topic.TopicFragmentArgs
import org.schulcloud.mobile.databinding.FragmentCourseBinding
import org.schulcloud.mobile.models.course.CourseRepository
import org.schulcloud.mobile.models.file.FileRepository
import org.schulcloud.mobile.models.topic.TopicRepository
import org.schulcloud.mobile.utils.map
import org.schulcloud.mobile.viewmodels.CourseViewModel
Expand Down Expand Up @@ -78,11 +80,11 @@ class CourseFragment : MainFragment<CourseViewModel>() {

override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
/*R.id.course_action_gotoFiles -> viewModel.course.value?.id?.also { id ->
R.id.course_action_gotoFiles -> viewModel.course.value?.id?.also { id ->
navController.navigate(R.id.action_global_fragment_file,
FileFragmentArgs.Builder(FileRepository.pathCourse(id))
FileFragmentArgs.Builder(FileRepository.CONTEXT_COURSE, id, null)
.build().toBundle())
}*/
}
else -> return super.onOptionsItemSelected(item)
}
return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ class CurrentEventViewHolder(binding: ItemEventCurrentBinding, private val onCou
toLocal()
}.timeOfDay

return (100 * (now - start) / (end - start)).toInt()
return if (start == end) 0
else (100 * (now - start) / (end - start)).toInt()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,20 @@ import androidx.core.view.children
import org.schulcloud.mobile.R
import org.schulcloud.mobile.models.course.Course
import org.schulcloud.mobile.models.file.FileRepository
import org.schulcloud.mobile.utils.combinePath
import org.schulcloud.mobile.utils.getPathParts
import org.schulcloud.mobile.utils.limit
import org.schulcloud.mobile.utils.ellipsizedSubstring
import org.schulcloud.mobile.views.CompatTextView

open class BreadcrumbsView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
@AttrRes defStyleAttr: Int = R.attr.breadcrumbsViewStyle
context: Context,
attrs: AttributeSet? = null,
@AttrRes defStyleAttr: Int = R.attr.breadcrumbsViewStyle
) : LinearLayoutCompat(context, attrs, defStyleAttr) {
companion object {
val TAG: String = BreadcrumbsView::class.java.simpleName
private const val PART_END_INDEX = 15
}

var onPathSelected: ((String) -> Unit)? = null
var onPathSelected: ((String, String, String?) -> Unit)? = null

private var textSize: Float = 0f

Expand All @@ -45,23 +44,19 @@ open class BreadcrumbsView @JvmOverloads constructor(
}
}

fun setPath(path: String?, course: Course? = null) {
fun setPath(pathParts: List<String?>, refOwnerModel: String, owner: String, parent: String?, course: Course? = null) {
removeAllViews()
if (path == null)
return

val parts = path.getPathParts()

val title = when (parts.first()) {
val title = when (refOwnerModel) {
FileRepository.CONTEXT_MY_API -> context.getString(R.string.file_directory_my)
FileRepository.CONTEXT_COURSES ->
FileRepository.CONTEXT_COURSE ->
course?.name ?: context.getString(R.string.file_directory_course_unknown)
else -> context.getString(R.string.file_directory_unknown)
}
addPartView(parts.limit(2).combinePath(), title)
addPartView(refOwnerModel, owner, null, title)

for (i in 2 until parts.size)
addPartView(parts.limit(i + 1).combinePath(), parts[i])
for (part in pathParts)
addPartView(refOwnerModel, owner, parent, part?.ellipsizedSubstring(0, PART_END_INDEX))
}

fun setTextColor(@ColorInt color: Int) {
Expand All @@ -70,11 +65,11 @@ open class BreadcrumbsView @JvmOverloads constructor(
dividerDrawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP)
}

private fun addPartView(path: String, title: String) {
private fun addPartView(refOwnerModel: String, owner: String, parent: String?, title: String?) {
addView(CompatTextView(context).also {
it.textSize = textSize
it.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize)
it.text = title
it.setOnClickListener { onPathSelected?.invoke(path) }
it.setOnClickListener { onPathSelected?.invoke(refOwnerModel, owner, parent) }

with(TypedValue()) {
context.theme.resolveAttribute(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import android.view.ViewGroup
import org.schulcloud.mobile.controllers.base.BaseAdapter
import org.schulcloud.mobile.controllers.base.BaseViewHolder
import org.schulcloud.mobile.databinding.ItemDirectoryBinding
import org.schulcloud.mobile.models.file.Directory
import org.schulcloud.mobile.models.file.File

class DirectoryAdapter(private val onSelected: (String) -> Unit) :
BaseAdapter<Directory, DirectoryAdapter.DirectoryViewHolder, ItemDirectoryBinding>() {
class DirectoryAdapter(private val onSelected: (String, String, String?) -> Unit) :
BaseAdapter<File, DirectoryAdapter.DirectoryViewHolder, ItemDirectoryBinding>() {

fun update(directoryList: List<Directory>) {
fun update(directoryList: List<File>) {
items = directoryList
}

Expand All @@ -21,7 +21,7 @@ class DirectoryAdapter(private val onSelected: (String) -> Unit) :
}

class DirectoryViewHolder(binding: ItemDirectoryBinding) :
BaseViewHolder<Directory, ItemDirectoryBinding>(binding) {
BaseViewHolder<File, ItemDirectoryBinding>(binding) {
override fun onItemSet() {
binding.directory = item
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@ import org.schulcloud.mobile.models.course.Course
import org.schulcloud.mobile.models.course.CourseRepository
import org.schulcloud.mobile.models.file.File
import org.schulcloud.mobile.models.file.FileRepository
import org.schulcloud.mobile.models.file.SignedUrlRequest
import org.schulcloud.mobile.network.ApiService
import org.schulcloud.mobile.utils.*
import org.schulcloud.mobile.viewmodels.FileViewModel
import org.schulcloud.mobile.viewmodels.IdViewModelFactory
import org.schulcloud.mobile.viewmodels.FileViewModelFactory
import retrofit2.HttpException
import ru.gildor.coroutines.retrofit.await
import javax.net.ssl.SSLHandshakeException


class FileFragment : MainFragment<FileViewModel>() {
Expand All @@ -42,9 +40,9 @@ class FileFragment : MainFragment<FileViewModel>() {
}

private val directoryAdapter: DirectoryAdapter by lazy {
DirectoryAdapter {
DirectoryAdapter { refOwnerModel, owner, parent ->
navController.navigate(R.id.action_global_fragment_file,
FileFragmentArgs.Builder(combinePath(viewModel.path, it)).build().toBundle())
FileFragmentArgs.Builder(refOwnerModel, owner, parent).build().toBundle())
}
}
private val fileAdapter: FileAdapter by lazy {
Expand All @@ -55,13 +53,10 @@ class FileFragment : MainFragment<FileViewModel>() {

override var url: String? = null
get() {
val parts = args.path.getPathParts()
val path = if (parts.size <= 2) ""
else "?dir=${parts.takeLast(parts.size - 2).combinePath().ensureSlashes()}"

return when (parts.first()) {
val path = idPathParts.combinePath()
return when (args.refOwnerModel) {
FileRepository.CONTEXT_MY_API -> "/files/my/$path"
FileRepository.CONTEXT_COURSES -> "/files/courses/${parts[1]}$path"
FileRepository.CONTEXT_COURSE -> "/files/courses/${args.owner}/$path"
else -> null
}
}
Expand All @@ -71,15 +66,14 @@ class FileFragment : MainFragment<FileViewModel>() {
CourseRepository.course(viewModel.realm, it)
} ?: null.asLiveData<Course>())
.map { course ->
breadcrumbs.setPath(args.path, course)
val parts = args.path.getPathParts()
breadcrumbs.setPath(namePathParts, args.refOwnerModel, args.owner, args.parent, course)

MainFragmentConfig(
title = when {
parts.size > 2 -> parts.last()
parts.first() == FileRepository.CONTEXT_MY_API ->
args.parent != null -> viewModel.directory((args.parent).toString())?.name
args.refOwnerModel == FileRepository.CONTEXT_MY_API ->
context?.getString(R.string.file_directory_my)
parts.first() == FileRepository.CONTEXT_COURSES ->
args.refOwnerModel == FileRepository.CONTEXT_COURSE ->
course?.name ?: context?.getString(R.string.file_directory_course_unknown)
else -> context?.getString(R.string.file_directory_unknown)
},
Expand All @@ -92,8 +86,15 @@ class FileFragment : MainFragment<FileViewModel>() {
)
}

private val namePathParts: List<String?>
get() = getDirectoryPathParts(args.parent, true)

private val idPathParts: List<String?>
get() = getDirectoryPathParts(args.parent)


override fun onCreate(savedInstanceState: Bundle?) {
viewModel = ViewModelProviders.of(this, IdViewModelFactory(args.path))
viewModel = ViewModelProviders.of(this, FileViewModelFactory(args.owner, args.parent))
.get(FileViewModel::class.java)
super.onCreate(savedInstanceState)
}
Expand Down Expand Up @@ -136,22 +137,23 @@ class FileFragment : MainFragment<FileViewModel>() {
adapter = fileAdapter
addItemDecoration(DividerItemDecoration(context, DividerItemDecoration.VERTICAL))
}

}

override fun onResume() {
super.onResume()

mainActivity.setToolbarWrapper(toolbarWrapper)

breadcrumbs.setPath(args.path)
breadcrumbs.onPathSelected = callback@{ path ->
if (path == args.path) {
breadcrumbs.setPath(namePathParts, args.refOwnerModel, args.owner, args.parent)
breadcrumbs.onPathSelected = callback@{ refOwnerModel, owner, parent ->
if (refOwnerModel == args.refOwnerModel && owner == args.owner && parent == args.parent) {
performRefresh()
return@callback
}

navController.navigate(R.id.action_global_fragment_file,
FileFragmentArgs.Builder(path).build().toBundle())
FileFragmentArgs.Builder(refOwnerModel, owner, parent).build().toBundle())
}
mainViewModel.toolbarColors.observe(this, Observer {
breadcrumbs.setTextColor(it.textColor)
Expand All @@ -170,29 +172,38 @@ class FileFragment : MainFragment<FileViewModel>() {
}

override suspend fun refresh() {
FileRepository.syncDirectory(viewModel.path)
FileRepository.syncDirectory(viewModel.owner, viewModel.parent)
FileRepository.syncDirectoriesForOwner(viewModel.owner)
getCourseFromFolder()?.also {
CourseRepository.syncCourse(it)
}
}


private fun getCourseFromFolder(): String? {
if (!args.path.startsWith(FileRepository.CONTEXT_COURSES))
if (args.refOwnerModel != FileRepository.CONTEXT_COURSE)
return null

return args.path.getPathParts()[1]
return args.owner
}

private fun getDirectoryPathParts(directoryId: String?, isNamePath: Boolean = false): List<String?> {
val pathParts = mutableListOf<String?>()
var currentId: String? = directoryId
var currentDirectory: File?
while (currentId != null){
currentDirectory = viewModel.directory(currentId)
pathParts.add(0, if (isNamePath) currentDirectory?.name else currentDirectory?.id)
currentId = currentDirectory?.parent
}
return pathParts.toList()
}


@Suppress("ComplexMethod")
private fun loadFile(file: File, download: Boolean) = launch(Dispatchers.Main) {
try {
val response = ApiService.getInstance().generateSignedUrl(
SignedUrlRequest().apply {
action = SignedUrlRequest.ACTION_GET
path = file.key
fileType = file.type
}).await()
val response = ApiService.getInstance().generateSignedUrl(file.id, download).await()

if (download) {
if (!requestPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
Expand All @@ -201,11 +212,7 @@ class FileFragment : MainFragment<FileViewModel>() {
}

this@FileFragment.context?.withProgressDialog(R.string.file_fileDownload_progress) {
val result = try {
ApiService.getInstance().downloadFile(response.url!!).await()
} catch (ex: SSLHandshakeException) {
ApiService.getFileDownloadInstance().downloadFile(response.url!!).await()
}
val result = ApiService.getInstance().downloadFile(response.url!!).await()
if (!result.writeToDisk(file.name.orEmpty())) {
this@FileFragment.context?.showGenericError(R.string.file_fileDownload_error_save)
return@withProgressDialog
Expand All @@ -215,7 +222,7 @@ class FileFragment : MainFragment<FileViewModel>() {
}
} else {
val intent = Intent(Intent.ACTION_VIEW).apply {
setDataAndType(Uri.parse(response.url), response.header?.contentType)
setDataAndType(Uri.parse(response.url), file.type)
}
val packageManager = activity?.packageManager
if (packageManager != null && intent.resolveActivity(packageManager) != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class FileOverviewFragment : MainFragment<FileOverviewViewModel>() {
private val coursesAdapter: FileOverviewCourseAdapter by lazy {
FileOverviewCourseAdapter {
navController.navigate(R.id.action_global_fragment_file,
FileFragmentArgs.Builder(FileRepository.pathCourse(it)).build().toBundle())
FileFragmentArgs.Builder(FileRepository.CONTEXT_COURSE, it, null).build().toBundle())
}
}

Expand All @@ -52,7 +52,7 @@ class FileOverviewFragment : MainFragment<FileOverviewViewModel>() {
super.onViewCreated(view, savedInstanceState)
personal_card.setOnClickListener(Navigation.createNavigateOnClickListener(
R.id.action_global_fragment_file,
FileFragmentArgs.Builder(FileRepository.pathPersonal()).build().toBundle()))
FileFragmentArgs.Builder(FileRepository.CONTEXT_MY_API, FileRepository.user, null).build().toBundle()))

coursesAdapter.emptyIndicator = empty
viewModel.courses.observe(this, Observer { courses ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ abstract class MainFragment<VM : ViewModel>(refreshableImpl: RefreshableImpl = R
var link = url ?: return true
if (link.startsWith('/'))
link = combinePath(HOST, link)
context?.shareLink(link, mainViewModel.config.value?.title)
context?.shareLink(link.asUri(), mainViewModel.config.value?.title)
}
R.id.base_action_refresh -> performRefresh()
// TODO: Remove when deep linking is readded
Expand Down

This file was deleted.

Loading

0 comments on commit dcc025a

Please sign in to comment.