Skip to content

Commit

Permalink
* Reformat code to align with KtLint rules
Browse files Browse the repository at this point in the history
* Move LiveData observation with Fragment's viewLifecycleOwner to onViewCreated instead of onCreate
  • Loading branch information
tuancoltech committed Nov 7, 2024
1 parent bd4dfda commit 1dc6f4b
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 111 deletions.
8 changes: 4 additions & 4 deletions app/src/main/java/dev/arkbuilders/arkmemo/di/AppModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import javax.inject.Singleton
@Module
@InstallIn(SingletonComponent::class)
object AppModule {

@Provides
@Singleton
fun provideApplicationContext(@ApplicationContext context: Context): Context {
fun provideApplicationContext(
@ApplicationContext context: Context,
): Context {
return context
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ data class GraphicNote(
@IgnoredOnParcel
override var resource: Resource? = null,
override var pendingForDelete: Boolean = false,
var thumb: Bitmap? = null

var thumb: Bitmap? = null,
) : Note, Parcelable
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import dev.arkbuilders.arklib.data.index.Resource
import dev.arkbuilders.arkmemo.R
import dev.arkbuilders.arkmemo.di.IO_DISPATCHER
import dev.arkbuilders.arkmemo.graphics.ColorCode
import dev.arkbuilders.arkmemo.models.GraphicNote
import dev.arkbuilders.arkmemo.preferences.MemoPreferences
import dev.arkbuilders.arkmemo.graphics.SVG
import dev.arkbuilders.arkmemo.models.GraphicNote
import dev.arkbuilders.arkmemo.models.SaveNoteResult
Expand All @@ -38,26 +36,27 @@ import kotlin.io.path.fileSize
import kotlin.io.path.getLastModifiedTime
import kotlin.io.path.name

class GraphicNotesRepo @Inject constructor(
private val memoPreferences: MemoPreferences,
@Named(IO_DISPATCHER) private val iODispatcher: CoroutineDispatcher,
private val helper: NotesRepoHelper,
@ApplicationContext private val context: Context
): NotesRepo<GraphicNote> {

private lateinit var root: Path

private val displayMetrics by lazy { Resources.getSystem().displayMetrics }
private val screenWidth by lazy { displayMetrics.widthPixels }
private val screenHeight by lazy { displayMetrics.heightPixels - 150.dpToPx() }
private val thumbViewWidth by lazy { context.resources.getDimension(R.dimen.graphic_thumb_width) }

private val thumbDirectory by lazy { context.getExternalFilesDir(Environment.DIRECTORY_PICTURES) }

override suspend fun init() {
helper.init()
root = memoPreferences.getNotesStorage()
}
class GraphicNotesRepo
@Inject
constructor(
private val memoPreferences: MemoPreferences,
@Named(IO_DISPATCHER) private val iODispatcher: CoroutineDispatcher,
private val helper: NotesRepoHelper,
@ApplicationContext private val context: Context,
) : NotesRepo<GraphicNote> {
private lateinit var root: Path

private val displayMetrics by lazy { Resources.getSystem().displayMetrics }
private val screenWidth by lazy { displayMetrics.widthPixels }
private val screenHeight by lazy { displayMetrics.heightPixels - 150.dpToPx() }
private val thumbViewWidth by lazy { context.resources.getDimension(R.dimen.graphic_thumb_width) }

private val thumbDirectory by lazy { context.getExternalFilesDir(Environment.DIRECTORY_PICTURES) }

override suspend fun init() {
helper.init()
root = memoPreferences.getNotesStorage()
}

override suspend fun save(
note: GraphicNote,
Expand Down Expand Up @@ -130,78 +129,81 @@ class GraphicNotesRepo @Inject constructor(
modified = path.getLastModifiedTime(),
)

val userNoteProperties = helper.readProperties(id, "")
val bitmap = exportBitmapFromSvg(fileName = id.toString(), svg = svg)

GraphicNote(
title = userNoteProperties.title,
description = userNoteProperties.description,
svg = svg,
resource = resource,
thumb = bitmap
)

}.filter { graphicNote -> graphicNote.svg != null }
}

private fun exportBitmapFromSvg(fileName: String, svg: SVG?): Bitmap? {

// Check if thumb bitmap already exists
val file = File(thumbDirectory, "$fileName.png")
try {
if (file.exists()) {
return BitmapFactory.decodeFile(file.absolutePath)
val userNoteProperties = helper.readProperties(id, "")
val bitmap = exportBitmapFromSvg(fileName = id.toString(), svg = svg)

GraphicNote(
title = userNoteProperties.title,
description = userNoteProperties.description,
svg = svg,
resource = resource,
thumb = bitmap,
)
}.filter { graphicNote -> graphicNote.svg != null }
}
} catch (e: Exception) {
e.printStackTrace()
}

// If thumb doesn't exist, create a bitmap and a canvas for offscreen drawing
val bitmap = Bitmap.createBitmap(
thumbViewWidth.toInt(), thumbViewWidth.toInt(), Bitmap.Config.ARGB_8888
)
val canvas = Canvas(bitmap)
private fun exportBitmapFromSvg(
fileName: String,
svg: SVG?,
): Bitmap? {
// Check if thumb bitmap already exists
val file = File(thumbDirectory, "$fileName.png")
try {
if (file.exists()) {
return BitmapFactory.decodeFile(file.absolutePath)
}
} catch (e: Exception) {
e.printStackTrace()
}

canvas.drawColor(ColorCode.lightYellow)
svg?.getPaths()?.forEach { path ->
// If thumb doesn't exist, create a bitmap and a canvas for offscreen drawing
val bitmap =
Bitmap.createBitmap(
thumbViewWidth.toInt(),
thumbViewWidth.toInt(),
Bitmap.Config.ARGB_8888,
)
val canvas = Canvas(bitmap)

canvas.save()
canvas.drawColor(ColorCode.lightYellow)
svg?.getPaths()?.forEach { path ->

// Scale factor to fit the SVG path into the view
val scaleX = thumbViewWidth / screenWidth
val scaleY = thumbViewWidth / screenHeight
canvas.save()

// Find the smallest scale to maintain the aspect ratio
val scale = minOf(scaleX, scaleY)
// Scale factor to fit the SVG path into the view
val scaleX = thumbViewWidth / screenWidth
val scaleY = thumbViewWidth / screenHeight

// Center the path in the view
val dx = (thumbViewWidth - screenWidth * scale) / 2f
val dy = (thumbViewWidth - screenHeight * scale) / 2f
// Find the smallest scale to maintain the aspect ratio
val scale = minOf(scaleX, scaleY)

// Apply scaling and translation to center the path
canvas.translate(dx, dy)
canvas.scale(scale, scale)
// Center the path in the view
val dx = (thumbViewWidth - screenWidth * scale) / 2f
val dy = (thumbViewWidth - screenHeight * scale) / 2f

canvas.drawPath(path.path, path.paint)
canvas.restore()
} ?: let {
return null
}
// Apply scaling and translation to center the path
canvas.translate(dx, dy)
canvas.scale(scale, scale)

// Save the bitmap to a file
try {
canvas.drawPath(path.path, path.paint)
canvas.restore()
} ?: let {
return null
}

// Open an output stream and write the bitmap to the file
FileOutputStream(file).use { outputStream ->
bitmap.compress(Bitmap.CompressFormat.PNG, 80, outputStream) // Save as PNG
// Save the bitmap to a file
try {
// Open an output stream and write the bitmap to the file
FileOutputStream(file).use { outputStream ->
bitmap.compress(Bitmap.CompressFormat.PNG, 80, outputStream) // Save as PNG
}
return bitmap
} catch (e: IOException) {
e.printStackTrace()
return null
}
return bitmap
} catch (e: IOException) {
e.printStackTrace()
return null
}
}
}

private const val GRAPHICS_REPO = "GraphicNotesRepo"
private const val SVG_EXT = "svg"
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ import dev.arkbuilders.arkmemo.utils.visible
class NotesListAdapter(
private var notes: MutableList<Note>,
private val onPlayPauseClick: (path: String, pos: Int?, stopCallback: ((pos: Int) -> Unit)?) -> Unit,
): RecyclerView.Adapter<NotesListAdapter.NoteViewHolder>() {

) : RecyclerView.Adapter<NotesListAdapter.NoteViewHolder>() {
private lateinit var activity: MainActivity

lateinit var observeItemSideEffect: () -> ArkMediaPlayerSideEffect
Expand Down Expand Up @@ -116,23 +115,26 @@ class NotesListAdapter(
}
}
} else if (note is GraphicNote) {
holder.ivGraphicThumb.background = BitmapDrawable(
holder.itemView.context.resources, note.thumb
)
holder.ivGraphicThumb.background =
BitmapDrawable(
holder.itemView.context.resources, note.thumb,
)
holder.ivGraphicThumb.visible()
holder.ivGraphicThumb.shapeAppearanceModel = ShapeAppearanceModel.builder()
.setBottomLeftCornerSize(0f)
.setTopLeftCornerSize(0f)
.setTopRightCorner(CornerFamily.ROUNDED, cornerRadius)
.setBottomRightCorner(CornerFamily.ROUNDED, cornerRadius)
.build()
holder.ivGraphicThumb.shapeAppearanceModel =
ShapeAppearanceModel.builder()
.setBottomLeftCornerSize(0f)
.setTopLeftCornerSize(0f)
.setTopRightCorner(CornerFamily.ROUNDED, cornerRadius)
.setBottomRightCorner(CornerFamily.ROUNDED, cornerRadius)
.build()
}

if (note.pendingForDelete) {
holder.tvDelete.visible()
if (note is GraphicNote) {
holder.ivGraphicThumb.shapeAppearanceModel = ShapeAppearanceModel.builder()
.setAllCorners(CornerFamily.ROUNDED, 0f).build()
holder.ivGraphicThumb.shapeAppearanceModel =
ShapeAppearanceModel.builder()
.setAllCorners(CornerFamily.ROUNDED, 0f).build()
}
} else {
holder.tvDelete.gone()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ class EditGraphicNotesFragment : BaseEditNoteFragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
notesViewModel.init {}
observeSaveResult(notesViewModel.getSaveNoteResultLiveData())
arguments?.getParcelableCompat(GRAPHICAL_NOTE_KEY, GraphicNote::class.java)?.let {
note = it
graphicNotesViewModel.onNoteOpened(note)
Expand Down Expand Up @@ -140,6 +139,7 @@ class EditGraphicNotesFragment : BaseEditNoteFragment() {
binding.editTextDescription.setText(this.note.description)
initBottomControls()
observeDrawEvent()
observeSaveResult(notesViewModel.getSaveNoteResultLiveData())
}

private fun observeDrawEvent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,14 @@ class NotesFragment : BaseFragment() {
private fun onNotesLoaded(notes: List<Note>) {
binding.pbLoading.gone()
if (notesAdapter == null) {
notesAdapter = NotesListAdapter(
notes.toMutableList(),
onPlayPauseClick = { path, pos, onStop ->
playingAudioPath = path
if (playingAudioPosition >= 0) {
refreshNoteItem(playingAudioPosition)
}
notesAdapter =
NotesListAdapter(
notes.toMutableList(),
onPlayPauseClick = { path, pos, onStop ->
playingAudioPath = path
if (playingAudioPosition >= 0) {
refreshNoteItem(playingAudioPosition)
}

if (playingAudioPosition >= 0 && playingAudioPosition != pos) {
// Another Voice note is being played compared to the previously played one
Expand All @@ -224,10 +225,9 @@ class NotesFragment : BaseFragment() {
mItemTouchHelper?.attachToRecyclerView(null)
}

arkMediaPlayerViewModel.onPlayOrPauseClick(path, pos, onStop)
}
)

arkMediaPlayerViewModel.onPlayOrPauseClick(path, pos, onStop)
},
)
} else {
notesAdapter?.setNotes(notes)
}
Expand Down

0 comments on commit 1dc6f4b

Please sign in to comment.