Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AC-776 Migrated Logs activity to kotlin #766

Merged
merged 2 commits into from
Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* The contents of this file are subject to the OpenMRS Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://license.openmrs.org
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
*/
package org.openmrs.mobile.activities.logs

import android.os.Bundle
import android.view.Menu
import org.openmrs.mobile.R
import org.openmrs.mobile.activities.ACBaseActivity

class LogsActivity : ACBaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_logs)
val actionBar = supportActionBar
if (actionBar != null) {
actionBar.elevation = 0f
actionBar.setDisplayHomeAsUpEnabled(true)
}
// Create fragment
var logsFragment = supportFragmentManager.findFragmentById(R.id.logsContentFragment) as LogsFragment?
if (logsFragment == null) {
logsFragment = LogsFragment.newInstance()
}
if (!logsFragment.isActive) {
addFragmentToActivity(supportFragmentManager,
logsFragment, R.id.logsContentFragment)
}
// Create the presenter
LogsPresenter(logsFragment, mOpenMRSLogger)
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
super.onCreateOptionsMenu(menu)
//Disable Settings Option in Menu
val settingsItem = menu.findItem(R.id.actionSettings)
settingsItem.isVisible = false
return true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,16 @@
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
*/


package org.openmrs.mobile.activities.logs;

import org.openmrs.mobile.activities.BasePresenterContract;
import org.openmrs.mobile.activities.BaseView;

public interface LogsContract {
interface View extends BaseView<Presenter> {
void attachLogsToTextView(String logs);

void fabCopyAll(String textLogs);
interface LogsContract {
interface View : BaseView<Presenter> {
fun attachLogsToTextView(logs: String?)
fun fabCopyAll(textLogs: String?)
}

interface Presenter extends BasePresenterContract {
String getLogs();
}
interface Presenter : BasePresenterContract
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* The contents of this file are subject to the OpenMRS Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://license.openmrs.org
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
*/
package org.openmrs.mobile.activities.logs

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import org.openmrs.mobile.R
import org.openmrs.mobile.activities.ACBaseFragment
import org.openmrs.mobile.databinding.FragmentLogsBinding

class LogsFragment : ACBaseFragment<LogsContract.Presenter>(), LogsContract.View {

private lateinit var _binding: FragmentLogsBinding

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rishabh-997 can we do away with the underscore is it necessary ??

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a practice followed in kotlin where unused variables are marked as _... As suggested by Android Studio

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay @rishabh-997 👍

savedInstanceState: Bundle?): View? {
_binding = FragmentLogsBinding.inflate(inflater, container, false)
return _binding.root
}

override fun attachLogsToTextView(logs: String?) {
_binding.tvLogs.text = logs
}

override fun fabCopyAll(textLogs: String?) {
_binding.fab.setOnClickListener {
setClipboard(context, textLogs)
Toast.makeText(context, R.string.logs_copied_to_clipboard_message,
Toast.LENGTH_SHORT).show()
}
}

private fun setClipboard(context: Context?, text: String?) {
val clipboard = context!!.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clip = ClipData.newPlainText(getString(R.string.copied_text), text)
clipboard.primaryClip = clip
}

companion object {
fun newInstance(): LogsFragment {
return LogsFragment()
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* The contents of this file are subject to the OpenMRS Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://license.openmrs.org
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
*/
package org.openmrs.mobile.activities.logs

import org.openmrs.mobile.activities.BasePresenter
import org.openmrs.mobile.application.OpenMRS
import org.openmrs.mobile.application.OpenMRSLogger
import java.io.*

class LogsPresenter(private val mLogsView: LogsContract.View, private val mOpenMRSLogger: OpenMRSLogger) : BasePresenter(), LogsContract.Presenter {

override fun subscribe() {
val logsText = getLogs()
mLogsView.attachLogsToTextView(logsText)
mLogsView.fabCopyAll(logsText)
}

init {
mLogsView.setPresenter(this)
}

private fun getLogs(): String {
var textLogs = ""
val filename = (OpenMRS.getInstance().openMRSDir
+ File.separator + mOpenMRSLogger.logFilename)
try {
val myFile = File(filename)
val fIn = FileInputStream(myFile)
val myReader = BufferedReader(InputStreamReader(fIn))
while (myReader.readLine() != null) {
textLogs += myReader.readLine()
}
myReader.close()
} catch (e: FileNotFoundException) {
e.printStackTrace()
} catch (e: IOException) {
e.printStackTrace()
}
return textLogs
}
}
Loading