Last active
October 13, 2019 18:56
-
-
Save francescogatto/3b0793fb5a9f75962343a1873625c21e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : AppCompatActivity() { | |
companion object { | |
val data = arrayOf( | |
DummyData("1", "Title1.pdf", "https://css4.pub/2015/icelandic/dictionary.pdf"), | |
DummyData("2", "Title2.pdf", "https://css4.pub/2015/icelandic/dictionary.pdf"), | |
DummyData("3", "Title3.pdf", "https://css4.pub/2015/icelandic/dictionary.pdf"), | |
DummyData("4", "Title4.pdf", "https://css4.pub/2017/newsletter/drylab.pdf"), | |
DummyData("5", "Title5.pdf", "https://css4.pub/2017/newsletter/drylab.pdf"), | |
DummyData("6", "Title6.pdf", "https://css4.pub/2017/newsletter/drylab.pdf") | |
) | |
} | |
lateinit var myAdapter: AttachmentAdapter | |
val ktor: HttpClient by inject() | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
with(recyclerView) { | |
layoutManager = LinearLayoutManager(this@MainActivity) | |
DividerItemDecoration(context, (layoutManager as LinearLayoutManager).orientation).apply { | |
addItemDecoration(this) | |
} | |
myAdapter = AttachmentAdapter(data) { dummy -> | |
manageClickAdapter(dummy) | |
} | |
adapter = myAdapter | |
} | |
} | |
fun manageClickAdapter(dummy: DummyData) { | |
when { | |
dummy.file.exists() -> openFile(dummy.file) | |
else -> { | |
myAdapter.setDownload(dummy, true) | |
CoroutineScope(Dispatchers.IO).launch { | |
ktor.downloadFile(dummy.file, dummy.url) { | |
if (it) { | |
Log.d("TAG", "File downloaded!") | |
} else { | |
dummy.file.delete() | |
Log.d("TAG", "File not downlaoded") | |
} | |
runOnUiThread { | |
myAdapter.setDownload(dummy, false) | |
} | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment